net: wireless: rockchip: add rtl8822be pcie wifi driver
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8822be / core / rtw_ap.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_AP_C_
21
22 #include <drv_types.h>
23
24
25 #ifdef CONFIG_AP_MODE
26
27 extern unsigned char    RTW_WPA_OUI[];
28 extern unsigned char    WMM_OUI[];
29 extern unsigned char    WPS_OUI[];
30 extern unsigned char    P2P_OUI[];
31 extern unsigned char    WFD_OUI[];
32
33 void init_mlme_ap_info(_adapter *padapter)
34 {
35         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
36         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
37
38         _rtw_spinlock_init(&pmlmepriv->bcn_update_lock);
39
40         /* pmlmeext->bstart_bss = _FALSE; */
41
42 }
43
44 void free_mlme_ap_info(_adapter *padapter)
45 {
46         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
47
48         stop_ap_mode(padapter);
49         _rtw_spinlock_free(&pmlmepriv->bcn_update_lock);
50
51 }
52
53 static void update_BCNTIM(_adapter *padapter)
54 {
55         struct sta_priv *pstapriv = &padapter->stapriv;
56         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
57         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
58         WLAN_BSSID_EX *pnetwork_mlmeext = &(pmlmeinfo->network);
59         unsigned char *pie = pnetwork_mlmeext->IEs;
60
61 #if 0
62
63
64         /* update TIM IE */
65         /* if(pstapriv->tim_bitmap) */
66 #endif
67         if (_TRUE) {
68                 u8 *p, *dst_ie, *premainder_ie = NULL, *pbackup_remainder_ie = NULL;
69                 u16 tim_bitmap_le;
70                 uint offset, tmp_len, tim_ielen, tim_ie_offset, remainder_ielen;
71
72                 tim_bitmap_le = cpu_to_le16(pstapriv->tim_bitmap);
73
74                 p = rtw_get_ie(pie + _FIXED_IE_LENGTH_, _TIM_IE_, &tim_ielen, pnetwork_mlmeext->IELength - _FIXED_IE_LENGTH_);
75                 if (p != NULL && tim_ielen > 0) {
76                         tim_ielen += 2;
77
78                         premainder_ie = p + tim_ielen;
79
80                         tim_ie_offset = (sint)(p - pie);
81
82                         remainder_ielen = pnetwork_mlmeext->IELength - tim_ie_offset - tim_ielen;
83
84                         /*append TIM IE from dst_ie offset*/
85                         dst_ie = p;
86                 } else {
87                         tim_ielen = 0;
88
89                         /*calculate head_len*/
90                         offset = _FIXED_IE_LENGTH_;
91
92                         /* get ssid_ie len */
93                         p = rtw_get_ie(pie + _BEACON_IE_OFFSET_, _SSID_IE_, &tmp_len, (pnetwork_mlmeext->IELength - _BEACON_IE_OFFSET_));
94                         if (p != NULL)
95                                 offset += tmp_len + 2;
96
97                         /*get supported rates len*/
98                         p = rtw_get_ie(pie + _BEACON_IE_OFFSET_, _SUPPORTEDRATES_IE_, &tmp_len, (pnetwork_mlmeext->IELength - _BEACON_IE_OFFSET_));
99                         if (p !=  NULL)
100                                 offset += tmp_len + 2;
101
102                         /*DS Parameter Set IE, len=3*/
103                         offset += 3;
104
105                         premainder_ie = pie + offset;
106
107                         remainder_ielen = pnetwork_mlmeext->IELength - offset - tim_ielen;
108
109                         /*append TIM IE from offset*/
110                         dst_ie = pie + offset;
111
112                 }
113
114                 if (remainder_ielen > 0) {
115                         pbackup_remainder_ie = rtw_malloc(remainder_ielen);
116                         if (pbackup_remainder_ie && premainder_ie)
117                                 _rtw_memcpy(pbackup_remainder_ie, premainder_ie, remainder_ielen);
118                 }
119
120                 *dst_ie++ = _TIM_IE_;
121
122                 if ((pstapriv->tim_bitmap & 0xff00) && (pstapriv->tim_bitmap & 0x00fe))
123                         tim_ielen = 5;
124                 else
125                         tim_ielen = 4;
126
127                 *dst_ie++ = tim_ielen;
128
129                 *dst_ie++ = 0;/*DTIM count*/
130                 *dst_ie++ = 1;/*DTIM period*/
131
132                 if (pstapriv->tim_bitmap & BIT(0))/*for bc/mc frames*/
133                         *dst_ie++ = BIT(0);/*bitmap ctrl */
134                 else
135                         *dst_ie++ = 0;
136
137                 if (tim_ielen == 4) {
138                         u8 pvb = 0;
139
140                         if (pstapriv->tim_bitmap & 0x00fe)
141                                 pvb = (u8)tim_bitmap_le;
142                         else if (pstapriv->tim_bitmap & 0xff00)
143                                 pvb = (u8)(tim_bitmap_le >> 8);
144                         else
145                                 pvb = (u8)tim_bitmap_le;
146
147                         *dst_ie++ = pvb;
148
149                 } else if (tim_ielen == 5) {
150                         _rtw_memcpy(dst_ie, &tim_bitmap_le, 2);
151                         dst_ie += 2;
152                 }
153
154                 /*copy remainder IE*/
155                 if (pbackup_remainder_ie) {
156                         _rtw_memcpy(dst_ie, pbackup_remainder_ie, remainder_ielen);
157
158                         rtw_mfree(pbackup_remainder_ie, remainder_ielen);
159                 }
160
161                 offset = (uint)(dst_ie - pie);
162                 pnetwork_mlmeext->IELength = offset + remainder_ielen;
163
164         }
165 }
166
167 void rtw_add_bcn_ie(_adapter *padapter, WLAN_BSSID_EX *pnetwork, u8 index, u8 *data, u8 len)
168 {
169         PNDIS_802_11_VARIABLE_IEs       pIE;
170         u8      bmatch = _FALSE;
171         u8      *pie = pnetwork->IEs;
172         u8      *p = NULL, *dst_ie = NULL, *premainder_ie = NULL, *pbackup_remainder_ie = NULL;
173         u32     i, offset, ielen, ie_offset, remainder_ielen = 0;
174
175         for (i = sizeof(NDIS_802_11_FIXED_IEs); i < pnetwork->IELength;) {
176                 pIE = (PNDIS_802_11_VARIABLE_IEs)(pnetwork->IEs + i);
177
178                 if (pIE->ElementID > index)
179                         break;
180                 else if (pIE->ElementID == index) { /* already exist the same IE */
181                         p = (u8 *)pIE;
182                         ielen = pIE->Length;
183                         bmatch = _TRUE;
184                         break;
185                 }
186
187                 p = (u8 *)pIE;
188                 ielen = pIE->Length;
189                 i += (pIE->Length + 2);
190         }
191
192         if (p != NULL && ielen > 0) {
193                 ielen += 2;
194
195                 premainder_ie = p + ielen;
196
197                 ie_offset = (sint)(p - pie);
198
199                 remainder_ielen = pnetwork->IELength - ie_offset - ielen;
200
201                 if (bmatch)
202                         dst_ie = p;
203                 else
204                         dst_ie = (p + ielen);
205         }
206
207         if (dst_ie == NULL)
208                 return;
209
210         if (remainder_ielen > 0) {
211                 pbackup_remainder_ie = rtw_malloc(remainder_ielen);
212                 if (pbackup_remainder_ie && premainder_ie)
213                         _rtw_memcpy(pbackup_remainder_ie, premainder_ie, remainder_ielen);
214         }
215
216         *dst_ie++ = index;
217         *dst_ie++ = len;
218
219         _rtw_memcpy(dst_ie, data, len);
220         dst_ie += len;
221
222         /* copy remainder IE */
223         if (pbackup_remainder_ie) {
224                 _rtw_memcpy(dst_ie, pbackup_remainder_ie, remainder_ielen);
225
226                 rtw_mfree(pbackup_remainder_ie, remainder_ielen);
227         }
228
229         offset = (uint)(dst_ie - pie);
230         pnetwork->IELength = offset + remainder_ielen;
231 }
232
233 void rtw_remove_bcn_ie(_adapter *padapter, WLAN_BSSID_EX *pnetwork, u8 index)
234 {
235         u8 *p, *dst_ie = NULL, *premainder_ie = NULL, *pbackup_remainder_ie = NULL;
236         uint offset, ielen, ie_offset, remainder_ielen = 0;
237         u8      *pie = pnetwork->IEs;
238
239         p = rtw_get_ie(pie + _FIXED_IE_LENGTH_, index, &ielen, pnetwork->IELength - _FIXED_IE_LENGTH_);
240         if (p != NULL && ielen > 0) {
241                 ielen += 2;
242
243                 premainder_ie = p + ielen;
244
245                 ie_offset = (sint)(p - pie);
246
247                 remainder_ielen = pnetwork->IELength - ie_offset - ielen;
248
249                 dst_ie = p;
250         } else
251                 return;
252
253         if (remainder_ielen > 0) {
254                 pbackup_remainder_ie = rtw_malloc(remainder_ielen);
255                 if (pbackup_remainder_ie && premainder_ie)
256                         _rtw_memcpy(pbackup_remainder_ie, premainder_ie, remainder_ielen);
257         }
258
259         /* copy remainder IE */
260         if (pbackup_remainder_ie) {
261                 _rtw_memcpy(dst_ie, pbackup_remainder_ie, remainder_ielen);
262
263                 rtw_mfree(pbackup_remainder_ie, remainder_ielen);
264         }
265
266         offset = (uint)(dst_ie - pie);
267         pnetwork->IELength = offset + remainder_ielen;
268 }
269
270
271 u8 chk_sta_is_alive(struct sta_info *psta);
272 u8 chk_sta_is_alive(struct sta_info *psta)
273 {
274         u8 ret = _FALSE;
275 #ifdef DBG_EXPIRATION_CHK
276         RTW_INFO("sta:"MAC_FMT", rssi:%d, rx:"STA_PKTS_FMT", expire_to:%u, %s%ssq_len:%u\n"
277                  , MAC_ARG(psta->hwaddr)
278                  , psta->rssi_stat.UndecoratedSmoothedPWDB
279                  /* , STA_RX_PKTS_ARG(psta) */
280                  , STA_RX_PKTS_DIFF_ARG(psta)
281                  , psta->expire_to
282                  , psta->state & WIFI_SLEEP_STATE ? "PS, " : ""
283                  , psta->state & WIFI_STA_ALIVE_CHK_STATE ? "SAC, " : ""
284                  , psta->sleepq_len
285                 );
286 #endif
287
288         /* if(sta_last_rx_pkts(psta) == sta_rx_pkts(psta)) */
289         if ((psta->sta_stats.last_rx_data_pkts + psta->sta_stats.last_rx_ctrl_pkts) == (psta->sta_stats.rx_data_pkts + psta->sta_stats.rx_ctrl_pkts)) {
290 #if 0
291                 if (psta->state & WIFI_SLEEP_STATE)
292                         ret = _TRUE;
293 #endif
294         } else
295                 ret = _TRUE;
296
297         sta_update_last_rx_pkts(psta);
298
299         return ret;
300 }
301
302 void    expire_timeout_chk(_adapter *padapter)
303 {
304         _irqL irqL;
305         _list   *phead, *plist;
306         u8 updated = _FALSE;
307         struct sta_info *psta = NULL;
308         struct sta_priv *pstapriv = &padapter->stapriv;
309         u8 chk_alive_num = 0;
310         char chk_alive_list[NUM_STA];
311         int i;
312
313
314 #ifdef CONFIG_MCC_MODE
315         /*      then driver may check fail due to not recv client's frame under sitesurvey,
316          *      don't expire timeout chk under MCC under sitesurvey */
317
318         if (rtw_hal_mcc_link_status_chk(padapter, __func__) == _FALSE)
319                 return;
320 #endif
321
322         _enter_critical_bh(&pstapriv->auth_list_lock, &irqL);
323
324         phead = &pstapriv->auth_list;
325         plist = get_next(phead);
326
327         /* check auth_queue */
328 #ifdef DBG_EXPIRATION_CHK
329         if (rtw_end_of_queue_search(phead, plist) == _FALSE) {
330                 RTW_INFO(FUNC_NDEV_FMT" auth_list, cnt:%u\n"
331                         , FUNC_NDEV_ARG(padapter->pnetdev), pstapriv->auth_list_cnt);
332         }
333 #endif
334         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
335                 psta = LIST_CONTAINOR(plist, struct sta_info, auth_list);
336
337                 plist = get_next(plist);
338
339
340 #ifdef CONFIG_ATMEL_RC_PATCH
341                 if (_TRUE == _rtw_memcmp((void *)(pstapriv->atmel_rc_pattern), (void *)(psta->hwaddr), ETH_ALEN))
342                         continue;
343                 if (psta->flag_atmel_rc)
344                         continue;
345 #endif
346                 if (psta->expire_to > 0) {
347                         psta->expire_to--;
348                         if (psta->expire_to == 0) {
349                                 rtw_list_delete(&psta->auth_list);
350                                 pstapriv->auth_list_cnt--;
351
352                                 RTW_INFO("auth expire %02X%02X%02X%02X%02X%02X\n",
353                                         psta->hwaddr[0], psta->hwaddr[1], psta->hwaddr[2], psta->hwaddr[3], psta->hwaddr[4], psta->hwaddr[5]);
354
355                                 _exit_critical_bh(&pstapriv->auth_list_lock, &irqL);
356
357                                 /* _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL);        */
358                                 rtw_free_stainfo(padapter, psta);
359                                 /* _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL);         */
360
361                                 _enter_critical_bh(&pstapriv->auth_list_lock, &irqL);
362                         }
363                 }
364
365         }
366
367         _exit_critical_bh(&pstapriv->auth_list_lock, &irqL);
368         psta = NULL;
369
370
371         _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
372
373         phead = &pstapriv->asoc_list;
374         plist = get_next(phead);
375
376         /* check asoc_queue */
377 #ifdef DBG_EXPIRATION_CHK
378         if (rtw_end_of_queue_search(phead, plist) == _FALSE) {
379                 RTW_INFO(FUNC_NDEV_FMT" asoc_list, cnt:%u\n"
380                         , FUNC_NDEV_ARG(padapter->pnetdev), pstapriv->asoc_list_cnt);
381         }
382 #endif
383         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
384                 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
385                 plist = get_next(plist);
386 #ifdef CONFIG_ATMEL_RC_PATCH
387                 RTW_INFO("%s:%d  psta=%p, %02x,%02x||%02x,%02x  \n\n", __func__,  __LINE__,
388                         psta, pstapriv->atmel_rc_pattern[0], pstapriv->atmel_rc_pattern[5], psta->hwaddr[0], psta->hwaddr[5]);
389                 if (_TRUE == _rtw_memcmp((void *)pstapriv->atmel_rc_pattern, (void *)(psta->hwaddr), ETH_ALEN))
390                         continue;
391                 if (psta->flag_atmel_rc)
392                         continue;
393                 RTW_INFO("%s: debug line:%d\n", __func__, __LINE__);
394 #endif
395 #ifdef CONFIG_AUTO_AP_MODE
396                 if (psta->isrc)
397                         continue;
398 #endif
399                 if (chk_sta_is_alive(psta) || !psta->expire_to) {
400                         psta->expire_to = pstapriv->expire_to;
401                         psta->keep_alive_trycnt = 0;
402 #ifdef CONFIG_TX_MCAST2UNI
403                         psta->under_exist_checking = 0;
404 #endif  /* CONFIG_TX_MCAST2UNI */
405                 } else
406                         psta->expire_to--;
407
408 #ifndef CONFIG_ACTIVE_KEEP_ALIVE_CHECK
409 #ifdef CONFIG_80211N_HT
410 #ifdef CONFIG_TX_MCAST2UNI
411                 if ((psta->flags & WLAN_STA_HT) && (psta->htpriv.agg_enable_bitmap || psta->under_exist_checking)) {
412                         /* check sta by delba(addba) for 11n STA */
413                         /* ToDo: use CCX report to check for all STAs */
414                         /* RTW_INFO("asoc check by DELBA/ADDBA! (pstapriv->expire_to=%d s)(psta->expire_to=%d s), [%02x, %d]\n", pstapriv->expire_to*2, psta->expire_to*2, psta->htpriv.agg_enable_bitmap, psta->under_exist_checking); */
415
416                         if (psta->expire_to <= (pstapriv->expire_to - 50)) {
417                                 RTW_INFO("asoc expire by DELBA/ADDBA! (%d s)\n", (pstapriv->expire_to - psta->expire_to) * 2);
418                                 psta->under_exist_checking = 0;
419                                 psta->expire_to = 0;
420                         } else if (psta->expire_to <= (pstapriv->expire_to - 3) && (psta->under_exist_checking == 0)) {
421                                 RTW_INFO("asoc check by DELBA/ADDBA! (%d s)\n", (pstapriv->expire_to - psta->expire_to) * 2);
422                                 psta->under_exist_checking = 1;
423                                 /* tear down TX AMPDU */
424                                 send_delba(padapter, 1, psta->hwaddr);/*  */ /* originator */
425                                 psta->htpriv.agg_enable_bitmap = 0x0;/* reset */
426                                 psta->htpriv.candidate_tid_bitmap = 0x0;/* reset */
427                         }
428                 }
429 #endif /* CONFIG_TX_MCAST2UNI */
430 #endif /* CONFIG_80211N_HT */
431 #endif /* CONFIG_ACTIVE_KEEP_ALIVE_CHECK */
432
433                 if (psta->expire_to <= 0) {
434                         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
435
436                         if (padapter->registrypriv.wifi_spec == 1) {
437                                 psta->expire_to = pstapriv->expire_to;
438                                 continue;
439                         }
440
441 #ifndef CONFIG_ACTIVE_KEEP_ALIVE_CHECK
442 #ifdef CONFIG_80211N_HT
443
444 #define KEEP_ALIVE_TRYCNT (3)
445
446                         if (psta->keep_alive_trycnt > 0 && psta->keep_alive_trycnt <= KEEP_ALIVE_TRYCNT) {
447                                 if (psta->state & WIFI_STA_ALIVE_CHK_STATE)
448                                         psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
449                                 else
450                                         psta->keep_alive_trycnt = 0;
451
452                         } else if ((psta->keep_alive_trycnt > KEEP_ALIVE_TRYCNT) && !(psta->state & WIFI_STA_ALIVE_CHK_STATE))
453                                 psta->keep_alive_trycnt = 0;
454                         if ((psta->htpriv.ht_option == _TRUE) && (psta->htpriv.ampdu_enable == _TRUE)) {
455                                 uint priority = 1; /* test using BK */
456                                 u8 issued = 0;
457
458                                 /* issued = (psta->htpriv.agg_enable_bitmap>>priority)&0x1; */
459                                 issued |= (psta->htpriv.candidate_tid_bitmap >> priority) & 0x1;
460
461                                 if (0 == issued) {
462                                         if (!(psta->state & WIFI_STA_ALIVE_CHK_STATE)) {
463                                                 psta->htpriv.candidate_tid_bitmap |= BIT((u8)priority);
464
465                                                 if (psta->state & WIFI_SLEEP_STATE)
466                                                         psta->expire_to = 2; /* 2x2=4 sec */
467                                                 else
468                                                         psta->expire_to = 1; /* 2 sec */
469
470                                                 psta->state |= WIFI_STA_ALIVE_CHK_STATE;
471
472                                                 /* add_ba_hdl(padapter, (u8*)paddbareq_parm); */
473
474                                                 RTW_INFO("issue addba_req to check if sta alive, keep_alive_trycnt=%d\n", psta->keep_alive_trycnt);
475
476                                                 issue_addba_req(padapter, psta->hwaddr, (u8)priority);
477
478                                                 _set_timer(&psta->addba_retry_timer, ADDBA_TO);
479
480                                                 psta->keep_alive_trycnt++;
481
482                                                 continue;
483                                         }
484                                 }
485                         }
486                         if (psta->keep_alive_trycnt > 0 && psta->state & WIFI_STA_ALIVE_CHK_STATE) {
487                                 psta->keep_alive_trycnt = 0;
488                                 psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
489                                 RTW_INFO("change to another methods to check alive if staion is at ps mode\n");
490                         }
491
492 #endif /* CONFIG_80211N_HT */
493 #endif /* CONFIG_ACTIVE_KEEP_ALIVE_CHECK         */
494                         if (psta->state & WIFI_SLEEP_STATE) {
495                                 if (!(psta->state & WIFI_STA_ALIVE_CHK_STATE)) {
496                                         /* to check if alive by another methods if staion is at ps mode.                                         */
497                                         psta->expire_to = pstapriv->expire_to;
498                                         psta->state |= WIFI_STA_ALIVE_CHK_STATE;
499
500                                         /* RTW_INFO("alive chk, sta:" MAC_FMT " is at ps mode!\n", MAC_ARG(psta->hwaddr)); */
501
502                                         /* to update bcn with tim_bitmap for this station */
503                                         pstapriv->tim_bitmap |= BIT(psta->aid);
504                                         update_beacon(padapter, _TIM_IE_, NULL, _TRUE);
505
506                                         if (!pmlmeext->active_keep_alive_check)
507                                                 continue;
508                                 }
509                         }
510 #ifdef CONFIG_ACTIVE_KEEP_ALIVE_CHECK
511                         if (pmlmeext->active_keep_alive_check) {
512                                 int stainfo_offset;
513
514                                 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
515                                 if (stainfo_offset_valid(stainfo_offset))
516                                         chk_alive_list[chk_alive_num++] = stainfo_offset;
517
518                                 continue;
519                         }
520 #endif /* CONFIG_ACTIVE_KEEP_ALIVE_CHECK */
521                         rtw_list_delete(&psta->asoc_list);
522                         pstapriv->asoc_list_cnt--;
523                         RTW_INFO("asoc expire "MAC_FMT", state=0x%x\n", MAC_ARG(psta->hwaddr), psta->state);
524                         updated = ap_free_sta(padapter, psta, _FALSE, WLAN_REASON_DEAUTH_LEAVING, _TRUE);
525                 } else {
526                         /* TODO: Aging mechanism to digest frames in sleep_q to avoid running out of xmitframe */
527                         if (psta->sleepq_len > (NR_XMITFRAME / pstapriv->asoc_list_cnt)
528                             && padapter->xmitpriv.free_xmitframe_cnt < ((NR_XMITFRAME / pstapriv->asoc_list_cnt) / 2)
529                            ) {
530                                 RTW_INFO("%s sta:"MAC_FMT", sleepq_len:%u, free_xmitframe_cnt:%u, asoc_list_cnt:%u, clear sleep_q\n", __func__
531                                          , MAC_ARG(psta->hwaddr)
532                                         , psta->sleepq_len, padapter->xmitpriv.free_xmitframe_cnt, pstapriv->asoc_list_cnt);
533                                 wakeup_sta_to_xmit(padapter, psta);
534                         }
535                 }
536         }
537
538         _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
539
540 #ifdef CONFIG_ACTIVE_KEEP_ALIVE_CHECK
541         if (chk_alive_num) {
542
543                 u8 backup_oper_channel = 0, switch_channel = _TRUE;
544                 struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
545
546 #ifdef CONFIG_MCC_MODE
547                 if (MCC_EN(padapter)) {
548                         /* driver doesn't switch channel under MCC */
549                         if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC))
550                                 switch_channel = _FALSE;
551                 }
552 #endif
553                 /* switch to correct channel of current network  before issue keep-alive frames */
554                 if (switch_channel) {
555                         if (rtw_get_oper_ch(padapter) != pmlmeext->cur_channel) {
556                                 backup_oper_channel = rtw_get_oper_ch(padapter);
557                                 SelectChannel(padapter, pmlmeext->cur_channel);
558                         }
559                 }
560
561                 /* issue null data to check sta alive*/
562                 for (i = 0; i < chk_alive_num; i++) {
563                         int ret = _FAIL;
564
565                         psta = rtw_get_stainfo_by_offset(pstapriv, chk_alive_list[i]);
566 #ifdef CONFIG_ATMEL_RC_PATCH
567                         if (_TRUE == _rtw_memcmp(pstapriv->atmel_rc_pattern, psta->hwaddr, ETH_ALEN))
568                                 continue;
569                         if (psta->flag_atmel_rc)
570                                 continue;
571 #endif
572                         if (!(psta->state & _FW_LINKED))
573                                 continue;
574
575                         if (psta->state & WIFI_SLEEP_STATE)
576                                 ret = issue_nulldata(padapter, psta->hwaddr, 0, 1, 50);
577                         else
578                                 ret = issue_nulldata(padapter, psta->hwaddr, 0, 3, 50);
579
580                         psta->keep_alive_trycnt++;
581                         if (ret == _SUCCESS) {
582                                 RTW_INFO("asoc check, sta(" MAC_FMT ") is alive\n", MAC_ARG(psta->hwaddr));
583                                 psta->expire_to = pstapriv->expire_to;
584                                 psta->keep_alive_trycnt = 0;
585                                 continue;
586                         } else if (psta->keep_alive_trycnt <= 3) {
587                                 RTW_INFO("ack check for asoc expire, keep_alive_trycnt=%d\n", psta->keep_alive_trycnt);
588                                 psta->expire_to = 1;
589                                 continue;
590                         }
591
592                         psta->keep_alive_trycnt = 0;
593                         RTW_INFO("asoc expire "MAC_FMT", state=0x%x\n", MAC_ARG(psta->hwaddr), psta->state);
594                         _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
595                         if (rtw_is_list_empty(&psta->asoc_list) == _FALSE) {
596                                 rtw_list_delete(&psta->asoc_list);
597                                 pstapriv->asoc_list_cnt--;
598                                 updated = ap_free_sta(padapter, psta, _FALSE, WLAN_REASON_DEAUTH_LEAVING, _TRUE);
599                         }
600                         _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
601
602                 }
603
604                 if (switch_channel) {
605                         if (backup_oper_channel > 0) /* back to the original operation channel */
606                                 SelectChannel(padapter, backup_oper_channel);
607                 }
608         }
609 #endif /* CONFIG_ACTIVE_KEEP_ALIVE_CHECK */
610
611         associated_clients_update(padapter, updated, STA_INFO_UPDATE_ALL);
612 }
613
614 void add_RATid(_adapter *padapter, struct sta_info *psta, u8 rssi_level)
615 {
616         int i;
617         u8 rf_type;
618         unsigned char sta_band = 0, shortGIrate = _FALSE;
619         u64 tx_ra_bitmap = 0;
620         struct ht_priv  *psta_ht = NULL;
621         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
622         WLAN_BSSID_EX *pcur_network = (WLAN_BSSID_EX *)&pmlmepriv->cur_network.network;
623
624 #ifdef CONFIG_80211N_HT
625         if (psta)
626                 psta_ht = &psta->htpriv;
627         else
628                 return;
629 #endif /* CONFIG_80211N_HT */
630
631         if (!(psta->state & _FW_LINKED))
632                 return;
633
634 #if 0/* gtest */
635         if (get_rf_mimo_mode(padapter) == RTL8712_RF_2T2R) {
636                 /* is this a 2r STA? */
637                 if ((pstat->tx_ra_bitmap & 0x0ff00000) != 0 && !(priv->pshare->has_2r_sta & BIT(pstat->aid))) {
638                         priv->pshare->has_2r_sta |= BIT(pstat->aid);
639                         if (rtw_read16(padapter, 0x102501f6) != 0xffff) {
640                                 rtw_write16(padapter, 0x102501f6, 0xffff);
641                                 reset_1r_sta_RA(priv, 0xffff);
642                                 Switch_1SS_Antenna(priv, 3);
643                         }
644                 } else { /* bg or 1R STA? */
645                         if ((priv->pmib->dot11BssType.net_work_type & WIRELESS_11N) && pstat->ht_cap_len && priv->pshare->has_2r_sta == 0) {
646                                 if (rtw_read16(padapter, 0x102501f6) != 0x7777) {
647                                         /* MCS7 SGI */
648                                         rtw_write16(padapter, 0x102501f6, 0x7777);
649                                         reset_1r_sta_RA(priv, 0x7777);
650                                         Switch_1SS_Antenna(priv, 2);
651                                 }
652                         }
653                 }
654
655         }
656
657         if ((pstat->rssi_level < 1) || (pstat->rssi_level > 3)) {
658                 if (pstat->rssi >= priv->pshare->rf_ft_var.raGoDownUpper)
659                         pstat->rssi_level = 1;
660                 else if ((pstat->rssi >= priv->pshare->rf_ft_var.raGoDown20MLower) ||
661                          ((priv->pshare->is_40m_bw) && (pstat->ht_cap_len) &&
662                         (pstat->rssi >= priv->pshare->rf_ft_var.raGoDown40MLower) &&
663                         (pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SUPPORT_CH_WDTH_))))
664                         pstat->rssi_level = 2;
665                 else
666                         pstat->rssi_level = 3;
667         }
668
669         /* rate adaptive by rssi */
670         if ((priv->pmib->dot11BssType.net_work_type & WIRELESS_11N) && pstat->ht_cap_len) {
671                 if ((get_rf_mimo_mode(priv) == MIMO_1T2R) || (get_rf_mimo_mode(priv) == MIMO_1T1R)) {
672                         switch (pstat->rssi_level) {
673                         case 1:
674                                 pstat->tx_ra_bitmap &= 0x100f0000;
675                                 break;
676                         case 2:
677                                 pstat->tx_ra_bitmap &= 0x100ff000;
678                                 break;
679                         case 3:
680                                 if (priv->pshare->is_40m_bw)
681                                         pstat->tx_ra_bitmap &= 0x100ff005;
682                                 else
683                                         pstat->tx_ra_bitmap &= 0x100ff001;
684
685                                 break;
686                         }
687                 } else {
688                         switch (pstat->rssi_level) {
689                         case 1:
690                                 pstat->tx_ra_bitmap &= 0x1f0f0000;
691                                 break;
692                         case 2:
693                                 pstat->tx_ra_bitmap &= 0x1f0ff000;
694                                 break;
695                         case 3:
696                                 if (priv->pshare->is_40m_bw)
697                                         pstat->tx_ra_bitmap &= 0x000ff005;
698                                 else
699                                         pstat->tx_ra_bitmap &= 0x000ff001;
700
701                                 break;
702                         }
703
704                         /* Don't need to mask high rates due to new rate adaptive parameters */
705                         /* if (pstat->is_broadcom_sta)           */ /* use MCS12 as the highest rate vs. Broadcom sta */
706                         /*      pstat->tx_ra_bitmap &= 0x81ffffff; */
707
708                         /* NIC driver will report not supporting MCS15 and MCS14 in asoc req */
709                         /* if (pstat->is_rtl8190_sta && !pstat->is_2t_mimo_sta) */
710                         /* pstat->tx_ra_bitmap &= 0x83ffffff;            */ /* if Realtek 1x2 sta, don't use MCS15 and MCS14 */
711                 }
712         } else if ((priv->pmib->dot11BssType.net_work_type & WIRELESS_11G) && isErpSta(pstat)) {
713                 switch (pstat->rssi_level) {
714                 case 1:
715                         pstat->tx_ra_bitmap &= 0x00000f00;
716                         break;
717                 case 2:
718                         pstat->tx_ra_bitmap &= 0x00000ff0;
719                         break;
720                 case 3:
721                         pstat->tx_ra_bitmap &= 0x00000ff5;
722                         break;
723                 }
724         } else
725                 pstat->tx_ra_bitmap &= 0x0000000d;
726
727         /* disable tx short GI when station cannot rx MCS15(AP is 2T2R) */
728         /* disable tx short GI when station cannot rx MCS7 (AP is 1T2R or 1T1R) */
729         /* if there is only 1r STA and we are 2T2R, DO NOT mask SGI rate */
730         if ((!(pstat->tx_ra_bitmap & 0x8000000) && (priv->pshare->has_2r_sta > 0) && (get_rf_mimo_mode(padapter) == RTL8712_RF_2T2R)) ||
731             (!(pstat->tx_ra_bitmap & 0x80000) && (get_rf_mimo_mode(padapter) != RTL8712_RF_2T2R)))
732                 pstat->tx_ra_bitmap &= ~BIT(28);
733 #endif
734
735         rtw_hal_update_sta_rate_mask(padapter, psta);
736         tx_ra_bitmap = psta->ra_mask;
737
738         shortGIrate = query_ra_short_GI(psta);
739
740         if (pcur_network->Configuration.DSConfig > 14) {
741
742                 if (tx_ra_bitmap & 0xffff000)
743                         sta_band |= WIRELESS_11_5N ;
744
745                 if (tx_ra_bitmap & 0xff0)
746                         sta_band |= WIRELESS_11A;
747
748                 /* 5G band */
749 #ifdef CONFIG_80211AC_VHT
750                 if (psta->vhtpriv.vht_option)
751                         sta_band = WIRELESS_11_5AC;
752 #endif
753
754         } else {
755                 if (tx_ra_bitmap & 0xffff000)
756                         sta_band |= WIRELESS_11_24N;
757
758                 if (tx_ra_bitmap & 0xff0)
759                         sta_band |= WIRELESS_11G;
760
761                 if (tx_ra_bitmap & 0x0f)
762                         sta_band |= WIRELESS_11B;
763         }
764
765         psta->wireless_mode = sta_band;
766         psta->raid = rtw_hal_networktype_to_raid(padapter, psta);
767
768         if (psta->aid < NUM_STA) {
769                 u8      arg[4] = {0};
770
771                 arg[0] = psta->mac_id;
772                 arg[1] = psta->raid;
773                 arg[2] = shortGIrate;
774                 arg[3] = psta->init_rate;
775
776                 RTW_INFO("%s=> mac_id:%d , raid:%d , shortGIrate=%d, tx_ra_bitmap:0x%016llx, networkType:0x%02x\n",
777                         __FUNCTION__, psta->mac_id, psta->raid, shortGIrate, tx_ra_bitmap, psta->wireless_mode);
778
779                 rtw_hal_add_ra_tid(padapter, tx_ra_bitmap, arg, rssi_level);
780         } else
781                 RTW_INFO("station aid %d exceed the max number\n", psta->aid);
782
783 }
784
785 void update_bmc_sta(_adapter *padapter)
786 {
787         _irqL   irqL;
788         unsigned char   network_type;
789         int supportRateNum = 0;
790         u64 tx_ra_bitmap = 0;
791         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
792         WLAN_BSSID_EX *pcur_network = (WLAN_BSSID_EX *)&pmlmepriv->cur_network.network;
793         struct sta_info *psta = rtw_get_bcmc_stainfo(padapter);
794
795         if (psta) {
796                 psta->aid = 0;/* default set to 0 */
797                 psta->qos_option = 0;
798 #ifdef CONFIG_80211N_HT
799                 psta->htpriv.ht_option = _FALSE;
800 #endif /* CONFIG_80211N_HT */
801
802                 psta->ieee8021x_blocked = 0;
803
804                 _rtw_memset((void *)&psta->sta_stats, 0, sizeof(struct stainfo_stats));
805
806                 /* psta->dot118021XPrivacy = _NO_PRIVACY_; */ /* !!! remove it, because it has been set before this. */
807
808                 /* prepare for add_RATid                 */
809                 supportRateNum = rtw_get_rateset_len((u8 *)&pcur_network->SupportedRates);
810                 network_type = rtw_check_network_type((u8 *)&pcur_network->SupportedRates, supportRateNum, pcur_network->Configuration.DSConfig);
811                 if (IsSupportedTxCCK(network_type))
812                         network_type = WIRELESS_11B;
813                 else if (network_type == WIRELESS_INVALID) { /* error handling */
814                         if (pcur_network->Configuration.DSConfig > 14)
815                                 network_type = WIRELESS_11A;
816                         else
817                                 network_type = WIRELESS_11B;
818                 }
819                 update_sta_basic_rate(psta, network_type);
820                 psta->wireless_mode = network_type;
821
822                 rtw_hal_update_sta_rate_mask(padapter, psta);
823                 tx_ra_bitmap = psta->ra_mask;
824
825                 psta->raid = rtw_hal_networktype_to_raid(padapter, psta);
826
827                 rtw_sta_media_status_rpt(padapter, psta, 1);
828
829                 _enter_critical_bh(&psta->lock, &irqL);
830                 psta->state = _FW_LINKED;
831                 _exit_critical_bh(&psta->lock, &irqL);
832
833         } else
834                 RTW_INFO("add_RATid_bmc_sta error!\n");
835
836 }
837
838 /* notes:
839  * AID: 1~MAX for sta and 0 for bc/mc in ap/adhoc mode  */
840 void update_sta_info_apmode(_adapter *padapter, struct sta_info *psta)
841 {
842         _irqL   irqL;
843         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
844         struct security_priv *psecuritypriv = &padapter->securitypriv;
845         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
846 #ifdef CONFIG_80211N_HT
847         struct ht_priv  *phtpriv_ap = &pmlmepriv->htpriv;
848         struct ht_priv  *phtpriv_sta = &psta->htpriv;
849 #endif /* CONFIG_80211N_HT */
850         u8      cur_ldpc_cap = 0, cur_stbc_cap = 0, cur_beamform_cap = 0;
851         /* set intf_tag to if1 */
852         /* psta->intf_tag = 0; */
853
854         RTW_INFO("%s\n", __FUNCTION__);
855
856         /*alloc macid when call rtw_alloc_stainfo(),release macid when call rtw_free_stainfo()*/
857
858         /* ap mode */
859         rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, _TRUE);
860
861         if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X)
862                 psta->ieee8021x_blocked = _TRUE;
863         else
864                 psta->ieee8021x_blocked = _FALSE;
865
866
867         /* update sta's cap */
868
869         /* ERP */
870         VCS_update(padapter, psta);
871 #ifdef CONFIG_80211N_HT
872         /* HT related cap */
873         if (phtpriv_sta->ht_option) {
874                 /* check if sta supports rx ampdu */
875                 phtpriv_sta->ampdu_enable = phtpriv_ap->ampdu_enable;
876
877                 phtpriv_sta->rx_ampdu_min_spacing = (phtpriv_sta->ht_cap.ampdu_params_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
878
879                 /* bwmode */
880                 if ((phtpriv_sta->ht_cap.cap_info & phtpriv_ap->ht_cap.cap_info) & cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH))
881                         psta->bw_mode = CHANNEL_WIDTH_40;
882                 else
883                         psta->bw_mode = CHANNEL_WIDTH_20;
884
885                 if (psta->ht_40mhz_intolerant)
886                         psta->bw_mode = CHANNEL_WIDTH_20;
887
888                 if (pmlmeext->cur_bwmode < psta->bw_mode)
889                         psta->bw_mode = pmlmeext->cur_bwmode;
890
891                 phtpriv_sta->ch_offset = pmlmeext->cur_ch_offset;
892
893
894                 /* check if sta support s Short GI 20M */
895                 if ((phtpriv_sta->ht_cap.cap_info & phtpriv_ap->ht_cap.cap_info) & cpu_to_le16(IEEE80211_HT_CAP_SGI_20))
896                         phtpriv_sta->sgi_20m = _TRUE;
897
898                 /* check if sta support s Short GI 40M */
899                 if ((phtpriv_sta->ht_cap.cap_info & phtpriv_ap->ht_cap.cap_info) & cpu_to_le16(IEEE80211_HT_CAP_SGI_40)) {
900                         if (psta->bw_mode == CHANNEL_WIDTH_40) /* according to psta->bw_mode */
901                                 phtpriv_sta->sgi_40m = _TRUE;
902                         else
903                                 phtpriv_sta->sgi_40m = _FALSE;
904                 }
905
906                 psta->qos_option = _TRUE;
907
908                 /* B0 Config LDPC Coding Capability */
909                 if (TEST_FLAG(phtpriv_ap->ldpc_cap, LDPC_HT_ENABLE_TX) &&
910                     GET_HT_CAP_ELE_LDPC_CAP((u8 *)(&phtpriv_sta->ht_cap))) {
911                         SET_FLAG(cur_ldpc_cap, (LDPC_HT_ENABLE_TX | LDPC_HT_CAP_TX));
912                         RTW_INFO("Enable HT Tx LDPC for STA(%d)\n", psta->aid);
913                 }
914
915                 /* B7 B8 B9 Config STBC setting */
916                 if (TEST_FLAG(phtpriv_ap->stbc_cap, STBC_HT_ENABLE_TX) &&
917                     GET_HT_CAP_ELE_RX_STBC((u8 *)(&phtpriv_sta->ht_cap))) {
918                         SET_FLAG(cur_stbc_cap, (STBC_HT_ENABLE_TX | STBC_HT_CAP_TX));
919                         RTW_INFO("Enable HT Tx STBC for STA(%d)\n", psta->aid);
920                 }
921
922 #ifdef CONFIG_BEAMFORMING
923                 /*Config Tx beamforming setting*/
924                 if (TEST_FLAG(phtpriv_ap->beamform_cap, BEAMFORMING_HT_BEAMFORMEE_ENABLE) &&
925                     GET_HT_CAP_TXBF_EXPLICIT_COMP_STEERING_CAP((u8 *)(&phtpriv_sta->ht_cap))) {
926                         SET_FLAG(cur_beamform_cap, BEAMFORMING_HT_BEAMFORMER_ENABLE);
927                         /*Shift to BEAMFORMING_HT_BEAMFORMEE_CHNL_EST_CAP*/
928                         SET_FLAG(cur_beamform_cap, GET_HT_CAP_TXBF_CHNL_ESTIMATION_NUM_ANTENNAS((u8 *)(&phtpriv_sta->ht_cap)) << 6);
929                 }
930
931                 if (TEST_FLAG(phtpriv_ap->beamform_cap, BEAMFORMING_HT_BEAMFORMER_ENABLE) &&
932                     GET_HT_CAP_TXBF_EXPLICIT_COMP_FEEDBACK_CAP((u8 *)(&phtpriv_sta->ht_cap))) {
933                         SET_FLAG(cur_beamform_cap, BEAMFORMING_HT_BEAMFORMEE_ENABLE);
934                         /*Shift to BEAMFORMING_HT_BEAMFORMER_STEER_NUM*/
935                         SET_FLAG(cur_beamform_cap, GET_HT_CAP_TXBF_COMP_STEERING_NUM_ANTENNAS((u8 *)(&phtpriv_sta->ht_cap)) << 4);
936                 }
937                 if (cur_beamform_cap)
938                         RTW_INFO("Client STA(%d) HT Beamforming Cap = 0x%02X\n", psta->aid, cur_beamform_cap);
939 #endif /*CONFIG_BEAMFORMING*/
940         } else {
941                 phtpriv_sta->ampdu_enable = _FALSE;
942
943                 phtpriv_sta->sgi_20m = _FALSE;
944                 phtpriv_sta->sgi_40m = _FALSE;
945                 psta->bw_mode = CHANNEL_WIDTH_20;
946                 phtpriv_sta->ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
947         }
948
949         phtpriv_sta->ldpc_cap = cur_ldpc_cap;
950         phtpriv_sta->stbc_cap = cur_stbc_cap;
951         phtpriv_sta->beamform_cap = cur_beamform_cap;
952
953         /* Rx AMPDU */
954         send_delba(padapter, 0, psta->hwaddr);/* recipient */
955
956         /* TX AMPDU */
957         send_delba(padapter, 1, psta->hwaddr);/*  */ /* originator */
958         phtpriv_sta->agg_enable_bitmap = 0x0;/* reset */
959         phtpriv_sta->candidate_tid_bitmap = 0x0;/* reset */
960 #endif /* CONFIG_80211N_HT */
961
962 #ifdef CONFIG_80211AC_VHT
963         update_sta_vht_info_apmode(padapter, psta);
964 #endif
965
966         update_ldpc_stbc_cap(psta);
967
968         /* todo: init other variables */
969
970         _rtw_memset((void *)&psta->sta_stats, 0, sizeof(struct stainfo_stats));
971
972
973         /* add ratid */
974         /* add_RATid(padapter, psta); */ /* move to ap_sta_info_defer_update() */
975
976
977         _enter_critical_bh(&psta->lock, &irqL);
978         psta->state |= _FW_LINKED;
979         _exit_critical_bh(&psta->lock, &irqL);
980
981
982 }
983
984 static void update_ap_info(_adapter *padapter, struct sta_info *psta)
985 {
986         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
987         WLAN_BSSID_EX *pnetwork = (WLAN_BSSID_EX *)&pmlmepriv->cur_network.network;
988         struct security_priv *psecuritypriv = &padapter->securitypriv;
989         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
990 #ifdef CONFIG_80211N_HT
991         struct ht_priv  *phtpriv_ap = &pmlmepriv->htpriv;
992 #endif /* CONFIG_80211N_HT */
993
994         psta->wireless_mode = pmlmeext->cur_wireless_mode;
995
996         psta->bssratelen = rtw_get_rateset_len(pnetwork->SupportedRates);
997         _rtw_memcpy(psta->bssrateset, pnetwork->SupportedRates, psta->bssratelen);
998
999 #ifdef CONFIG_80211N_HT
1000         /* HT related cap */
1001         if (phtpriv_ap->ht_option) {
1002                 /* check if sta supports rx ampdu */
1003                 /* phtpriv_ap->ampdu_enable = phtpriv_ap->ampdu_enable; */
1004
1005                 /* check if sta support s Short GI 20M */
1006                 if ((phtpriv_ap->ht_cap.cap_info) & cpu_to_le16(IEEE80211_HT_CAP_SGI_20))
1007                         phtpriv_ap->sgi_20m = _TRUE;
1008                 /* check if sta support s Short GI 40M */
1009                 if ((phtpriv_ap->ht_cap.cap_info) & cpu_to_le16(IEEE80211_HT_CAP_SGI_40))
1010                         phtpriv_ap->sgi_40m = _TRUE;
1011
1012                 psta->qos_option = _TRUE;
1013         } else {
1014                 phtpriv_ap->ampdu_enable = _FALSE;
1015
1016                 phtpriv_ap->sgi_20m = _FALSE;
1017                 phtpriv_ap->sgi_40m = _FALSE;
1018         }
1019
1020         psta->bw_mode = pmlmeext->cur_bwmode;
1021         phtpriv_ap->ch_offset = pmlmeext->cur_ch_offset;
1022
1023         phtpriv_ap->agg_enable_bitmap = 0x0;/* reset */
1024         phtpriv_ap->candidate_tid_bitmap = 0x0;/* reset */
1025
1026         _rtw_memcpy(&psta->htpriv, &pmlmepriv->htpriv, sizeof(struct ht_priv));
1027
1028 #ifdef CONFIG_80211AC_VHT
1029         _rtw_memcpy(&psta->vhtpriv, &pmlmepriv->vhtpriv, sizeof(struct vht_priv));
1030 #endif /* CONFIG_80211AC_VHT */
1031
1032 #endif /* CONFIG_80211N_HT */
1033
1034         psta->state |= WIFI_AP_STATE; /* Aries, add,fix bug of flush_cam_entry at STOP AP mode , 0724 */
1035 }
1036
1037 static void rtw_set_hw_wmm_param(_adapter *padapter)
1038 {
1039         u8      ACI, ACM, AIFS, ECWMin, ECWMax, aSifsTime;
1040         u8      acm_mask;
1041         u16     TXOP;
1042         u32     acParm, i;
1043         u32     edca[4], inx[4];
1044         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1045         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1046         struct xmit_priv                *pxmitpriv = &padapter->xmitpriv;
1047         struct registry_priv    *pregpriv = &padapter->registrypriv;
1048
1049         acm_mask = 0;
1050
1051         if (IsSupported5G(pmlmeext->cur_wireless_mode) ||
1052             (pmlmeext->cur_wireless_mode & WIRELESS_11_24N))
1053                 aSifsTime = 16;
1054         else
1055                 aSifsTime = 10;
1056
1057         if (pmlmeinfo->WMM_enable == 0) {
1058                 padapter->mlmepriv.acm_mask = 0;
1059
1060                 AIFS = aSifsTime + (2 * pmlmeinfo->slotTime);
1061
1062                 if (pmlmeext->cur_wireless_mode & (WIRELESS_11G | WIRELESS_11A)) {
1063                         ECWMin = 4;
1064                         ECWMax = 10;
1065                 } else if (pmlmeext->cur_wireless_mode & WIRELESS_11B) {
1066                         ECWMin = 5;
1067                         ECWMax = 10;
1068                 } else {
1069                         ECWMin = 4;
1070                         ECWMax = 10;
1071                 }
1072
1073                 TXOP = 0;
1074                 acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
1075                 rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BE, (u8 *)(&acParm));
1076                 rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BK, (u8 *)(&acParm));
1077                 rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VI, (u8 *)(&acParm));
1078
1079                 ECWMin = 2;
1080                 ECWMax = 3;
1081                 TXOP = 0x2f;
1082                 acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
1083                 rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VO, (u8 *)(&acParm));
1084
1085         } else {
1086                 edca[0] = edca[1] = edca[2] = edca[3] = 0;
1087
1088                 /*TODO:*/
1089                 acm_mask = 0;
1090                 padapter->mlmepriv.acm_mask = acm_mask;
1091
1092 #if 0
1093                 /* BK */
1094                 /* AIFS = AIFSN * slot time + SIFS - r2t phy delay */
1095 #endif
1096                 AIFS = (7 * pmlmeinfo->slotTime) + aSifsTime;
1097                 ECWMin = 4;
1098                 ECWMax = 10;
1099                 TXOP = 0;
1100                 acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
1101                 rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BK, (u8 *)(&acParm));
1102                 edca[XMIT_BK_QUEUE] = acParm;
1103                 RTW_INFO("WMM(BK): %x\n", acParm);
1104
1105                 /* BE */
1106                 AIFS = (3 * pmlmeinfo->slotTime) + aSifsTime;
1107                 ECWMin = 4;
1108                 ECWMax = 6;
1109                 TXOP = 0;
1110                 acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
1111                 rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BE, (u8 *)(&acParm));
1112                 edca[XMIT_BE_QUEUE] = acParm;
1113                 RTW_INFO("WMM(BE): %x\n", acParm);
1114
1115                 /* VI */
1116                 AIFS = (1 * pmlmeinfo->slotTime) + aSifsTime;
1117                 ECWMin = 3;
1118                 ECWMax = 4;
1119                 TXOP = 94;
1120                 acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
1121                 rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VI, (u8 *)(&acParm));
1122                 edca[XMIT_VI_QUEUE] = acParm;
1123                 RTW_INFO("WMM(VI): %x\n", acParm);
1124
1125                 /* VO */
1126                 AIFS = (1 * pmlmeinfo->slotTime) + aSifsTime;
1127                 ECWMin = 2;
1128                 ECWMax = 3;
1129                 TXOP = 47;
1130                 acParm = AIFS | (ECWMin << 8) | (ECWMax << 12) | (TXOP << 16);
1131                 rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VO, (u8 *)(&acParm));
1132                 edca[XMIT_VO_QUEUE] = acParm;
1133                 RTW_INFO("WMM(VO): %x\n", acParm);
1134
1135
1136                 if (padapter->registrypriv.acm_method == 1)
1137                         rtw_hal_set_hwreg(padapter, HW_VAR_ACM_CTRL, (u8 *)(&acm_mask));
1138                 else
1139                         padapter->mlmepriv.acm_mask = acm_mask;
1140
1141                 inx[0] = 0;
1142                 inx[1] = 1;
1143                 inx[2] = 2;
1144                 inx[3] = 3;
1145
1146                 if (pregpriv->wifi_spec == 1) {
1147                         u32     j, tmp, change_inx = _FALSE;
1148
1149                         /* entry indx: 0->vo, 1->vi, 2->be, 3->bk. */
1150                         for (i = 0 ; i < 4 ; i++) {
1151                                 for (j = i + 1 ; j < 4 ; j++) {
1152                                         /* compare CW and AIFS */
1153                                         if ((edca[j] & 0xFFFF) < (edca[i] & 0xFFFF))
1154                                                 change_inx = _TRUE;
1155                                         else if ((edca[j] & 0xFFFF) == (edca[i] & 0xFFFF)) {
1156                                                 /* compare TXOP */
1157                                                 if ((edca[j] >> 16) > (edca[i] >> 16))
1158                                                         change_inx = _TRUE;
1159                                         }
1160
1161                                         if (change_inx) {
1162                                                 tmp = edca[i];
1163                                                 edca[i] = edca[j];
1164                                                 edca[j] = tmp;
1165
1166                                                 tmp = inx[i];
1167                                                 inx[i] = inx[j];
1168                                                 inx[j] = tmp;
1169
1170                                                 change_inx = _FALSE;
1171                                         }
1172                                 }
1173                         }
1174                 }
1175
1176                 for (i = 0 ; i < 4 ; i++) {
1177                         pxmitpriv->wmm_para_seq[i] = inx[i];
1178                         RTW_INFO("wmm_para_seq(%d): %d\n", i, pxmitpriv->wmm_para_seq[i]);
1179                 }
1180
1181         }
1182
1183 }
1184
1185 static void update_hw_ht_param(_adapter *padapter)
1186 {
1187         unsigned char           max_AMPDU_len;
1188         unsigned char           min_MPDU_spacing;
1189         struct registry_priv    *pregpriv = &padapter->registrypriv;
1190         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1191         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1192
1193         RTW_INFO("%s\n", __FUNCTION__);
1194
1195
1196         /* handle A-MPDU parameter field */
1197         /*
1198                 AMPDU_para [1:0]:Max AMPDU Len => 0:8k , 1:16k, 2:32k, 3:64k
1199                 AMPDU_para [4:2]:Min MPDU Start Spacing
1200         */
1201         max_AMPDU_len = pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x03;
1202
1203         min_MPDU_spacing = (pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para & 0x1c) >> 2;
1204
1205         rtw_hal_set_hwreg(padapter, HW_VAR_AMPDU_MIN_SPACE, (u8 *)(&min_MPDU_spacing));
1206
1207         rtw_hal_set_hwreg(padapter, HW_VAR_AMPDU_FACTOR, (u8 *)(&max_AMPDU_len));
1208
1209         /*  */
1210         /* Config SM Power Save setting */
1211         /*  */
1212         pmlmeinfo->SM_PS = (pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info & 0x0C) >> 2;
1213         if (pmlmeinfo->SM_PS == WLAN_HT_CAP_SM_PS_STATIC) {
1214 #if 0
1215                 u8 i;
1216                 /* update the MCS rates */
1217                 for (i = 0; i < 16; i++)
1218                         pmlmeinfo->HT_caps.HT_cap_element.MCS_rate[i] &= MCS_rate_1R[i];
1219 #endif
1220                 RTW_INFO("%s(): WLAN_HT_CAP_SM_PS_STATIC\n", __FUNCTION__);
1221         }
1222
1223         /*  */
1224         /* Config current HT Protection mode. */
1225         /*  */
1226         /* pmlmeinfo->HT_protection = pmlmeinfo->HT_info.infos[1] & 0x3; */
1227
1228 }
1229
1230 static void rtw_ap_check_scan(_adapter *padapter)
1231 {
1232         _irqL   irqL;
1233         _list           *plist, *phead;
1234         u32     delta_time, lifetime;
1235         struct  wlan_network    *pnetwork = NULL;
1236         WLAN_BSSID_EX *pbss = NULL;
1237         struct  mlme_priv       *pmlmepriv = &(padapter->mlmepriv);
1238         _queue  *queue  = &(pmlmepriv->scanned_queue);
1239         u8 do_scan = _FALSE;
1240         u8 reason = RTW_AUTO_SCAN_REASON_UNSPECIFIED;
1241
1242         lifetime = SCANQUEUE_LIFETIME; /* 20 sec */
1243
1244         _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL);
1245         phead = get_list_head(queue);
1246         if (rtw_end_of_queue_search(phead, get_next(phead)) == _TRUE)
1247                 if (padapter->registrypriv.wifi_spec) {
1248                         do_scan = _TRUE;
1249                         reason |= RTW_AUTO_SCAN_REASON_2040_BSS;
1250                 }
1251         _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL);
1252
1253 #ifdef CONFIG_AUTO_CHNL_SEL_NHM
1254         if (padapter->registrypriv.acs_auto_scan) {
1255                 do_scan = _TRUE;
1256                 reason |= RTW_AUTO_SCAN_REASON_ACS;
1257                 rtw_acs_start(padapter, _TRUE);
1258         }
1259 #endif
1260
1261         if (_TRUE == do_scan) {
1262                 RTW_INFO("%s : drv scans by itself and wait_completed\n", __func__);
1263                 rtw_drv_scan_by_self(padapter, reason);
1264                 rtw_scan_wait_completed(padapter);
1265         }
1266
1267 #ifdef CONFIG_AUTO_CHNL_SEL_NHM
1268         if (padapter->registrypriv.acs_auto_scan)
1269                 rtw_acs_start(padapter, _FALSE);
1270 #endif
1271         _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL);
1272
1273         phead = get_list_head(queue);
1274         plist = get_next(phead);
1275
1276         while (1) {
1277
1278                 if (rtw_end_of_queue_search(phead, plist) == _TRUE)
1279                         break;
1280
1281                 pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
1282
1283                 if (rtw_ch_set_search_ch(padapter->mlmeextpriv.channel_set, pnetwork->network.Configuration.DSConfig) >= 0
1284                     && rtw_mlme_band_check(padapter, pnetwork->network.Configuration.DSConfig) == _TRUE
1285                     && _TRUE == rtw_validate_ssid(&(pnetwork->network.Ssid))) {
1286                         delta_time = (u32) rtw_get_passing_time_ms(pnetwork->last_scanned);
1287
1288                         if (delta_time < lifetime) {
1289
1290                                 uint ie_len = 0;
1291                                 u8 *pbuf = NULL;
1292                                 u8 *ie = NULL;
1293
1294                                 pbss = &pnetwork->network;
1295                                 ie = pbss->IEs;
1296
1297                                 /*check if HT CAP INFO IE exists or not*/
1298                                 pbuf = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_CAPABILITY_IE_, &ie_len, (pbss->IELength - _BEACON_IE_OFFSET_));
1299                                 if (pbuf == NULL) {
1300                                         /* HT CAP INFO IE don't exist, it is b/g mode bss.*/
1301
1302                                         if (pmlmepriv->olbc == _FALSE)
1303                                                 pmlmepriv->olbc = _TRUE;
1304
1305                                         if (pmlmepriv->olbc_ht == _FALSE)
1306                                                 pmlmepriv->olbc_ht = _TRUE;
1307                                 }
1308                         }
1309                 }
1310
1311                 plist = get_next(plist);
1312
1313         }
1314
1315         _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL);
1316
1317         pmlmepriv->num_sta_no_ht = 0; /* reset to 0 after ap do scanning*/
1318
1319 }
1320
1321 void rtw_start_bss_hdl_after_chbw_decided(_adapter *adapter)
1322 {
1323         WLAN_BSSID_EX *pnetwork = &(adapter->mlmepriv.cur_network.network);
1324         struct sta_info *sta = NULL;
1325
1326         /* update cur_wireless_mode */
1327         update_wireless_mode(adapter);
1328
1329         /* update RRSR and RTS_INIT_RATE register after set channel and bandwidth */
1330         UpdateBrateTbl(adapter, pnetwork->SupportedRates);
1331         rtw_hal_set_hwreg(adapter, HW_VAR_BASIC_RATE, pnetwork->SupportedRates);
1332
1333         /* update capability after cur_wireless_mode updated */
1334         update_capinfo(adapter, rtw_get_capability(pnetwork));
1335
1336         /* update bc/mc sta_info */
1337         update_bmc_sta(adapter);
1338
1339         /* update AP's sta info */
1340         sta = rtw_get_stainfo(&adapter->stapriv, pnetwork->MacAddress);
1341         if (!sta) {
1342                 RTW_INFO(FUNC_ADPT_FMT" !sta for macaddr="MAC_FMT"\n", FUNC_ADPT_ARG(adapter), MAC_ARG(pnetwork->MacAddress));
1343                 rtw_warn_on(1);
1344                 return;
1345         }
1346
1347         update_ap_info(adapter, sta);
1348 }
1349
1350 void start_bss_network(_adapter *padapter, struct createbss_parm *parm)
1351 {
1352 #define DUMP_ADAPTERS_STATUS 0
1353
1354         u8 val8;
1355         u16 bcn_interval;
1356         u32     acparm;
1357         struct registry_priv    *pregpriv = &padapter->registrypriv;
1358         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1359         struct security_priv *psecuritypriv = &(padapter->securitypriv);
1360         WLAN_BSSID_EX *pnetwork = (WLAN_BSSID_EX *)&pmlmepriv->cur_network.network; /* used as input */
1361         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1362         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1363         WLAN_BSSID_EX *pnetwork_mlmeext = &(pmlmeinfo->network);
1364         struct dvobj_priv *pdvobj = padapter->dvobj;
1365         s16 req_ch = -1, req_bw = -1, req_offset = -1;
1366         bool ch_setting_changed = _FALSE;
1367         u8 ch_to_set = 0, bw_to_set, offset_to_set;
1368         u8 doiqk = _FALSE;
1369         /* use for check ch bw offset can be allowed or not */
1370         u8 chbw_allow = _TRUE;
1371
1372         if (parm->req_ch != 0) {
1373                 /* bypass other setting, go checking ch, bw, offset */
1374                 req_ch = parm->req_ch;
1375                 req_bw = parm->req_bw;
1376                 req_offset = parm->req_offset;
1377                 goto chbw_decision;
1378         }
1379
1380         bcn_interval = (u16)pnetwork->Configuration.BeaconPeriod;
1381
1382         /* check if there is wps ie, */
1383         /* if there is wpsie in beacon, the hostapd will update beacon twice when stating hostapd, */
1384         /* and at first time the security ie ( RSN/WPA IE) will not include in beacon. */
1385         if (NULL == rtw_get_wps_ie(pnetwork->IEs + _FIXED_IE_LENGTH_, pnetwork->IELength - _FIXED_IE_LENGTH_, NULL, NULL))
1386                 pmlmeext->bstart_bss = _TRUE;
1387
1388         /* todo: update wmm, ht cap */
1389         /* pmlmeinfo->WMM_enable; */
1390         /* pmlmeinfo->HT_enable; */
1391         if (pmlmepriv->qospriv.qos_option)
1392                 pmlmeinfo->WMM_enable = _TRUE;
1393 #ifdef CONFIG_80211N_HT
1394         if (pmlmepriv->htpriv.ht_option) {
1395                 pmlmeinfo->WMM_enable = _TRUE;
1396                 pmlmeinfo->HT_enable = _TRUE;
1397                 /* pmlmeinfo->HT_info_enable = _TRUE; */
1398                 /* pmlmeinfo->HT_caps_enable = _TRUE; */
1399
1400                 update_hw_ht_param(padapter);
1401         }
1402 #endif /* #CONFIG_80211N_HT */
1403
1404 #ifdef CONFIG_80211AC_VHT
1405         if (pmlmepriv->vhtpriv.vht_option) {
1406                 pmlmeinfo->VHT_enable = _TRUE;
1407                 update_hw_vht_param(padapter);
1408         }
1409 #endif /* CONFIG_80211AC_VHT */
1410
1411         if (pmlmepriv->cur_network.join_res != _TRUE) { /* setting only at  first time */
1412                 /* WEP Key will be set before this function, do not clear CAM. */
1413                 if ((psecuritypriv->dot11PrivacyAlgrthm != _WEP40_) && (psecuritypriv->dot11PrivacyAlgrthm != _WEP104_))
1414                         flush_all_cam_entry(padapter);  /* clear CAM */
1415         }
1416
1417         /* set MSR to AP_Mode            */
1418         Set_MSR(padapter, _HW_STATE_AP_);
1419
1420         /* Set BSSID REG */
1421         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, pnetwork->MacAddress);
1422
1423         /* Set EDCA param reg */
1424 #ifdef CONFIG_CONCURRENT_MODE
1425         acparm = 0x005ea42b;
1426 #else
1427         acparm = 0x002F3217; /* VO */
1428 #endif
1429         rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VO, (u8 *)(&acparm));
1430         acparm = 0x005E4317; /* VI */
1431         rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_VI, (u8 *)(&acparm));
1432         /* acparm = 0x00105320; */ /* BE */
1433         acparm = 0x005ea42b;
1434         rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BE, (u8 *)(&acparm));
1435         acparm = 0x0000A444; /* BK */
1436         rtw_hal_set_hwreg(padapter, HW_VAR_AC_PARAM_BK, (u8 *)(&acparm));
1437
1438         /* Set Security */
1439         val8 = (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) ? 0xcc : 0xcf;
1440         rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
1441
1442         /* Beacon Control related register */
1443         rtw_hal_set_hwreg(padapter, HW_VAR_BEACON_INTERVAL, (u8 *)(&bcn_interval));
1444
1445 chbw_decision:
1446         ch_setting_changed = rtw_ap_chbw_decision(padapter, req_ch, req_bw, req_offset
1447                      , &ch_to_set, &bw_to_set, &offset_to_set, &chbw_allow);
1448
1449         /* let pnetwork_mlmeext == pnetwork_mlme. */
1450         _rtw_memcpy(pnetwork_mlmeext, pnetwork, pnetwork->Length);
1451
1452         rtw_start_bss_hdl_after_chbw_decided(padapter);
1453
1454 #if defined(CONFIG_DFS_MASTER)
1455         rtw_dfs_master_status_apply(padapter, MLME_AP_STARTED);
1456 #endif
1457
1458 #ifdef CONFIG_MCC_MODE
1459         /* issue null data to AP for all interface connecting to AP before switch channel setting for softap */
1460         rtw_hal_mcc_issue_null_data(padapter, chbw_allow, 1);
1461 #endif /* CONFIG_MCC_MODE */
1462
1463         doiqk = _TRUE;
1464         rtw_hal_set_hwreg(padapter , HW_VAR_DO_IQK , &doiqk);
1465
1466         if (ch_to_set != 0) {
1467                 set_channel_bwmode(padapter, ch_to_set, offset_to_set, bw_to_set);
1468                 rtw_mi_update_union_chan_inf(padapter, ch_to_set, offset_to_set, bw_to_set);
1469         }
1470
1471         doiqk = _FALSE;
1472         rtw_hal_set_hwreg(padapter , HW_VAR_DO_IQK , &doiqk);
1473
1474 #ifdef CONFIG_MCC_MODE
1475         /* after set_channel_bwmode for backup IQK */
1476         rtw_hal_set_mcc_setting_start_bss_network(padapter, chbw_allow);
1477 #endif
1478
1479         if (DUMP_ADAPTERS_STATUS) {
1480                 RTW_INFO(FUNC_ADPT_FMT" done\n", FUNC_ADPT_ARG(padapter));
1481                 dump_adapters_status(RTW_DBGDUMP , adapter_to_dvobj(padapter));
1482         }
1483
1484         /* update beacon content only if bstart_bss is _TRUE */
1485         if (_TRUE == pmlmeext->bstart_bss) {
1486
1487                 _irqL irqL;
1488
1489                 if ((pmlmepriv->olbc == _TRUE) || (pmlmepriv->olbc_ht == _TRUE)) {
1490
1491                         /* AP is not starting a 40 MHz BSS in presence of an 802.11g BSS. */
1492
1493                         pmlmepriv->ht_op_mode &= (~HT_INFO_OPERATION_MODE_OP_MODE_MASK);
1494                         pmlmepriv->ht_op_mode |= OP_MODE_MAY_BE_LEGACY_STAS;
1495                         update_beacon(padapter, _HT_ADD_INFO_IE_, NULL, _FALSE);
1496                 }
1497
1498                 update_beacon(padapter, _TIM_IE_, NULL, _FALSE);
1499
1500 #ifdef CONFIG_SWTIMER_BASED_TXBCN
1501                 _enter_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
1502                 if (rtw_is_list_empty(&padapter->list)) {
1503                         rtw_list_insert_tail(&padapter->list, get_list_head(&pdvobj->ap_if_q));
1504                         pdvobj->nr_ap_if++;
1505                         pdvobj->inter_bcn_space = DEFAULT_BCN_INTERVAL / pdvobj->nr_ap_if;
1506                 }
1507                 _exit_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
1508
1509                 rtw_hal_set_hwreg(padapter, HW_VAR_BEACON_INTERVAL, (u8 *)(&pdvobj->inter_bcn_space));
1510
1511 #endif /*CONFIG_SWTIMER_BASED_TXBCN*/
1512
1513         }
1514
1515         rtw_scan_wait_completed(padapter);
1516
1517         /* send beacon */
1518         if ((0 == rtw_mi_check_fwstate(padapter, _FW_UNDER_SURVEY))
1519             && (0 == rtw_mi_check_fwstate(padapter, WIFI_OP_CH_SWITCHING))
1520            ) {
1521
1522                 /*update_beacon(padapter, _TIM_IE_, NULL, _TRUE);*/
1523
1524 #if !defined(CONFIG_INTERRUPT_BASED_TXBCN)
1525 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
1526 #ifdef CONFIG_SWTIMER_BASED_TXBCN
1527                 if (pdvobj->nr_ap_if == 1) {
1528                         RTW_INFO("start SW BCN TIMER!\n");
1529                         _set_timer(&pdvobj->txbcn_timer, bcn_interval);
1530                 }
1531 #else
1532                 /* other case will  tx beacon when bcn interrupt coming in. */
1533                 if (send_beacon(padapter) == _FAIL)
1534                         RTW_INFO("issue_beacon, fail!\n");
1535 #endif
1536 #endif
1537 #endif /* !defined(CONFIG_INTERRUPT_BASED_TXBCN) */
1538         }
1539
1540         /*Set EDCA param reg after update cur_wireless_mode & update_capinfo*/
1541         if (pregpriv->wifi_spec == 1)
1542                 rtw_set_hw_wmm_param(padapter);
1543
1544         /*pmlmeext->bstart_bss = _TRUE;*/
1545 }
1546
1547 int rtw_check_beacon_data(_adapter *padapter, u8 *pbuf,  int len)
1548 {
1549         int ret = _SUCCESS;
1550         u8 *p;
1551         u8 *pHT_caps_ie = NULL;
1552         u8 *pHT_info_ie = NULL;
1553         u16 cap, ht_cap = _FALSE;
1554         uint ie_len = 0;
1555         int group_cipher, pairwise_cipher;
1556         u8      channel, network_type, supportRate[NDIS_802_11_LENGTH_RATES_EX];
1557         int supportRateNum = 0;
1558         u8 OUI1[] = {0x00, 0x50, 0xf2, 0x01};
1559         u8 wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
1560         u8 WMM_PARA_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
1561         struct registry_priv *pregistrypriv = &padapter->registrypriv;
1562         struct security_priv *psecuritypriv = &padapter->securitypriv;
1563         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1564         WLAN_BSSID_EX *pbss_network = (WLAN_BSSID_EX *)&pmlmepriv->cur_network.network;
1565         struct sta_priv *pstapriv = &padapter->stapriv;
1566         u8 *ie = pbss_network->IEs;
1567         u8 vht_cap = _FALSE;
1568         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1569         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1570         u8 rf_num = 0;
1571
1572         /* SSID */
1573         /* Supported rates */
1574         /* DS Params */
1575         /* WLAN_EID_COUNTRY */
1576         /* ERP Information element */
1577         /* Extended supported rates */
1578         /* WPA/WPA2 */
1579         /* Wi-Fi Wireless Multimedia Extensions */
1580         /* ht_capab, ht_oper */
1581         /* WPS IE */
1582
1583         RTW_INFO("%s, len=%d\n", __FUNCTION__, len);
1584
1585         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != _TRUE)
1586                 return _FAIL;
1587
1588
1589         if (len > MAX_IE_SZ)
1590                 return _FAIL;
1591
1592         pbss_network->IELength = len;
1593
1594         _rtw_memset(ie, 0, MAX_IE_SZ);
1595
1596         _rtw_memcpy(ie, pbuf, pbss_network->IELength);
1597
1598
1599         if (pbss_network->InfrastructureMode != Ndis802_11APMode)
1600                 return _FAIL;
1601
1602
1603         rtw_ap_check_scan(padapter);
1604
1605
1606         pbss_network->Rssi = 0;
1607
1608         _rtw_memcpy(pbss_network->MacAddress, adapter_mac_addr(padapter), ETH_ALEN);
1609
1610         /* beacon interval */
1611         p = rtw_get_beacon_interval_from_ie(ie);/* ie + 8;       */ /* 8: TimeStamp, 2: Beacon Interval 2:Capability */
1612         /* pbss_network->Configuration.BeaconPeriod = le16_to_cpu(*(unsigned short*)p); */
1613         pbss_network->Configuration.BeaconPeriod = RTW_GET_LE16(p);
1614
1615         /* capability */
1616         /* cap = *(unsigned short *)rtw_get_capability_from_ie(ie); */
1617         /* cap = le16_to_cpu(cap); */
1618         cap = RTW_GET_LE16(ie);
1619
1620         /* SSID */
1621         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _SSID_IE_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_));
1622         if (p && ie_len > 0) {
1623                 _rtw_memset(&pbss_network->Ssid, 0, sizeof(NDIS_802_11_SSID));
1624                 _rtw_memcpy(pbss_network->Ssid.Ssid, (p + 2), ie_len);
1625                 pbss_network->Ssid.SsidLength = ie_len;
1626 #ifdef CONFIG_P2P
1627                 _rtw_memcpy(padapter->wdinfo.p2p_group_ssid, pbss_network->Ssid.Ssid, pbss_network->Ssid.SsidLength);
1628                 padapter->wdinfo.p2p_group_ssid_len = pbss_network->Ssid.SsidLength;
1629 #endif
1630         }
1631
1632         /* chnnel */
1633         channel = 0;
1634         pbss_network->Configuration.Length = 0;
1635         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _DSSET_IE_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_));
1636         if (p && ie_len > 0)
1637                 channel = *(p + 2);
1638
1639         pbss_network->Configuration.DSConfig = channel;
1640
1641
1642         _rtw_memset(supportRate, 0, NDIS_802_11_LENGTH_RATES_EX);
1643         /* get supported rates */
1644         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _SUPPORTEDRATES_IE_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_));
1645         if (p !=  NULL) {
1646                 _rtw_memcpy(supportRate, p + 2, ie_len);
1647                 supportRateNum = ie_len;
1648         }
1649
1650         /* get ext_supported rates */
1651         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _EXT_SUPPORTEDRATES_IE_, &ie_len, pbss_network->IELength - _BEACON_IE_OFFSET_);
1652         if (p !=  NULL) {
1653                 _rtw_memcpy(supportRate + supportRateNum, p + 2, ie_len);
1654                 supportRateNum += ie_len;
1655
1656         }
1657
1658         network_type = rtw_check_network_type(supportRate, supportRateNum, channel);
1659
1660         rtw_set_supported_rate(pbss_network->SupportedRates, network_type);
1661
1662
1663         /* parsing ERP_IE */
1664         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _ERPINFO_IE_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_));
1665         if (p && ie_len > 0)
1666                 ERP_IE_handler(padapter, (PNDIS_802_11_VARIABLE_IEs)p);
1667
1668         /* update privacy/security */
1669         if (cap & BIT(4))
1670                 pbss_network->Privacy = 1;
1671         else
1672                 pbss_network->Privacy = 0;
1673
1674         psecuritypriv->wpa_psk = 0;
1675
1676         /* wpa2 */
1677         group_cipher = 0;
1678         pairwise_cipher = 0;
1679         psecuritypriv->wpa2_group_cipher = _NO_PRIVACY_;
1680         psecuritypriv->wpa2_pairwise_cipher = _NO_PRIVACY_;
1681         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _RSN_IE_2_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_));
1682         if (p && ie_len > 0) {
1683                 if (rtw_parse_wpa2_ie(p, ie_len + 2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1684                         psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1685
1686                         psecuritypriv->dot8021xalg = 1;/* psk,  todo:802.1x */
1687                         psecuritypriv->wpa_psk |= BIT(1);
1688
1689                         psecuritypriv->wpa2_group_cipher = group_cipher;
1690                         psecuritypriv->wpa2_pairwise_cipher = pairwise_cipher;
1691 #if 0
1692                         switch (group_cipher) {
1693                         case WPA_CIPHER_NONE:
1694                                 psecuritypriv->wpa2_group_cipher = _NO_PRIVACY_;
1695                                 break;
1696                         case WPA_CIPHER_WEP40:
1697                                 psecuritypriv->wpa2_group_cipher = _WEP40_;
1698                                 break;
1699                         case WPA_CIPHER_TKIP:
1700                                 psecuritypriv->wpa2_group_cipher = _TKIP_;
1701                                 break;
1702                         case WPA_CIPHER_CCMP:
1703                                 psecuritypriv->wpa2_group_cipher = _AES_;
1704                                 break;
1705                         case WPA_CIPHER_WEP104:
1706                                 psecuritypriv->wpa2_group_cipher = _WEP104_;
1707                                 break;
1708                         }
1709
1710                         switch (pairwise_cipher) {
1711                         case WPA_CIPHER_NONE:
1712                                 psecuritypriv->wpa2_pairwise_cipher = _NO_PRIVACY_;
1713                                 break;
1714                         case WPA_CIPHER_WEP40:
1715                                 psecuritypriv->wpa2_pairwise_cipher = _WEP40_;
1716                                 break;
1717                         case WPA_CIPHER_TKIP:
1718                                 psecuritypriv->wpa2_pairwise_cipher = _TKIP_;
1719                                 break;
1720                         case WPA_CIPHER_CCMP:
1721                                 psecuritypriv->wpa2_pairwise_cipher = _AES_;
1722                                 break;
1723                         case WPA_CIPHER_WEP104:
1724                                 psecuritypriv->wpa2_pairwise_cipher = _WEP104_;
1725                                 break;
1726                         }
1727 #endif
1728                 }
1729
1730         }
1731
1732         /* wpa */
1733         ie_len = 0;
1734         group_cipher = 0;
1735         pairwise_cipher = 0;
1736         psecuritypriv->wpa_group_cipher = _NO_PRIVACY_;
1737         psecuritypriv->wpa_pairwise_cipher = _NO_PRIVACY_;
1738         for (p = ie + _BEACON_IE_OFFSET_; ; p += (ie_len + 2)) {
1739                 p = rtw_get_ie(p, _SSN_IE_1_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_ - (ie_len + 2)));
1740                 if ((p) && (_rtw_memcmp(p + 2, OUI1, 4))) {
1741                         if (rtw_parse_wpa_ie(p, ie_len + 2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1742                                 psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1743
1744                                 psecuritypriv->dot8021xalg = 1;/* psk,  todo:802.1x */
1745
1746                                 psecuritypriv->wpa_psk |= BIT(0);
1747
1748                                 psecuritypriv->wpa_group_cipher = group_cipher;
1749                                 psecuritypriv->wpa_pairwise_cipher = pairwise_cipher;
1750
1751 #if 0
1752                                 switch (group_cipher) {
1753                                 case WPA_CIPHER_NONE:
1754                                         psecuritypriv->wpa_group_cipher = _NO_PRIVACY_;
1755                                         break;
1756                                 case WPA_CIPHER_WEP40:
1757                                         psecuritypriv->wpa_group_cipher = _WEP40_;
1758                                         break;
1759                                 case WPA_CIPHER_TKIP:
1760                                         psecuritypriv->wpa_group_cipher = _TKIP_;
1761                                         break;
1762                                 case WPA_CIPHER_CCMP:
1763                                         psecuritypriv->wpa_group_cipher = _AES_;
1764                                         break;
1765                                 case WPA_CIPHER_WEP104:
1766                                         psecuritypriv->wpa_group_cipher = _WEP104_;
1767                                         break;
1768                                 }
1769
1770                                 switch (pairwise_cipher) {
1771                                 case WPA_CIPHER_NONE:
1772                                         psecuritypriv->wpa_pairwise_cipher = _NO_PRIVACY_;
1773                                         break;
1774                                 case WPA_CIPHER_WEP40:
1775                                         psecuritypriv->wpa_pairwise_cipher = _WEP40_;
1776                                         break;
1777                                 case WPA_CIPHER_TKIP:
1778                                         psecuritypriv->wpa_pairwise_cipher = _TKIP_;
1779                                         break;
1780                                 case WPA_CIPHER_CCMP:
1781                                         psecuritypriv->wpa_pairwise_cipher = _AES_;
1782                                         break;
1783                                 case WPA_CIPHER_WEP104:
1784                                         psecuritypriv->wpa_pairwise_cipher = _WEP104_;
1785                                         break;
1786                                 }
1787 #endif
1788                         }
1789
1790                         break;
1791
1792                 }
1793
1794                 if ((p == NULL) || (ie_len == 0))
1795                         break;
1796
1797         }
1798
1799         /* wmm */
1800         ie_len = 0;
1801         pmlmepriv->qospriv.qos_option = 0;
1802         if (pregistrypriv->wmm_enable) {
1803                 for (p = ie + _BEACON_IE_OFFSET_; ; p += (ie_len + 2)) {
1804                         p = rtw_get_ie(p, _VENDOR_SPECIFIC_IE_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_ - (ie_len + 2)));
1805                         if ((p) && _rtw_memcmp(p + 2, WMM_PARA_IE, 6)) {
1806                                 pmlmepriv->qospriv.qos_option = 1;
1807
1808                                 *(p + 8) |= BIT(7); /* QoS Info, support U-APSD */
1809
1810                                 /* disable all ACM bits since the WMM admission control is not supported */
1811                                 *(p + 10) &= ~BIT(4); /* BE */
1812                                 *(p + 14) &= ~BIT(4); /* BK */
1813                                 *(p + 18) &= ~BIT(4); /* VI */
1814                                 *(p + 22) &= ~BIT(4); /* VO */
1815
1816                                 break;
1817                         }
1818
1819                         if ((p == NULL) || (ie_len == 0))
1820                                 break;
1821                 }
1822         }
1823 #ifdef CONFIG_80211N_HT
1824         /* parsing HT_CAP_IE */
1825         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_CAPABILITY_IE_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_));
1826         if (p && ie_len > 0) {
1827                 u8 rf_type = 0;
1828                 HT_CAP_AMPDU_FACTOR max_rx_ampdu_factor = MAX_AMPDU_FACTOR_64K;
1829                 struct rtw_ieee80211_ht_cap *pht_cap = (struct rtw_ieee80211_ht_cap *)(p + 2);
1830
1831                 if (0) {
1832                         RTW_INFO(FUNC_ADPT_FMT" HT_CAP_IE from upper layer:\n", FUNC_ADPT_ARG(padapter));
1833                         dump_ht_cap_ie_content(RTW_DBGDUMP, p + 2, ie_len);
1834                 }
1835
1836                 pHT_caps_ie = p;
1837
1838                 ht_cap = _TRUE;
1839                 network_type |= WIRELESS_11_24N;
1840
1841                 rtw_ht_use_default_setting(padapter);
1842
1843                 /* Update HT Capabilities Info field */
1844                 if (pmlmepriv->htpriv.sgi_20m == _FALSE)
1845                         pht_cap->cap_info &= ~(IEEE80211_HT_CAP_SGI_20);
1846
1847                 if (pmlmepriv->htpriv.sgi_40m == _FALSE)
1848                         pht_cap->cap_info &= ~(IEEE80211_HT_CAP_SGI_40);
1849
1850                 if (!TEST_FLAG(pmlmepriv->htpriv.ldpc_cap, LDPC_HT_ENABLE_RX))
1851                         pht_cap->cap_info &= ~(IEEE80211_HT_CAP_LDPC_CODING);
1852
1853                 if (!TEST_FLAG(pmlmepriv->htpriv.stbc_cap, STBC_HT_ENABLE_TX))
1854                         pht_cap->cap_info &= ~(IEEE80211_HT_CAP_TX_STBC);
1855
1856                 if (!TEST_FLAG(pmlmepriv->htpriv.stbc_cap, STBC_HT_ENABLE_RX))
1857                         pht_cap->cap_info &= ~(IEEE80211_HT_CAP_RX_STBC_3R);
1858
1859                 /* Update A-MPDU Parameters field */
1860                 pht_cap->ampdu_params_info &= ~(IEEE80211_HT_CAP_AMPDU_FACTOR | IEEE80211_HT_CAP_AMPDU_DENSITY);
1861
1862                 if ((psecuritypriv->wpa_pairwise_cipher & WPA_CIPHER_CCMP) ||
1863                     (psecuritypriv->wpa2_pairwise_cipher & WPA_CIPHER_CCMP))
1864                         pht_cap->ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_DENSITY & (0x07 << 2));
1865                 else
1866                         pht_cap->ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_DENSITY & 0x00);
1867
1868                 rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor);
1869                 pht_cap->ampdu_params_info |= (IEEE80211_HT_CAP_AMPDU_FACTOR & max_rx_ampdu_factor); /* set  Max Rx AMPDU size  to 64K */
1870
1871                 _rtw_memcpy(&(pmlmeinfo->HT_caps), pht_cap, sizeof(struct HT_caps_element));
1872
1873                 /* Update Supported MCS Set field */
1874                 {
1875                         int i;
1876
1877                         rtw_hal_get_hwreg(padapter, HW_VAR_RF_TYPE, (u8 *)(&rf_type));
1878
1879                         /* RX MCS Bitmask */
1880                         switch (rf_type) {
1881                         case RF_1T1R:
1882                         case RF_1T2R: /* ? */
1883                                 set_mcs_rate_by_mask(HT_CAP_ELE_RX_MCS_MAP(pht_cap), MCS_RATE_1R);
1884                                 break;
1885                         case RF_2T2R:
1886                                 set_mcs_rate_by_mask(HT_CAP_ELE_RX_MCS_MAP(pht_cap), MCS_RATE_2R);
1887                                 break;
1888                         case RF_3T3R:
1889                                 set_mcs_rate_by_mask(HT_CAP_ELE_RX_MCS_MAP(pht_cap), MCS_RATE_3R);
1890                                 break;
1891                         default:
1892                                 RTW_INFO("[warning] rf_type %d is not expected\n", rf_type);
1893                         }
1894                         for (i = 0; i < 10; i++)
1895                                 *(HT_CAP_ELE_RX_MCS_MAP(pht_cap) + i) &= padapter->mlmeextpriv.default_supported_mcs_set[i];
1896                 }
1897
1898 #ifdef CONFIG_BEAMFORMING
1899                 /* Use registry value to enable HT Beamforming. */
1900                 /* ToDo: use configure file to set these capability. */
1901                 pht_cap->tx_BF_cap_info = 0;
1902
1903                 /* HT Beamformer */
1904                 if (TEST_FLAG(pmlmepriv->htpriv.beamform_cap, BEAMFORMING_HT_BEAMFORMER_ENABLE)) {
1905                         /* Transmit NDP Capable */
1906                         SET_HT_CAP_TXBF_TRANSMIT_NDP_CAP(pht_cap, 1);
1907                         /* Explicit Compressed Steering Capable */
1908                         SET_HT_CAP_TXBF_EXPLICIT_COMP_STEERING_CAP(pht_cap, 1);
1909                         /* Compressed Steering Number Antennas */
1910                         SET_HT_CAP_TXBF_COMP_STEERING_NUM_ANTENNAS(pht_cap, 1);
1911                         rtw_hal_get_def_var(padapter, HAL_DEF_BEAMFORMER_CAP, (u8 *)&rf_num);
1912                         SET_HT_CAP_TXBF_CHNL_ESTIMATION_NUM_ANTENNAS(pht_cap, rf_num);
1913                 }
1914
1915                 /* HT Beamformee */
1916                 if (TEST_FLAG(pmlmepriv->htpriv.beamform_cap, BEAMFORMING_HT_BEAMFORMEE_ENABLE)) {
1917                         /* Receive NDP Capable */
1918                         SET_HT_CAP_TXBF_RECEIVE_NDP_CAP(pht_cap, 1);
1919                         /* Explicit Compressed Beamforming Feedback Capable */
1920                         SET_HT_CAP_TXBF_EXPLICIT_COMP_FEEDBACK_CAP(pht_cap, 2);
1921                         rtw_hal_get_def_var(padapter, HAL_DEF_BEAMFORMEE_CAP, (u8 *)&rf_num);
1922                         SET_HT_CAP_TXBF_COMP_STEERING_NUM_ANTENNAS(pht_cap, rf_num);
1923                 }
1924 #endif /* CONFIG_BEAMFORMING */
1925
1926                 _rtw_memcpy(&pmlmepriv->htpriv.ht_cap, p + 2, ie_len);
1927
1928                 if (0) {
1929                         RTW_INFO(FUNC_ADPT_FMT" HT_CAP_IE driver masked:\n", FUNC_ADPT_ARG(padapter));
1930                         dump_ht_cap_ie_content(RTW_DBGDUMP, p + 2, ie_len);
1931                 }
1932         }
1933
1934         /* parsing HT_INFO_IE */
1935         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_ADD_INFO_IE_, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_));
1936         if (p && ie_len > 0)
1937                 pHT_info_ie = p;
1938 #endif /* CONFIG_80211N_HT */
1939         switch (network_type) {
1940         case WIRELESS_11B:
1941                 pbss_network->NetworkTypeInUse = Ndis802_11DS;
1942                 break;
1943         case WIRELESS_11G:
1944         case WIRELESS_11BG:
1945         case WIRELESS_11G_24N:
1946         case WIRELESS_11BG_24N:
1947                 pbss_network->NetworkTypeInUse = Ndis802_11OFDM24;
1948                 break;
1949         case WIRELESS_11A:
1950                 pbss_network->NetworkTypeInUse = Ndis802_11OFDM5;
1951                 break;
1952         default:
1953                 pbss_network->NetworkTypeInUse = Ndis802_11OFDM24;
1954                 break;
1955         }
1956
1957         pmlmepriv->cur_network.network_type = network_type;
1958
1959 #ifdef CONFIG_80211N_HT
1960         pmlmepriv->htpriv.ht_option = _FALSE;
1961
1962         if ((psecuritypriv->wpa2_pairwise_cipher & WPA_CIPHER_TKIP) ||
1963             (psecuritypriv->wpa_pairwise_cipher & WPA_CIPHER_TKIP)) {
1964                 /* todo: */
1965                 /* ht_cap = _FALSE; */
1966         }
1967
1968         /* ht_cap        */
1969         if (pregistrypriv->ht_enable && ht_cap == _TRUE) {
1970                 pmlmepriv->htpriv.ht_option = _TRUE;
1971                 pmlmepriv->qospriv.qos_option = 1;
1972
1973                 if (pregistrypriv->ampdu_enable == 1)
1974                         pmlmepriv->htpriv.ampdu_enable = _TRUE;
1975
1976                 HT_caps_handler(padapter, (PNDIS_802_11_VARIABLE_IEs)pHT_caps_ie);
1977
1978                 HT_info_handler(padapter, (PNDIS_802_11_VARIABLE_IEs)pHT_info_ie);
1979         }
1980 #endif
1981
1982 #ifdef CONFIG_80211AC_VHT
1983
1984         /* Parsing VHT CAP IE */
1985         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, EID_VHTCapability, &ie_len, (pbss_network->IELength - _BEACON_IE_OFFSET_));
1986         if (p && ie_len > 0)
1987                 vht_cap = _TRUE;
1988         /* Parsing VHT OPERATION IE */
1989
1990
1991         pmlmepriv->vhtpriv.vht_option = _FALSE;
1992         /* if channel in 5G band, then add vht ie . */
1993         if ((pbss_network->Configuration.DSConfig > 14)
1994             && (pmlmepriv->htpriv.ht_option == _TRUE)
1995             && REGSTY_IS_11AC_ENABLE(pregistrypriv)
1996             && hal_chk_proto_cap(padapter, PROTO_CAP_11AC)
1997             && (!pmlmepriv->country_ent || COUNTRY_CHPLAN_EN_11AC(pmlmepriv->country_ent))
1998            ) {
1999                 if (vht_cap == _TRUE)
2000                         pmlmepriv->vhtpriv.vht_option = _TRUE;
2001                 else if (REGSTY_IS_11AC_AUTO(pregistrypriv)) {
2002                         u8      cap_len, operation_len;
2003
2004                         rtw_vht_use_default_setting(padapter);
2005
2006                         {
2007                                 /* VHT Operation mode notifiy bit in Extended IE (127) */
2008                                 uint len = 0;
2009
2010                                 SET_EXT_CAPABILITY_ELE_OP_MODE_NOTIF(pmlmepriv->ext_capab_ie_data, 1);
2011                                 pmlmepriv->ext_capab_ie_len = 10;
2012                                 rtw_set_ie(pbss_network->IEs + pbss_network->IELength, EID_EXTCapability, 8, pmlmepriv->ext_capab_ie_data, &len);
2013                                 pbss_network->IELength += pmlmepriv->ext_capab_ie_len;
2014                         }
2015
2016                         /* VHT Capabilities element */
2017                         cap_len = rtw_build_vht_cap_ie(padapter, pbss_network->IEs + pbss_network->IELength);
2018                         pbss_network->IELength += cap_len;
2019
2020                         /* VHT Operation element */
2021                         operation_len = rtw_build_vht_operation_ie(padapter, pbss_network->IEs + pbss_network->IELength, pbss_network->Configuration.DSConfig);
2022                         pbss_network->IELength += operation_len;
2023
2024                         pmlmepriv->vhtpriv.vht_option = _TRUE;
2025                 }
2026         }
2027 #endif /* CONFIG_80211AC_VHT */
2028
2029         pbss_network->Length = get_WLAN_BSSID_EX_sz((WLAN_BSSID_EX *)pbss_network);
2030
2031         rtw_ies_get_chbw(pbss_network->IEs + _BEACON_IE_OFFSET_, pbss_network->IELength - _BEACON_IE_OFFSET_
2032                 , &pmlmepriv->ori_ch, &pmlmepriv->ori_bw, &pmlmepriv->ori_offset);
2033         rtw_warn_on(pmlmepriv->ori_ch == 0);
2034
2035         {
2036                 /* alloc sta_info for ap itself */
2037
2038                 struct sta_info *sta;
2039
2040                 sta = rtw_get_stainfo(&padapter->stapriv, pbss_network->MacAddress);
2041                 if (!sta) {
2042                         sta = rtw_alloc_stainfo(&padapter->stapriv, pbss_network->MacAddress);
2043                         if (sta == NULL)
2044                                 return _FAIL;
2045                 }
2046         }
2047
2048         rtw_startbss_cmd(padapter, RTW_CMDF_WAIT_ACK);
2049         {
2050                 int sk_band = RTW_GET_SCAN_BAND_SKIP(padapter);
2051
2052                 if (sk_band)
2053                         RTW_CLR_SCAN_BAND_SKIP(padapter, sk_band);
2054         }
2055
2056         rtw_indicate_connect(padapter);
2057
2058         pmlmepriv->cur_network.join_res = _TRUE;/* for check if already set beacon */
2059
2060         /* update bc/mc sta_info */
2061         /* update_bmc_sta(padapter); */
2062
2063         return ret;
2064
2065 }
2066
2067 #if CONFIG_RTW_MACADDR_ACL
2068 void rtw_macaddr_acl_init(_adapter *adapter)
2069 {
2070         struct sta_priv *stapriv = &adapter->stapriv;
2071         struct wlan_acl_pool *acl = &stapriv->acl_list;
2072         _queue *acl_node_q = &acl->acl_node_q;
2073         int i;
2074         _irqL irqL;
2075
2076         _enter_critical_bh(&(acl_node_q->lock), &irqL);
2077         _rtw_init_listhead(&(acl_node_q->queue));
2078         acl->num = 0;
2079         acl->mode = RTW_ACL_MODE_DISABLED;
2080         for (i = 0; i < NUM_ACL; i++) {
2081                 _rtw_init_listhead(&acl->aclnode[i].list);
2082                 acl->aclnode[i].valid = _FALSE;
2083         }
2084         _exit_critical_bh(&(acl_node_q->lock), &irqL);
2085 }
2086
2087 void rtw_macaddr_acl_deinit(_adapter *adapter)
2088 {
2089         struct sta_priv *stapriv = &adapter->stapriv;
2090         struct wlan_acl_pool *acl = &stapriv->acl_list;
2091         _queue *acl_node_q = &acl->acl_node_q;
2092         _irqL irqL;
2093         _list *head, *list;
2094         struct rtw_wlan_acl_node *acl_node;
2095
2096         _enter_critical_bh(&(acl_node_q->lock), &irqL);
2097         head = get_list_head(acl_node_q);
2098         list = get_next(head);
2099         while (rtw_end_of_queue_search(head, list) == _FALSE) {
2100                 acl_node = LIST_CONTAINOR(list, struct rtw_wlan_acl_node, list);
2101                 list = get_next(list);
2102
2103                 if (acl_node->valid == _TRUE) {
2104                         acl_node->valid = _FALSE;
2105                         rtw_list_delete(&acl_node->list);
2106                         acl->num--;
2107                 }
2108         }
2109         _exit_critical_bh(&(acl_node_q->lock), &irqL);
2110
2111         rtw_warn_on(acl->num);
2112         acl->mode = RTW_ACL_MODE_DISABLED;
2113 }
2114
2115 void rtw_set_macaddr_acl(_adapter *adapter, int mode)
2116 {
2117         struct sta_priv *stapriv = &adapter->stapriv;
2118         struct wlan_acl_pool *acl = &stapriv->acl_list;
2119
2120         RTW_INFO(FUNC_ADPT_FMT" mode=%d\n", FUNC_ADPT_ARG(adapter), mode);
2121
2122         acl->mode = mode;
2123
2124         if (mode == RTW_ACL_MODE_DISABLED)
2125                 rtw_macaddr_acl_deinit(adapter);
2126 }
2127
2128 int rtw_acl_add_sta(_adapter *adapter, const u8 *addr)
2129 {
2130         _irqL irqL;
2131         _list *list, *head;
2132         u8 existed = 0;
2133         int i = -1, ret = 0;
2134         struct rtw_wlan_acl_node *acl_node;
2135         struct sta_priv *stapriv = &adapter->stapriv;
2136         struct wlan_acl_pool *acl = &stapriv->acl_list;
2137         _queue *acl_node_q = &acl->acl_node_q;
2138
2139         _enter_critical_bh(&(acl_node_q->lock), &irqL);
2140
2141         head = get_list_head(acl_node_q);
2142         list = get_next(head);
2143
2144         /* search for existed entry */
2145         while (rtw_end_of_queue_search(head, list) == _FALSE) {
2146                 acl_node = LIST_CONTAINOR(list, struct rtw_wlan_acl_node, list);
2147                 list = get_next(list);
2148
2149                 if (_rtw_memcmp(acl_node->addr, addr, ETH_ALEN)) {
2150                         if (acl_node->valid == _TRUE) {
2151                                 existed = 1;
2152                                 break;
2153                         }
2154                 }
2155         }
2156         if (existed)
2157                 goto release_lock;
2158
2159         if (acl->num >= NUM_ACL)
2160                 goto release_lock;
2161
2162         /* find empty one and use */
2163         for (i = 0; i < NUM_ACL; i++) {
2164
2165                 acl_node = &acl->aclnode[i];
2166                 if (acl_node->valid == _FALSE) {
2167
2168                         _rtw_init_listhead(&acl_node->list);
2169                         _rtw_memcpy(acl_node->addr, addr, ETH_ALEN);
2170                         acl_node->valid = _TRUE;
2171
2172                         rtw_list_insert_tail(&acl_node->list, get_list_head(acl_node_q));
2173                         acl->num++;
2174                         break;
2175                 }
2176         }
2177
2178 release_lock:
2179         _exit_critical_bh(&(acl_node_q->lock), &irqL);
2180
2181         if (!existed && (i < 0 || i >= NUM_ACL))
2182                 ret = -1;
2183
2184         RTW_INFO(FUNC_ADPT_FMT" "MAC_FMT" %s (acl_num=%d)\n"
2185                  , FUNC_ADPT_ARG(adapter), MAC_ARG(addr)
2186                 , (existed ? "existed" : ((i < 0 || i >= NUM_ACL) ? "no room" : "added"))
2187                  , acl->num);
2188
2189         return ret;
2190 }
2191
2192 int rtw_acl_remove_sta(_adapter *adapter, const u8 *addr)
2193 {
2194         _irqL irqL;
2195         _list *list, *head;
2196         int ret = 0;
2197         struct rtw_wlan_acl_node *acl_node;
2198         struct sta_priv *stapriv = &adapter->stapriv;
2199         struct wlan_acl_pool *acl = &stapriv->acl_list;
2200         _queue  *acl_node_q = &acl->acl_node_q;
2201         u8 is_baddr = is_broadcast_mac_addr(addr);
2202         u8 match = 0;
2203
2204         _enter_critical_bh(&(acl_node_q->lock), &irqL);
2205
2206         head = get_list_head(acl_node_q);
2207         list = get_next(head);
2208
2209         while (rtw_end_of_queue_search(head, list) == _FALSE) {
2210                 acl_node = LIST_CONTAINOR(list, struct rtw_wlan_acl_node, list);
2211                 list = get_next(list);
2212
2213                 if (is_baddr || _rtw_memcmp(acl_node->addr, addr, ETH_ALEN)) {
2214                         if (acl_node->valid == _TRUE) {
2215                                 acl_node->valid = _FALSE;
2216                                 rtw_list_delete(&acl_node->list);
2217                                 acl->num--;
2218                                 match = 1;
2219                         }
2220                 }
2221         }
2222
2223         _exit_critical_bh(&(acl_node_q->lock), &irqL);
2224
2225         RTW_INFO(FUNC_ADPT_FMT" "MAC_FMT" %s (acl_num=%d)\n"
2226                  , FUNC_ADPT_ARG(adapter), MAC_ARG(addr)
2227                  , is_baddr ? "clear all" : (match ? "match" : "no found")
2228                  , acl->num);
2229
2230         return ret;
2231 }
2232 #endif /* CONFIG_RTW_MACADDR_ACL */
2233
2234 u8 rtw_ap_set_pairwise_key(_adapter *padapter, struct sta_info *psta)
2235 {
2236         struct cmd_obj                  *ph2c;
2237         struct set_stakey_parm  *psetstakey_para;
2238         struct cmd_priv                 *pcmdpriv = &padapter->cmdpriv;
2239         u8      res = _SUCCESS;
2240
2241         ph2c = (struct cmd_obj *)rtw_zmalloc(sizeof(struct cmd_obj));
2242         if (ph2c == NULL) {
2243                 res = _FAIL;
2244                 goto exit;
2245         }
2246
2247         psetstakey_para = (struct set_stakey_parm *)rtw_zmalloc(sizeof(struct set_stakey_parm));
2248         if (psetstakey_para == NULL) {
2249                 rtw_mfree((u8 *) ph2c, sizeof(struct cmd_obj));
2250                 res = _FAIL;
2251                 goto exit;
2252         }
2253
2254         init_h2fwcmd_w_parm_no_rsp(ph2c, psetstakey_para, _SetStaKey_CMD_);
2255
2256
2257         psetstakey_para->algorithm = (u8)psta->dot118021XPrivacy;
2258
2259         _rtw_memcpy(psetstakey_para->addr, psta->hwaddr, ETH_ALEN);
2260
2261         _rtw_memcpy(psetstakey_para->key, &psta->dot118021x_UncstKey, 16);
2262
2263
2264         res = rtw_enqueue_cmd(pcmdpriv, ph2c);
2265
2266 exit:
2267
2268         return res;
2269
2270 }
2271
2272 static int rtw_ap_set_key(_adapter *padapter, u8 *key, u8 alg, int keyid, u8 set_tx)
2273 {
2274         u8 keylen;
2275         struct cmd_obj *pcmd;
2276         struct setkey_parm *psetkeyparm;
2277         struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
2278         int res = _SUCCESS;
2279
2280         /* RTW_INFO("%s\n", __FUNCTION__); */
2281
2282         pcmd = (struct cmd_obj *)rtw_zmalloc(sizeof(struct cmd_obj));
2283         if (pcmd == NULL) {
2284                 res = _FAIL;
2285                 goto exit;
2286         }
2287         psetkeyparm = (struct setkey_parm *)rtw_zmalloc(sizeof(struct setkey_parm));
2288         if (psetkeyparm == NULL) {
2289                 rtw_mfree((unsigned char *)pcmd, sizeof(struct cmd_obj));
2290                 res = _FAIL;
2291                 goto exit;
2292         }
2293
2294         _rtw_memset(psetkeyparm, 0, sizeof(struct setkey_parm));
2295
2296         psetkeyparm->keyid = (u8)keyid;
2297         if (is_wep_enc(alg))
2298                 padapter->securitypriv.key_mask |= BIT(psetkeyparm->keyid);
2299
2300         psetkeyparm->algorithm = alg;
2301
2302         psetkeyparm->set_tx = set_tx;
2303
2304         switch (alg) {
2305         case _WEP40_:
2306                 keylen = 5;
2307                 break;
2308         case _WEP104_:
2309                 keylen = 13;
2310                 break;
2311         case _TKIP_:
2312         case _TKIP_WTMIC_:
2313         case _AES_:
2314         default:
2315                 keylen = 16;
2316         }
2317
2318         _rtw_memcpy(&(psetkeyparm->key[0]), key, keylen);
2319
2320         pcmd->cmdcode = _SetKey_CMD_;
2321         pcmd->parmbuf = (u8 *)psetkeyparm;
2322         pcmd->cmdsz = (sizeof(struct setkey_parm));
2323         pcmd->rsp = NULL;
2324         pcmd->rspsz = 0;
2325
2326
2327         _rtw_init_listhead(&pcmd->list);
2328
2329         res = rtw_enqueue_cmd(pcmdpriv, pcmd);
2330
2331 exit:
2332
2333         return res;
2334 }
2335
2336 int rtw_ap_set_group_key(_adapter *padapter, u8 *key, u8 alg, int keyid)
2337 {
2338         RTW_INFO("%s\n", __FUNCTION__);
2339
2340         return rtw_ap_set_key(padapter, key, alg, keyid, 1);
2341 }
2342
2343 int rtw_ap_set_wep_key(_adapter *padapter, u8 *key, u8 keylen, int keyid, u8 set_tx)
2344 {
2345         u8 alg;
2346
2347         switch (keylen) {
2348         case 5:
2349                 alg = _WEP40_;
2350                 break;
2351         case 13:
2352                 alg = _WEP104_;
2353                 break;
2354         default:
2355                 alg = _NO_PRIVACY_;
2356         }
2357
2358         RTW_INFO("%s\n", __FUNCTION__);
2359
2360         return rtw_ap_set_key(padapter, key, alg, keyid, set_tx);
2361 }
2362
2363 u8 rtw_ap_bmc_frames_hdl(_adapter *padapter)
2364 {
2365 #define HIQ_XMIT_COUNTS (6)
2366         _irqL irqL;
2367         struct sta_info *psta_bmc;
2368         _list   *xmitframe_plist, *xmitframe_phead;
2369         struct xmit_frame *pxmitframe = NULL;
2370         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2371         struct sta_priv  *pstapriv = &padapter->stapriv;
2372         bool update_tim = _FALSE;
2373
2374
2375         if (padapter->registrypriv.wifi_spec != 1)
2376                 return H2C_SUCCESS;
2377
2378
2379         psta_bmc = rtw_get_bcmc_stainfo(padapter);
2380         if (!psta_bmc)
2381                 return H2C_SUCCESS;
2382
2383
2384         _enter_critical_bh(&pxmitpriv->lock, &irqL);
2385
2386         if ((pstapriv->tim_bitmap & BIT(0)) && (psta_bmc->sleepq_len > 0)) {
2387                 int tx_counts = 0;
2388
2389                 _update_beacon(padapter, _TIM_IE_, NULL, _FALSE, "update TIM with TIB=1");
2390
2391                 RTW_INFO("sleepq_len of bmc_sta = %d\n", psta_bmc->sleepq_len);
2392
2393                 xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
2394                 xmitframe_plist = get_next(xmitframe_phead);
2395
2396                 while ((rtw_end_of_queue_search(xmitframe_phead, xmitframe_plist)) == _FALSE) {
2397                         pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
2398
2399                         xmitframe_plist = get_next(xmitframe_plist);
2400
2401                         rtw_list_delete(&pxmitframe->list);
2402
2403                         psta_bmc->sleepq_len--;
2404                         tx_counts++;
2405
2406                         if (psta_bmc->sleepq_len > 0)
2407                                 pxmitframe->attrib.mdata = 1;
2408                         else
2409                                 pxmitframe->attrib.mdata = 0;
2410
2411                         if (tx_counts == HIQ_XMIT_COUNTS)
2412                                 pxmitframe->attrib.mdata = 0;
2413
2414                         pxmitframe->attrib.triggered = 1;
2415
2416                         if (xmitframe_hiq_filter(pxmitframe) == _TRUE)
2417                                 pxmitframe->attrib.qsel = QSLT_HIGH;/*HIQ*/
2418
2419                         rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
2420
2421                         if (tx_counts == HIQ_XMIT_COUNTS)
2422                                 break;
2423
2424                 }
2425
2426         } else {
2427                 if (psta_bmc->sleepq_len == 0) {
2428
2429                         /*RTW_INFO("sleepq_len of bmc_sta = %d\n", psta_bmc->sleepq_len);*/
2430
2431                         if (pstapriv->tim_bitmap & BIT(0))
2432                                 update_tim = _TRUE;
2433
2434                         pstapriv->tim_bitmap &= ~BIT(0);
2435                         pstapriv->sta_dz_bitmap &= ~BIT(0);
2436
2437                         if (update_tim == _TRUE) {
2438                                 RTW_INFO("clear TIB\n");
2439                                 _update_beacon(padapter, _TIM_IE_, NULL, _TRUE, "bmc sleepq and HIQ empty");
2440                         }
2441                 }
2442         }
2443
2444         _exit_critical_bh(&pxmitpriv->lock, &irqL);
2445
2446 #if 0
2447         /* HIQ Check */
2448         rtw_hal_get_hwreg(padapter, HW_VAR_CHK_HI_QUEUE_EMPTY, &empty);
2449
2450         while (_FALSE == empty && rtw_get_passing_time_ms(start) < 3000) {
2451                 rtw_msleep_os(100);
2452                 rtw_hal_get_hwreg(padapter, HW_VAR_CHK_HI_QUEUE_EMPTY, &empty);
2453         }
2454
2455
2456         printk("check if hiq empty=%d\n", empty);
2457 #endif
2458
2459         return H2C_SUCCESS;
2460 }
2461
2462 #ifdef CONFIG_NATIVEAP_MLME
2463
2464 static void associated_stainfo_update(_adapter *padapter, struct sta_info *psta, u32 sta_info_type)
2465 {
2466         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2467
2468         RTW_INFO("%s: "MAC_FMT", updated_type=0x%x\n", __func__, MAC_ARG(psta->hwaddr), sta_info_type);
2469
2470         if (sta_info_type & STA_INFO_UPDATE_BW) {
2471
2472                 if ((psta->flags & WLAN_STA_HT) && !psta->ht_20mhz_set) {
2473                         if (pmlmepriv->sw_to_20mhz) {
2474                                 psta->bw_mode = CHANNEL_WIDTH_20;
2475                                 /*psta->htpriv.ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;*/
2476                                 psta->htpriv.sgi_40m = _FALSE;
2477                         } else {
2478                                 /*TODO: Switch back to 40MHZ?80MHZ*/
2479                         }
2480                 }
2481         }
2482
2483         /*
2484                 if (sta_info_type & STA_INFO_UPDATE_RATE) {
2485
2486                 }
2487         */
2488
2489         if (sta_info_type & STA_INFO_UPDATE_PROTECTION_MODE)
2490                 VCS_update(padapter, psta);
2491
2492         /*
2493                 if (sta_info_type & STA_INFO_UPDATE_CAP) {
2494
2495                 }
2496
2497                 if (sta_info_type & STA_INFO_UPDATE_HT_CAP) {
2498
2499                 }
2500
2501                 if (sta_info_type & STA_INFO_UPDATE_VHT_CAP) {
2502
2503                 }
2504         */
2505
2506 }
2507
2508 static void update_bcn_ext_capab_ie(_adapter *padapter)
2509 {
2510         sint ie_len = 0;
2511         unsigned char   *pbuf;
2512         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2513         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2514         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2515         WLAN_BSSID_EX *pnetwork = &(pmlmeinfo->network);
2516         u8 *ie = pnetwork->IEs;
2517         u8 null_extcap_data[8] = {0};
2518
2519         pbuf = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _EXT_CAP_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
2520         if (pbuf && ie_len > 0)
2521                 rtw_remove_bcn_ie(padapter, pnetwork, _EXT_CAP_IE_);
2522
2523         if ((pmlmepriv->ext_capab_ie_len > 0) &&
2524             (_rtw_memcmp(pmlmepriv->ext_capab_ie_data, null_extcap_data, sizeof(null_extcap_data)) == _FALSE))
2525                 rtw_add_bcn_ie(padapter, pnetwork, _EXT_CAP_IE_, pmlmepriv->ext_capab_ie_data, pmlmepriv->ext_capab_ie_len);
2526
2527 }
2528
2529 static void update_bcn_fixed_ie(_adapter *padapter)
2530 {
2531         RTW_INFO("%s\n", __FUNCTION__);
2532
2533 }
2534
2535 static void update_bcn_erpinfo_ie(_adapter *padapter)
2536 {
2537         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2538         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2539         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2540         WLAN_BSSID_EX *pnetwork = &(pmlmeinfo->network);
2541         unsigned char *p, *ie = pnetwork->IEs;
2542         u32 len = 0;
2543
2544         RTW_INFO("%s, ERP_enable=%d\n", __FUNCTION__, pmlmeinfo->ERP_enable);
2545
2546         if (!pmlmeinfo->ERP_enable)
2547                 return;
2548
2549         /* parsing ERP_IE */
2550         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _ERPINFO_IE_, &len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
2551         if (p && len > 0) {
2552                 PNDIS_802_11_VARIABLE_IEs pIE = (PNDIS_802_11_VARIABLE_IEs)p;
2553
2554                 if (pmlmepriv->num_sta_non_erp == 1)
2555                         pIE->data[0] |= RTW_ERP_INFO_NON_ERP_PRESENT | RTW_ERP_INFO_USE_PROTECTION;
2556                 else
2557                         pIE->data[0] &= ~(RTW_ERP_INFO_NON_ERP_PRESENT | RTW_ERP_INFO_USE_PROTECTION);
2558
2559                 if (pmlmepriv->num_sta_no_short_preamble > 0)
2560                         pIE->data[0] |= RTW_ERP_INFO_BARKER_PREAMBLE_MODE;
2561                 else
2562                         pIE->data[0] &= ~(RTW_ERP_INFO_BARKER_PREAMBLE_MODE);
2563
2564                 ERP_IE_handler(padapter, pIE);
2565         }
2566
2567 }
2568
2569 static void update_bcn_htcap_ie(_adapter *padapter)
2570 {
2571         RTW_INFO("%s\n", __FUNCTION__);
2572
2573 }
2574
2575 static void update_bcn_htinfo_ie(_adapter *padapter)
2576 {
2577         /*
2578         u8 beacon_updated = _FALSE;
2579         u32 sta_info_update_type = STA_INFO_UPDATE_NONE;
2580         */
2581         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2582         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2583         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2584         WLAN_BSSID_EX *pnetwork = &(pmlmeinfo->network);
2585         unsigned char *p, *ie = pnetwork->IEs;
2586         u32 len = 0;
2587
2588         if (pmlmepriv->htpriv.ht_option == _FALSE)
2589                 return;
2590
2591         if (pmlmeinfo->HT_info_enable != 1)
2592                 return;
2593
2594
2595         RTW_INFO("%s current operation mode=0x%X\n",
2596                  __FUNCTION__, pmlmepriv->ht_op_mode);
2597
2598         RTW_INFO("num_sta_40mhz_intolerant(%d), 20mhz_width_req(%d), intolerant_ch_rpt(%d), olbc(%d)\n",
2599                 pmlmepriv->num_sta_40mhz_intolerant, pmlmepriv->ht_20mhz_width_req, pmlmepriv->ht_intolerant_ch_reported, pmlmepriv->olbc);
2600
2601         /*parsing HT_INFO_IE, currently only update ht_op_mode - pht_info->infos[1] & pht_info->infos[2] for wifi logo test*/
2602         p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_ADD_INFO_IE_, &len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
2603         if (p && len > 0) {
2604                 struct HT_info_element *pht_info = NULL;
2605
2606                 pht_info = (struct HT_info_element *)(p + 2);
2607
2608                 /* for STA Channel Width/Secondary Channel Offset*/
2609                 if ((pmlmepriv->sw_to_20mhz == 0) && (pmlmeext->cur_channel <= 14)) {
2610                         if ((pmlmepriv->num_sta_40mhz_intolerant > 0) || (pmlmepriv->ht_20mhz_width_req == _TRUE)
2611                             || (pmlmepriv->ht_intolerant_ch_reported == _TRUE) || (pmlmepriv->olbc == _TRUE)) {
2612                                 SET_HT_OP_ELE_2ND_CHL_OFFSET(pht_info, 0);
2613                                 SET_HT_OP_ELE_STA_CHL_WIDTH(pht_info, 0);
2614
2615                                 pmlmepriv->sw_to_20mhz = 1;
2616                                 /*
2617                                 sta_info_update_type |= STA_INFO_UPDATE_BW;
2618                                 beacon_updated = _TRUE;
2619                                 */
2620
2621                                 RTW_INFO("%s:switching to 20Mhz\n", __FUNCTION__);
2622
2623                                 /*TODO : cur_bwmode/cur_ch_offset switches to 20Mhz*/
2624                         }
2625                 } else {
2626
2627                         if ((pmlmepriv->num_sta_40mhz_intolerant == 0) && (pmlmepriv->ht_20mhz_width_req == _FALSE)
2628                             && (pmlmepriv->ht_intolerant_ch_reported == _FALSE) && (pmlmepriv->olbc == _FALSE)) {
2629
2630                                 if (pmlmeext->cur_bwmode >= CHANNEL_WIDTH_40) {
2631
2632                                         SET_HT_OP_ELE_STA_CHL_WIDTH(pht_info, 1);
2633
2634                                         SET_HT_OP_ELE_2ND_CHL_OFFSET(pht_info,
2635                                                 (pmlmeext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER) ?
2636                                                 HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE : HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW);
2637
2638                                         pmlmepriv->sw_to_20mhz = 0;
2639                                         /*
2640                                         sta_info_update_type |= STA_INFO_UPDATE_BW;
2641                                         beacon_updated = _TRUE;
2642                                         */
2643
2644                                         RTW_INFO("%s:switching back to 40Mhz\n", __FUNCTION__);
2645                                 }
2646                         }
2647                 }
2648
2649                 /* to update  ht_op_mode*/
2650                 *(u16 *)(pht_info->infos + 1) = cpu_to_le16(pmlmepriv->ht_op_mode);
2651
2652         }
2653
2654         /*associated_clients_update(padapter, beacon_updated, sta_info_update_type);*/
2655
2656 }
2657
2658 static void update_bcn_rsn_ie(_adapter *padapter)
2659 {
2660         RTW_INFO("%s\n", __FUNCTION__);
2661
2662 }
2663
2664 static void update_bcn_wpa_ie(_adapter *padapter)
2665 {
2666         RTW_INFO("%s\n", __FUNCTION__);
2667
2668 }
2669
2670 static void update_bcn_wmm_ie(_adapter *padapter)
2671 {
2672         RTW_INFO("%s\n", __FUNCTION__);
2673
2674 }
2675
2676 static void update_bcn_wps_ie(_adapter *padapter)
2677 {
2678         u8 *pwps_ie = NULL, *pwps_ie_src, *premainder_ie, *pbackup_remainder_ie = NULL;
2679         uint wps_ielen = 0, wps_offset, remainder_ielen;
2680         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2681         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2682         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2683         WLAN_BSSID_EX *pnetwork = &(pmlmeinfo->network);
2684         unsigned char *ie = pnetwork->IEs;
2685         u32 ielen = pnetwork->IELength;
2686
2687
2688         RTW_INFO("%s\n", __FUNCTION__);
2689
2690         pwps_ie = rtw_get_wps_ie(ie + _FIXED_IE_LENGTH_, ielen - _FIXED_IE_LENGTH_, NULL, &wps_ielen);
2691
2692         if (pwps_ie == NULL || wps_ielen == 0)
2693                 return;
2694
2695         pwps_ie_src = pmlmepriv->wps_beacon_ie;
2696         if (pwps_ie_src == NULL)
2697                 return;
2698
2699         wps_offset = (uint)(pwps_ie - ie);
2700
2701         premainder_ie = pwps_ie + wps_ielen;
2702
2703         remainder_ielen = ielen - wps_offset - wps_ielen;
2704
2705         if (remainder_ielen > 0) {
2706                 pbackup_remainder_ie = rtw_malloc(remainder_ielen);
2707                 if (pbackup_remainder_ie)
2708                         _rtw_memcpy(pbackup_remainder_ie, premainder_ie, remainder_ielen);
2709         }
2710
2711         wps_ielen = (uint)pwps_ie_src[1];/* to get ie data len */
2712         if ((wps_offset + wps_ielen + 2 + remainder_ielen) <= MAX_IE_SZ) {
2713                 _rtw_memcpy(pwps_ie, pwps_ie_src, wps_ielen + 2);
2714                 pwps_ie += (wps_ielen + 2);
2715
2716                 if (pbackup_remainder_ie)
2717                         _rtw_memcpy(pwps_ie, pbackup_remainder_ie, remainder_ielen);
2718
2719                 /* update IELength */
2720                 pnetwork->IELength = wps_offset + (wps_ielen + 2) + remainder_ielen;
2721         }
2722
2723         if (pbackup_remainder_ie)
2724                 rtw_mfree(pbackup_remainder_ie, remainder_ielen);
2725
2726         /* deal with the case without set_tx_beacon_cmd() in update_beacon() */
2727 #if defined(CONFIG_INTERRUPT_BASED_TXBCN) || defined(CONFIG_PCI_HCI)
2728         if ((pmlmeinfo->state & 0x03) == WIFI_FW_AP_STATE) {
2729                 u8 sr = 0;
2730                 rtw_get_wps_attr_content(pwps_ie_src,  wps_ielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL);
2731
2732                 if (sr) {
2733                         set_fwstate(pmlmepriv, WIFI_UNDER_WPS);
2734                         RTW_INFO("%s, set WIFI_UNDER_WPS\n", __func__);
2735                 }
2736         }
2737 #endif
2738 }
2739
2740 static void update_bcn_p2p_ie(_adapter *padapter)
2741 {
2742
2743 }
2744
2745 static void update_bcn_vendor_spec_ie(_adapter *padapter, u8 *oui)
2746 {
2747         RTW_INFO("%s\n", __FUNCTION__);
2748
2749         if (_rtw_memcmp(RTW_WPA_OUI, oui, 4))
2750                 update_bcn_wpa_ie(padapter);
2751         else if (_rtw_memcmp(WMM_OUI, oui, 4))
2752                 update_bcn_wmm_ie(padapter);
2753         else if (_rtw_memcmp(WPS_OUI, oui, 4))
2754                 update_bcn_wps_ie(padapter);
2755         else if (_rtw_memcmp(P2P_OUI, oui, 4))
2756                 update_bcn_p2p_ie(padapter);
2757         else
2758                 RTW_INFO("unknown OUI type!\n");
2759
2760
2761 }
2762
2763 void _update_beacon(_adapter *padapter, u8 ie_id, u8 *oui, u8 tx, const char *tag)
2764 {
2765         _irqL irqL;
2766         struct mlme_priv *pmlmepriv;
2767         struct mlme_ext_priv    *pmlmeext;
2768         /* struct mlme_ext_info *pmlmeinfo; */
2769
2770         /* RTW_INFO("%s\n", __FUNCTION__); */
2771
2772         if (!padapter)
2773                 return;
2774
2775         pmlmepriv = &(padapter->mlmepriv);
2776         pmlmeext = &(padapter->mlmeextpriv);
2777         /* pmlmeinfo = &(pmlmeext->mlmext_info); */
2778
2779         if (_FALSE == pmlmeext->bstart_bss)
2780                 return;
2781
2782         _enter_critical_bh(&pmlmepriv->bcn_update_lock, &irqL);
2783
2784         switch (ie_id) {
2785         case 0xFF:
2786
2787                 update_bcn_fixed_ie(padapter);/* 8: TimeStamp, 2: Beacon Interval 2:Capability */
2788
2789                 break;
2790
2791         case _TIM_IE_:
2792
2793                 update_BCNTIM(padapter);
2794
2795                 break;
2796
2797         case _ERPINFO_IE_:
2798
2799                 update_bcn_erpinfo_ie(padapter);
2800
2801                 break;
2802
2803         case _HT_CAPABILITY_IE_:
2804
2805                 update_bcn_htcap_ie(padapter);
2806
2807                 break;
2808
2809         case _RSN_IE_2_:
2810
2811                 update_bcn_rsn_ie(padapter);
2812
2813                 break;
2814
2815         case _HT_ADD_INFO_IE_:
2816
2817                 update_bcn_htinfo_ie(padapter);
2818
2819                 break;
2820
2821         case _EXT_CAP_IE_:
2822
2823                 update_bcn_ext_capab_ie(padapter);
2824
2825                 break;
2826
2827         case _VENDOR_SPECIFIC_IE_:
2828
2829                 update_bcn_vendor_spec_ie(padapter, oui);
2830
2831                 break;
2832
2833         default:
2834                 break;
2835         }
2836
2837         pmlmepriv->update_bcn = _TRUE;
2838
2839         _exit_critical_bh(&pmlmepriv->bcn_update_lock, &irqL);
2840
2841 #ifndef CONFIG_INTERRUPT_BASED_TXBCN
2842 #if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
2843         if (tx) {
2844                 /* send_beacon(padapter); */ /* send_beacon must execute on TSR level */
2845                 if (0)
2846                         RTW_INFO(FUNC_ADPT_FMT" ie_id:%u - %s\n", FUNC_ADPT_ARG(padapter), ie_id, tag);
2847                 set_tx_beacon_cmd(padapter);
2848         }
2849 #else
2850         {
2851                 /* PCI will issue beacon when BCN interrupt occurs.              */
2852         }
2853 #endif
2854 #endif /* !CONFIG_INTERRUPT_BASED_TXBCN */
2855
2856 }
2857
2858 #ifdef CONFIG_80211N_HT
2859
2860 void rtw_process_public_act_bsscoex(_adapter *padapter, u8 *pframe, uint frame_len)
2861 {
2862         struct sta_info *psta;
2863         struct sta_priv *pstapriv = &padapter->stapriv;
2864         u8 beacon_updated = _FALSE;
2865         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2866         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
2867         uint frame_body_len = frame_len - sizeof(struct rtw_ieee80211_hdr_3addr);
2868         u8 category, action;
2869
2870         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
2871         if (psta == NULL)
2872                 return;
2873
2874
2875         category = frame_body[0];
2876         action = frame_body[1];
2877
2878         if (frame_body_len > 0) {
2879                 if ((frame_body[2] == EID_BSSCoexistence) && (frame_body[3] > 0)) {
2880                         u8 ie_data = frame_body[4];
2881
2882                         if (ie_data & RTW_WLAN_20_40_BSS_COEX_40MHZ_INTOL) {
2883                                 if (psta->ht_40mhz_intolerant == 0) {
2884                                         psta->ht_40mhz_intolerant = 1;
2885                                         pmlmepriv->num_sta_40mhz_intolerant++;
2886                                         beacon_updated = _TRUE;
2887                                 }
2888                         } else if (ie_data & RTW_WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ)   {
2889                                 if (pmlmepriv->ht_20mhz_width_req == _FALSE) {
2890                                         pmlmepriv->ht_20mhz_width_req = _TRUE;
2891                                         beacon_updated = _TRUE;
2892                                 }
2893                         } else
2894                                 beacon_updated = _FALSE;
2895                 }
2896         }
2897
2898         if (frame_body_len > 8) {
2899                 /* if EID_BSSIntolerantChlReport ie exists */
2900                 if ((frame_body[5] == EID_BSSIntolerantChlReport) && (frame_body[6] > 0)) {
2901                         /*todo:*/
2902                         if (pmlmepriv->ht_intolerant_ch_reported == _FALSE) {
2903                                 pmlmepriv->ht_intolerant_ch_reported = _TRUE;
2904                                 beacon_updated = _TRUE;
2905                         }
2906                 }
2907         }
2908
2909         if (beacon_updated) {
2910
2911                 update_beacon(padapter, _HT_ADD_INFO_IE_, NULL, _TRUE);
2912
2913                 associated_stainfo_update(padapter, psta, STA_INFO_UPDATE_BW);
2914         }
2915
2916
2917
2918 }
2919
2920 void rtw_process_ht_action_smps(_adapter *padapter, u8 *ta, u8 ctrl_field)
2921 {
2922         u8 e_field, m_field;
2923         struct sta_info *psta;
2924         struct sta_priv *pstapriv = &padapter->stapriv;
2925
2926         psta = rtw_get_stainfo(pstapriv, ta);
2927         if (psta == NULL)
2928                 return;
2929
2930         e_field = (ctrl_field & BIT(0)) ? 1 : 0;
2931         m_field = (ctrl_field & BIT(1)) ? 1 : 0;
2932
2933         if (e_field) {
2934
2935                 /* enable */
2936                 /* 0:static SMPS, 1:dynamic SMPS, 3:SMPS disabled, 2:reserved*/
2937
2938                 if (m_field) /*mode*/
2939                         psta->htpriv.smps_cap = 1;
2940                 else
2941                         psta->htpriv.smps_cap = 0;
2942         } else {
2943                 /*disable*/
2944                 psta->htpriv.smps_cap = 3;
2945         }
2946
2947         rtw_dm_ra_mask_wk_cmd(padapter, (u8 *)psta);
2948
2949 }
2950
2951 /*
2952 op_mode
2953 Set to 0 (HT pure) under the followign conditions
2954         - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
2955         - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
2956 Set to 1 (HT non-member protection) if there may be non-HT STAs
2957         in both the primary and the secondary channel
2958 Set to 2 if only HT STAs are associated in BSS,
2959         however and at least one 20 MHz HT STA is associated
2960 Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
2961         (currently non-GF HT station is considered as non-HT STA also)
2962 */
2963 static int rtw_ht_operation_update(_adapter *padapter)
2964 {
2965         u16 cur_op_mode, new_op_mode;
2966         int op_mode_changes = 0;
2967         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2968         struct ht_priv  *phtpriv_ap = &pmlmepriv->htpriv;
2969
2970         if (pmlmepriv->htpriv.ht_option == _FALSE)
2971                 return 0;
2972
2973         /*if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
2974                 return 0;*/
2975
2976         RTW_INFO("%s current operation mode=0x%X\n",
2977                  __FUNCTION__, pmlmepriv->ht_op_mode);
2978
2979         if (!(pmlmepriv->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
2980             && pmlmepriv->num_sta_ht_no_gf) {
2981                 pmlmepriv->ht_op_mode |=
2982                         HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
2983                 op_mode_changes++;
2984         } else if ((pmlmepriv->ht_op_mode &
2985                     HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
2986                    pmlmepriv->num_sta_ht_no_gf == 0) {
2987                 pmlmepriv->ht_op_mode &=
2988                         ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
2989                 op_mode_changes++;
2990         }
2991
2992         if (!(pmlmepriv->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
2993             (pmlmepriv->num_sta_no_ht || pmlmepriv->olbc_ht)) {
2994                 pmlmepriv->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
2995                 op_mode_changes++;
2996         } else if ((pmlmepriv->ht_op_mode &
2997                     HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
2998                    (pmlmepriv->num_sta_no_ht == 0 && !pmlmepriv->olbc_ht)) {
2999                 pmlmepriv->ht_op_mode &=
3000                         ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
3001                 op_mode_changes++;
3002         }
3003
3004         /* Note: currently we switch to the MIXED op mode if HT non-greenfield
3005          * station is associated. Probably it's a theoretical case, since
3006          * it looks like all known HT STAs support greenfield.
3007          */
3008         new_op_mode = 0;
3009         if (pmlmepriv->num_sta_no_ht /*||
3010             (pmlmepriv->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)*/)
3011                 new_op_mode = OP_MODE_MIXED;
3012         else if ((phtpriv_ap->ht_cap.cap_info & IEEE80211_HT_CAP_SUP_WIDTH)
3013                  && pmlmepriv->num_sta_ht_20mhz)
3014                 new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
3015         else if (pmlmepriv->olbc_ht)
3016                 new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
3017         else
3018                 new_op_mode = OP_MODE_PURE;
3019
3020         cur_op_mode = pmlmepriv->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
3021         if (cur_op_mode != new_op_mode) {
3022                 pmlmepriv->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
3023                 pmlmepriv->ht_op_mode |= new_op_mode;
3024                 op_mode_changes++;
3025         }
3026
3027         RTW_INFO("%s new operation mode=0x%X changes=%d\n",
3028                  __FUNCTION__, pmlmepriv->ht_op_mode, op_mode_changes);
3029
3030         return op_mode_changes;
3031
3032 }
3033
3034 #endif /* CONFIG_80211N_HT */
3035
3036 void associated_clients_update(_adapter *padapter, u8 updated, u32 sta_info_type)
3037 {
3038         /* update associcated stations cap. */
3039         if (updated == _TRUE) {
3040                 _irqL irqL;
3041                 _list   *phead, *plist;
3042                 struct sta_info *psta = NULL;
3043                 struct sta_priv *pstapriv = &padapter->stapriv;
3044
3045                 _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
3046
3047                 phead = &pstapriv->asoc_list;
3048                 plist = get_next(phead);
3049
3050                 /* check asoc_queue */
3051                 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
3052                         psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
3053
3054                         plist = get_next(plist);
3055
3056                         associated_stainfo_update(padapter, psta, sta_info_type);
3057                 }
3058
3059                 _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
3060
3061         }
3062
3063 }
3064
3065 /* called > TSR LEVEL for USB or SDIO Interface*/
3066 void bss_cap_update_on_sta_join(_adapter *padapter, struct sta_info *psta)
3067 {
3068         u8 beacon_updated = _FALSE;
3069         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3070         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3071
3072
3073 #if 0
3074         if (!(psta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
3075             !psta->no_short_preamble_set) {
3076                 psta->no_short_preamble_set = 1;
3077                 pmlmepriv->num_sta_no_short_preamble++;
3078                 if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
3079                     (pmlmepriv->num_sta_no_short_preamble == 1))
3080                         ieee802_11_set_beacons(hapd->iface);
3081         }
3082 #endif
3083
3084
3085         if (!(psta->flags & WLAN_STA_SHORT_PREAMBLE)) {
3086                 if (!psta->no_short_preamble_set) {
3087                         psta->no_short_preamble_set = 1;
3088
3089                         pmlmepriv->num_sta_no_short_preamble++;
3090
3091                         if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
3092                             (pmlmepriv->num_sta_no_short_preamble == 1)) {
3093                                 beacon_updated = _TRUE;
3094                                 update_beacon(padapter, 0xFF, NULL, _TRUE);
3095                         }
3096
3097                 }
3098         } else {
3099                 if (psta->no_short_preamble_set) {
3100                         psta->no_short_preamble_set = 0;
3101
3102                         pmlmepriv->num_sta_no_short_preamble--;
3103
3104                         if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
3105                             (pmlmepriv->num_sta_no_short_preamble == 0)) {
3106                                 beacon_updated = _TRUE;
3107                                 update_beacon(padapter, 0xFF, NULL, _TRUE);
3108                         }
3109
3110                 }
3111         }
3112
3113 #if 0
3114         if (psta->flags & WLAN_STA_NONERP && !psta->nonerp_set) {
3115                 psta->nonerp_set = 1;
3116                 pmlmepriv->num_sta_non_erp++;
3117                 if (pmlmepriv->num_sta_non_erp == 1)
3118                         ieee802_11_set_beacons(hapd->iface);
3119         }
3120 #endif
3121
3122         if (psta->flags & WLAN_STA_NONERP) {
3123                 if (!psta->nonerp_set) {
3124                         psta->nonerp_set = 1;
3125
3126                         pmlmepriv->num_sta_non_erp++;
3127
3128                         if (pmlmepriv->num_sta_non_erp == 1) {
3129                                 beacon_updated = _TRUE;
3130                                 update_beacon(padapter, _ERPINFO_IE_, NULL, _TRUE);
3131                         }
3132                 }
3133
3134         } else {
3135                 if (psta->nonerp_set) {
3136                         psta->nonerp_set = 0;
3137
3138                         pmlmepriv->num_sta_non_erp--;
3139
3140                         if (pmlmepriv->num_sta_non_erp == 0) {
3141                                 beacon_updated = _TRUE;
3142                                 update_beacon(padapter, _ERPINFO_IE_, NULL, _TRUE);
3143                         }
3144                 }
3145
3146         }
3147
3148
3149 #if 0
3150         if (!(psta->capability & WLAN_CAPABILITY_SHORT_SLOT) &&
3151             !psta->no_short_slot_time_set) {
3152                 psta->no_short_slot_time_set = 1;
3153                 pmlmepriv->num_sta_no_short_slot_time++;
3154                 if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
3155                     (pmlmepriv->num_sta_no_short_slot_time == 1))
3156                         ieee802_11_set_beacons(hapd->iface);
3157         }
3158 #endif
3159
3160         if (!(psta->capability & WLAN_CAPABILITY_SHORT_SLOT)) {
3161                 if (!psta->no_short_slot_time_set) {
3162                         psta->no_short_slot_time_set = 1;
3163
3164                         pmlmepriv->num_sta_no_short_slot_time++;
3165
3166                         if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
3167                             (pmlmepriv->num_sta_no_short_slot_time == 1)) {
3168                                 beacon_updated = _TRUE;
3169                                 update_beacon(padapter, 0xFF, NULL, _TRUE);
3170                         }
3171
3172                 }
3173         } else {
3174                 if (psta->no_short_slot_time_set) {
3175                         psta->no_short_slot_time_set = 0;
3176
3177                         pmlmepriv->num_sta_no_short_slot_time--;
3178
3179                         if ((pmlmeext->cur_wireless_mode > WIRELESS_11B) &&
3180                             (pmlmepriv->num_sta_no_short_slot_time == 0)) {
3181                                 beacon_updated = _TRUE;
3182                                 update_beacon(padapter, 0xFF, NULL, _TRUE);
3183                         }
3184                 }
3185         }
3186
3187 #ifdef CONFIG_80211N_HT
3188
3189         if (psta->flags & WLAN_STA_HT) {
3190                 u16 ht_capab = le16_to_cpu(psta->htpriv.ht_cap.cap_info);
3191
3192                 RTW_INFO("HT: STA " MAC_FMT " HT Capabilities "
3193                          "Info: 0x%04x\n", MAC_ARG(psta->hwaddr), ht_capab);
3194
3195                 if (psta->no_ht_set) {
3196                         psta->no_ht_set = 0;
3197                         pmlmepriv->num_sta_no_ht--;
3198                 }
3199
3200                 if ((ht_capab & IEEE80211_HT_CAP_GRN_FLD) == 0) {
3201                         if (!psta->no_ht_gf_set) {
3202                                 psta->no_ht_gf_set = 1;
3203                                 pmlmepriv->num_sta_ht_no_gf++;
3204                         }
3205                         RTW_INFO("%s STA " MAC_FMT " - no "
3206                                  "greenfield, num of non-gf stations %d\n",
3207                                  __FUNCTION__, MAC_ARG(psta->hwaddr),
3208                                  pmlmepriv->num_sta_ht_no_gf);
3209                 }
3210
3211                 if ((ht_capab & IEEE80211_HT_CAP_SUP_WIDTH) == 0) {
3212                         if (!psta->ht_20mhz_set) {
3213                                 psta->ht_20mhz_set = 1;
3214                                 pmlmepriv->num_sta_ht_20mhz++;
3215                         }
3216                         RTW_INFO("%s STA " MAC_FMT " - 20 MHz HT, "
3217                                  "num of 20MHz HT STAs %d\n",
3218                                  __FUNCTION__, MAC_ARG(psta->hwaddr),
3219                                  pmlmepriv->num_sta_ht_20mhz);
3220                 }
3221
3222
3223                 if (ht_capab & RTW_IEEE80211_HT_CAP_40MHZ_INTOLERANT) {
3224
3225                         if (!psta->ht_40mhz_intolerant) {
3226                                 psta->ht_40mhz_intolerant = 1;
3227                                 pmlmepriv->num_sta_40mhz_intolerant++;
3228                                 RTW_INFO("%s STA " MAC_FMT " - HT_CAP_40MHZ_INTOLERANT is set\n" ,
3229                                          __FUNCTION__, MAC_ARG(psta->hwaddr));
3230                                 beacon_updated = _TRUE;
3231                         }
3232
3233                         /*
3234                                                 if (pmlmepriv->ht_40mhz_intolerant == _FALSE) {
3235
3236                                                         pmlmepriv->ht_40mhz_intolerant = _TRUE;
3237
3238                                                         RTW_INFO("%s STA " MAC_FMT " - HT_CAP_40MHZ_INTOLERANT is set\n" ,
3239                                                            __FUNCTION__, MAC_ARG(psta->hwaddr));
3240
3241                                                         beacon_updated = _TRUE;
3242                                                 }
3243                         */
3244
3245                         /*update ext_capab_ie_len & ext_capab_ie_data for beacon, probersp, assocrsp.*/
3246                         if (pmlmepriv->ext_capab_ie_len == 0)
3247                                 pmlmepriv->ext_capab_ie_len = 1;
3248                         SET_EXT_CAPABILITY_ELE_BSS_COEXIST(pmlmepriv->ext_capab_ie_data, 1);
3249
3250                         update_beacon(padapter, _EXT_CAP_IE_, NULL, _FALSE);
3251                 }
3252
3253         } else {
3254                 if (!psta->no_ht_set) {
3255                         psta->no_ht_set = 1;
3256                         pmlmepriv->num_sta_no_ht++;
3257                 }
3258                 if (pmlmepriv->htpriv.ht_option == _TRUE) {
3259                         RTW_INFO("%s STA " MAC_FMT
3260                                  " - no HT, num of non-HT stations %d\n",
3261                                  __FUNCTION__, MAC_ARG(psta->hwaddr),
3262                                  pmlmepriv->num_sta_no_ht);
3263                 }
3264         }
3265
3266         if (rtw_ht_operation_update(padapter) > 0) {
3267                 update_beacon(padapter, _HT_CAPABILITY_IE_, NULL, _FALSE);
3268                 update_beacon(padapter, _HT_ADD_INFO_IE_, NULL, _TRUE);
3269                 /*beacon_updated = _TRUE;*/
3270         }
3271
3272 #endif /* CONFIG_80211N_HT */
3273
3274         /* update associcated stations cap. */
3275         associated_clients_update(padapter,  beacon_updated, STA_INFO_UPDATE_ALL);
3276
3277         RTW_INFO("%s, updated=%d\n", __func__, beacon_updated);
3278
3279 }
3280
3281 u8 bss_cap_update_on_sta_leave(_adapter *padapter, struct sta_info *psta)
3282 {
3283         u8 beacon_updated = _FALSE;
3284         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3285         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3286
3287         if (!psta)
3288                 return beacon_updated;
3289
3290         if (psta->no_short_preamble_set) {
3291                 psta->no_short_preamble_set = 0;
3292                 pmlmepriv->num_sta_no_short_preamble--;
3293                 if (pmlmeext->cur_wireless_mode > WIRELESS_11B
3294                     && pmlmepriv->num_sta_no_short_preamble == 0) {
3295                         beacon_updated = _TRUE;
3296                         update_beacon(padapter, 0xFF, NULL, _TRUE);
3297                 }
3298         }
3299
3300         if (psta->nonerp_set) {
3301                 psta->nonerp_set = 0;
3302                 pmlmepriv->num_sta_non_erp--;
3303                 if (pmlmepriv->num_sta_non_erp == 0) {
3304                         beacon_updated = _TRUE;
3305                         update_beacon(padapter, _ERPINFO_IE_, NULL, _TRUE);
3306                 }
3307         }
3308
3309         if (psta->no_short_slot_time_set) {
3310                 psta->no_short_slot_time_set = 0;
3311                 pmlmepriv->num_sta_no_short_slot_time--;
3312                 if (pmlmeext->cur_wireless_mode > WIRELESS_11B
3313                     && pmlmepriv->num_sta_no_short_slot_time == 0) {
3314                         beacon_updated = _TRUE;
3315                         update_beacon(padapter, 0xFF, NULL, _TRUE);
3316                 }
3317         }
3318
3319 #ifdef CONFIG_80211N_HT
3320
3321         if (psta->no_ht_gf_set) {
3322                 psta->no_ht_gf_set = 0;
3323                 pmlmepriv->num_sta_ht_no_gf--;
3324         }
3325
3326         if (psta->no_ht_set) {
3327                 psta->no_ht_set = 0;
3328                 pmlmepriv->num_sta_no_ht--;
3329         }
3330
3331         if (psta->ht_20mhz_set) {
3332                 psta->ht_20mhz_set = 0;
3333                 pmlmepriv->num_sta_ht_20mhz--;
3334         }
3335
3336         if (psta->ht_40mhz_intolerant) {
3337                 psta->ht_40mhz_intolerant = 0;
3338                 pmlmepriv->num_sta_40mhz_intolerant--;
3339
3340                 /*update ext_capab_ie_len & ext_capab_ie_data for beacon, probersp, assocrsp.*/
3341                 if ((pmlmepriv->ext_capab_ie_len > 0) && (pmlmepriv->num_sta_40mhz_intolerant == 0)) {
3342                         SET_EXT_CAPABILITY_ELE_BSS_COEXIST(pmlmepriv->ext_capab_ie_data, 0);
3343                         update_beacon(padapter, _EXT_CAP_IE_, NULL, _FALSE);
3344                 }
3345
3346                 beacon_updated = _TRUE;
3347
3348                 update_beacon(padapter, _HT_ADD_INFO_IE_, NULL, _FALSE);
3349         }
3350
3351         if (rtw_ht_operation_update(padapter) > 0) {
3352                 update_beacon(padapter, _HT_CAPABILITY_IE_, NULL, _FALSE);
3353                 update_beacon(padapter, _HT_ADD_INFO_IE_, NULL, _TRUE);
3354         }
3355
3356 #endif /* CONFIG_80211N_HT */
3357
3358 #if 0
3359         /* update associated stations cap. */
3360         associated_clients_update(padapter,  beacon_updated, STA_INFO_UPDATE_ALL); /* move it to avoid deadlock */
3361 #endif
3362
3363         RTW_INFO("%s, updated=%d\n", __func__, beacon_updated);
3364
3365         return beacon_updated;
3366
3367 }
3368
3369 u8 ap_free_sta(_adapter *padapter, struct sta_info *psta, bool active, u16 reason, bool enqueue)
3370 {
3371         _irqL irqL;
3372         u8 beacon_updated = _FALSE;
3373         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3374         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
3375         struct sta_priv *pstapriv = &padapter->stapriv;
3376
3377         if (!psta)
3378                 return beacon_updated;
3379
3380         if (active == _TRUE) {
3381 #ifdef CONFIG_80211N_HT
3382                 /* tear down Rx AMPDU */
3383                 send_delba(padapter, 0, psta->hwaddr);/* recipient */
3384
3385                 /* tear down TX AMPDU */
3386                 send_delba(padapter, 1, psta->hwaddr);/*  */ /* originator */
3387
3388 #endif /* CONFIG_80211N_HT */
3389
3390                 issue_deauth(padapter, psta->hwaddr, reason);
3391         }
3392
3393 #ifdef CONFIG_BEAMFORMING
3394         beamforming_wk_cmd(padapter, BEAMFORMING_CTRL_LEAVE, psta->hwaddr, ETH_ALEN, 1);
3395 #endif
3396
3397         psta->htpriv.agg_enable_bitmap = 0x0;/* reset */
3398         psta->htpriv.candidate_tid_bitmap = 0x0;/* reset */
3399
3400         /* clear cam entry / key */
3401         rtw_clearstakey_cmd(padapter, psta, enqueue);
3402
3403
3404         _enter_critical_bh(&psta->lock, &irqL);
3405         psta->state &= ~_FW_LINKED;
3406         _exit_critical_bh(&psta->lock, &irqL);
3407
3408 #ifdef CONFIG_IOCTL_CFG80211
3409         if (1) {
3410 #ifdef COMPAT_KERNEL_RELEASE
3411                 rtw_cfg80211_indicate_sta_disassoc(padapter, psta->hwaddr, reason);
3412 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) && !defined(CONFIG_CFG80211_FORCE_COMPATIBLE_2_6_37_UNDER)
3413                 rtw_cfg80211_indicate_sta_disassoc(padapter, psta->hwaddr, reason);
3414 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) && !defined(CONFIG_CFG80211_FORCE_COMPATIBLE_2_6_37_UNDER) */
3415                 /* will call rtw_cfg80211_indicate_sta_disassoc() in cmd_thread for old API context */
3416 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)) && !defined(CONFIG_CFG80211_FORCE_COMPATIBLE_2_6_37_UNDER) */
3417         } else
3418 #endif /* CONFIG_IOCTL_CFG80211 */
3419         {
3420                 rtw_indicate_sta_disassoc_event(padapter, psta);
3421         }
3422
3423         report_del_sta_event(padapter, psta->hwaddr, reason, enqueue, _FALSE);
3424
3425         beacon_updated = bss_cap_update_on_sta_leave(padapter, psta);
3426
3427         /* _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL);                                        */
3428         rtw_free_stainfo(padapter, psta);
3429         /* _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL); */
3430
3431
3432         return beacon_updated;
3433
3434 }
3435
3436 int rtw_ap_inform_ch_switch(_adapter *padapter, u8 new_ch, u8 ch_offset)
3437 {
3438         _irqL irqL;
3439         _list   *phead, *plist;
3440         int ret = 0;
3441         struct sta_info *psta = NULL;
3442         struct sta_priv *pstapriv = &padapter->stapriv;
3443         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3444         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3445         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3446
3447         if ((pmlmeinfo->state & 0x03) != WIFI_FW_AP_STATE)
3448                 return ret;
3449
3450         RTW_INFO(FUNC_NDEV_FMT" with ch:%u, offset:%u\n",
3451                  FUNC_NDEV_ARG(padapter->pnetdev), new_ch, ch_offset);
3452
3453         _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
3454         phead = &pstapriv->asoc_list;
3455         plist = get_next(phead);
3456
3457         /* for each sta in asoc_queue */
3458         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
3459                 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
3460                 plist = get_next(plist);
3461
3462                 issue_action_spct_ch_switch(padapter, psta->hwaddr, new_ch, ch_offset);
3463                 psta->expire_to = ((pstapriv->expire_to * 2) > 5) ? 5 : (pstapriv->expire_to * 2);
3464         }
3465         _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
3466
3467         issue_action_spct_ch_switch(padapter, bc_addr, new_ch, ch_offset);
3468
3469         return ret;
3470 }
3471
3472 int rtw_sta_flush(_adapter *padapter, bool enqueue)
3473 {
3474         _irqL irqL;
3475         _list   *phead, *plist;
3476         int ret = 0;
3477         struct sta_info *psta = NULL;
3478         struct sta_priv *pstapriv = &padapter->stapriv;
3479         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3480         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3481         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3482         u8 flush_num = 0;
3483         char flush_list[NUM_STA];
3484         int i;
3485
3486         if ((pmlmeinfo->state & 0x03) != WIFI_FW_AP_STATE)
3487                 return ret;
3488
3489         RTW_INFO(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(padapter->pnetdev));
3490
3491         /* pick sta from sta asoc_queue */
3492         _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
3493         phead = &pstapriv->asoc_list;
3494         plist = get_next(phead);
3495         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
3496                 int stainfo_offset;
3497
3498                 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
3499                 plist = get_next(plist);
3500
3501                 rtw_list_delete(&psta->asoc_list);
3502                 pstapriv->asoc_list_cnt--;
3503
3504                 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
3505                 if (stainfo_offset_valid(stainfo_offset))
3506                         flush_list[flush_num++] = stainfo_offset;
3507                 else
3508                         rtw_warn_on(1);
3509         }
3510         _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
3511
3512         /* call ap_free_sta() for each sta picked */
3513         for (i = 0; i < flush_num; i++) {
3514                 psta = rtw_get_stainfo_by_offset(pstapriv, flush_list[i]);
3515                 ap_free_sta(padapter, psta, _TRUE, WLAN_REASON_DEAUTH_LEAVING, enqueue);
3516         }
3517
3518         issue_deauth(padapter, bc_addr, WLAN_REASON_DEAUTH_LEAVING);
3519
3520         associated_clients_update(padapter, _TRUE, STA_INFO_UPDATE_ALL);
3521
3522         return ret;
3523 }
3524
3525 /* called > TSR LEVEL for USB or SDIO Interface*/
3526 void sta_info_update(_adapter *padapter, struct sta_info *psta)
3527 {
3528         int flags = psta->flags;
3529         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3530
3531
3532         /* update wmm cap. */
3533         if (WLAN_STA_WME & flags)
3534                 psta->qos_option = 1;
3535         else
3536                 psta->qos_option = 0;
3537
3538         if (pmlmepriv->qospriv.qos_option == 0)
3539                 psta->qos_option = 0;
3540
3541
3542 #ifdef CONFIG_80211N_HT
3543         /* update 802.11n ht cap. */
3544         if (WLAN_STA_HT & flags) {
3545                 psta->htpriv.ht_option = _TRUE;
3546                 psta->qos_option = 1;
3547
3548                 psta->htpriv.smps_cap = (psta->htpriv.ht_cap.cap_info & IEEE80211_HT_CAP_SM_PS) >> 2;
3549         } else
3550                 psta->htpriv.ht_option = _FALSE;
3551
3552         if (pmlmepriv->htpriv.ht_option == _FALSE)
3553                 psta->htpriv.ht_option = _FALSE;
3554 #endif
3555
3556 #ifdef CONFIG_80211AC_VHT
3557         /* update 802.11AC vht cap. */
3558         if (WLAN_STA_VHT & flags)
3559                 psta->vhtpriv.vht_option = _TRUE;
3560         else
3561                 psta->vhtpriv.vht_option = _FALSE;
3562
3563         if (pmlmepriv->vhtpriv.vht_option == _FALSE)
3564                 psta->vhtpriv.vht_option = _FALSE;
3565 #endif
3566
3567
3568         update_sta_info_apmode(padapter, psta);
3569
3570
3571 }
3572
3573 /* called >= TSR LEVEL for USB or SDIO Interface*/
3574 void ap_sta_info_defer_update(_adapter *padapter, struct sta_info *psta)
3575 {
3576         if (psta->state & _FW_LINKED) {
3577                 /* add ratid */
3578                 add_RATid(padapter, psta, 0);/* DM_RATR_STA_INIT */
3579         }
3580 }
3581 /* restore hw setting from sw data structures */
3582 void rtw_ap_restore_network(_adapter *padapter)
3583 {
3584         struct mlme_priv *mlmepriv = &padapter->mlmepriv;
3585         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3586         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3587         struct sta_priv *pstapriv = &padapter->stapriv;
3588         struct sta_info *psta;
3589         struct security_priv *psecuritypriv = &(padapter->securitypriv);
3590         _irqL irqL;
3591         _list   *phead, *plist;
3592         u8 chk_alive_num = 0;
3593         char chk_alive_list[NUM_STA];
3594         int i;
3595
3596         rtw_setopmode_cmd(padapter, Ndis802_11APMode, _FALSE);
3597
3598         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
3599
3600         rtw_startbss_cmd(padapter, RTW_CMDF_DIRECTLY);
3601
3602         if ((padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_) ||
3603             (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)) {
3604                 /* restore group key, WEP keys is restored in ips_leave() */
3605                 rtw_set_key(padapter, psecuritypriv, psecuritypriv->dot118021XGrpKeyid, 0, _FALSE);
3606         }
3607
3608         _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
3609
3610         phead = &pstapriv->asoc_list;
3611         plist = get_next(phead);
3612
3613         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
3614                 int stainfo_offset;
3615
3616                 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
3617                 plist = get_next(plist);
3618
3619                 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
3620                 if (stainfo_offset_valid(stainfo_offset))
3621                         chk_alive_list[chk_alive_num++] = stainfo_offset;
3622         }
3623
3624         _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
3625
3626         for (i = 0; i < chk_alive_num; i++) {
3627                 psta = rtw_get_stainfo_by_offset(pstapriv, chk_alive_list[i]);
3628
3629                 if (psta == NULL)
3630                         RTW_INFO(FUNC_ADPT_FMT" sta_info is null\n", FUNC_ADPT_ARG(padapter));
3631                 else if (psta->state & _FW_LINKED) {
3632                         rtw_sta_media_status_rpt(padapter, psta, 1);
3633                         Update_RA_Entry(padapter, psta);
3634                         /* pairwise key */
3635                         /* per sta pairwise key and settings */
3636                         if ((padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_) ||
3637                             (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_))
3638                                 rtw_setstakey_cmd(padapter, psta, UNICAST_KEY, _FALSE);
3639                 }
3640         }
3641
3642 }
3643
3644 void start_ap_mode(_adapter *padapter)
3645 {
3646         int i;
3647         struct sta_info *psta = NULL;
3648         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3649         struct sta_priv *pstapriv = &padapter->stapriv;
3650         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3651         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3652         struct security_priv *psecuritypriv = &padapter->securitypriv;
3653
3654         pmlmepriv->update_bcn = _FALSE;
3655
3656         /*init_mlme_ap_info(padapter);*/
3657
3658         pmlmeext->bstart_bss = _FALSE;
3659
3660         pmlmepriv->num_sta_non_erp = 0;
3661
3662         pmlmepriv->num_sta_no_short_slot_time = 0;
3663
3664         pmlmepriv->num_sta_no_short_preamble = 0;
3665
3666         pmlmepriv->num_sta_ht_no_gf = 0;
3667 #ifdef CONFIG_80211N_HT
3668         pmlmepriv->num_sta_no_ht = 0;
3669 #endif /* CONFIG_80211N_HT */
3670         pmlmeinfo->HT_info_enable = 0;
3671         pmlmeinfo->HT_caps_enable = 0;
3672         pmlmeinfo->HT_enable = 0;
3673
3674         pmlmepriv->num_sta_ht_20mhz = 0;
3675         pmlmepriv->num_sta_40mhz_intolerant = 0;
3676         pmlmepriv->olbc = _FALSE;
3677         pmlmepriv->olbc_ht = _FALSE;
3678
3679 #ifdef CONFIG_80211N_HT
3680         pmlmepriv->ht_20mhz_width_req = _FALSE;
3681         pmlmepriv->ht_intolerant_ch_reported = _FALSE;
3682         pmlmepriv->ht_op_mode = 0;
3683         pmlmepriv->sw_to_20mhz = 0;
3684 #endif
3685
3686         _rtw_memset(pmlmepriv->ext_capab_ie_data, 0, sizeof(pmlmepriv->ext_capab_ie_data));
3687         pmlmepriv->ext_capab_ie_len = 0;
3688
3689 #ifdef CONFIG_CONCURRENT_MODE
3690         psecuritypriv->dot118021x_bmc_cam_id = INVALID_SEC_MAC_CAM_ID;
3691 #endif
3692
3693         for (i = 0 ;  i < NUM_STA ; i++)
3694                 pstapriv->sta_aid[i] = NULL;
3695
3696 #if CONFIG_RTW_MACADDR_ACL
3697         rtw_macaddr_acl_init(padapter);
3698 #endif
3699
3700         psta = rtw_get_bcmc_stainfo(padapter);
3701         /*_enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL);*/
3702         if (psta)
3703                 rtw_free_stainfo(padapter, psta);
3704         /*_exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL);*/
3705
3706         rtw_init_bcmc_stainfo(padapter);
3707
3708         if (rtw_mi_get_ap_num(padapter))
3709                 RTW_SET_SCAN_BAND_SKIP(padapter, BAND_5G);
3710
3711 }
3712
3713 void stop_ap_mode(_adapter *padapter)
3714 {
3715         _irqL irqL;
3716         struct sta_info *psta = NULL;
3717         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3718         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3719         struct dvobj_priv *pdvobj = padapter->dvobj;
3720
3721         RTW_INFO("%s -"ADPT_FMT"\n", __func__, ADPT_ARG(padapter));
3722
3723         pmlmepriv->update_bcn = _FALSE;
3724         /*pmlmeext->bstart_bss = _FALSE;*/
3725         padapter->netif_up = _FALSE;
3726         /* _rtw_spinlock_free(&pmlmepriv->bcn_update_lock); */
3727
3728         /* reset and init security priv , this can refine with rtw_reset_securitypriv */
3729         _rtw_memset((unsigned char *)&padapter->securitypriv, 0, sizeof(struct security_priv));
3730         padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeOpen;
3731         padapter->securitypriv.ndisencryptstatus = Ndis802_11WEPDisabled;
3732
3733 #ifdef CONFIG_DFS_MASTER
3734         rtw_dfs_master_status_apply(padapter, MLME_AP_STOPPED);
3735 #endif
3736
3737         /* free scan queue */
3738         rtw_free_network_queue(padapter, _TRUE);
3739
3740 #if CONFIG_RTW_MACADDR_ACL
3741         rtw_macaddr_acl_deinit(padapter);
3742 #endif
3743
3744         rtw_sta_flush(padapter, _TRUE);
3745
3746         /* free_assoc_sta_resources      */
3747         rtw_free_all_stainfo(padapter);
3748
3749         psta = rtw_get_bcmc_stainfo(padapter);
3750         /* _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL);                */
3751         rtw_free_stainfo(padapter, psta);
3752         /*_exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL);*/
3753
3754         rtw_free_mlme_priv_ie_data(pmlmepriv);
3755
3756 #ifdef CONFIG_SWTIMER_BASED_TXBCN
3757         if (pmlmeext->bstart_bss == _TRUE) {
3758                 _enter_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
3759                 pdvobj->nr_ap_if--;
3760                 if (pdvobj->nr_ap_if > 0)
3761                         pdvobj->inter_bcn_space = DEFAULT_BCN_INTERVAL / pdvobj->nr_ap_if;
3762                 else
3763                         pdvobj->inter_bcn_space = DEFAULT_BCN_INTERVAL;
3764
3765                 rtw_list_delete(&padapter->list);
3766                 _exit_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
3767
3768                 rtw_hal_set_hwreg(padapter, HW_VAR_BEACON_INTERVAL, (u8 *)(&pdvobj->inter_bcn_space));
3769
3770                 if (pdvobj->nr_ap_if == 0)
3771                         _cancel_timer_ex(&pdvobj->txbcn_timer);
3772         }
3773 #endif
3774
3775         pmlmeext->bstart_bss = _FALSE;
3776
3777 #ifdef CONFIG_BT_COEXIST
3778         rtw_btcoex_MediaStatusNotify(padapter, 0); /* disconnect */
3779 #endif
3780
3781 }
3782
3783 #endif /* CONFIG_NATIVEAP_MLME */
3784
3785 void rtw_ap_update_bss_chbw(_adapter *adapter, WLAN_BSSID_EX *bss, u8 ch, u8 bw, u8 offset)
3786 {
3787 #define UPDATE_VHT_CAP 1
3788 #define UPDATE_HT_CAP 1
3789
3790 #ifdef CONFIG_80211AC_VHT
3791         {
3792                 struct vht_priv *vhtpriv = &adapter->mlmepriv.vhtpriv;
3793                 u8 *vht_cap_ie, *vht_op_ie;
3794                 int vht_cap_ielen, vht_op_ielen;
3795                 u8      center_freq;
3796
3797                 vht_cap_ie = rtw_get_ie((bss->IEs + sizeof(NDIS_802_11_FIXED_IEs)), EID_VHTCapability, &vht_cap_ielen, (bss->IELength - sizeof(NDIS_802_11_FIXED_IEs)));
3798                 vht_op_ie = rtw_get_ie((bss->IEs + sizeof(NDIS_802_11_FIXED_IEs)), EID_VHTOperation, &vht_op_ielen, (bss->IELength - sizeof(NDIS_802_11_FIXED_IEs)));
3799                 center_freq = rtw_get_center_ch(ch, bw, offset);
3800
3801                 /* update vht cap ie */
3802                 if (vht_cap_ie && vht_cap_ielen) {
3803 #if UPDATE_VHT_CAP
3804                         /* if ((bw == CHANNEL_WIDTH_160 || bw == CHANNEL_WIDTH_80_80) && pvhtpriv->sgi_160m)
3805                                 SET_VHT_CAPABILITY_ELE_SHORT_GI160M(pvht_cap_ie + 2, 1);
3806                         else */
3807                         SET_VHT_CAPABILITY_ELE_SHORT_GI160M(vht_cap_ie + 2, 0);
3808
3809                         if (bw >= CHANNEL_WIDTH_80 && vhtpriv->sgi_80m)
3810                                 SET_VHT_CAPABILITY_ELE_SHORT_GI80M(vht_cap_ie + 2, 1);
3811                         else
3812                                 SET_VHT_CAPABILITY_ELE_SHORT_GI80M(vht_cap_ie + 2, 0);
3813 #endif
3814                 }
3815
3816                 /* update vht op ie */
3817                 if (vht_op_ie && vht_op_ielen) {
3818                         if (bw < CHANNEL_WIDTH_80) {
3819                                 SET_VHT_OPERATION_ELE_CHL_WIDTH(vht_op_ie + 2, 0);
3820                                 SET_VHT_OPERATION_ELE_CHL_CENTER_FREQ1(vht_op_ie + 2, 0);
3821                                 SET_VHT_OPERATION_ELE_CHL_CENTER_FREQ2(vht_op_ie + 2, 0);
3822                         } else if (bw == CHANNEL_WIDTH_80) {
3823                                 SET_VHT_OPERATION_ELE_CHL_WIDTH(vht_op_ie + 2, 1);
3824                                 SET_VHT_OPERATION_ELE_CHL_CENTER_FREQ1(vht_op_ie + 2, center_freq);
3825                                 SET_VHT_OPERATION_ELE_CHL_CENTER_FREQ2(vht_op_ie + 2, 0);
3826                         } else {
3827                                 RTW_ERR(FUNC_ADPT_FMT" unsupported BW:%u\n", FUNC_ADPT_ARG(adapter), bw);
3828                                 rtw_warn_on(1);
3829                         }
3830                 }
3831         }
3832 #endif /* CONFIG_80211AC_VHT */
3833 #ifdef CONFIG_80211N_HT
3834         {
3835                 struct ht_priv  *htpriv = &adapter->mlmepriv.htpriv;
3836                 u8 *ht_cap_ie, *ht_op_ie;
3837                 int ht_cap_ielen, ht_op_ielen;
3838
3839                 ht_cap_ie = rtw_get_ie((bss->IEs + sizeof(NDIS_802_11_FIXED_IEs)), EID_HTCapability, &ht_cap_ielen, (bss->IELength - sizeof(NDIS_802_11_FIXED_IEs)));
3840                 ht_op_ie = rtw_get_ie((bss->IEs + sizeof(NDIS_802_11_FIXED_IEs)), EID_HTInfo, &ht_op_ielen, (bss->IELength - sizeof(NDIS_802_11_FIXED_IEs)));
3841
3842                 /* update ht cap ie */
3843                 if (ht_cap_ie && ht_cap_ielen) {
3844 #if UPDATE_HT_CAP
3845                         if (bw >= CHANNEL_WIDTH_40)
3846                                 SET_HT_CAP_ELE_CHL_WIDTH(ht_cap_ie + 2, 1);
3847                         else
3848                                 SET_HT_CAP_ELE_CHL_WIDTH(ht_cap_ie + 2, 0);
3849
3850                         if (bw >= CHANNEL_WIDTH_40 && htpriv->sgi_40m)
3851                                 SET_HT_CAP_ELE_SHORT_GI40M(ht_cap_ie + 2, 1);
3852                         else
3853                                 SET_HT_CAP_ELE_SHORT_GI40M(ht_cap_ie + 2, 0);
3854
3855                         if (htpriv->sgi_20m)
3856                                 SET_HT_CAP_ELE_SHORT_GI20M(ht_cap_ie + 2, 1);
3857                         else
3858                                 SET_HT_CAP_ELE_SHORT_GI20M(ht_cap_ie + 2, 0);
3859 #endif
3860                 }
3861
3862                 /* update ht op ie */
3863                 if (ht_op_ie && ht_op_ielen) {
3864                         SET_HT_OP_ELE_PRI_CHL(ht_op_ie + 2, ch);
3865                         switch (offset) {
3866                         case HAL_PRIME_CHNL_OFFSET_LOWER:
3867                                 SET_HT_OP_ELE_2ND_CHL_OFFSET(ht_op_ie + 2, SCA);
3868                                 break;
3869                         case HAL_PRIME_CHNL_OFFSET_UPPER:
3870                                 SET_HT_OP_ELE_2ND_CHL_OFFSET(ht_op_ie + 2, SCB);
3871                                 break;
3872                         case HAL_PRIME_CHNL_OFFSET_DONT_CARE:
3873                         default:
3874                                 SET_HT_OP_ELE_2ND_CHL_OFFSET(ht_op_ie + 2, SCN);
3875                                 break;
3876                         }
3877
3878                         if (bw >= CHANNEL_WIDTH_40)
3879                                 SET_HT_OP_ELE_STA_CHL_WIDTH(ht_op_ie + 2, 1);
3880                         else
3881                                 SET_HT_OP_ELE_STA_CHL_WIDTH(ht_op_ie + 2, 0);
3882                 }
3883         }
3884 #endif /* CONFIG_80211N_HT */
3885
3886         {
3887                 u8 *p;
3888                 int ie_len;
3889                 u8 old_ch = bss->Configuration.DSConfig;
3890                 bool change_band = _FALSE;
3891
3892                 if ((ch <= 14 && old_ch >= 36) || (ch >= 36 && old_ch <= 14))
3893                         change_band = _TRUE;
3894
3895                 /* update channel in IE */
3896                 p = rtw_get_ie((bss->IEs + sizeof(NDIS_802_11_FIXED_IEs)), _DSSET_IE_, &ie_len, (bss->IELength - sizeof(NDIS_802_11_FIXED_IEs)));
3897                 if (p && ie_len > 0)
3898                         *(p + 2) = ch;
3899
3900                 bss->Configuration.DSConfig = ch;
3901
3902                 /* band is changed, update ERP, support rate, ext support rate IE */
3903                 if (change_band == _TRUE)
3904                         change_band_update_ie(adapter, bss, ch);
3905         }
3906
3907 }
3908
3909 bool rtw_ap_chbw_decision(_adapter *adapter, s16 req_ch, s8 req_bw, s8 req_offset
3910                           , u8 *ch, u8 *bw, u8 *offset, u8 *chbw_allow)
3911 {
3912         u8 cur_ie_ch, cur_ie_bw, cur_ie_offset;
3913         u8 dec_ch, dec_bw, dec_offset;
3914         u8 u_ch = 0, u_offset, u_bw;
3915         bool changed = _FALSE;
3916         struct mlme_ext_priv *mlmeext = &(adapter->mlmeextpriv);
3917         WLAN_BSSID_EX *network = &(adapter->mlmepriv.cur_network.network);
3918         u8 sta_num;
3919         u8 ld_sta_num;
3920         u8 lg_sta_num;
3921         u8 ap_num;
3922         u8 ld_ap_num;
3923         bool set_u_ch = _FALSE, set_dec_ch = _FALSE;
3924
3925         rtw_ies_get_chbw(network->IEs + sizeof(NDIS_802_11_FIXED_IEs)
3926                          , network->IELength - sizeof(NDIS_802_11_FIXED_IEs)
3927                          , &cur_ie_ch, &cur_ie_bw, &cur_ie_offset);
3928
3929         /* use chbw of cur_ie updated with specifying req as temporary decision */
3930         dec_ch = (req_ch <= 0) ? cur_ie_ch : req_ch;
3931         dec_bw = (req_bw < 0) ? cur_ie_bw : req_bw;
3932         dec_offset = (req_offset < 0) ? cur_ie_offset : req_offset;
3933
3934         rtw_mi_status_no_self(adapter, &sta_num, &ld_sta_num, &lg_sta_num, &ap_num, &ld_ap_num, NULL);
3935         RTW_INFO(FUNC_ADPT_FMT" ld_sta_num:%u, lg_sta_num%u, ap_num:%u\n"
3936                  , FUNC_ADPT_ARG(adapter), ld_sta_num, lg_sta_num, ap_num);
3937
3938         if (ld_sta_num || ap_num) {
3939                 /* has linked STA or AP mode, follow */
3940
3941                 rtw_warn_on(!rtw_mi_get_ch_setting_union_no_self(adapter, &u_ch, &u_bw, &u_offset));
3942
3943                 RTW_INFO(FUNC_ADPT_FMT" union no self: %u,%u,%u\n", FUNC_ADPT_ARG(adapter), u_ch, u_bw, u_offset);
3944                 RTW_INFO(FUNC_ADPT_FMT" req: %d,%d,%d\n", FUNC_ADPT_ARG(adapter), req_ch, req_bw, req_offset);
3945
3946                 rtw_adjust_chbw(adapter, u_ch, &dec_bw, &dec_offset);
3947 #ifdef CONFIG_MCC_MODE
3948                 if (MCC_EN(adapter)) {
3949                         if (!rtw_is_chbw_grouped(u_ch, u_bw, u_offset, dec_ch, dec_bw, dec_offset)) {
3950                                 mlmeext->cur_channel = *ch = dec_ch;
3951                                 mlmeext->cur_bwmode = *bw = dec_bw;
3952                                 mlmeext->cur_ch_offset = *offset = dec_offset;
3953                                 /* channel bw offset can not be allowed, need MCC */
3954                                 *chbw_allow = _FALSE;
3955                                 RTW_INFO(FUNC_ADPT_FMT" enable mcc: %u,%u,%u\n", FUNC_ADPT_ARG(adapter)
3956                                          , *ch, *bw, *offset);
3957                                 goto exit;
3958                         } else
3959                                 /* channel bw offset can be allowed, not need MCC */
3960                                 *chbw_allow = _TRUE;
3961                 }
3962 #endif /* CONFIG_MCC_MODE */
3963                 rtw_sync_chbw(&dec_ch, &dec_bw, &dec_offset
3964                               , &u_ch, &u_bw, &u_offset);
3965
3966                 rtw_ap_update_bss_chbw(adapter, &(adapter->mlmepriv.cur_network.network)
3967                                        , dec_ch, dec_bw, dec_offset);
3968
3969                 set_u_ch = _TRUE;
3970         } else if (lg_sta_num) {
3971                 /* has linking STA */
3972
3973                 rtw_warn_on(!rtw_mi_get_ch_setting_union_no_self(adapter, &u_ch, &u_bw, &u_offset));
3974
3975                 RTW_INFO(FUNC_ADPT_FMT" union no self: %u,%u,%u\n", FUNC_ADPT_ARG(adapter), u_ch, u_bw, u_offset);
3976                 RTW_INFO(FUNC_ADPT_FMT" req: %d,%d,%d\n", FUNC_ADPT_ARG(adapter), req_ch, req_bw, req_offset);
3977
3978                 rtw_adjust_chbw(adapter, dec_ch, &dec_bw, &dec_offset);
3979
3980                 if (rtw_is_chbw_grouped(u_ch, u_bw, u_offset, dec_ch, dec_bw, dec_offset)) {
3981
3982                         rtw_sync_chbw(&dec_ch, &dec_bw, &dec_offset
3983                                       , &u_ch, &u_bw, &u_offset);
3984
3985                         rtw_ap_update_bss_chbw(adapter, &(adapter->mlmepriv.cur_network.network)
3986                                                , dec_ch, dec_bw, dec_offset);
3987
3988                         set_u_ch = _TRUE;
3989
3990                         /* channel bw offset can be allowed, not need MCC */
3991                         *chbw_allow = _TRUE;
3992                 } else {
3993 #ifdef CONFIG_MCC_MODE
3994                         if (MCC_EN(adapter)) {
3995                                 mlmeext->cur_channel = *ch = dec_ch;
3996                                 mlmeext->cur_bwmode = *bw = dec_bw;
3997                                 mlmeext->cur_ch_offset = *offset = dec_offset;
3998
3999                                 /* channel bw offset can not be allowed, need MCC */
4000                                 *chbw_allow = _FALSE;
4001                                 RTW_INFO(FUNC_ADPT_FMT" enable mcc: %u,%u,%u\n", FUNC_ADPT_ARG(adapter)
4002                                          , *ch, *bw, *offset);
4003                                 goto exit;
4004                         }
4005 #endif /* CONFIG_MCC_MODE */
4006                         /* set this for possible ch change when join down*/
4007                         set_fwstate(&adapter->mlmepriv, WIFI_OP_CH_SWITCHING);
4008                 }
4009         } else {
4010                 /* single AP mode */
4011
4012                 RTW_INFO(FUNC_ADPT_FMT" req: %d,%d,%d\n", FUNC_ADPT_ARG(adapter), req_ch, req_bw, req_offset);
4013
4014                 /* check temporary decision first */
4015                 rtw_adjust_chbw(adapter, dec_ch, &dec_bw, &dec_offset);
4016                 if (!rtw_get_offset_by_chbw(dec_ch, dec_bw, &dec_offset)) {
4017 #if defined(CONFIG_DFS_MASTER)
4018                         if (req_ch == -1 || req_bw == -1)
4019                                 goto choose_chbw;
4020 #endif
4021                         RTW_WARN(FUNC_ADPT_FMT" req: %u,%u has no valid offset\n", FUNC_ADPT_ARG(adapter), dec_ch, dec_bw);
4022                         *chbw_allow = _FALSE;
4023                         goto exit;
4024                 }
4025
4026                 if (!rtw_chset_is_chbw_valid(mlmeext->channel_set, dec_ch, dec_bw, dec_offset)) {
4027 #if defined(CONFIG_DFS_MASTER)
4028                         if (req_ch == -1 || req_bw == -1)
4029                                 goto choose_chbw;
4030 #endif
4031                         RTW_WARN(FUNC_ADPT_FMT" req: %u,%u,%u doesn't fit in chplan\n", FUNC_ADPT_ARG(adapter), dec_ch, dec_bw, dec_offset);
4032                         *chbw_allow = _FALSE;
4033                         goto exit;
4034                 }
4035
4036                 if (rtw_chset_is_ch_non_ocp(mlmeext->channel_set, dec_ch, dec_bw, dec_offset) == _FALSE)
4037                         goto update_bss_chbw;
4038
4039 choose_chbw:
4040                 if (req_bw < 0)
4041                         req_bw = cur_ie_bw;
4042
4043 #if defined(CONFIG_DFS_MASTER)
4044                 /* choose 5G DFS channel for debug */
4045                 if (adapter_to_rfctl(adapter)->dbg_dfs_master_choose_dfs_ch_first
4046                     && rtw_choose_shortest_waiting_ch(adapter, req_bw, &dec_ch, &dec_bw, &dec_offset, RTW_CHF_2G | RTW_CHF_NON_DFS) == _TRUE)
4047                         RTW_INFO(FUNC_ADPT_FMT" choose 5G DFS channel for debug\n", FUNC_ADPT_ARG(adapter));
4048                 else if (adapter_to_rfctl(adapter)->dfs_ch_sel_d_flags
4049                         && rtw_choose_shortest_waiting_ch(adapter, req_bw, &dec_ch, &dec_bw, &dec_offset, adapter_to_rfctl(adapter)->dfs_ch_sel_d_flags) == _TRUE)
4050                         RTW_INFO(FUNC_ADPT_FMT" choose with dfs_ch_sel_d_flags:0x%02x for debug\n", FUNC_ADPT_ARG(adapter), adapter_to_rfctl(adapter)->dfs_ch_sel_d_flags);
4051                 else if (rtw_choose_shortest_waiting_ch(adapter, req_bw, &dec_ch, &dec_bw, &dec_offset, 0) == _FALSE) {
4052                         RTW_WARN(FUNC_ADPT_FMT" no available channel\n", FUNC_ADPT_ARG(adapter));
4053                         *chbw_allow = _FALSE;
4054                         goto exit;
4055                 }
4056 #endif /* defined(CONFIG_DFS_MASTER) */
4057
4058 update_bss_chbw:
4059                 rtw_ap_update_bss_chbw(adapter, &(adapter->mlmepriv.cur_network.network)
4060                                        , dec_ch, dec_bw, dec_offset);
4061
4062                 /* channel bw offset can be allowed for single AP, not need MCC */
4063                 *chbw_allow = _TRUE;
4064                 set_dec_ch = _TRUE;
4065         }
4066
4067         if (rtw_mi_check_fwstate(adapter, _FW_UNDER_SURVEY)) {
4068                 /* scanning, leave ch setting to scan state machine */
4069                 set_u_ch = set_dec_ch = _FALSE;
4070         }
4071
4072         if (mlmeext->cur_channel != dec_ch
4073             || mlmeext->cur_bwmode != dec_bw
4074             || mlmeext->cur_ch_offset != dec_offset)
4075                 changed = _TRUE;
4076
4077         if (changed == _TRUE && rtw_linked_check(adapter) == _TRUE) {
4078 #ifdef CONFIG_SPCT_CH_SWITCH
4079                 if (1)
4080                         rtw_ap_inform_ch_switch(adapter, dec_ch, dec_offset);
4081                 else
4082 #endif
4083                         rtw_sta_flush(adapter, _FALSE);
4084         }
4085
4086         mlmeext->cur_channel = dec_ch;
4087         mlmeext->cur_bwmode = dec_bw;
4088         mlmeext->cur_ch_offset = dec_offset;
4089
4090         if (u_ch != 0)
4091                 RTW_INFO(FUNC_ADPT_FMT" union: %u,%u,%u\n", FUNC_ADPT_ARG(adapter), u_ch, u_bw, u_offset);
4092
4093         RTW_INFO(FUNC_ADPT_FMT" dec: %u,%u,%u\n", FUNC_ADPT_ARG(adapter), dec_ch, dec_bw, dec_offset);
4094
4095         if (set_u_ch == _TRUE) {
4096                 *ch = u_ch;
4097                 *bw = u_bw;
4098                 *offset = u_offset;
4099         } else if (set_dec_ch == _TRUE) {
4100                 *ch = dec_ch;
4101                 *bw = dec_bw;
4102                 *offset = dec_offset;
4103         }
4104 exit:
4105         return changed;
4106 }
4107
4108 /*#define DBG_SWTIMER_BASED_TXBCN*/
4109
4110 #ifdef CONFIG_SWTIMER_BASED_TXBCN
4111 void tx_beacon_handlder(struct dvobj_priv *pdvobj)
4112 {
4113 #define BEACON_EARLY_TIME               20      /* unit:TU*/
4114         _irqL irqL;
4115         _list   *plist, *phead;
4116         u32 timestamp[2];
4117         u32 bcn_interval_us; /* unit : usec */
4118         u64 time;
4119         u32 cur_tick, time_offset; /* unit : usec */
4120         u32 inter_bcn_space_us; /* unit : usec */
4121         int nr_vap, idx, bcn_idx;
4122         int i;
4123         u8 val8, late = 0;
4124         _adapter *padapter = NULL;
4125
4126         i = 0;
4127
4128         /* get first ap mode interface */
4129         _enter_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
4130         if (rtw_is_list_empty(&pdvobj->ap_if_q.queue) || (pdvobj->nr_ap_if == 0)) {
4131                 RTW_INFO("[%s] ERROR: ap_if_q is empty!or nr_ap = %d\n", __func__, pdvobj->nr_ap_if);
4132                 _exit_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
4133                 return;
4134         } else
4135                 padapter = LIST_CONTAINOR(get_next(&(pdvobj->ap_if_q.queue)), struct _ADAPTER, list);
4136         _exit_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
4137
4138         if (NULL == padapter) {
4139                 RTW_INFO("[%s] ERROR: no any ap interface!\n", __func__);
4140                 return;
4141         }
4142
4143
4144         bcn_interval_us = DEFAULT_BCN_INTERVAL * NET80211_TU_TO_US;
4145         if (0 == bcn_interval_us) {
4146                 RTW_INFO("[%s] ERROR: beacon interval = 0\n", __func__);
4147                 return;
4148         }
4149
4150         /* read TSF */
4151         timestamp[1] = rtw_read32(padapter, 0x560 + 4);
4152         timestamp[0] = rtw_read32(padapter, 0x560);
4153         while (timestamp[1]) {
4154                 time = (0xFFFFFFFF % bcn_interval_us + 1) * timestamp[1] + timestamp[0];
4155                 timestamp[0] = (u32)time;
4156                 timestamp[1] = (u32)(time >> 32);
4157         }
4158         cur_tick = timestamp[0] % bcn_interval_us;
4159
4160
4161         _enter_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
4162
4163         nr_vap = (pdvobj->nr_ap_if - 1);
4164         if (nr_vap > 0) {
4165                 inter_bcn_space_us = pdvobj->inter_bcn_space * NET80211_TU_TO_US; /* beacon_interval / (nr_vap+1); */
4166                 idx = cur_tick / inter_bcn_space_us;
4167                 if (idx < nr_vap)       /* if (idx < (nr_vap+1))*/
4168                         bcn_idx = idx + 1;      /* bcn_idx = (idx + 1) % (nr_vap+1);*/
4169                 else
4170                         bcn_idx = 0;
4171
4172                 /* to get padapter based on bcn_idx */
4173                 padapter = NULL;
4174                 phead = get_list_head(&pdvobj->ap_if_q);
4175                 plist = get_next(phead);
4176                 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
4177                         padapter = LIST_CONTAINOR(plist, struct _ADAPTER, list);
4178
4179                         plist = get_next(plist);
4180
4181                         if (i == bcn_idx)
4182                                 break;
4183
4184                         i++;
4185                 }
4186                 if ((NULL == padapter) || (i > pdvobj->nr_ap_if)) {
4187                         RTW_INFO("[%s] ERROR: nr_ap_if = %d, padapter=%p, bcn_idx=%d, index=%d\n",
4188                                 __func__, pdvobj->nr_ap_if, padapter, bcn_idx, i);
4189                         _exit_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
4190                         return;
4191                 }
4192 #ifdef DBG_SWTIMER_BASED_TXBCN
4193                 RTW_INFO("BCN_IDX=%d, cur_tick=%d, padapter=%p\n", bcn_idx, cur_tick, padapter);
4194 #endif
4195                 if (((idx + 2 == nr_vap + 1) && (idx < nr_vap + 1)) || (0 == bcn_idx)) {
4196                         time_offset = bcn_interval_us - cur_tick - BEACON_EARLY_TIME * NET80211_TU_TO_US;
4197                         if ((s32)time_offset < 0)
4198                                 time_offset += inter_bcn_space_us;
4199
4200                 } else {
4201                         time_offset = (idx + 2) * inter_bcn_space_us - cur_tick - BEACON_EARLY_TIME * NET80211_TU_TO_US;
4202                         if (time_offset > (inter_bcn_space_us + (inter_bcn_space_us >> 1))) {
4203                                 time_offset -= inter_bcn_space_us;
4204                                 late = 1;
4205                         }
4206                 }
4207         } else
4208                 /*#endif*/ { /* MBSSID */
4209                 time_offset = 2 * bcn_interval_us - cur_tick - BEACON_EARLY_TIME * NET80211_TU_TO_US;
4210                 if (time_offset > (bcn_interval_us + (bcn_interval_us >> 1))) {
4211                         time_offset -= bcn_interval_us;
4212                         late = 1;
4213                 }
4214         }
4215         _exit_critical_bh(&pdvobj->ap_if_q.lock, &irqL);
4216
4217 #ifdef DBG_SWTIMER_BASED_TXBCN
4218         RTW_INFO("set sw bcn timer %d us\n", time_offset);
4219 #endif
4220         _set_timer(&pdvobj->txbcn_timer, time_offset / NET80211_TU_TO_US);
4221
4222         if (padapter) {
4223 #ifdef DBG_SWTIMER_BASED_TXBCN
4224                 RTW_INFO("padapter=%p, PORT=%d\n", padapter, padapter->hw_port);
4225 #endif
4226                 /*update_beacon(padapter, _TIM_IE_, NULL, _FALSE);*/
4227                 issue_beacon(padapter, 0);
4228         }
4229
4230 #if 0
4231         /* handle any buffered BC/MC frames*/
4232         /* Don't dynamically change DIS_ATIM due to HW will auto send ACQ after HIQ empty.*/
4233         val8 = *((unsigned char *)priv->beaconbuf + priv->timoffset + 4);
4234         if (val8 & 0x01) {
4235                 process_mcast_dzqueue(priv);
4236                 priv->pkt_in_dtimQ = 0;
4237         }
4238 #endif
4239
4240 }
4241
4242 void tx_beacon_timer_handlder(struct dvobj_priv *pdvobj)
4243 {
4244         _adapter *padapter = pdvobj->padapters[0];
4245
4246         if (padapter)
4247                 set_tx_beacon_cmd(padapter);
4248 }
4249 #endif
4250
4251 #endif /* CONFIG_AP_MODE */