root/drivers/staging/rtl8188eu/core/rtw_mlme.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rtw_init_mlme_priv
  2. rtw_free_mlme_ie_data
  3. rtw_free_mlme_priv_ie_data
  4. rtw_free_mlme_priv_ie_data
  5. rtw_free_mlme_priv
  6. rtw_alloc_network
  7. _rtw_free_network
  8. rtw_free_network_nolock
  9. rtw_find_network
  10. rtw_free_network_queue
  11. rtw_if_up
  12. rtw_generate_random_ibss
  13. rtw_get_capability_from_ie
  14. rtw_get_capability
  15. rtw_get_beacon_interval_from_ie
  16. rtw_is_same_ibss
  17. is_same_ess
  18. is_same_network
  19. rtw_get_oldest_wlan_network
  20. update_network
  21. update_current_network
  22. rtw_update_scanned_network
  23. rtw_add_network
  24. rtw_is_desired_network
  25. rtw_atimdone_event_callback
  26. rtw_survey_event_callback
  27. rtw_surveydone_event_callback
  28. rtw_dummy_event_callback
  29. rtw_fwdbg_event_callback
  30. free_scanqueue
  31. rtw_free_assoc_resources
  32. rtw_free_assoc_resources_locked
  33. rtw_indicate_connect
  34. rtw_indicate_disconnect
  35. rtw_indicate_scan_done
  36. rtw_joinbss_update_stainfo
  37. rtw_joinbss_update_network
  38. rtw_joinbss_event_prehandle
  39. rtw_joinbss_event_callback
  40. search_max_mac_id
  41. rtw_stassoc_hw_rpt
  42. rtw_stassoc_event_callback
  43. rtw_stadel_event_callback
  44. rtw_cpwm_event_callback
  45. _rtw_join_timeout_handler
  46. rtw_scan_timeout_handler
  47. rtw_auto_scan_handler
  48. rtw_dynamic_check_timer_handlder
  49. rtw_check_join_candidate
  50. rtw_select_and_join_from_scanned_queue
  51. rtw_set_auth
  52. rtw_set_key
  53. rtw_restruct_wmm_ie
  54. SecIsInPMKIDList
  55. rtw_append_pmkid
  56. rtw_restruct_sec_ie
  57. rtw_init_registrypriv_dev_network
  58. rtw_update_registrypriv_dev_network
  59. rtw_get_encrypt_decrypt_from_registrypriv
  60. rtw_joinbss_reset
  61. rtw_restructure_ht_ie
  62. rtw_update_ht_cap
  63. rtw_issue_addbareq_cmd
  64. rtw_roaming
  65. _rtw_roaming

   1 // SPDX-License-Identifier: GPL-2.0
   2 /******************************************************************************
   3  *
   4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
   5  *
   6  ******************************************************************************/
   7 #define _RTW_MLME_C_
   8 
   9 #include <linux/ieee80211.h>
  10 
  11 #include <osdep_service.h>
  12 #include <drv_types.h>
  13 #include <recv_osdep.h>
  14 #include <xmit_osdep.h>
  15 #include <hal_intf.h>
  16 #include <mlme_osdep.h>
  17 #include <sta_info.h>
  18 #include <wifi.h>
  19 #include <wlan_bssdef.h>
  20 #include <rtw_ioctl_set.h>
  21 #include <linux/vmalloc.h>
  22 
  23 extern const u8 MCS_rate_1R[16];
  24 
  25 int rtw_init_mlme_priv(struct adapter *padapter)
  26 {
  27         int i;
  28         u8 *pbuf;
  29         struct wlan_network *pnetwork;
  30         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
  31         int res = _SUCCESS;
  32 
  33         /*  We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
  34 
  35         pmlmepriv->nic_hdl = (u8 *)padapter;
  36 
  37         pmlmepriv->pscanned = NULL;
  38         pmlmepriv->fw_state = 0;
  39         pmlmepriv->cur_network.network.InfrastructureMode = Ndis802_11AutoUnknown;
  40         pmlmepriv->scan_mode = SCAN_ACTIVE;/*  1: active, 0: pasive. Maybe someday we should rename this varable to "active_mode" (Jeff) */
  41 
  42         spin_lock_init(&pmlmepriv->lock);
  43         _rtw_init_queue(&pmlmepriv->free_bss_pool);
  44         _rtw_init_queue(&pmlmepriv->scanned_queue);
  45 
  46         memset(&pmlmepriv->assoc_ssid, 0, sizeof(struct ndis_802_11_ssid));
  47 
  48         pbuf = vzalloc(array_size(MAX_BSS_CNT, sizeof(struct wlan_network)));
  49 
  50         if (!pbuf) {
  51                 res = _FAIL;
  52                 goto exit;
  53         }
  54         pmlmepriv->free_bss_buf = pbuf;
  55 
  56         pnetwork = (struct wlan_network *)pbuf;
  57 
  58         for (i = 0; i < MAX_BSS_CNT; i++) {
  59                 INIT_LIST_HEAD(&pnetwork->list);
  60 
  61                 list_add_tail(&pnetwork->list, &pmlmepriv->free_bss_pool.queue);
  62 
  63                 pnetwork++;
  64         }
  65 
  66         /* allocate DMA-able/Non-Page memory for cmd_buf and rsp_buf */
  67 
  68         rtw_clear_scan_deny(padapter);
  69 
  70         rtw_init_mlme_timer(padapter);
  71 
  72 exit:
  73         return res;
  74 }
  75 
  76 #if defined(CONFIG_88EU_AP_MODE)
  77 static void rtw_free_mlme_ie_data(u8 **ppie, u32 *plen)
  78 {
  79         kfree(*ppie);
  80         *plen = 0;
  81         *ppie = NULL;
  82 }
  83 
  84 void rtw_free_mlme_priv_ie_data(struct mlme_priv *pmlmepriv)
  85 {
  86         rtw_buf_free(&pmlmepriv->assoc_req, &pmlmepriv->assoc_req_len);
  87         rtw_buf_free(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len);
  88         rtw_free_mlme_ie_data(&pmlmepriv->wps_beacon_ie, &pmlmepriv->wps_beacon_ie_len);
  89         rtw_free_mlme_ie_data(&pmlmepriv->wps_probe_req_ie, &pmlmepriv->wps_probe_req_ie_len);
  90         rtw_free_mlme_ie_data(&pmlmepriv->wps_probe_resp_ie, &pmlmepriv->wps_probe_resp_ie_len);
  91         rtw_free_mlme_ie_data(&pmlmepriv->wps_assoc_resp_ie, &pmlmepriv->wps_assoc_resp_ie_len);
  92 }
  93 #else
  94 void rtw_free_mlme_priv_ie_data(struct mlme_priv *pmlmepriv)
  95 {
  96 }
  97 #endif
  98 
  99 void rtw_free_mlme_priv(struct mlme_priv *pmlmepriv)
 100 {
 101         if (pmlmepriv) {
 102                 rtw_free_mlme_priv_ie_data(pmlmepriv);
 103                 vfree(pmlmepriv->free_bss_buf);
 104         }
 105 }
 106 
 107 struct wlan_network *rtw_alloc_network(struct mlme_priv *pmlmepriv)
 108                                         /* _queue *free_queue) */
 109 {
 110         struct wlan_network *pnetwork;
 111         struct __queue *free_queue = &pmlmepriv->free_bss_pool;
 112 
 113         spin_lock_bh(&free_queue->lock);
 114         pnetwork = list_first_entry_or_null(&free_queue->queue,
 115                                             struct wlan_network, list);
 116         if (!pnetwork)
 117                 goto exit;
 118 
 119         list_del_init(&pnetwork->list);
 120 
 121         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
 122                  ("rtw_alloc_network: ptr=%p\n", &pnetwork->list));
 123         pnetwork->network_type = 0;
 124         pnetwork->fixed = false;
 125         pnetwork->last_scanned = jiffies;
 126         pnetwork->aid = 0;
 127         pnetwork->join_res = 0;
 128 
 129 exit:
 130         spin_unlock_bh(&free_queue->lock);
 131 
 132         return pnetwork;
 133 }
 134 
 135 static void _rtw_free_network(struct mlme_priv *pmlmepriv, struct wlan_network *pnetwork, u8 isfreeall)
 136 {
 137         unsigned long curr_time;
 138         u32 delta_time;
 139         u32 lifetime = SCANQUEUE_LIFETIME;
 140         struct __queue *free_queue = &pmlmepriv->free_bss_pool;
 141 
 142         if (!pnetwork)
 143                 return;
 144 
 145         if (pnetwork->fixed)
 146                 return;
 147         curr_time = jiffies;
 148         if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) ||
 149             (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)))
 150                 lifetime = 1;
 151         if (!isfreeall) {
 152                 delta_time = (curr_time - pnetwork->last_scanned)/HZ;
 153                 if (delta_time < lifetime)/*  unit:sec */
 154                         return;
 155         }
 156         spin_lock_bh(&free_queue->lock);
 157         list_del_init(&pnetwork->list);
 158         list_add_tail(&pnetwork->list, &free_queue->queue);
 159         spin_unlock_bh(&free_queue->lock);
 160 }
 161 
 162 static void rtw_free_network_nolock(struct mlme_priv *pmlmepriv,
 163                                     struct wlan_network *pnetwork)
 164 {
 165         struct __queue *free_queue = &pmlmepriv->free_bss_pool;
 166 
 167         if (!pnetwork)
 168                 return;
 169         if (pnetwork->fixed)
 170                 return;
 171         list_del_init(&pnetwork->list);
 172         list_add_tail(&pnetwork->list, get_list_head(free_queue));
 173 }
 174 
 175 /*
 176  * return the wlan_network with the matching addr
 177  *
 178  * Shall be called under atomic context... to avoid possible racing condition...
 179  */
 180 struct wlan_network *rtw_find_network(struct __queue *scanned_queue, u8 *addr)
 181 {
 182         struct list_head *phead, *plist;
 183         struct wlan_network *pnetwork = NULL;
 184         u8 zero_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
 185 
 186         if (!memcmp(zero_addr, addr, ETH_ALEN)) {
 187                 pnetwork = NULL;
 188                 goto exit;
 189         }
 190         phead = get_list_head(scanned_queue);
 191         plist = phead->next;
 192 
 193         while (plist != phead) {
 194                 pnetwork = container_of(plist, struct wlan_network, list);
 195                 if (!memcmp(addr, pnetwork->network.MacAddress, ETH_ALEN))
 196                         break;
 197                 plist = plist->next;
 198         }
 199         if (plist == phead)
 200                 pnetwork = NULL;
 201 exit:
 202         return pnetwork;
 203 }
 204 
 205 void rtw_free_network_queue(struct adapter *padapter, u8 isfreeall)
 206 {
 207         struct list_head *phead, *plist;
 208         struct wlan_network *pnetwork;
 209         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 210         struct __queue *scanned_queue = &pmlmepriv->scanned_queue;
 211 
 212         spin_lock_bh(&scanned_queue->lock);
 213 
 214         phead = get_list_head(scanned_queue);
 215         plist = phead->next;
 216 
 217         while (phead != plist) {
 218                 pnetwork = container_of(plist, struct wlan_network, list);
 219 
 220                 plist = plist->next;
 221 
 222                 _rtw_free_network(pmlmepriv, pnetwork, isfreeall);
 223         }
 224         spin_unlock_bh(&scanned_queue->lock);
 225 }
 226 
 227 int rtw_if_up(struct adapter *padapter)
 228 {
 229         int res;
 230 
 231         if (padapter->bDriverStopped || padapter->bSurpriseRemoved ||
 232             !check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
 233                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
 234                          ("%s:bDriverStopped(%d) OR bSurpriseRemoved(%d)",
 235                           __func__, padapter->bDriverStopped,
 236                           padapter->bSurpriseRemoved));
 237                 res = false;
 238         } else {
 239                 res =  true;
 240         }
 241         return res;
 242 }
 243 
 244 void rtw_generate_random_ibss(u8 *pibss)
 245 {
 246         unsigned long curtime = jiffies;
 247 
 248         pibss[0] = 0x02;  /* in ad-hoc mode bit1 must set to 1 */
 249         pibss[1] = 0x11;
 250         pibss[2] = 0x87;
 251         pibss[3] = (u8)(curtime & 0xff);/* p[0]; */
 252         pibss[4] = (u8)((curtime>>8) & 0xff);/* p[1]; */
 253         pibss[5] = (u8)((curtime>>16) & 0xff);/* p[2]; */
 254 }
 255 
 256 u8 *rtw_get_capability_from_ie(u8 *ie)
 257 {
 258         return ie + 8 + 2;
 259 }
 260 
 261 u16 rtw_get_capability(struct wlan_bssid_ex *bss)
 262 {
 263         __le16 val;
 264 
 265         memcpy((u8 *)&val, rtw_get_capability_from_ie(bss->ies), 2);
 266 
 267         return le16_to_cpu(val);
 268 }
 269 
 270 u8 *rtw_get_beacon_interval_from_ie(u8 *ie)
 271 {
 272         return ie + 8;
 273 }
 274 
 275 int rtw_is_same_ibss(struct adapter *adapter, struct wlan_network *pnetwork)
 276 {
 277         int ret = true;
 278         struct security_priv *psecuritypriv = &adapter->securitypriv;
 279 
 280         if ((psecuritypriv->dot11PrivacyAlgrthm != _NO_PRIVACY_) &&
 281             (pnetwork->network.Privacy == 0))
 282                 ret = false;
 283         else if ((psecuritypriv->dot11PrivacyAlgrthm == _NO_PRIVACY_) &&
 284                  (pnetwork->network.Privacy == 1))
 285                 ret = false;
 286         else
 287                 ret = true;
 288         return ret;
 289 }
 290 
 291 static int is_same_ess(struct wlan_bssid_ex *a, struct wlan_bssid_ex *b)
 292 {
 293         return (a->ssid.ssid_length == b->ssid.ssid_length) &&
 294                !memcmp(a->ssid.ssid, b->ssid.ssid, a->ssid.ssid_length);
 295 }
 296 
 297 int is_same_network(struct wlan_bssid_ex *src, struct wlan_bssid_ex *dst)
 298 {
 299         u16 s_cap, d_cap;
 300         __le16 le_scap, le_dcap;
 301 
 302         memcpy((u8 *)&le_scap, rtw_get_capability_from_ie(src->ies), 2);
 303         memcpy((u8 *)&le_dcap, rtw_get_capability_from_ie(dst->ies), 2);
 304 
 305         s_cap = le16_to_cpu(le_scap);
 306         d_cap = le16_to_cpu(le_dcap);
 307 
 308         return ((src->ssid.ssid_length == dst->ssid.ssid_length) &&
 309                 (!memcmp(src->MacAddress, dst->MacAddress, ETH_ALEN)) &&
 310                 (!memcmp(src->ssid.ssid, dst->ssid.ssid, src->ssid.ssid_length)) &&
 311                 ((s_cap & WLAN_CAPABILITY_IBSS) ==
 312                 (d_cap & WLAN_CAPABILITY_IBSS)) &&
 313                 ((s_cap & WLAN_CAPABILITY_ESS) ==
 314                 (d_cap & WLAN_CAPABILITY_ESS)));
 315 }
 316 
 317 struct wlan_network *rtw_get_oldest_wlan_network(struct __queue *scanned_queue)
 318 {
 319         struct list_head *plist, *phead;
 320         struct wlan_network *pwlan = NULL;
 321         struct wlan_network *oldest = NULL;
 322 
 323         phead = get_list_head(scanned_queue);
 324 
 325         for (plist = phead->next; plist != phead; plist = plist->next) {
 326                 pwlan = container_of(plist, struct wlan_network, list);
 327 
 328                 if (!pwlan->fixed) {
 329                         if (!oldest || time_after(oldest->last_scanned, pwlan->last_scanned))
 330                                 oldest = pwlan;
 331                 }
 332         }
 333         return oldest;
 334 }
 335 
 336 void update_network(struct wlan_bssid_ex *dst, struct wlan_bssid_ex *src,
 337         struct adapter *padapter, bool update_ie)
 338 {
 339         long rssi_ori = dst->Rssi;
 340         u8 sq_smp = src->PhyInfo.SignalQuality;
 341         u8 ss_final;
 342         u8 sq_final;
 343         long rssi_final;
 344 
 345         rtw_hal_antdiv_rssi_compared(padapter, dst, src); /* this will update src.Rssi, need consider again */
 346 
 347         /* The rule below is 1/5 for sample value, 4/5 for history value */
 348         if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) &&
 349             is_same_network(&padapter->mlmepriv.cur_network.network, src)) {
 350                 /* Take the recvpriv's value for the connected AP*/
 351                 ss_final = padapter->recvpriv.signal_strength;
 352                 sq_final = padapter->recvpriv.signal_qual;
 353                 /* the rssi value here is undecorated, and will be used for antenna diversity */
 354                 if (sq_smp != 101) /* from the right channel */
 355                         rssi_final = (src->Rssi + dst->Rssi * 4) / 5;
 356                 else
 357                         rssi_final = rssi_ori;
 358         } else {
 359                 if (sq_smp != 101) { /* from the right channel */
 360                         ss_final = ((u32)(src->PhyInfo.SignalStrength)+(u32)(dst->PhyInfo.SignalStrength)*4)/5;
 361                         sq_final = ((u32)(src->PhyInfo.SignalQuality)+(u32)(dst->PhyInfo.SignalQuality)*4)/5;
 362                         rssi_final = (src->Rssi+dst->Rssi*4)/5;
 363                 } else {
 364                         /* bss info not receiving from the right channel, use the original RX signal infos */
 365                         ss_final = dst->PhyInfo.SignalStrength;
 366                         sq_final = dst->PhyInfo.SignalQuality;
 367                         rssi_final = dst->Rssi;
 368                 }
 369         }
 370         if (update_ie)
 371                 memcpy((u8 *)dst, (u8 *)src, get_wlan_bssid_ex_sz(src));
 372         dst->PhyInfo.SignalStrength = ss_final;
 373         dst->PhyInfo.SignalQuality = sq_final;
 374         dst->Rssi = rssi_final;
 375 }
 376 
 377 static void update_current_network(struct adapter *adapter, struct wlan_bssid_ex *pnetwork)
 378 {
 379         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
 380 
 381         if (check_fwstate(pmlmepriv, _FW_LINKED) &&
 382             is_same_network(&pmlmepriv->cur_network.network, pnetwork)) {
 383                 update_network(&pmlmepriv->cur_network.network, pnetwork, adapter, true);
 384                 rtw_update_protection(adapter, (pmlmepriv->cur_network.network.ies) + sizeof(struct ndis_802_11_fixed_ie),
 385                                       pmlmepriv->cur_network.network.ie_length);
 386         }
 387 }
 388 
 389 /*
 390  * Caller must hold pmlmepriv->lock first.
 391  */
 392 void rtw_update_scanned_network(struct adapter *adapter, struct wlan_bssid_ex *target)
 393 {
 394         struct list_head *plist, *phead;
 395         u32 bssid_ex_sz;
 396         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
 397         struct __queue *queue = &pmlmepriv->scanned_queue;
 398         struct wlan_network *pnetwork = NULL;
 399         struct wlan_network *oldest = NULL;
 400 
 401         spin_lock_bh(&queue->lock);
 402         phead = get_list_head(queue);
 403         plist = phead->next;
 404 
 405         while (phead != plist) {
 406                 pnetwork = container_of(plist, struct wlan_network, list);
 407 
 408                 if (is_same_network(&pnetwork->network, target))
 409                         break;
 410                 if ((oldest == ((struct wlan_network *)0)) ||
 411                     time_after(oldest->last_scanned, pnetwork->last_scanned))
 412                         oldest = pnetwork;
 413                 plist = plist->next;
 414         }
 415         /* If we didn't find a match, then get a new network slot to initialize
 416          * with this beacon's information
 417          */
 418         if (phead == plist) {
 419                 if (list_empty(&pmlmepriv->free_bss_pool.queue)) {
 420                         /* If there are no more slots, expire the oldest */
 421                         pnetwork = oldest;
 422 
 423                         rtw_hal_get_def_var(adapter, HAL_DEF_CURRENT_ANTENNA,
 424                                             &target->PhyInfo.Optimum_antenna);
 425                         memcpy(&pnetwork->network, target,
 426                                get_wlan_bssid_ex_sz(target));
 427                         /*  variable initialize */
 428                         pnetwork->fixed = false;
 429                         pnetwork->last_scanned = jiffies;
 430 
 431                         pnetwork->network_type = 0;
 432                         pnetwork->aid = 0;
 433                         pnetwork->join_res = 0;
 434 
 435                         /* bss info not receiving from the right channel */
 436                         if (pnetwork->network.PhyInfo.SignalQuality == 101)
 437                                 pnetwork->network.PhyInfo.SignalQuality = 0;
 438                 } else {
 439                         /* Otherwise just pull from the free list */
 440 
 441                         pnetwork = rtw_alloc_network(pmlmepriv); /*  will update scan_time */
 442 
 443                         if (!pnetwork) {
 444                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
 445                                          ("\n\n\nsomething wrong here\n\n\n"));
 446                                 goto exit;
 447                         }
 448 
 449                         bssid_ex_sz = get_wlan_bssid_ex_sz(target);
 450                         target->Length = bssid_ex_sz;
 451                         rtw_hal_get_def_var(adapter, HAL_DEF_CURRENT_ANTENNA,
 452                                             &target->PhyInfo.Optimum_antenna);
 453                         memcpy(&pnetwork->network, target, bssid_ex_sz);
 454 
 455                         pnetwork->last_scanned = jiffies;
 456 
 457                         /* bss info not receiving from the right channel */
 458                         if (pnetwork->network.PhyInfo.SignalQuality == 101)
 459                                 pnetwork->network.PhyInfo.SignalQuality = 0;
 460                         list_add_tail(&pnetwork->list, &queue->queue);
 461                 }
 462         } else {
 463                 /* we have an entry and we are going to update it. But this
 464                  * entry may be already expired. In this case we do the same
 465                  * as we found a new net and call the new_net handler
 466                  */
 467                 bool update_ie = true;
 468 
 469                 pnetwork->last_scanned = jiffies;
 470 
 471                 /* target.Reserved[0]== 1, means that scanned network is a bcn frame. */
 472                 if ((pnetwork->network.ie_length > target->ie_length) && (target->Reserved[0] == 1))
 473                         update_ie = false;
 474 
 475                 update_network(&pnetwork->network, target, adapter, update_ie);
 476         }
 477 
 478 exit:
 479         spin_unlock_bh(&queue->lock);
 480 }
 481 
 482 static void rtw_add_network(struct adapter *adapter,
 483                             struct wlan_bssid_ex *pnetwork)
 484 {
 485         update_current_network(adapter, pnetwork);
 486         rtw_update_scanned_network(adapter, pnetwork);
 487 }
 488 
 489 /*
 490  * select the desired network based on the capability of the (i)bss.
 491  * check items: (1) security
 492  *                      (2) network_type
 493  *                      (3) WMM
 494  *                      (4) HT
 495  *                      (5) others
 496  */
 497 static int rtw_is_desired_network(struct adapter *adapter, struct wlan_network *pnetwork)
 498 {
 499         struct security_priv *psecuritypriv = &adapter->securitypriv;
 500         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
 501         u32 desired_encmode;
 502         u32 privacy;
 503 
 504         /* u8 wps_ie[512]; */
 505         uint wps_ielen;
 506 
 507         int bselected = true;
 508 
 509         desired_encmode = psecuritypriv->ndisencryptstatus;
 510         privacy = pnetwork->network.Privacy;
 511 
 512         if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) {
 513                 if (rtw_get_wps_ie(pnetwork->network.ies+_FIXED_IE_LENGTH_, pnetwork->network.ie_length-_FIXED_IE_LENGTH_, NULL, &wps_ielen))
 514                         return true;
 515                 else
 516                         return false;
 517         }
 518         if (adapter->registrypriv.wifi_spec == 1) { /* for  correct flow of 8021X  to do.... */
 519                 if ((desired_encmode == Ndis802_11EncryptionDisabled) && (privacy != 0))
 520                         bselected = false;
 521         }
 522 
 523         if ((desired_encmode != Ndis802_11EncryptionDisabled) && (privacy == 0)) {
 524                 DBG_88E("desired_encmode: %d, privacy: %d\n", desired_encmode, privacy);
 525                 bselected = false;
 526         }
 527 
 528         if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
 529                 if (pnetwork->network.InfrastructureMode != pmlmepriv->cur_network.network.InfrastructureMode)
 530                         bselected = false;
 531         }
 532 
 533         return bselected;
 534 }
 535 
 536 /* TODO: Perry: For Power Management */
 537 void rtw_atimdone_event_callback(struct adapter *adapter, u8 *pbuf)
 538 {
 539         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("receive atimdone_event\n"));
 540 }
 541 
 542 void rtw_survey_event_callback(struct adapter   *adapter, u8 *pbuf)
 543 {
 544         u32 len;
 545         struct wlan_bssid_ex *pnetwork;
 546         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
 547 
 548         pnetwork = (struct wlan_bssid_ex *)pbuf;
 549 
 550         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
 551                  ("%s, ssid=%s\n", __func__, pnetwork->ssid.ssid));
 552 
 553         len = get_wlan_bssid_ex_sz(pnetwork);
 554         if (len > (sizeof(struct wlan_bssid_ex))) {
 555                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
 556                          ("\n****%s: return a wrong bss ***\n", __func__));
 557                 return;
 558         }
 559         spin_lock_bh(&pmlmepriv->lock);
 560 
 561         /*  update IBSS_network 's timestamp */
 562         if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
 563                 if (!memcmp(&pmlmepriv->cur_network.network.MacAddress, pnetwork->MacAddress, ETH_ALEN)) {
 564                         struct wlan_network *ibss_wlan = NULL;
 565 
 566                         memcpy(pmlmepriv->cur_network.network.ies, pnetwork->ies, 8);
 567                         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
 568                         ibss_wlan = rtw_find_network(&pmlmepriv->scanned_queue,  pnetwork->MacAddress);
 569                         if (ibss_wlan) {
 570                                 memcpy(ibss_wlan->network.ies, pnetwork->ies, 8);
 571                                 spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
 572                                 goto exit;
 573                         }
 574                         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
 575                 }
 576         }
 577 
 578         /*  lock pmlmepriv->lock when you accessing network_q */
 579         if (!check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
 580                 if (pnetwork->ssid.ssid[0] == 0)
 581                         pnetwork->ssid.ssid_length = 0;
 582                 rtw_add_network(adapter, pnetwork);
 583         }
 584 
 585 exit:
 586         spin_unlock_bh(&pmlmepriv->lock);
 587 }
 588 
 589 void rtw_surveydone_event_callback(struct adapter       *adapter, u8 *pbuf)
 590 {
 591         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
 592 
 593         spin_lock_bh(&pmlmepriv->lock);
 594 
 595         if (pmlmepriv->wps_probe_req_ie) {
 596                 pmlmepriv->wps_probe_req_ie_len = 0;
 597                 kfree(pmlmepriv->wps_probe_req_ie);
 598                 pmlmepriv->wps_probe_req_ie = NULL;
 599         }
 600 
 601         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
 602                  ("%s: fw_state:%x\n\n", __func__, get_fwstate(pmlmepriv)));
 603 
 604         if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) {
 605                 del_timer_sync(&pmlmepriv->scan_to_timer);
 606                 _clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
 607         } else {
 608                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("nic status=%x, survey done event comes too late!\n", get_fwstate(pmlmepriv)));
 609         }
 610 
 611         rtw_set_signal_stat_timer(&adapter->recvpriv);
 612 
 613         if (pmlmepriv->to_join) {
 614                 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
 615                         if (!check_fwstate(pmlmepriv, _FW_LINKED)) {
 616                                 set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
 617 
 618                                 if (rtw_select_and_join_from_scanned_queue(pmlmepriv) == _SUCCESS) {
 619                                         mod_timer(&pmlmepriv->assoc_timer,
 620                                                   jiffies + msecs_to_jiffies(MAX_JOIN_TIMEOUT));
 621                                 } else {
 622                                         struct wlan_bssid_ex *pdev_network = &adapter->registrypriv.dev_network;
 623                                         u8 *pibss = adapter->registrypriv.dev_network.MacAddress;
 624 
 625                                         _clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
 626 
 627                                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("switching to adhoc master\n"));
 628 
 629                                         memcpy(&pdev_network->ssid, &pmlmepriv->assoc_ssid, sizeof(struct ndis_802_11_ssid));
 630 
 631                                         rtw_update_registrypriv_dev_network(adapter);
 632                                         rtw_generate_random_ibss(pibss);
 633 
 634                                         pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
 635 
 636                                         if (rtw_createbss_cmd(adapter) != _SUCCESS)
 637                                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Error=>rtw_createbss_cmd status FAIL\n"));
 638                                         pmlmepriv->to_join = false;
 639                                 }
 640                         }
 641                 } else {
 642                         int s_ret;
 643 
 644                         set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
 645                         pmlmepriv->to_join = false;
 646                         s_ret = rtw_select_and_join_from_scanned_queue(pmlmepriv);
 647                         if (s_ret == _SUCCESS) {
 648                                 mod_timer(&pmlmepriv->assoc_timer,
 649                                         jiffies + msecs_to_jiffies(MAX_JOIN_TIMEOUT));
 650                         } else if (s_ret == 2) { /* there is no need to wait for join */
 651                                 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
 652                                 rtw_indicate_connect(adapter);
 653                         } else {
 654                                 DBG_88E("try_to_join, but select scanning queue fail, to_roaming:%d\n", pmlmepriv->to_roaming);
 655                                 if (pmlmepriv->to_roaming != 0) {
 656                                         if (--pmlmepriv->to_roaming == 0 ||
 657                                             rtw_sitesurvey_cmd(adapter, &pmlmepriv->assoc_ssid, 1, NULL, 0) != _SUCCESS) {
 658                                                 pmlmepriv->to_roaming = 0;
 659                                                 rtw_free_assoc_resources(adapter);
 660                                                 rtw_indicate_disconnect(adapter);
 661                                         } else {
 662                                                 pmlmepriv->to_join = true;
 663                                         }
 664                                 }
 665                                 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
 666                         }
 667                 }
 668         }
 669 
 670         indicate_wx_scan_complete_event(adapter);
 671 
 672         spin_unlock_bh(&pmlmepriv->lock);
 673 
 674         rtw_os_xmit_schedule(adapter);
 675 }
 676 
 677 void rtw_dummy_event_callback(struct adapter *adapter, u8 *pbuf)
 678 {
 679 }
 680 
 681 void rtw_fwdbg_event_callback(struct adapter *adapter, u8 *pbuf)
 682 {
 683 }
 684 
 685 static void free_scanqueue(struct       mlme_priv *pmlmepriv)
 686 {
 687         struct __queue *free_queue = &pmlmepriv->free_bss_pool;
 688         struct __queue *scan_queue = &pmlmepriv->scanned_queue;
 689         struct list_head *plist, *phead, *ptemp;
 690 
 691         RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_, ("+%s\n", __func__));
 692         spin_lock_bh(&scan_queue->lock);
 693         spin_lock_bh(&free_queue->lock);
 694 
 695         phead = get_list_head(scan_queue);
 696         plist = phead->next;
 697 
 698         while (plist != phead) {
 699                 ptemp = plist->next;
 700                 list_del_init(plist);
 701                 list_add_tail(plist, &free_queue->queue);
 702                 plist = ptemp;
 703         }
 704 
 705         spin_unlock_bh(&free_queue->lock);
 706         spin_unlock_bh(&scan_queue->lock);
 707 }
 708 
 709 /*
 710  * rtw_free_assoc_resources: the caller has to lock pmlmepriv->lock
 711  */
 712 void rtw_free_assoc_resources(struct adapter *adapter)
 713 {
 714         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
 715 
 716         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
 717         rtw_free_assoc_resources_locked(adapter);
 718         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
 719 }
 720 
 721 /*
 722  * rtw_free_assoc_resources_locked: the caller has to lock pmlmepriv->lock
 723  */
 724 void rtw_free_assoc_resources_locked(struct adapter *adapter)
 725 {
 726         struct wlan_network *pwlan = NULL;
 727         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
 728         struct sta_priv *pstapriv = &adapter->stapriv;
 729         struct wlan_network *tgt_network = &pmlmepriv->cur_network;
 730 
 731         RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_, ("+rtw_free_assoc_resources\n"));
 732         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
 733                  ("tgt_network->network.MacAddress=%pM ssid=%s\n",
 734                  tgt_network->network.MacAddress, tgt_network->network.ssid.ssid));
 735 
 736         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_AP_STATE)) {
 737                 struct sta_info *psta;
 738 
 739                 psta = rtw_get_stainfo(&adapter->stapriv, tgt_network->network.MacAddress);
 740 
 741                 spin_lock_bh(&pstapriv->sta_hash_lock);
 742                 rtw_free_stainfo(adapter,  psta);
 743                 spin_unlock_bh(&pstapriv->sta_hash_lock);
 744         }
 745 
 746         if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE | WIFI_AP_STATE)) {
 747                 struct sta_info *psta;
 748 
 749                 rtw_free_all_stainfo(adapter);
 750 
 751                 psta = rtw_get_bcmc_stainfo(adapter);
 752                 spin_lock_bh(&pstapriv->sta_hash_lock);
 753                 rtw_free_stainfo(adapter, psta);
 754                 spin_unlock_bh(&pstapriv->sta_hash_lock);
 755 
 756                 rtw_init_bcmc_stainfo(adapter);
 757         }
 758 
 759         pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
 760         if (pwlan)
 761                 pwlan->fixed = false;
 762         else
 763                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("rtw_free_assoc_resources:pwlan==NULL\n\n"));
 764 
 765         if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) && (adapter->stapriv.asoc_sta_count == 1)))
 766                 rtw_free_network_nolock(pmlmepriv, pwlan);
 767 
 768         pmlmepriv->key_mask = 0;
 769 }
 770 
 771 /*
 772  * rtw_indicate_connect: the caller has to lock pmlmepriv->lock
 773  */
 774 void rtw_indicate_connect(struct adapter *padapter)
 775 {
 776         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 777 
 778         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("+%s\n", __func__));
 779 
 780         pmlmepriv->to_join = false;
 781 
 782         if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
 783                 set_fwstate(pmlmepriv, _FW_LINKED);
 784 
 785                 led_control_8188eu(padapter, LED_CTL_LINK);
 786 
 787                 rtw_os_indicate_connect(padapter);
 788         }
 789 
 790         pmlmepriv->to_roaming = 0;
 791 
 792         rtw_set_scan_deny(padapter, 3000);
 793 
 794         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("-%s: fw_state=0x%08x\n", __func__, get_fwstate(pmlmepriv)));
 795 }
 796 
 797 /*
 798  * rtw_indicate_disconnect: the caller has to lock pmlmepriv->lock
 799  */
 800 void rtw_indicate_disconnect(struct adapter *padapter)
 801 {
 802         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 803 
 804         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("+%s\n", __func__));
 805 
 806         _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING | WIFI_UNDER_WPS);
 807 
 808         if (pmlmepriv->to_roaming > 0)
 809                 _clr_fwstate_(pmlmepriv, _FW_LINKED);
 810 
 811         if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) ||
 812             (pmlmepriv->to_roaming <= 0)) {
 813                 rtw_os_indicate_disconnect(padapter);
 814 
 815                 _clr_fwstate_(pmlmepriv, _FW_LINKED);
 816                 led_control_8188eu(padapter, LED_CTL_NO_LINK);
 817                 rtw_clear_scan_deny(padapter);
 818         }
 819 
 820         rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, 1);
 821 }
 822 
 823 inline void rtw_indicate_scan_done(struct adapter *padapter, bool aborted)
 824 {
 825         indicate_wx_scan_complete_event(padapter);
 826 }
 827 
 828 static struct sta_info *rtw_joinbss_update_stainfo(struct adapter *padapter, struct wlan_network *pnetwork)
 829 {
 830         int i;
 831         struct sta_info *bmc_sta, *psta = NULL;
 832         struct recv_reorder_ctrl *preorder_ctrl;
 833         struct sta_priv *pstapriv = &padapter->stapriv;
 834 
 835         psta = rtw_get_stainfo(pstapriv, pnetwork->network.MacAddress);
 836         if (!psta)
 837                 psta = rtw_alloc_stainfo(pstapriv, pnetwork->network.MacAddress);
 838 
 839         if (psta) { /* update ptarget_sta */
 840                 DBG_88E("%s\n", __func__);
 841                 psta->aid  = pnetwork->join_res;
 842                 psta->mac_id = 0;
 843                 /* sta mode */
 844                 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, true);
 845                 /* security related */
 846                 if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) {
 847                         padapter->securitypriv.binstallGrpkey = false;
 848                         padapter->securitypriv.busetkipkey = false;
 849                         padapter->securitypriv.bgrpkey_handshake = false;
 850                         psta->ieee8021x_blocked = true;
 851                         psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
 852                         memset((u8 *)&psta->dot118021x_UncstKey, 0, sizeof(union Keytype));
 853                         memset((u8 *)&psta->dot11tkiprxmickey, 0, sizeof(union Keytype));
 854                         memset((u8 *)&psta->dot11tkiptxmickey, 0, sizeof(union Keytype));
 855                         memset((u8 *)&psta->dot11txpn, 0, sizeof(union pn48));
 856                         memset((u8 *)&psta->dot11rxpn, 0, sizeof(union pn48));
 857                 }
 858                 /*
 859                  * Commented by Albert 2012/07/21
 860                  * When doing the WPS, the wps_ie_len won't equal to 0
 861                  * And the Wi-Fi driver shouldn't allow the data
 862                  * packet to be transmitted.
 863                  */
 864                 if (padapter->securitypriv.wps_ie_len != 0) {
 865                         psta->ieee8021x_blocked = true;
 866                         padapter->securitypriv.wps_ie_len = 0;
 867                 }
 868                 /* for A-MPDU Rx reordering buffer control for bmc_sta & sta_info */
 869                 /* if A-MPDU Rx is enabled, resetting  rx_ordering_ctrl wstart_b(indicate_seq) to default value = 0xffff */
 870                 /* todo: check if AP can send A-MPDU packets */
 871                 for (i = 0; i < 16; i++) {
 872                         /* preorder_ctrl = &precvpriv->recvreorder_ctrl[i]; */
 873                         preorder_ctrl = &psta->recvreorder_ctrl[i];
 874                         preorder_ctrl->enable = false;
 875                         preorder_ctrl->indicate_seq = 0xffff;
 876                         preorder_ctrl->wend_b = 0xffff;
 877                         preorder_ctrl->wsize_b = 64;/* max_ampdu_sz; ex. 32(kbytes) -> wsize_b = 32 */
 878                 }
 879                 bmc_sta = rtw_get_bcmc_stainfo(padapter);
 880                 if (bmc_sta) {
 881                         for (i = 0; i < 16; i++) {
 882                                 /* preorder_ctrl = &precvpriv->recvreorder_ctrl[i]; */
 883                                 preorder_ctrl = &bmc_sta->recvreorder_ctrl[i];
 884                                 preorder_ctrl->enable = false;
 885                                 preorder_ctrl->indicate_seq = 0xffff;
 886                                 preorder_ctrl->wend_b = 0xffff;
 887                                 preorder_ctrl->wsize_b = 64;/* max_ampdu_sz; ex. 32(kbytes) -> wsize_b = 32 */
 888                         }
 889                 }
 890                 /* misc. */
 891                 update_sta_info(padapter, psta);
 892         }
 893         return psta;
 894 }
 895 
 896 /* pnetwork: returns from rtw_joinbss_event_callback */
 897 /* ptarget_wlan: found from scanned_queue */
 898 static void rtw_joinbss_update_network(struct adapter *padapter, struct wlan_network *ptarget_wlan, struct wlan_network  *pnetwork)
 899 {
 900         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 901         struct wlan_network *cur_network = &pmlmepriv->cur_network;
 902 
 903         DBG_88E("%s\n", __func__);
 904 
 905         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
 906                  ("\nfw_state:%x, BSSID:%pM\n",
 907                  get_fwstate(pmlmepriv), pnetwork->network.MacAddress));
 908 
 909         /*  why not use ptarget_wlan?? */
 910         memcpy(&cur_network->network, &pnetwork->network, pnetwork->network.Length);
 911         /*  some ies in pnetwork is wrong, so we should use ptarget_wlan ies */
 912         cur_network->network.ie_length = ptarget_wlan->network.ie_length;
 913         memcpy(&cur_network->network.ies[0], &ptarget_wlan->network.ies[0], MAX_IE_SZ);
 914 
 915         cur_network->aid = pnetwork->join_res;
 916 
 917         rtw_set_signal_stat_timer(&padapter->recvpriv);
 918         padapter->recvpriv.signal_strength = ptarget_wlan->network.PhyInfo.SignalStrength;
 919         padapter->recvpriv.signal_qual = ptarget_wlan->network.PhyInfo.SignalQuality;
 920         /* the ptarget_wlan->network.Rssi is raw data, we use ptarget_wlan->network.PhyInfo.SignalStrength instead (has scaled) */
 921         padapter->recvpriv.rssi = translate_percentage_to_dbm(ptarget_wlan->network.PhyInfo.SignalStrength);
 922         rtw_set_signal_stat_timer(&padapter->recvpriv);
 923 
 924         /* update fw_state will clr _FW_UNDER_LINKING here indirectly */
 925         switch (pnetwork->network.InfrastructureMode) {
 926         case Ndis802_11Infrastructure:
 927                 if (pmlmepriv->fw_state&WIFI_UNDER_WPS)
 928                         pmlmepriv->fw_state = WIFI_STATION_STATE|WIFI_UNDER_WPS;
 929                 else
 930                         pmlmepriv->fw_state = WIFI_STATION_STATE;
 931                 break;
 932         case Ndis802_11IBSS:
 933                 pmlmepriv->fw_state = WIFI_ADHOC_STATE;
 934                 break;
 935         default:
 936                 pmlmepriv->fw_state = WIFI_NULL_STATE;
 937                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Invalid network_mode\n"));
 938                 break;
 939         }
 940 
 941         rtw_update_protection(padapter, (cur_network->network.ies) +
 942                               sizeof(struct ndis_802_11_fixed_ie),
 943                               (cur_network->network.ie_length));
 944         rtw_update_ht_cap(padapter, cur_network->network.ies, cur_network->network.ie_length);
 945 }
 946 
 947 /* Notes: the function could be > passive_level (the same context as Rx tasklet) */
 948 /* pnetwork: returns from rtw_joinbss_event_callback */
 949 /* ptarget_wlan: found from scanned_queue */
 950 /* if join_res > 0, for (fw_state == WIFI_STATION_STATE), we check if  "ptarget_sta" & "ptarget_wlan" exist. */
 951 /* if join_res > 0, for (fw_state == WIFI_ADHOC_STATE), we only check if "ptarget_wlan" exist. */
 952 /* if join_res > 0, update "cur_network->network" from "pnetwork->network" if (ptarget_wlan != NULL). */
 953 
 954 void rtw_joinbss_event_prehandle(struct adapter *adapter, u8 *pbuf)
 955 {
 956         struct sta_info *ptarget_sta = NULL, *pcur_sta = NULL;
 957         struct sta_priv *pstapriv = &adapter->stapriv;
 958         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
 959         struct wlan_network *pnetwork = (struct wlan_network *)pbuf;
 960         struct wlan_network *cur_network = &pmlmepriv->cur_network;
 961         struct wlan_network *pcur_wlan = NULL, *ptarget_wlan = NULL;
 962         unsigned int the_same_macaddr = false;
 963 
 964         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("joinbss event call back received with res=%d\n", pnetwork->join_res));
 965 
 966         rtw_get_encrypt_decrypt_from_registrypriv(adapter);
 967 
 968         if (pmlmepriv->assoc_ssid.ssid_length == 0)
 969                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("@@@@@   joinbss event call back  for Any SSid\n"));
 970         else
 971                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("@@@@@   rtw_joinbss_event_callback for SSid:%s\n", pmlmepriv->assoc_ssid.ssid));
 972 
 973         the_same_macaddr = !memcmp(pnetwork->network.MacAddress, cur_network->network.MacAddress, ETH_ALEN);
 974 
 975         pnetwork->network.Length = get_wlan_bssid_ex_sz(&pnetwork->network);
 976         if (pnetwork->network.Length > sizeof(struct wlan_bssid_ex)) {
 977                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("\n\n ***joinbss_evt_callback return a wrong bss ***\n\n"));
 978                 return;
 979         }
 980 
 981         spin_lock_bh(&pmlmepriv->lock);
 982 
 983         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\nrtw_joinbss_event_callback!! _enter_critical\n"));
 984 
 985         if (pnetwork->join_res > 0) {
 986                 spin_lock_bh(&pmlmepriv->scanned_queue.lock);
 987                 if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
 988                         /* s1. find ptarget_wlan */
 989                         if (check_fwstate(pmlmepriv, _FW_LINKED)) {
 990                                 if (the_same_macaddr) {
 991                                         ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
 992                                 } else {
 993                                         pcur_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
 994                                         if (pcur_wlan)
 995                                                 pcur_wlan->fixed = false;
 996 
 997                                         pcur_sta = rtw_get_stainfo(pstapriv, cur_network->network.MacAddress);
 998                                         if (pcur_sta) {
 999                                                 spin_lock_bh(&pstapriv->sta_hash_lock);
1000                                                 rtw_free_stainfo(adapter,  pcur_sta);
1001                                                 spin_unlock_bh(&pstapriv->sta_hash_lock);
1002                                         }
1003 
1004                                         ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->network.MacAddress);
1005                                         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1006                                                 if (ptarget_wlan)
1007                                                         ptarget_wlan->fixed = true;
1008                                         }
1009                                 }
1010                         } else {
1011                                 ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->network.MacAddress);
1012                                 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1013                                         if (ptarget_wlan)
1014                                                 ptarget_wlan->fixed = true;
1015                                 }
1016                         }
1017 
1018                         /* s2. update cur_network */
1019                         if (ptarget_wlan) {
1020                                 rtw_joinbss_update_network(adapter, ptarget_wlan, pnetwork);
1021                         } else {
1022                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Can't find ptarget_wlan when joinbss_event callback\n"));
1023                                 spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1024                                 goto ignore_joinbss_callback;
1025                         }
1026 
1027                         /* s3. find ptarget_sta & update ptarget_sta after update cur_network only for station mode */
1028                         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1029                                 ptarget_sta = rtw_joinbss_update_stainfo(adapter, pnetwork);
1030                                 if (!ptarget_sta) {
1031                                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Can't update stainfo when joinbss_event callback\n"));
1032                                         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1033                                         goto ignore_joinbss_callback;
1034                                 }
1035                         }
1036 
1037                         /* s4. indicate connect */
1038                         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1039                                 rtw_indicate_connect(adapter);
1040                         } else {
1041                                 /* adhoc mode will rtw_indicate_connect when rtw_stassoc_event_callback */
1042                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("adhoc mode, fw_state:%x", get_fwstate(pmlmepriv)));
1043                         }
1044 
1045                         /* s5. Cancel assoc_timer */
1046                         del_timer_sync(&pmlmepriv->assoc_timer);
1047 
1048                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("Cancel assoc_timer\n"));
1049 
1050                 } else {
1051                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("rtw_joinbss_event_callback err: fw_state:%x", get_fwstate(pmlmepriv)));
1052                         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1053                         goto ignore_joinbss_callback;
1054                 }
1055 
1056                 spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1057 
1058         } else if (pnetwork->join_res == -4) {
1059                 rtw_reset_securitypriv(adapter);
1060                 mod_timer(&pmlmepriv->assoc_timer,
1061                           jiffies + msecs_to_jiffies(1));
1062 
1063                 if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
1064                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("fail! clear _FW_UNDER_LINKING ^^^fw_state=%x\n", get_fwstate(pmlmepriv)));
1065                         _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1066                 }
1067         } else { /* if join_res < 0 (join fails), then try again */
1068                 mod_timer(&pmlmepriv->assoc_timer,
1069                           jiffies + msecs_to_jiffies(1));
1070                 _clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1071         }
1072 
1073 ignore_joinbss_callback:
1074         spin_unlock_bh(&pmlmepriv->lock);
1075 }
1076 
1077 void rtw_joinbss_event_callback(struct adapter *adapter, u8 *pbuf)
1078 {
1079         struct wlan_network *pnetwork = (struct wlan_network *)pbuf;
1080 
1081         mlmeext_joinbss_event_callback(adapter, pnetwork->join_res);
1082 
1083         rtw_os_xmit_schedule(adapter);
1084 }
1085 
1086 static u8 search_max_mac_id(struct adapter *padapter)
1087 {
1088         u8 mac_id;
1089 #if defined(CONFIG_88EU_AP_MODE)
1090         u8 aid;
1091         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1092         struct sta_priv *pstapriv = &padapter->stapriv;
1093 #endif
1094         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1095         struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
1096 
1097 #if defined(CONFIG_88EU_AP_MODE)
1098         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1099                 for (aid = pstapriv->max_num_sta; aid > 0; aid--) {
1100                         if (pstapriv->sta_aid[aid-1])
1101                                 break;
1102                 }
1103                 mac_id = aid + 1;
1104         } else
1105 #endif
1106         {/* adhoc  id =  31~2 */
1107                 for (mac_id = NUM_STA-1; mac_id >= IBSS_START_MAC_ID; mac_id--) {
1108                         if (pmlmeinfo->FW_sta_info[mac_id].status == 1)
1109                                 break;
1110                 }
1111         }
1112         return mac_id;
1113 }
1114 
1115 /* FOR AP , AD-HOC mode */
1116 void rtw_stassoc_hw_rpt(struct adapter *adapter, struct sta_info *psta)
1117 {
1118         u16 media_status;
1119         u8 macid;
1120 
1121         if (!psta)
1122                 return;
1123 
1124         macid = search_max_mac_id(adapter);
1125         rtw_hal_set_hwreg(adapter, HW_VAR_TX_RPT_MAX_MACID, (u8 *)&macid);
1126         media_status = (psta->mac_id<<8)|1; /*   MACID|OPMODE:1 connect */
1127         rtw_hal_set_hwreg(adapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status);
1128 }
1129 
1130 void rtw_stassoc_event_callback(struct adapter *adapter, u8 *pbuf)
1131 {
1132         struct sta_info *psta;
1133         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1134         struct stassoc_event *pstassoc = (struct stassoc_event *)pbuf;
1135         struct wlan_network *cur_network = &pmlmepriv->cur_network;
1136         struct wlan_network *ptarget_wlan = NULL;
1137 
1138         if (!rtw_access_ctrl(adapter, pstassoc->macaddr))
1139                 return;
1140 
1141 #if defined(CONFIG_88EU_AP_MODE)
1142         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1143                 psta = rtw_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
1144                 if (psta) {
1145                         ap_sta_info_defer_update(adapter, psta);
1146                         rtw_stassoc_hw_rpt(adapter, psta);
1147                 }
1148                 return;
1149         }
1150 #endif
1151         /* for AD-HOC mode */
1152         psta = rtw_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
1153         if (psta) {
1154                 /* the sta have been in sta_info_queue => do nothing */
1155                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
1156                          ("Error: %s: sta has been in sta_hash_queue\n",
1157                           __func__));
1158                 return; /* between drv has received this event before and  fw have not yet to set key to CAM_ENTRY) */
1159         }
1160         psta = rtw_alloc_stainfo(&adapter->stapriv, pstassoc->macaddr);
1161         if (!psta) {
1162                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
1163                          ("Can't alloc sta_info when %s\n", __func__));
1164                 return;
1165         }
1166         /* to do: init sta_info variable */
1167         psta->qos_option = 0;
1168         psta->mac_id = (uint)pstassoc->cam_id;
1169         DBG_88E("%s\n", __func__);
1170         /* for ad-hoc mode */
1171         rtw_hal_set_odm_var(adapter, HAL_ODM_STA_INFO, psta, true);
1172         rtw_stassoc_hw_rpt(adapter, psta);
1173         if (adapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X)
1174                 psta->dot118021XPrivacy = adapter->securitypriv.dot11PrivacyAlgrthm;
1175         psta->ieee8021x_blocked = false;
1176         spin_lock_bh(&pmlmepriv->lock);
1177         if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) ||
1178             (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE))) {
1179                 if (adapter->stapriv.asoc_sta_count == 2) {
1180                         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1181                         ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
1182                         if (ptarget_wlan)
1183                                 ptarget_wlan->fixed = true;
1184                         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1185                         /*  a sta + bc/mc_stainfo (not Ibss_stainfo) */
1186                         rtw_indicate_connect(adapter);
1187                 }
1188         }
1189         spin_unlock_bh(&pmlmepriv->lock);
1190         mlmeext_sta_add_event_callback(adapter, psta);
1191 }
1192 
1193 void rtw_stadel_event_callback(struct adapter *adapter, u8 *pbuf)
1194 {
1195         int mac_id = -1;
1196         struct sta_info *psta;
1197         struct wlan_network *pwlan = NULL;
1198         struct wlan_bssid_ex *pdev_network = NULL;
1199         u8 *pibss = NULL;
1200         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1201         struct stadel_event *pstadel = (struct stadel_event *)pbuf;
1202         struct sta_priv *pstapriv = &adapter->stapriv;
1203         struct wlan_network *tgt_network = &pmlmepriv->cur_network;
1204 
1205         psta = rtw_get_stainfo(&adapter->stapriv, pstadel->macaddr);
1206         if (psta)
1207                 mac_id = psta->mac_id;
1208         else
1209                 mac_id = pstadel->mac_id;
1210 
1211         DBG_88E("%s(mac_id=%d)=%pM\n", __func__, mac_id, pstadel->macaddr);
1212 
1213         if (mac_id >= 0) {
1214                 u16 media_status;
1215 
1216                 media_status = (mac_id<<8)|0; /*   MACID|OPMODE:0 means disconnect */
1217                 /* for STA, AP, ADHOC mode, report disconnect stauts to FW */
1218                 rtw_hal_set_hwreg(adapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status);
1219         }
1220 
1221         if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
1222                 return;
1223 
1224         mlmeext_sta_del_event_callback(adapter);
1225 
1226         spin_lock_bh(&pmlmepriv->lock);
1227 
1228         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1229                 if (pmlmepriv->to_roaming > 0)
1230                         pmlmepriv->to_roaming--; /*  this stadel_event is caused by roaming, decrease to_roaming */
1231                 else if (pmlmepriv->to_roaming == 0)
1232                         pmlmepriv->to_roaming = adapter->registrypriv.max_roaming_times;
1233 
1234                 if (*((unsigned short *)(pstadel->rsvd)) != WLAN_REASON_EXPIRATION_CHK)
1235                         pmlmepriv->to_roaming = 0; /*  don't roam */
1236 
1237                 rtw_free_uc_swdec_pending_queue(adapter);
1238 
1239                 rtw_free_assoc_resources(adapter);
1240                 rtw_indicate_disconnect(adapter);
1241                 spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1242                 /*  remove the network entry in scanned_queue */
1243                 pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
1244                 if (pwlan) {
1245                         pwlan->fixed = false;
1246                         rtw_free_network_nolock(pmlmepriv, pwlan);
1247                 }
1248                 spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1249                 _rtw_roaming(adapter, tgt_network);
1250         }
1251         if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) ||
1252             check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1253                 spin_lock_bh(&pstapriv->sta_hash_lock);
1254                 rtw_free_stainfo(adapter,  psta);
1255                 spin_unlock_bh(&pstapriv->sta_hash_lock);
1256 
1257                 if (adapter->stapriv.asoc_sta_count == 1) { /* a sta + bc/mc_stainfo (not Ibss_stainfo) */
1258                         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1259                         /* free old ibss network */
1260                         pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
1261                         if (pwlan) {
1262                                 pwlan->fixed = false;
1263                                 rtw_free_network_nolock(pmlmepriv, pwlan);
1264                         }
1265                         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1266                         /* re-create ibss */
1267                         pdev_network = &adapter->registrypriv.dev_network;
1268                         pibss = adapter->registrypriv.dev_network.MacAddress;
1269 
1270                         memcpy(pdev_network, &tgt_network->network, get_wlan_bssid_ex_sz(&tgt_network->network));
1271 
1272                         memcpy(&pdev_network->ssid, &pmlmepriv->assoc_ssid, sizeof(struct ndis_802_11_ssid));
1273 
1274                         rtw_update_registrypriv_dev_network(adapter);
1275 
1276                         rtw_generate_random_ibss(pibss);
1277 
1278                         if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1279                                 set_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE);
1280                                 _clr_fwstate_(pmlmepriv, WIFI_ADHOC_STATE);
1281                         }
1282 
1283                         if (rtw_createbss_cmd(adapter) != _SUCCESS)
1284                                 RT_TRACE(_module_rtl871x_ioctl_set_c_, _drv_err_, ("***Error=>stadel_event_callback: rtw_createbss_cmd status FAIL***\n "));
1285                 }
1286         }
1287         spin_unlock_bh(&pmlmepriv->lock);
1288 }
1289 
1290 void rtw_cpwm_event_callback(struct adapter *padapter, u8 *pbuf)
1291 {
1292         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("+%s !!!\n", __func__));
1293 }
1294 
1295 /*
1296  * _rtw_join_timeout_handler - Timeout/failure handler for CMD JoinBss
1297  * @adapter: pointer to struct adapter structure
1298  */
1299 void _rtw_join_timeout_handler (struct timer_list *t)
1300 {
1301         struct adapter *adapter = from_timer(adapter, t, mlmepriv.assoc_timer);
1302         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1303         int do_join_r;
1304 
1305         DBG_88E("%s, fw_state=%x\n", __func__, get_fwstate(pmlmepriv));
1306 
1307         if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1308                 return;
1309 
1310         spin_lock_bh(&pmlmepriv->lock);
1311 
1312         if (pmlmepriv->to_roaming > 0) { /*  join timeout caused by roaming */
1313                 while (1) {
1314                         pmlmepriv->to_roaming--;
1315                         if (pmlmepriv->to_roaming != 0) { /* try another , */
1316                                 DBG_88E("%s try another roaming\n", __func__);
1317                                 do_join_r = rtw_do_join(adapter);
1318                                 if (do_join_r != _SUCCESS) {
1319                                         DBG_88E("%s roaming do_join return %d\n", __func__, do_join_r);
1320                                         continue;
1321                                 }
1322                                 break;
1323                         }
1324                         DBG_88E("%s We've try roaming but fail\n", __func__);
1325                         rtw_indicate_disconnect(adapter);
1326                         break;
1327                 }
1328         } else {
1329                 rtw_indicate_disconnect(adapter);
1330                 free_scanqueue(pmlmepriv);/*  */
1331         }
1332         spin_unlock_bh(&pmlmepriv->lock);
1333 }
1334 
1335 /*
1336  * rtw_scan_timeout_handler - Timeout/Failure handler for CMD SiteSurvey
1337  * @adapter: pointer to struct adapter structure
1338  */
1339 void rtw_scan_timeout_handler (struct timer_list *t)
1340 {
1341         struct adapter *adapter = from_timer(adapter, t,
1342                                              mlmepriv.scan_to_timer);
1343         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1344 
1345         DBG_88E(FUNC_ADPT_FMT" fw_state=%x\n", FUNC_ADPT_ARG(adapter), get_fwstate(pmlmepriv));
1346         spin_lock_bh(&pmlmepriv->lock);
1347         _clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
1348         spin_unlock_bh(&pmlmepriv->lock);
1349         rtw_indicate_scan_done(adapter, true);
1350 }
1351 
1352 static void rtw_auto_scan_handler(struct adapter *padapter)
1353 {
1354         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1355 
1356         /* auto site survey per 60sec */
1357         if (pmlmepriv->scan_interval > 0) {
1358                 pmlmepriv->scan_interval--;
1359                 if (pmlmepriv->scan_interval == 0) {
1360                         DBG_88E("%s\n", __func__);
1361                         rtw_set_802_11_bssid_list_scan(padapter, NULL, 0);
1362                         pmlmepriv->scan_interval = SCAN_INTERVAL;/*  30*2 sec = 60sec */
1363                 }
1364         }
1365 }
1366 
1367 void rtw_dynamic_check_timer_handlder(struct timer_list *t)
1368 {
1369         struct adapter *adapter = from_timer(adapter, t,
1370                                              mlmepriv.dynamic_chk_timer);
1371         struct registry_priv *pregistrypriv = &adapter->registrypriv;
1372 
1373         if (!adapter)
1374                 return;
1375         if (!adapter->hw_init_completed)
1376                 goto exit;
1377         if ((adapter->bDriverStopped) || (adapter->bSurpriseRemoved))
1378                 goto exit;
1379         if (adapter->net_closed)
1380                 goto exit;
1381         rtw_dynamic_chk_wk_cmd(adapter);
1382 
1383         if (pregistrypriv->wifi_spec == 1) {
1384                 /* auto site survey */
1385                 rtw_auto_scan_handler(adapter);
1386         }
1387 exit:
1388         mod_timer(&adapter->mlmepriv.dynamic_chk_timer,
1389                   jiffies + msecs_to_jiffies(2000));
1390 }
1391 
1392 #define RTW_SCAN_RESULT_EXPIRE 2000
1393 
1394 /*
1395  * Select a new join candidate from the original @param candidate and @param competitor
1396  * @return true: candidate is updated
1397  * @return false: candidate is not updated
1398  */
1399 static int rtw_check_join_candidate(struct mlme_priv *pmlmepriv
1400         , struct wlan_network **candidate, struct wlan_network *competitor)
1401 {
1402         int updated = false;
1403         unsigned long since_scan;
1404         struct adapter *adapter = container_of(pmlmepriv, struct adapter,
1405                                                mlmepriv);
1406 
1407         /* check bssid, if needed */
1408         if (pmlmepriv->assoc_by_bssid) {
1409                 if (memcmp(competitor->network.MacAddress, pmlmepriv->assoc_bssid, ETH_ALEN))
1410                         goto exit;
1411         }
1412 
1413         /* check ssid, if needed */
1414         if (pmlmepriv->assoc_ssid.ssid_length) {
1415                 if (competitor->network.ssid.ssid_length != pmlmepriv->assoc_ssid.ssid_length ||
1416                     memcmp(competitor->network.ssid.ssid, pmlmepriv->assoc_ssid.ssid, pmlmepriv->assoc_ssid.ssid_length))
1417                         goto exit;
1418         }
1419 
1420         if (!rtw_is_desired_network(adapter, competitor))
1421                 goto exit;
1422 
1423         if (pmlmepriv->to_roaming) {
1424                 since_scan = jiffies - competitor->last_scanned;
1425                 if (jiffies_to_msecs(since_scan) >= RTW_SCAN_RESULT_EXPIRE ||
1426                     !is_same_ess(&competitor->network, &pmlmepriv->cur_network.network))
1427                         goto exit;
1428         }
1429 
1430         if (!*candidate || (*candidate)->network.Rssi < competitor->network.Rssi) {
1431                 *candidate = competitor;
1432                 updated = true;
1433         }
1434         if (updated) {
1435                 DBG_88E("[by_bssid:%u][assoc_ssid:%s]new candidate: %s(%pM rssi:%d\n",
1436                         pmlmepriv->assoc_by_bssid,
1437                         pmlmepriv->assoc_ssid.ssid,
1438                         (*candidate)->network.ssid.ssid,
1439                         (*candidate)->network.MacAddress,
1440                         (int)(*candidate)->network.Rssi);
1441                 DBG_88E("[to_roaming:%u]\n", pmlmepriv->to_roaming);
1442         }
1443 
1444 exit:
1445         return updated;
1446 }
1447 
1448 /*
1449  * Calling context:
1450  * The caller of the sub-routine will be in critical section...
1451  * The caller must hold the following spinlock
1452  * pmlmepriv->lock
1453  */
1454 
1455 int rtw_select_and_join_from_scanned_queue(struct mlme_priv *pmlmepriv)
1456 {
1457         int ret;
1458         struct list_head *phead;
1459         struct adapter *adapter;
1460         struct __queue *queue = &pmlmepriv->scanned_queue;
1461         struct wlan_network *pnetwork = NULL;
1462         struct wlan_network *candidate = NULL;
1463         u8 supp_ant_div = false;
1464 
1465         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1466         phead = get_list_head(queue);
1467         adapter = (struct adapter *)pmlmepriv->nic_hdl;
1468         pmlmepriv->pscanned = phead->next;
1469         while (phead != pmlmepriv->pscanned) {
1470                 pnetwork = container_of(pmlmepriv->pscanned, struct wlan_network, list);
1471                 if (!pnetwork) {
1472                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s return _FAIL:(pnetwork==NULL)\n", __func__));
1473                         ret = _FAIL;
1474                         goto exit;
1475                 }
1476                 pmlmepriv->pscanned = pmlmepriv->pscanned->next;
1477                 rtw_check_join_candidate(pmlmepriv, &candidate, pnetwork);
1478         }
1479         if (!candidate) {
1480                 DBG_88E("%s: return _FAIL(candidate==NULL)\n", __func__);
1481                 ret = _FAIL;
1482                 goto exit;
1483         } else {
1484                 DBG_88E("%s: candidate: %s(%pM ch:%u)\n", __func__,
1485                         candidate->network.ssid.ssid, candidate->network.MacAddress,
1486                         candidate->network.Configuration.DSConfig);
1487         }
1488 
1489         /*  check for situation of  _FW_LINKED */
1490         if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1491                 DBG_88E("%s: _FW_LINKED while ask_for_joinbss!!!\n", __func__);
1492 
1493                 rtw_disassoc_cmd(adapter, 0, true);
1494                 rtw_indicate_disconnect(adapter);
1495                 rtw_free_assoc_resources_locked(adapter);
1496         }
1497 
1498         rtw_hal_get_def_var(adapter, HAL_DEF_IS_SUPPORT_ANT_DIV, &(supp_ant_div));
1499         if (supp_ant_div) {
1500                 u8 cur_ant;
1501 
1502                 rtw_hal_get_def_var(adapter, HAL_DEF_CURRENT_ANTENNA, &(cur_ant));
1503                 DBG_88E("#### Opt_Ant_(%s), cur_Ant(%s)\n",
1504                         (candidate->network.PhyInfo.Optimum_antenna == 2) ? "A" : "B",
1505                         (cur_ant == 2) ? "A" : "B"
1506                 );
1507         }
1508 
1509         ret = rtw_joinbss_cmd(adapter, candidate);
1510 
1511 exit:
1512         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1513         return ret;
1514 }
1515 
1516 int rtw_set_auth(struct adapter *adapter, struct security_priv *psecuritypriv)
1517 {
1518         struct cmd_obj *pcmd;
1519         struct setauth_parm *psetauthparm;
1520         struct cmd_priv *pcmdpriv = &adapter->cmdpriv;
1521         int res = _SUCCESS;
1522 
1523         pcmd = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
1524         if (!pcmd) {
1525                 res = _FAIL;  /* try again */
1526                 goto exit;
1527         }
1528 
1529         psetauthparm = kzalloc(sizeof(struct setauth_parm), GFP_KERNEL);
1530         if (!psetauthparm) {
1531                 kfree(pcmd);
1532                 res = _FAIL;
1533                 goto exit;
1534         }
1535         psetauthparm->mode = (unsigned char)psecuritypriv->dot11AuthAlgrthm;
1536         pcmd->cmdcode = _SetAuth_CMD_;
1537         pcmd->parmbuf = (unsigned char *)psetauthparm;
1538         pcmd->cmdsz =  sizeof(struct setauth_parm);
1539         pcmd->rsp = NULL;
1540         pcmd->rspsz = 0;
1541         INIT_LIST_HEAD(&pcmd->list);
1542         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
1543                  ("after enqueue set_auth_cmd, auth_mode=%x\n",
1544                  psecuritypriv->dot11AuthAlgrthm));
1545         res = rtw_enqueue_cmd(pcmdpriv, pcmd);
1546 exit:
1547         return res;
1548 }
1549 
1550 int rtw_set_key(struct adapter *adapter, struct security_priv *psecuritypriv, int keyid, u8 set_tx)
1551 {
1552         u8 keylen;
1553         struct cmd_obj *pcmd;
1554         struct setkey_parm *psetkeyparm;
1555         struct cmd_priv *pcmdpriv = &adapter->cmdpriv;
1556         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1557         int res = _SUCCESS;
1558 
1559         pcmd = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
1560         if (!pcmd)
1561                 return _FAIL;  /* try again */
1562 
1563         psetkeyparm = kzalloc(sizeof(struct setkey_parm), GFP_KERNEL);
1564         if (!psetkeyparm) {
1565                 res = _FAIL;
1566                 goto err_free_cmd;
1567         }
1568 
1569         if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) {
1570                 psetkeyparm->algorithm = (unsigned char)psecuritypriv->dot118021XGrpPrivacy;
1571                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
1572                          ("\n %s: psetkeyparm->algorithm=(unsigned char)psecuritypriv->dot118021XGrpPrivacy=%d\n",
1573                           __func__, psetkeyparm->algorithm));
1574         } else {
1575                 psetkeyparm->algorithm = (u8)psecuritypriv->dot11PrivacyAlgrthm;
1576                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
1577                          ("\n %s: psetkeyparm->algorithm=(u8)psecuritypriv->dot11PrivacyAlgrthm=%d\n",
1578                           __func__, psetkeyparm->algorithm));
1579         }
1580         psetkeyparm->keyid = (u8)keyid;/* 0~3 */
1581         psetkeyparm->set_tx = set_tx;
1582         pmlmepriv->key_mask |= BIT(psetkeyparm->keyid);
1583         DBG_88E("==> %s algorithm(%x), keyid(%x), key_mask(%x)\n",
1584                 __func__, psetkeyparm->algorithm, psetkeyparm->keyid,
1585                 pmlmepriv->key_mask);
1586         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
1587                  ("\n %s: psetkeyparm->algorithm=%d psetkeyparm->keyid=(u8)keyid=%d\n",
1588                   __func__, psetkeyparm->algorithm, keyid));
1589 
1590         switch (psetkeyparm->algorithm) {
1591         case _WEP40_:
1592                 keylen = 5;
1593                 memcpy(&psetkeyparm->key[0],
1594                        &psecuritypriv->dot11DefKey[keyid].skey[0], keylen);
1595                 break;
1596         case _WEP104_:
1597                 keylen = 13;
1598                 memcpy(&psetkeyparm->key[0],
1599                        &psecuritypriv->dot11DefKey[keyid].skey[0], keylen);
1600                 break;
1601         case _TKIP_:
1602                 keylen = 16;
1603                 memcpy(&psetkeyparm->key, &psecuritypriv->dot118021XGrpKey[keyid], keylen);
1604                 psetkeyparm->grpkey = 1;
1605                 break;
1606         case _AES_:
1607                 keylen = 16;
1608                 memcpy(&psetkeyparm->key, &psecuritypriv->dot118021XGrpKey[keyid], keylen);
1609                 psetkeyparm->grpkey = 1;
1610                 break;
1611         default:
1612                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
1613                          ("\n %s:psecuritypriv->dot11PrivacyAlgrthm=%x (must be 1 or 2 or 4 or 5)\n",
1614                           __func__, psecuritypriv->dot11PrivacyAlgrthm));
1615                 res = _FAIL;
1616                 goto err_free_parm;
1617         }
1618         pcmd->cmdcode = _SetKey_CMD_;
1619         pcmd->parmbuf = (u8 *)psetkeyparm;
1620         pcmd->cmdsz =  sizeof(struct setkey_parm);
1621         pcmd->rsp = NULL;
1622         pcmd->rspsz = 0;
1623         INIT_LIST_HEAD(&pcmd->list);
1624         return rtw_enqueue_cmd(pcmdpriv, pcmd);
1625 
1626 err_free_parm:
1627         kfree(psetkeyparm);
1628 err_free_cmd:
1629         kfree(pcmd);
1630         return res;
1631 }
1632 
1633 /* adjust ies for rtw_joinbss_cmd in WMM */
1634 int rtw_restruct_wmm_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_len, uint initial_out_len)
1635 {
1636         unsigned int ielength = 0;
1637         unsigned int i, j;
1638 
1639         /* i = 12; after the fixed IE */
1640         for (i = 12; i < in_len; i += (in_ie[i + 1] + 2) /* to the next IE element */) {
1641                 ielength = initial_out_len;
1642 
1643                 if (in_ie[i] == 0xDD && in_ie[i+2] == 0x00 && in_ie[i+3] == 0x50  && in_ie[i+4] == 0xF2 && in_ie[i+5] == 0x02 && i+5 < in_len) {
1644                         /* WMM element ID and OUI */
1645                         /* Append WMM IE to the last index of out_ie */
1646 
1647                         for (j = i; j < i + 9; j++) {
1648                                 out_ie[ielength] = in_ie[j];
1649                                 ielength++;
1650                         }
1651                         out_ie[initial_out_len + 1] = 0x07;
1652                         out_ie[initial_out_len + 6] = 0x00;
1653                         out_ie[initial_out_len + 8] = 0x00;
1654                         break;
1655                 }
1656         }
1657         return ielength;
1658 }
1659 
1660 /*
1661  * Ported from 8185: IsInPreAuthKeyList().
1662  * (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.)
1663  * Added by Annie, 2006-05-07.
1664  * Search by BSSID,
1665  * Return Value:
1666  *              -1      :if there is no pre-auth key in the table
1667  *              >= 0    :if there is pre-auth key, and return the entry id
1668  */
1669 static int SecIsInPMKIDList(struct adapter *Adapter, u8 *bssid)
1670 {
1671         struct security_priv *psecuritypriv = &Adapter->securitypriv;
1672         int i = 0;
1673 
1674         do {
1675                 if ((psecuritypriv->PMKIDList[i].bUsed) &&
1676                     (!memcmp(psecuritypriv->PMKIDList[i].Bssid, bssid, ETH_ALEN)))
1677                         break;
1678         } while (++i < NUM_PMKID_CACHE);
1679 
1680         if (i == NUM_PMKID_CACHE)
1681                 i = -1;/*  Could not find. */
1682 
1683         return i;
1684 }
1685 
1686 /*  */
1687 /*  Check the RSN IE length */
1688 /*  If the RSN IE length <= 20, the RSN IE didn't include the PMKID information */
1689 /*  0-11th element in the array are the fixed IE */
1690 /*  12th element in the array is the IE */
1691 /*  13th element in the array is the IE length */
1692 /*  */
1693 
1694 static int rtw_append_pmkid(struct adapter *Adapter, int iEntry, u8 *ie, uint ie_len)
1695 {
1696         struct security_priv *psecuritypriv = &Adapter->securitypriv;
1697 
1698         if (ie[13] <= 20) {
1699                 /*  The RSN IE didn't include the PMK ID, append the PMK information */
1700                 ie[ie_len] = 1;
1701                 ie_len++;
1702                 ie[ie_len] = 0; /* PMKID count = 0x0100 */
1703                 ie_len++;
1704                 memcpy(&ie[ie_len], &psecuritypriv->PMKIDList[iEntry].PMKID, 16);
1705 
1706                 ie_len += 16;
1707                 ie[13] += 18;/* PMKID length = 2+16 */
1708         }
1709         return ie_len;
1710 }
1711 
1712 int rtw_restruct_sec_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_len)
1713 {
1714         u8 authmode;
1715         uint ielength;
1716         int iEntry;
1717         struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1718         struct security_priv *psecuritypriv = &adapter->securitypriv;
1719         uint ndisauthmode = psecuritypriv->ndisauthtype;
1720         uint ndissecuritytype = psecuritypriv->ndisencryptstatus;
1721 
1722         RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
1723                  ("+%s: ndisauthmode=%d ndissecuritytype=%d\n", __func__,
1724                   ndisauthmode, ndissecuritytype));
1725 
1726         /* copy fixed ie only */
1727         memcpy(out_ie, in_ie, 12);
1728         ielength = 12;
1729         if ((ndisauthmode == Ndis802_11AuthModeWPA) ||
1730             (ndisauthmode == Ndis802_11AuthModeWPAPSK))
1731                 authmode = _WPA_IE_ID_;
1732         if ((ndisauthmode == Ndis802_11AuthModeWPA2) ||
1733             (ndisauthmode == Ndis802_11AuthModeWPA2PSK))
1734                 authmode = _WPA2_IE_ID_;
1735 
1736         if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) {
1737                 memcpy(out_ie+ielength, psecuritypriv->wps_ie, psecuritypriv->wps_ie_len);
1738 
1739                 ielength += psecuritypriv->wps_ie_len;
1740         } else if ((authmode == _WPA_IE_ID_) || (authmode == _WPA2_IE_ID_)) {
1741                 /* copy RSN or SSN */
1742                 memcpy(&out_ie[ielength], &psecuritypriv->supplicant_ie[0], psecuritypriv->supplicant_ie[1]+2);
1743                 ielength += psecuritypriv->supplicant_ie[1]+2;
1744                 rtw_report_sec_ie(adapter, authmode, psecuritypriv->supplicant_ie);
1745         }
1746 
1747         iEntry = SecIsInPMKIDList(adapter, pmlmepriv->assoc_bssid);
1748         if (iEntry >= 0 && authmode == _WPA2_IE_ID_)
1749                 ielength = rtw_append_pmkid(adapter, iEntry, out_ie, ielength);
1750 
1751         return ielength;
1752 }
1753 
1754 void rtw_init_registrypriv_dev_network(struct adapter *adapter)
1755 {
1756         struct registry_priv *pregistrypriv = &adapter->registrypriv;
1757         struct eeprom_priv *peepriv = &adapter->eeprompriv;
1758         struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
1759         u8 *myhwaddr = myid(peepriv);
1760 
1761         memcpy(pdev_network->MacAddress, myhwaddr, ETH_ALEN);
1762 
1763         memcpy(&pdev_network->ssid, &pregistrypriv->ssid, sizeof(struct ndis_802_11_ssid));
1764 
1765         pdev_network->Configuration.Length = sizeof(struct ndis_802_11_config);
1766         pdev_network->Configuration.BeaconPeriod = 100;
1767         pdev_network->Configuration.FHConfig.Length = 0;
1768         pdev_network->Configuration.FHConfig.HopPattern = 0;
1769         pdev_network->Configuration.FHConfig.HopSet = 0;
1770         pdev_network->Configuration.FHConfig.DwellTime = 0;
1771 }
1772 
1773 void rtw_update_registrypriv_dev_network(struct adapter *adapter)
1774 {
1775         int sz = 0;
1776         struct registry_priv *pregistrypriv = &adapter->registrypriv;
1777         struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
1778         struct security_priv *psecuritypriv = &adapter->securitypriv;
1779         struct wlan_network *cur_network = &adapter->mlmepriv.cur_network;
1780 
1781         pdev_network->Privacy = psecuritypriv->dot11PrivacyAlgrthm > 0 ? 1 : 0; /*  adhoc no 802.1x */
1782 
1783         pdev_network->Rssi = 0;
1784 
1785         switch (pregistrypriv->wireless_mode) {
1786         case WIRELESS_11B:
1787                 pdev_network->NetworkTypeInUse = Ndis802_11DS;
1788                 break;
1789         case WIRELESS_11G:
1790         case WIRELESS_11BG:
1791         case WIRELESS_11_24N:
1792         case WIRELESS_11G_24N:
1793         case WIRELESS_11BG_24N:
1794                 pdev_network->NetworkTypeInUse = Ndis802_11OFDM24;
1795                 break;
1796         default:
1797                 pdev_network->NetworkTypeInUse = Ndis802_11OFDM24;
1798                 break;
1799         }
1800 
1801         pdev_network->Configuration.DSConfig = pregistrypriv->channel;
1802         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
1803                  ("pregistrypriv->channel=%d, pdev_network->Configuration.DSConfig=0x%x\n",
1804                  pregistrypriv->channel, pdev_network->Configuration.DSConfig));
1805 
1806         if (cur_network->network.InfrastructureMode == Ndis802_11IBSS)
1807                 pdev_network->Configuration.ATIMWindow = 0;
1808 
1809         pdev_network->InfrastructureMode = cur_network->network.InfrastructureMode;
1810 
1811         /*  1. Supported rates */
1812         /*  2. IE */
1813 
1814         sz = rtw_generate_ie(pregistrypriv);
1815         pdev_network->ie_length = sz;
1816         pdev_network->Length = get_wlan_bssid_ex_sz((struct wlan_bssid_ex  *)pdev_network);
1817 
1818         /* notes: translate ie_length & Length after assign the Length to cmdsz in createbss_cmd(); */
1819         /* pdev_network->ie_length = cpu_to_le32(sz); */
1820 }
1821 
1822 void rtw_get_encrypt_decrypt_from_registrypriv(struct adapter *adapter)
1823 {
1824 }
1825 
1826 /* the function is at passive_level */
1827 void rtw_joinbss_reset(struct adapter *padapter)
1828 {
1829         u8 threshold;
1830         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1831         struct ht_priv *phtpriv = &pmlmepriv->htpriv;
1832 
1833         /* todo: if you want to do something io/reg/hw setting before join_bss, please add code here */
1834         pmlmepriv->num_FortyMHzIntolerant = 0;
1835 
1836         pmlmepriv->num_sta_no_ht = 0;
1837 
1838         phtpriv->ampdu_enable = false;/* reset to disabled */
1839 
1840         /*  TH = 1 => means that invalidate usb rx aggregation */
1841         /*  TH = 0 => means that validate usb rx aggregation, use init value. */
1842         if (phtpriv->ht_option) {
1843                 if (padapter->registrypriv.wifi_spec == 1)
1844                         threshold = 1;
1845                 else
1846                         threshold = 0;
1847                 rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, (u8 *)(&threshold));
1848         } else {
1849                 threshold = 1;
1850                 rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, (u8 *)(&threshold));
1851         }
1852 }
1853 
1854 /* the function is >= passive_level */
1855 unsigned int rtw_restructure_ht_ie(struct adapter *padapter, u8 *in_ie, u8 *out_ie, uint in_len, uint *pout_len)
1856 {
1857         u32 ielen, out_len;
1858         enum ht_cap_ampdu_factor max_rx_ampdu_factor;
1859         unsigned char *p;
1860         unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01, 0x00};
1861         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1862         struct qos_priv *pqospriv = &pmlmepriv->qospriv;
1863         struct ht_priv *phtpriv = &pmlmepriv->htpriv;
1864         u32 rx_packet_offset, max_recvbuf_sz;
1865 
1866         phtpriv->ht_option = false;
1867 
1868         p = rtw_get_ie(in_ie+12, _HT_CAPABILITY_IE_, &ielen, in_len-12);
1869 
1870         if (p && ielen > 0) {
1871                 struct ieee80211_ht_cap ht_cap;
1872 
1873                 if (pqospriv->qos_option == 0) {
1874                         out_len = *pout_len;
1875                         rtw_set_ie(out_ie + out_len, _VENDOR_SPECIFIC_IE_,
1876                                    _WMM_IE_Length_, WMM_IE, pout_len);
1877 
1878                         pqospriv->qos_option = 1;
1879                 }
1880 
1881                 out_len = *pout_len;
1882 
1883                 memset(&ht_cap, 0, sizeof(struct ieee80211_ht_cap));
1884 
1885                 ht_cap.cap_info = cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH |
1886                                               IEEE80211_HT_CAP_SGI_20 |
1887                                               IEEE80211_HT_CAP_SGI_40 |
1888                                               IEEE80211_HT_CAP_TX_STBC |
1889                                               IEEE80211_HT_CAP_DSSSCCK40);
1890 
1891                 rtw_hal_get_def_var(padapter, HAL_DEF_RX_PACKET_OFFSET, &rx_packet_offset);
1892                 rtw_hal_get_def_var(padapter, HAL_DEF_MAX_RECVBUF_SZ, &max_recvbuf_sz);
1893 
1894                 /*
1895                 ampdu_params_info [1:0]:Max AMPDU Len => 0:8k , 1:16k, 2:32k, 3:64k
1896                 ampdu_params_info [4:2]:Min MPDU Start Spacing
1897                 */
1898 
1899                 rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor);
1900                 ht_cap.ampdu_params_info = max_rx_ampdu_factor & 0x03;
1901 
1902                 if (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)
1903                         ht_cap.ampdu_params_info |= IEEE80211_HT_CAP_AMPDU_DENSITY & (0x07 << 2);
1904                 else
1905                         ht_cap.ampdu_params_info |= IEEE80211_HT_CAP_AMPDU_DENSITY & 0x00;
1906 
1907                 rtw_set_ie(out_ie+out_len, _HT_CAPABILITY_IE_,
1908                            sizeof(struct ieee80211_ht_cap),
1909                            (unsigned char *)&ht_cap, pout_len);
1910 
1911                 phtpriv->ht_option = true;
1912 
1913                 p = rtw_get_ie(in_ie+12, _HT_ADD_INFO_IE_, &ielen, in_len-12);
1914                 if (p && (ielen == sizeof(struct ieee80211_ht_addt_info))) {
1915                         out_len = *pout_len;
1916                         rtw_set_ie(out_ie+out_len, _HT_ADD_INFO_IE_, ielen, p+2, pout_len);
1917                 }
1918         }
1919         return phtpriv->ht_option;
1920 }
1921 
1922 /* the function is > passive_level (in critical_section) */
1923 void rtw_update_ht_cap(struct adapter *padapter, u8 *pie, uint ie_len)
1924 {
1925         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1926         struct ht_priv *phtpriv = &pmlmepriv->htpriv;
1927         struct registry_priv *pregistrypriv = &padapter->registrypriv;
1928         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1929         struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
1930 
1931         if (!phtpriv->ht_option)
1932                 return;
1933 
1934         if ((!pmlmeinfo->HT_info_enable) || (!pmlmeinfo->HT_caps_enable))
1935                 return;
1936 
1937         DBG_88E("+%s()\n", __func__);
1938 
1939         /* maybe needs check if ap supports rx ampdu. */
1940         if ((!phtpriv->ampdu_enable) && (pregistrypriv->ampdu_enable == 1)) {
1941                 if (pregistrypriv->wifi_spec == 1)
1942                         phtpriv->ampdu_enable = false;
1943                 else
1944                         phtpriv->ampdu_enable = true;
1945         } else if (pregistrypriv->ampdu_enable == 2) {
1946                 phtpriv->ampdu_enable = true;
1947         }
1948 
1949         /* update cur_bwmode & cur_ch_offset */
1950         if ((pregistrypriv->cbw40_enable) &&
1951             (le16_to_cpu(pmlmeinfo->HT_caps.cap_info) & BIT(1)) &&
1952             (pmlmeinfo->HT_info.infos[0] & BIT(2))) {
1953                 int i;
1954 
1955                 /* update the MCS rates */
1956                 for (i = 0; i < 16; i++)
1957                         ((u8 *)&pmlmeinfo->HT_caps.mcs)[i] &= MCS_rate_1R[i];
1958                 /* switch to the 40M Hz mode according to the AP */
1959                 pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_40;
1960                 switch ((pmlmeinfo->HT_info.infos[0] & 0x3)) {
1961                 case HT_EXTCHNL_OFFSET_UPPER:
1962                         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
1963                         break;
1964                 case HT_EXTCHNL_OFFSET_LOWER:
1965                         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
1966                         break;
1967                 default:
1968                         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
1969                         break;
1970                 }
1971         }
1972 
1973         /*  Config SM Power Save setting */
1974         pmlmeinfo->SM_PS = (le16_to_cpu(pmlmeinfo->HT_caps.cap_info) & 0x0C) >> 2;
1975         if (pmlmeinfo->SM_PS == WLAN_HT_CAP_SM_PS_STATIC)
1976                 DBG_88E("%s(): WLAN_HT_CAP_SM_PS_STATIC\n", __func__);
1977 
1978         /*  Config current HT Protection mode. */
1979         pmlmeinfo->HT_protection = pmlmeinfo->HT_info.infos[1] & 0x3;
1980 }
1981 
1982 void rtw_issue_addbareq_cmd(struct adapter *padapter, struct xmit_frame *pxmitframe)
1983 {
1984         u8 issued;
1985         int priority;
1986         struct sta_info *psta = NULL;
1987         struct ht_priv *phtpriv;
1988         struct pkt_attrib *pattrib = &pxmitframe->attrib;
1989 
1990         if (is_multicast_ether_addr(pattrib->ra) ||
1991             padapter->mlmepriv.LinkDetectInfo.NumTxOkInPeriod < 100)
1992                 return;
1993 
1994         priority = pattrib->priority;
1995 
1996         if (pattrib->psta)
1997                 psta = pattrib->psta;
1998         else
1999                 psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
2000 
2001         if (!psta)
2002                 return;
2003 
2004         phtpriv = &psta->htpriv;
2005 
2006         if ((phtpriv->ht_option) && (phtpriv->ampdu_enable)) {
2007                 issued = (phtpriv->agg_enable_bitmap >> priority) & 0x1;
2008                 issued |= (phtpriv->candidate_tid_bitmap >> priority) & 0x1;
2009 
2010                 if (issued == 0) {
2011                         DBG_88E("%s, p=%d\n", __func__, priority);
2012                         psta->htpriv.candidate_tid_bitmap |= BIT((u8)priority);
2013                         rtw_addbareq_cmd(padapter, (u8)priority, pattrib->ra);
2014                 }
2015         }
2016 }
2017 
2018 void rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network)
2019 {
2020         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2021 
2022         spin_lock_bh(&pmlmepriv->lock);
2023         _rtw_roaming(padapter, tgt_network);
2024         spin_unlock_bh(&pmlmepriv->lock);
2025 }
2026 
2027 void _rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network)
2028 {
2029         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2030         int do_join_r;
2031         struct wlan_network *pnetwork;
2032 
2033         if (tgt_network)
2034                 pnetwork = tgt_network;
2035         else
2036                 pnetwork = &pmlmepriv->cur_network;
2037 
2038         if (pmlmepriv->to_roaming > 0) {
2039                 DBG_88E("roaming from %s(%pM length:%d\n",
2040                         pnetwork->network.ssid.ssid, pnetwork->network.MacAddress,
2041                         pnetwork->network.ssid.ssid_length);
2042                 memcpy(&pmlmepriv->assoc_ssid, &pnetwork->network.ssid, sizeof(struct ndis_802_11_ssid));
2043 
2044                 pmlmepriv->assoc_by_bssid = false;
2045 
2046                 while (1) {
2047                         do_join_r = rtw_do_join(padapter);
2048                         if (do_join_r == _SUCCESS) {
2049                                 break;
2050                         }
2051                         DBG_88E("roaming do_join return %d\n", do_join_r);
2052                         pmlmepriv->to_roaming--;
2053 
2054                         if (pmlmepriv->to_roaming > 0) {
2055                                 continue;
2056                         } else {
2057                                 DBG_88E("%s(%d) -to roaming fail, indicate_disconnect\n", __func__, __LINE__);
2058                                 rtw_indicate_disconnect(padapter);
2059                                 break;
2060                         }
2061                 }
2062         }
2063 }

/* [<][>][^][v][top][bottom][index][help] */