]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8188eu/core/rtw_mlme_ext.c
ipc/msg.c: use freezable blocking call
[karo-tx-linux.git] / drivers / staging / rtl8188eu / core / rtw_mlme_ext.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_MLME_EXT_C_
21
22 #include <linux/ieee80211.h>
23
24 #include <osdep_service.h>
25 #include <drv_types.h>
26 #include <wifi.h>
27 #include <rtw_mlme_ext.h>
28 #include <wlan_bssdef.h>
29 #include <mlme_osdep.h>
30 #include <recv_osdep.h>
31
32 static u8 null_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
33
34 /**************************************************
35 OUI definitions for the vendor specific IE
36 ***************************************************/
37 unsigned char   RTW_WPA_OUI[] = {0x00, 0x50, 0xf2, 0x01};
38 unsigned char WMM_OUI[] = {0x00, 0x50, 0xf2, 0x02};
39 unsigned char   WPS_OUI[] = {0x00, 0x50, 0xf2, 0x04};
40 unsigned char   P2P_OUI[] = {0x50, 0x6F, 0x9A, 0x09};
41 unsigned char   WFD_OUI[] = {0x50, 0x6F, 0x9A, 0x0A};
42
43 unsigned char   WMM_INFO_OUI[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
44 unsigned char   WMM_PARA_OUI[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
45
46 unsigned char WPA_TKIP_CIPHER[4] = {0x00, 0x50, 0xf2, 0x02};
47 unsigned char RSN_TKIP_CIPHER[4] = {0x00, 0x0f, 0xac, 0x02};
48
49 extern unsigned char REALTEK_96B_IE[];
50
51 /********************************************************
52 MCS rate definitions
53 *********************************************************/
54 unsigned char   MCS_rate_2R[16] = {0xff, 0xff, 0x0, 0x0, 0x01, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
55 unsigned char   MCS_rate_1R[16] = {0xff, 0x00, 0x0, 0x0, 0x01, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
56
57 /********************************************************
58 ChannelPlan definitions
59 *********************************************************/
60 static struct rt_channel_plan_2g RTW_ChannelPlan2G[RT_CHANNEL_DOMAIN_2G_MAX] = {
61         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 13},              /*  0x00, RT_CHANNEL_DOMAIN_2G_WORLD , Passive scan CH 12, 13 */
62         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 13},              /*  0x01, RT_CHANNEL_DOMAIN_2G_ETSI1 */
63         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 11},                      /*  0x02, RT_CHANNEL_DOMAIN_2G_FCC1 */
64         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 14},  /*  0x03, RT_CHANNEL_DOMAIN_2G_MIKK1 */
65         {{10, 11, 12, 13}, 4},                                  /*  0x04, RT_CHANNEL_DOMAIN_2G_ETSI2 */
66         {{}, 0},                                                                        /*  0x05, RT_CHANNEL_DOMAIN_2G_NULL */
67 };
68
69 static struct rt_channel_plan_map       RTW_ChannelPlanMap[RT_CHANNEL_DOMAIN_MAX] = {
70         /*  0x00 ~ 0x1F , Old Define ===== */
71         {0x02}, /* 0x00, RT_CHANNEL_DOMAIN_FCC */
72         {0x02}, /* 0x01, RT_CHANNEL_DOMAIN_IC */
73         {0x01}, /* 0x02, RT_CHANNEL_DOMAIN_ETSI */
74         {0x01}, /* 0x03, RT_CHANNEL_DOMAIN_SPAIN */
75         {0x01}, /* 0x04, RT_CHANNEL_DOMAIN_FRANCE */
76         {0x03}, /* 0x05, RT_CHANNEL_DOMAIN_MKK */
77         {0x03}, /* 0x06, RT_CHANNEL_DOMAIN_MKK1 */
78         {0x01}, /* 0x07, RT_CHANNEL_DOMAIN_ISRAEL */
79         {0x03}, /* 0x08, RT_CHANNEL_DOMAIN_TELEC */
80         {0x03}, /* 0x09, RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN */
81         {0x00}, /* 0x0A, RT_CHANNEL_DOMAIN_WORLD_WIDE_13 */
82         {0x02}, /* 0x0B, RT_CHANNEL_DOMAIN_TAIWAN */
83         {0x01}, /* 0x0C, RT_CHANNEL_DOMAIN_CHINA */
84         {0x02}, /* 0x0D, RT_CHANNEL_DOMAIN_SINGAPORE_INDIA_MEXICO */
85         {0x02}, /* 0x0E, RT_CHANNEL_DOMAIN_KOREA */
86         {0x02}, /* 0x0F, RT_CHANNEL_DOMAIN_TURKEY */
87         {0x01}, /* 0x10, RT_CHANNEL_DOMAIN_JAPAN */
88         {0x02}, /* 0x11, RT_CHANNEL_DOMAIN_FCC_NO_DFS */
89         {0x01}, /* 0x12, RT_CHANNEL_DOMAIN_JAPAN_NO_DFS */
90         {0x00}, /* 0x13, RT_CHANNEL_DOMAIN_WORLD_WIDE_5G */
91         {0x02}, /* 0x14, RT_CHANNEL_DOMAIN_TAIWAN_NO_DFS */
92         {0x00}, /* 0x15, RT_CHANNEL_DOMAIN_ETSI_NO_DFS */
93         {0x00}, /* 0x16, RT_CHANNEL_DOMAIN_KOREA_NO_DFS */
94         {0x03}, /* 0x17, RT_CHANNEL_DOMAIN_JAPAN_NO_DFS */
95         {0x05}, /* 0x18, RT_CHANNEL_DOMAIN_PAKISTAN_NO_DFS */
96         {0x02}, /* 0x19, RT_CHANNEL_DOMAIN_TAIWAN2_NO_DFS */
97         {0x00}, /* 0x1A, */
98         {0x00}, /* 0x1B, */
99         {0x00}, /* 0x1C, */
100         {0x00}, /* 0x1D, */
101         {0x00}, /* 0x1E, */
102         {0x05}, /* 0x1F, RT_CHANNEL_DOMAIN_WORLD_WIDE_ONLY_5G */
103         /*  0x20 ~ 0x7F , New Define ===== */
104         {0x00}, /* 0x20, RT_CHANNEL_DOMAIN_WORLD_NULL */
105         {0x01}, /* 0x21, RT_CHANNEL_DOMAIN_ETSI1_NULL */
106         {0x02}, /* 0x22, RT_CHANNEL_DOMAIN_FCC1_NULL */
107         {0x03}, /* 0x23, RT_CHANNEL_DOMAIN_MKK1_NULL */
108         {0x04}, /* 0x24, RT_CHANNEL_DOMAIN_ETSI2_NULL */
109         {0x02}, /* 0x25, RT_CHANNEL_DOMAIN_FCC1_FCC1 */
110         {0x00}, /* 0x26, RT_CHANNEL_DOMAIN_WORLD_ETSI1 */
111         {0x03}, /* 0x27, RT_CHANNEL_DOMAIN_MKK1_MKK1 */
112         {0x00}, /* 0x28, RT_CHANNEL_DOMAIN_WORLD_KCC1 */
113         {0x00}, /* 0x29, RT_CHANNEL_DOMAIN_WORLD_FCC2 */
114         {0x00}, /* 0x2A, */
115         {0x00}, /* 0x2B, */
116         {0x00}, /* 0x2C, */
117         {0x00}, /* 0x2D, */
118         {0x00}, /* 0x2E, */
119         {0x00}, /* 0x2F, */
120         {0x00}, /* 0x30, RT_CHANNEL_DOMAIN_WORLD_FCC3 */
121         {0x00}, /* 0x31, RT_CHANNEL_DOMAIN_WORLD_FCC4 */
122         {0x00}, /* 0x32, RT_CHANNEL_DOMAIN_WORLD_FCC5 */
123         {0x00}, /* 0x33, RT_CHANNEL_DOMAIN_WORLD_FCC6 */
124         {0x02}, /* 0x34, RT_CHANNEL_DOMAIN_FCC1_FCC7 */
125         {0x00}, /* 0x35, RT_CHANNEL_DOMAIN_WORLD_ETSI2 */
126         {0x00}, /* 0x36, RT_CHANNEL_DOMAIN_WORLD_ETSI3 */
127         {0x03}, /* 0x37, RT_CHANNEL_DOMAIN_MKK1_MKK2 */
128         {0x03}, /* 0x38, RT_CHANNEL_DOMAIN_MKK1_MKK3 */
129         {0x02}, /* 0x39, RT_CHANNEL_DOMAIN_FCC1_NCC1 */
130         {0x00}, /* 0x3A, */
131         {0x00}, /* 0x3B, */
132         {0x00}, /* 0x3C, */
133         {0x00}, /* 0x3D, */
134         {0x00}, /* 0x3E, */
135         {0x00}, /* 0x3F, */
136         {0x02}, /* 0x40, RT_CHANNEL_DOMAIN_FCC1_NCC2 */
137         {0x03}, /* 0x41, RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN_2G */
138 };
139
140 static struct rt_channel_plan_map RTW_CHANNEL_PLAN_MAP_REALTEK_DEFINE = {0x03}; /* use the combination for max channel numbers */
141
142 /*
143  * Search the @param channel_num in given @param channel_set
144  * @ch_set: the given channel set
145  * @ch: the given channel number
146  *
147  * return the index of channel_num in channel_set, -1 if not found
148  */
149 int rtw_ch_set_search_ch(struct rt_channel_info *ch_set, const u32 ch)
150 {
151         int i;
152         for (i = 0; ch_set[i].ChannelNum != 0; i++) {
153                 if (ch == ch_set[i].ChannelNum)
154                         break;
155         }
156
157         if (i >= ch_set[i].ChannelNum)
158                 return -1;
159         return i;
160 }
161
162 struct xmit_frame *alloc_mgtxmitframe(struct xmit_priv *pxmitpriv)
163 {
164         struct xmit_frame                       *pmgntframe;
165         struct xmit_buf                         *pxmitbuf;
166
167         pmgntframe = rtw_alloc_xmitframe(pxmitpriv);
168         if (pmgntframe == NULL) {
169                 DBG_88E("%s, alloc xmitframe fail\n", __func__);
170                 return NULL;
171         }
172
173         pxmitbuf = rtw_alloc_xmitbuf_ext(pxmitpriv);
174         if (pxmitbuf == NULL) {
175                 DBG_88E("%s, alloc xmitbuf fail\n", __func__);
176                 rtw_free_xmitframe(pxmitpriv, pmgntframe);
177                 return NULL;
178         }
179         pmgntframe->frame_tag = MGNT_FRAMETAG;
180         pmgntframe->pxmitbuf = pxmitbuf;
181         pmgntframe->buf_addr = pxmitbuf->pbuf;
182         pxmitbuf->priv_data = pmgntframe;
183         return pmgntframe;
184 }
185
186 /****************************************************************************
187
188 Following are some TX functions for WiFi MLME
189
190 *****************************************************************************/
191
192 void update_mgnt_tx_rate(struct adapter *padapter, u8 rate)
193 {
194         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
195
196         pmlmeext->tx_rate = rate;
197         DBG_88E("%s(): rate = %x\n", __func__, rate);
198 }
199
200 void update_mgntframe_attrib(struct adapter *padapter, struct pkt_attrib *pattrib)
201 {
202         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
203
204         memset((u8 *)(pattrib), 0, sizeof(struct pkt_attrib));
205
206         pattrib->hdrlen = 24;
207         pattrib->nr_frags = 1;
208         pattrib->priority = 7;
209         pattrib->mac_id = 0;
210         pattrib->qsel = 0x12;
211
212         pattrib->pktlen = 0;
213
214         if (pmlmeext->cur_wireless_mode & WIRELESS_11B)
215                 pattrib->raid = 6;/* b mode */
216         else
217                 pattrib->raid = 5;/* a/g mode */
218
219         pattrib->encrypt = _NO_PRIVACY_;
220         pattrib->bswenc = false;
221
222         pattrib->qos_en = false;
223         pattrib->ht_en = false;
224         pattrib->bwmode = HT_CHANNEL_WIDTH_20;
225         pattrib->ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
226         pattrib->sgi = false;
227
228         pattrib->seqnum = pmlmeext->mgnt_seq;
229
230         pattrib->retry_ctrl = true;
231 }
232
233 static void dump_mgntframe(struct adapter *padapter,
234                            struct xmit_frame *pmgntframe)
235 {
236         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
237                 return;
238
239         rtw_hal_mgnt_xmit(padapter, pmgntframe);
240 }
241
242 static s32 dump_mgntframe_and_wait(struct adapter *padapter,
243                                    struct xmit_frame *pmgntframe,
244                                    int timeout_ms)
245 {
246         s32 ret = _FAIL;
247         struct xmit_buf *pxmitbuf = pmgntframe->pxmitbuf;
248         struct submit_ctx sctx;
249
250         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
251                 return ret;
252
253         rtw_sctx_init(&sctx, timeout_ms);
254         pxmitbuf->sctx = &sctx;
255
256         ret = rtw_hal_mgnt_xmit(padapter, pmgntframe);
257
258         if (ret == _SUCCESS)
259                 ret = rtw_sctx_wait(&sctx);
260
261         return ret;
262 }
263
264 static s32 dump_mgntframe_and_wait_ack(struct adapter *padapter,
265                                        struct xmit_frame *pmgntframe)
266 {
267         s32 ret = _FAIL;
268         u32 timeout_ms = 500;/*   500ms */
269         struct xmit_priv        *pxmitpriv = &padapter->xmitpriv;
270
271         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
272                 return -1;
273
274         if (mutex_lock_interruptible(&pxmitpriv->ack_tx_mutex))
275                 return _FAIL;
276         pxmitpriv->ack_tx = true;
277
278         pmgntframe->ack_report = 1;
279         if (rtw_hal_mgnt_xmit(padapter, pmgntframe) == _SUCCESS) {
280                 ret = rtw_ack_tx_wait(pxmitpriv, timeout_ms);
281         }
282
283         pxmitpriv->ack_tx = false;
284         mutex_unlock(&pxmitpriv->ack_tx_mutex);
285
286         return ret;
287 }
288
289 static int update_hidden_ssid(u8 *ies, u32 ies_len, u8 hidden_ssid_mode)
290 {
291         u8 *ssid_ie;
292         int ssid_len_ori;
293         int len_diff = 0;
294
295         ssid_ie = rtw_get_ie(ies,  WLAN_EID_SSID, &ssid_len_ori, ies_len);
296
297         if (ssid_ie && ssid_len_ori > 0) {
298                 switch (hidden_ssid_mode) {
299                 case 1: {
300                         u8 *next_ie = ssid_ie + 2 + ssid_len_ori;
301                         u32 remain_len = 0;
302
303                         remain_len = ies_len - (next_ie - ies);
304
305                         ssid_ie[1] = 0;
306                         memcpy(ssid_ie+2, next_ie, remain_len);
307                         len_diff -= ssid_len_ori;
308
309                         break;
310                 }
311                 case 2:
312                         memset(&ssid_ie[2], 0, ssid_len_ori);
313                         break;
314                 default:
315                         break;
316                 }
317         }
318
319         return len_diff;
320 }
321
322 static void issue_beacon(struct adapter *padapter, int timeout_ms)
323 {
324         struct xmit_frame       *pmgntframe;
325         struct pkt_attrib       *pattrib;
326         unsigned char   *pframe;
327         struct rtw_ieee80211_hdr *pwlanhdr;
328         __le16 *fctrl;
329         unsigned int    rate_len;
330         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
331         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
332         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
333         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
334         struct wlan_bssid_ex            *cur_network = &(pmlmeinfo->network);
335         u8      bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
336
337         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
338         if (pmgntframe == NULL) {
339                 DBG_88E("%s, alloc mgnt frame fail\n", __func__);
340                 return;
341         }
342 #if defined(CONFIG_88EU_AP_MODE)
343         spin_lock_bh(&pmlmepriv->bcn_update_lock);
344 #endif /* if defined (CONFIG_88EU_AP_MODE) */
345
346         /* update attribute */
347         pattrib = &pmgntframe->attrib;
348         update_mgntframe_attrib(padapter, pattrib);
349         pattrib->qsel = 0x10;
350
351         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
352
353         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
354         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
355
356
357         fctrl = &(pwlanhdr->frame_ctl);
358         *(fctrl) = 0;
359
360         memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN);
361         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
362         memcpy(pwlanhdr->addr3, cur_network->MacAddress, ETH_ALEN);
363
364         SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
365         /* pmlmeext->mgnt_seq++; */
366         SetFrameSubType(pframe, WIFI_BEACON);
367
368         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
369         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
370
371         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
372                 int len_diff;
373                 u8 *wps_ie;
374                 uint wps_ielen;
375                 u8 sr = 0;
376                 memcpy(pframe, cur_network->IEs, cur_network->IELength);
377                 len_diff = update_hidden_ssid(
378                         pframe+_BEACON_IE_OFFSET_
379                         , cur_network->IELength-_BEACON_IE_OFFSET_
380                         , pmlmeinfo->hidden_ssid_mode
381                         );
382                 pframe += (cur_network->IELength+len_diff);
383                 pattrib->pktlen += (cur_network->IELength+len_diff);
384                 wps_ie = rtw_get_wps_ie(pmgntframe->buf_addr+TXDESC_OFFSET+sizeof(struct rtw_ieee80211_hdr_3addr)+_BEACON_IE_OFFSET_,
385                         pattrib->pktlen-sizeof(struct rtw_ieee80211_hdr_3addr)-_BEACON_IE_OFFSET_, NULL, &wps_ielen);
386                 if (wps_ie && wps_ielen > 0)
387                         rtw_get_wps_attr_content(wps_ie,  wps_ielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL);
388                 if (sr != 0)
389                         set_fwstate(pmlmepriv, WIFI_UNDER_WPS);
390                 else
391                         _clr_fwstate_(pmlmepriv, WIFI_UNDER_WPS);
392
393                 goto _issue_bcn;
394         }
395
396         /* below for ad-hoc mode */
397
398         /* timestamp will be inserted by hardware */
399         pframe += 8;
400         pattrib->pktlen += 8;
401
402         /*  beacon interval: 2 bytes */
403
404         memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2);
405
406         pframe += 2;
407         pattrib->pktlen += 2;
408
409         /*  capability info: 2 bytes */
410
411         memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2);
412
413         pframe += 2;
414         pattrib->pktlen += 2;
415
416         /*  SSID */
417         pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pattrib->pktlen);
418
419         /*  supported rates... */
420         rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
421         pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, min_t(unsigned int, rate_len, 8), cur_network->SupportedRates, &pattrib->pktlen);
422
423         /*  DS parameter set */
424         pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pattrib->pktlen);
425
426         {
427                 u8 erpinfo = 0;
428                 u32 ATIMWindow;
429                 /*  IBSS Parameter Set... */
430                 ATIMWindow = 0;
431                 pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pattrib->pktlen);
432
433                 /* ERP IE */
434                 pframe = rtw_set_ie(pframe, _ERPINFO_IE_, 1, &erpinfo, &pattrib->pktlen);
435         }
436
437         /*  EXTERNDED SUPPORTED RATE */
438         if (rate_len > 8)
439                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pattrib->pktlen);
440         /* todo:HT for adhoc */
441 _issue_bcn:
442
443 #if defined(CONFIG_88EU_AP_MODE)
444         pmlmepriv->update_bcn = false;
445
446         spin_unlock_bh(&pmlmepriv->bcn_update_lock);
447 #endif /* if defined (CONFIG_88EU_AP_MODE) */
448
449         if ((pattrib->pktlen + TXDESC_SIZE) > 512) {
450                 DBG_88E("beacon frame too large\n");
451                 return;
452         }
453
454         pattrib->last_txcmdsz = pattrib->pktlen;
455
456         /* DBG_88E("issue bcn_sz=%d\n", pattrib->last_txcmdsz); */
457         if (timeout_ms > 0)
458                 dump_mgntframe_and_wait(padapter, pmgntframe, timeout_ms);
459         else
460                 dump_mgntframe(padapter, pmgntframe);
461 }
462
463 static void issue_probersp(struct adapter *padapter, unsigned char *da)
464 {
465         struct xmit_frame                       *pmgntframe;
466         struct pkt_attrib                       *pattrib;
467         unsigned char                                   *pframe;
468         struct rtw_ieee80211_hdr        *pwlanhdr;
469         __le16 *fctrl;
470         unsigned char                                   *mac, *bssid;
471         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
472 #if defined(CONFIG_88EU_AP_MODE)
473         u8 *pwps_ie;
474         uint wps_ielen;
475         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
476 #endif /* if defined (CONFIG_88EU_AP_MODE) */
477         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
478         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
479         struct wlan_bssid_ex            *cur_network = &(pmlmeinfo->network);
480         unsigned int    rate_len;
481
482         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
483         if (pmgntframe == NULL) {
484                 DBG_88E("%s, alloc mgnt frame fail\n", __func__);
485                 return;
486         }
487
488         /* update attribute */
489         pattrib = &pmgntframe->attrib;
490         update_mgntframe_attrib(padapter, pattrib);
491
492         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
493
494         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
495         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
496
497         mac = myid(&(padapter->eeprompriv));
498         bssid = cur_network->MacAddress;
499
500         fctrl = &(pwlanhdr->frame_ctl);
501         *(fctrl) = 0;
502         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
503         memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
504         memcpy(pwlanhdr->addr3, bssid, ETH_ALEN);
505
506         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
507         pmlmeext->mgnt_seq++;
508         SetFrameSubType(fctrl, WIFI_PROBERSP);
509
510         pattrib->hdrlen = sizeof(struct rtw_ieee80211_hdr_3addr);
511         pattrib->pktlen = pattrib->hdrlen;
512         pframe += pattrib->hdrlen;
513
514         if (cur_network->IELength > MAX_IE_SZ)
515                 return;
516
517 #if defined(CONFIG_88EU_AP_MODE)
518         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
519                 pwps_ie = rtw_get_wps_ie(cur_network->IEs+_FIXED_IE_LENGTH_, cur_network->IELength-_FIXED_IE_LENGTH_, NULL, &wps_ielen);
520
521                 /* inerset & update wps_probe_resp_ie */
522                 if ((pmlmepriv->wps_probe_resp_ie != NULL) && pwps_ie && (wps_ielen > 0)) {
523                         uint wps_offset, remainder_ielen;
524                         u8 *premainder_ie;
525
526                         wps_offset = (uint)(pwps_ie - cur_network->IEs);
527
528                         premainder_ie = pwps_ie + wps_ielen;
529
530                         remainder_ielen = cur_network->IELength - wps_offset - wps_ielen;
531
532                         memcpy(pframe, cur_network->IEs, wps_offset);
533                         pframe += wps_offset;
534                         pattrib->pktlen += wps_offset;
535
536                         wps_ielen = (uint)pmlmepriv->wps_probe_resp_ie[1];/* to get ie data len */
537                         if ((wps_offset+wps_ielen+2) <= MAX_IE_SZ) {
538                                 memcpy(pframe, pmlmepriv->wps_probe_resp_ie, wps_ielen+2);
539                                 pframe += wps_ielen+2;
540                                 pattrib->pktlen += wps_ielen+2;
541                         }
542
543                         if ((wps_offset+wps_ielen+2+remainder_ielen) <= MAX_IE_SZ) {
544                                 memcpy(pframe, premainder_ie, remainder_ielen);
545                                 pframe += remainder_ielen;
546                                 pattrib->pktlen += remainder_ielen;
547                         }
548                 } else {
549                         memcpy(pframe, cur_network->IEs, cur_network->IELength);
550                         pframe += cur_network->IELength;
551                         pattrib->pktlen += cur_network->IELength;
552                 }
553         } else
554 #endif
555         {
556                 /* timestamp will be inserted by hardware */
557                 pframe += 8;
558                 pattrib->pktlen += 8;
559
560                 /*  beacon interval: 2 bytes */
561
562                 memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2);
563
564                 pframe += 2;
565                 pattrib->pktlen += 2;
566
567                 /*  capability info: 2 bytes */
568
569                 memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2);
570
571                 pframe += 2;
572                 pattrib->pktlen += 2;
573
574                 /* below for ad-hoc mode */
575
576                 /*  SSID */
577                 pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pattrib->pktlen);
578
579                 /*  supported rates... */
580                 rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
581                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, min_t(unsigned int, rate_len, 8), cur_network->SupportedRates, &pattrib->pktlen);
582
583                 /*  DS parameter set */
584                 pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pattrib->pktlen);
585
586                 if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
587                         u8 erpinfo = 0;
588                         u32 ATIMWindow;
589                         /*  IBSS Parameter Set... */
590                         /* ATIMWindow = cur->Configuration.ATIMWindow; */
591                         ATIMWindow = 0;
592                         pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pattrib->pktlen);
593
594                         /* ERP IE */
595                         pframe = rtw_set_ie(pframe, _ERPINFO_IE_, 1, &erpinfo, &pattrib->pktlen);
596                 }
597
598
599                 /*  EXTERNDED SUPPORTED RATE */
600                 if (rate_len > 8)
601                         pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pattrib->pktlen);
602                 /* todo:HT for adhoc */
603         }
604
605         pattrib->last_txcmdsz = pattrib->pktlen;
606
607         dump_mgntframe(padapter, pmgntframe);
608
609         return;
610 }
611
612 static int issue_probereq(struct adapter *padapter, struct ndis_802_11_ssid *pssid, u8 *da, bool wait_ack)
613 {
614         int ret = _FAIL;
615         struct xmit_frame               *pmgntframe;
616         struct pkt_attrib               *pattrib;
617         unsigned char                   *pframe;
618         struct rtw_ieee80211_hdr        *pwlanhdr;
619         __le16 *fctrl;
620         unsigned char                   *mac;
621         unsigned char                   bssrate[NumRates];
622         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
623         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
624         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
625         int     bssrate_len = 0;
626         u8      bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
627
628         RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_, ("+issue_probereq\n"));
629
630         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
631         if (pmgntframe == NULL)
632                 goto exit;
633
634         /* update attribute */
635         pattrib = &pmgntframe->attrib;
636         update_mgntframe_attrib(padapter, pattrib);
637
638
639         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
640
641         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
642         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
643
644         mac = myid(&(padapter->eeprompriv));
645
646         fctrl = &(pwlanhdr->frame_ctl);
647         *(fctrl) = 0;
648
649         if (da) {
650                 /*      unicast probe request frame */
651                 memcpy(pwlanhdr->addr1, da, ETH_ALEN);
652                 memcpy(pwlanhdr->addr3, da, ETH_ALEN);
653         } else {
654                 /*      broadcast probe request frame */
655                 memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN);
656                 memcpy(pwlanhdr->addr3, bc_addr, ETH_ALEN);
657         }
658
659         memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
660
661         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
662         pmlmeext->mgnt_seq++;
663         SetFrameSubType(pframe, WIFI_PROBEREQ);
664
665         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
666         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
667
668         if (pssid)
669                 pframe = rtw_set_ie(pframe, _SSID_IE_, pssid->SsidLength, pssid->Ssid, &(pattrib->pktlen));
670         else
671                 pframe = rtw_set_ie(pframe, _SSID_IE_, 0, NULL, &(pattrib->pktlen));
672
673         get_rate_set(padapter, bssrate, &bssrate_len);
674
675         if (bssrate_len > 8) {
676                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_ , 8, bssrate, &(pattrib->pktlen));
677                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_ , (bssrate_len - 8), (bssrate + 8), &(pattrib->pktlen));
678         } else {
679                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_ , bssrate_len , bssrate, &(pattrib->pktlen));
680         }
681
682         /* add wps_ie for wps2.0 */
683         if (pmlmepriv->wps_probe_req_ie_len > 0 && pmlmepriv->wps_probe_req_ie) {
684                 memcpy(pframe, pmlmepriv->wps_probe_req_ie, pmlmepriv->wps_probe_req_ie_len);
685                 pframe += pmlmepriv->wps_probe_req_ie_len;
686                 pattrib->pktlen += pmlmepriv->wps_probe_req_ie_len;
687         }
688
689         pattrib->last_txcmdsz = pattrib->pktlen;
690
691         RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
692                  ("issuing probe_req, tx_len=%d\n", pattrib->last_txcmdsz));
693
694         if (wait_ack) {
695                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
696         } else {
697                 dump_mgntframe(padapter, pmgntframe);
698                 ret = _SUCCESS;
699         }
700
701 exit:
702         return ret;
703 }
704
705 static int issue_probereq_ex(struct adapter *padapter,
706                              struct ndis_802_11_ssid *pssid, u8 *da,
707                              int try_cnt, int wait_ms)
708 {
709         int ret;
710         int i = 0;
711         unsigned long start = jiffies;
712
713         do {
714                 ret = issue_probereq(padapter, pssid, da, wait_ms > 0 ? true : false);
715
716                 i++;
717
718                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
719                         break;
720
721                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
722                         msleep(wait_ms);
723
724         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
725
726         if (ret != _FAIL) {
727                 ret = _SUCCESS;
728                 goto exit;
729         }
730
731         if (try_cnt && wait_ms) {
732                 if (da)
733                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
734                                 FUNC_ADPT_ARG(padapter), da, rtw_get_oper_ch(padapter),
735                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt,
736                                 jiffies_to_msecs(jiffies - start));
737                 else
738                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
739                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
740                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt,
741                                 jiffies_to_msecs(jiffies - start));
742         }
743 exit:
744         return ret;
745 }
746
747 /*  if psta == NULL, indicate we are station(client) now... */
748 static void issue_auth(struct adapter *padapter, struct sta_info *psta,
749                        unsigned short status)
750 {
751         struct xmit_frame *pmgntframe;
752         struct pkt_attrib *pattrib;
753         unsigned char *pframe;
754         struct rtw_ieee80211_hdr *pwlanhdr;
755         __le16 *fctrl;
756         unsigned int val32;
757         u16 val16;
758 #ifdef CONFIG_88EU_AP_MODE
759         __le16 le_val16;
760 #endif
761         int use_shared_key = 0;
762         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
763         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
764         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
765         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
766
767         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
768         if (pmgntframe == NULL)
769                 return;
770
771         /* update attribute */
772         pattrib = &pmgntframe->attrib;
773         update_mgntframe_attrib(padapter, pattrib);
774
775         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
776
777         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
778         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
779
780         fctrl = &(pwlanhdr->frame_ctl);
781         *(fctrl) = 0;
782
783         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
784         pmlmeext->mgnt_seq++;
785         SetFrameSubType(pframe, WIFI_AUTH);
786
787         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
788         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
789
790
791         if (psta) {/*  for AP mode */
792 #ifdef CONFIG_88EU_AP_MODE
793
794                 memcpy(pwlanhdr->addr1, psta->hwaddr, ETH_ALEN);
795                 memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
796                 memcpy(pwlanhdr->addr3, myid(&(padapter->eeprompriv)), ETH_ALEN);
797
798
799                 /*  setting auth algo number */
800                 val16 = (u16)psta->authalg;
801
802                 if (status != _STATS_SUCCESSFUL_)
803                         val16 = 0;
804
805                 if (val16) {
806                         le_val16 = cpu_to_le16(val16);
807                         use_shared_key = 1;
808                 } else {
809                         le_val16 = 0;
810                 }
811
812                 pframe = rtw_set_fixed_ie(pframe, _AUTH_ALGM_NUM_, &le_val16,
813                                           &pattrib->pktlen);
814
815                 /*  setting auth seq number */
816                 val16 = (u16)psta->auth_seq;
817                 le_val16 = cpu_to_le16(val16);
818                 pframe = rtw_set_fixed_ie(pframe, _AUTH_SEQ_NUM_, &le_val16,
819                                           &pattrib->pktlen);
820
821                 /*  setting status code... */
822                 val16 = status;
823                 le_val16 = cpu_to_le16(val16);
824                 pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_,
825                                           &le_val16, &pattrib->pktlen);
826
827                 /*  added challenging text... */
828                 if ((psta->auth_seq == 2) && (psta->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1))
829                         pframe = rtw_set_ie(pframe, _CHLGETXT_IE_, 128, psta->chg_txt, &(pattrib->pktlen));
830 #endif
831         } else {
832                 __le32 le_tmp32;
833                 __le16 le_tmp16;
834                 memcpy(pwlanhdr->addr1, pnetwork->MacAddress, ETH_ALEN);
835                 memcpy(pwlanhdr->addr2, myid(&padapter->eeprompriv), ETH_ALEN);
836                 memcpy(pwlanhdr->addr3, pnetwork->MacAddress, ETH_ALEN);
837
838                 /*  setting auth algo number */
839                 val16 = (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared) ? 1 : 0;/*  0:OPEN System, 1:Shared key */
840                 if (val16)
841                         use_shared_key = 1;
842
843                 /* setting IV for auth seq #3 */
844                 if ((pmlmeinfo->auth_seq == 3) && (pmlmeinfo->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1)) {
845                         val32 = (pmlmeinfo->iv++) | (pmlmeinfo->key_index << 30);
846                         le_tmp32 = cpu_to_le32(val32);
847                         pframe = rtw_set_fixed_ie(pframe, 4, &le_tmp32,
848                                                   &pattrib->pktlen);
849
850                         pattrib->iv_len = 4;
851                 }
852
853                 le_tmp16 = cpu_to_le16(val16);
854                 pframe = rtw_set_fixed_ie(pframe, _AUTH_ALGM_NUM_, &le_tmp16,
855                                           &pattrib->pktlen);
856
857                 /*  setting auth seq number */
858                 val16 = pmlmeinfo->auth_seq;
859                 le_tmp16 = cpu_to_le16(val16);
860                 pframe = rtw_set_fixed_ie(pframe, _AUTH_SEQ_NUM_, &le_tmp16,
861                                           &pattrib->pktlen);
862
863
864                 /*  setting status code... */
865                 le_tmp16 = cpu_to_le16(status);
866                 pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_, &le_tmp16,
867                                           &pattrib->pktlen);
868
869                 /*  then checking to see if sending challenging text... */
870                 if ((pmlmeinfo->auth_seq == 3) && (pmlmeinfo->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1)) {
871                         pframe = rtw_set_ie(pframe, _CHLGETXT_IE_, 128, pmlmeinfo->chg_txt, &(pattrib->pktlen));
872
873                         SetPrivacy(fctrl);
874
875                         pattrib->hdrlen = sizeof(struct rtw_ieee80211_hdr_3addr);
876
877                         pattrib->encrypt = _WEP40_;
878
879                         pattrib->icv_len = 4;
880
881                         pattrib->pktlen += pattrib->icv_len;
882                 }
883         }
884
885         pattrib->last_txcmdsz = pattrib->pktlen;
886
887         rtw_wep_encrypt(padapter, (u8 *)pmgntframe);
888         DBG_88E("%s\n", __func__);
889         dump_mgntframe(padapter, pmgntframe);
890
891         return;
892 }
893
894
895 #ifdef CONFIG_88EU_AP_MODE
896 static void issue_asocrsp(struct adapter *padapter, unsigned short status,
897                           struct sta_info *pstat, int pkt_type)
898 {
899         struct xmit_frame       *pmgntframe;
900         struct rtw_ieee80211_hdr        *pwlanhdr;
901         struct pkt_attrib *pattrib;
902         unsigned char   *pbuf, *pframe;
903         unsigned short val;
904         __le16 *fctrl;
905         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
906         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
907         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
908         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
909         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
910         u8 *ie = pnetwork->IEs;
911         __le16 lestatus, leval;
912
913         DBG_88E("%s\n", __func__);
914
915         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
916         if (pmgntframe == NULL)
917                 return;
918
919         /* update attribute */
920         pattrib = &pmgntframe->attrib;
921         update_mgntframe_attrib(padapter, pattrib);
922
923
924         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
925
926         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
927         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
928
929         fctrl = &(pwlanhdr->frame_ctl);
930         *(fctrl) = 0;
931
932         memcpy((void *)GetAddr1Ptr(pwlanhdr), pstat->hwaddr, ETH_ALEN);
933         memcpy((void *)GetAddr2Ptr(pwlanhdr), myid(&(padapter->eeprompriv)), ETH_ALEN);
934         memcpy((void *)GetAddr3Ptr(pwlanhdr), pnetwork->MacAddress, ETH_ALEN);
935
936
937         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
938         pmlmeext->mgnt_seq++;
939         if ((pkt_type == WIFI_ASSOCRSP) || (pkt_type == WIFI_REASSOCRSP))
940                 SetFrameSubType(pwlanhdr, pkt_type);
941         else
942                 return;
943
944         pattrib->hdrlen = sizeof(struct rtw_ieee80211_hdr_3addr);
945         pattrib->pktlen += pattrib->hdrlen;
946         pframe += pattrib->hdrlen;
947
948         /* capability */
949         val = *(unsigned short *)rtw_get_capability_from_ie(ie);
950
951         pframe = rtw_set_fixed_ie(pframe, _CAPABILITY_, &val, &pattrib->pktlen);
952
953         lestatus = cpu_to_le16(status);
954         pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_, &lestatus,
955                                   &pattrib->pktlen);
956
957         leval = cpu_to_le16(pstat->aid | BIT(14) | BIT(15));
958         pframe = rtw_set_fixed_ie(pframe, _ASOC_ID_, &leval, &pattrib->pktlen);
959
960         if (pstat->bssratelen <= 8) {
961                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, pstat->bssratelen, pstat->bssrateset, &(pattrib->pktlen));
962         } else {
963                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, pstat->bssrateset, &(pattrib->pktlen));
964                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (pstat->bssratelen-8), pstat->bssrateset+8, &(pattrib->pktlen));
965         }
966
967         if ((pstat->flags & WLAN_STA_HT) && (pmlmepriv->htpriv.ht_option)) {
968                 uint ie_len = 0;
969
970                 /* FILL HT CAP INFO IE */
971                 pbuf = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_CAPABILITY_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
972                 if (pbuf && ie_len > 0) {
973                         memcpy(pframe, pbuf, ie_len+2);
974                         pframe += (ie_len+2);
975                         pattrib->pktlen += (ie_len+2);
976                 }
977
978                 /* FILL HT ADD INFO IE */
979                 pbuf = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_ADD_INFO_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
980                 if (pbuf && ie_len > 0) {
981                         memcpy(pframe, pbuf, ie_len+2);
982                         pframe += (ie_len+2);
983                         pattrib->pktlen += (ie_len+2);
984                 }
985         }
986
987         /* FILL WMM IE */
988         if ((pstat->flags & WLAN_STA_WME) && (pmlmepriv->qospriv.qos_option)) {
989                 uint ie_len = 0;
990                 unsigned char WMM_PARA_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
991
992                 for (pbuf = ie + _BEACON_IE_OFFSET_;; pbuf += (ie_len + 2)) {
993                         pbuf = rtw_get_ie(pbuf, _VENDOR_SPECIFIC_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_ - (ie_len + 2)));
994                         if (pbuf && !memcmp(pbuf+2, WMM_PARA_IE, 6)) {
995                                 memcpy(pframe, pbuf, ie_len+2);
996                                 pframe += (ie_len+2);
997                                 pattrib->pktlen += (ie_len+2);
998                                 break;
999                         }
1000
1001                         if ((pbuf == NULL) || (ie_len == 0))
1002                                 break;
1003                 }
1004         }
1005
1006         if (pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_REALTEK)
1007                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, 6 , REALTEK_96B_IE, &(pattrib->pktlen));
1008
1009         /* add WPS IE ie for wps 2.0 */
1010         if (pmlmepriv->wps_assoc_resp_ie && pmlmepriv->wps_assoc_resp_ie_len > 0) {
1011                 memcpy(pframe, pmlmepriv->wps_assoc_resp_ie, pmlmepriv->wps_assoc_resp_ie_len);
1012
1013                 pframe += pmlmepriv->wps_assoc_resp_ie_len;
1014                 pattrib->pktlen += pmlmepriv->wps_assoc_resp_ie_len;
1015         }
1016
1017         pattrib->last_txcmdsz = pattrib->pktlen;
1018         dump_mgntframe(padapter, pmgntframe);
1019 }
1020 #endif /* CONFIG_88EU_AP_MODE */
1021
1022 static void issue_assocreq(struct adapter *padapter)
1023 {
1024         int ret = _FAIL;
1025         struct xmit_frame       *pmgntframe;
1026         struct pkt_attrib       *pattrib;
1027         unsigned char           *pframe, *p;
1028         struct rtw_ieee80211_hdr        *pwlanhdr;
1029         __le16 *fctrl;
1030         __le16          le_tmp;
1031         unsigned int    i, j, ie_len, index = 0;
1032         unsigned char   rf_type, bssrate[NumRates], sta_bssrate[NumRates];
1033         struct ndis_802_11_var_ie *pIE;
1034         struct registry_priv    *pregpriv = &padapter->registrypriv;
1035         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
1036         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1037         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1038         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1039         int     bssrate_len = 0, sta_bssrate_len = 0;
1040         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
1041
1042         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1043         if (pmgntframe == NULL)
1044                 goto exit;
1045
1046         /* update attribute */
1047         pattrib = &pmgntframe->attrib;
1048         update_mgntframe_attrib(padapter, pattrib);
1049
1050         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1051         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1052         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1053
1054         fctrl = &(pwlanhdr->frame_ctl);
1055         *(fctrl) = 0;
1056         memcpy(pwlanhdr->addr1, pnetwork->MacAddress, ETH_ALEN);
1057         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1058         memcpy(pwlanhdr->addr3, pnetwork->MacAddress, ETH_ALEN);
1059
1060         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1061         pmlmeext->mgnt_seq++;
1062         SetFrameSubType(pframe, WIFI_ASSOCREQ);
1063
1064         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1065         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1066
1067         /* caps */
1068
1069         memcpy(pframe, rtw_get_capability_from_ie(pmlmeinfo->network.IEs), 2);
1070
1071         pframe += 2;
1072         pattrib->pktlen += 2;
1073
1074         /* listen interval */
1075         /* todo: listen interval for power saving */
1076         le_tmp = cpu_to_le16(3);
1077         memcpy(pframe , (unsigned char *)&le_tmp, 2);
1078         pframe += 2;
1079         pattrib->pktlen += 2;
1080
1081         /* SSID */
1082         pframe = rtw_set_ie(pframe, _SSID_IE_,  pmlmeinfo->network.Ssid.SsidLength, pmlmeinfo->network.Ssid.Ssid, &(pattrib->pktlen));
1083
1084         /* supported rate & extended supported rate */
1085
1086         /*  Check if the AP's supported rates are also supported by STA. */
1087         get_rate_set(padapter, sta_bssrate, &sta_bssrate_len);
1088
1089         if (pmlmeext->cur_channel == 14)/*  for JAPAN, channel 14 can only uses B Mode(CCK) */
1090                 sta_bssrate_len = 4;
1091
1092         for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) {
1093                 if (pmlmeinfo->network.SupportedRates[i] == 0)
1094                         break;
1095                 DBG_88E("network.SupportedRates[%d]=%02X\n", i, pmlmeinfo->network.SupportedRates[i]);
1096         }
1097
1098         for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) {
1099                 if (pmlmeinfo->network.SupportedRates[i] == 0)
1100                         break;
1101
1102                 /*  Check if the AP's supported rates are also supported by STA. */
1103                 for (j = 0; j < sta_bssrate_len; j++) {
1104                         /*  Avoid the proprietary data rate (22Mbps) of Handlink WSG-4000 AP */
1105                         if ((pmlmeinfo->network.SupportedRates[i]|IEEE80211_BASIC_RATE_MASK)
1106                                         == (sta_bssrate[j]|IEEE80211_BASIC_RATE_MASK))
1107                                 break;
1108                 }
1109
1110                 if (j == sta_bssrate_len) {
1111                         /*  the rate is not supported by STA */
1112                         DBG_88E("%s(): the rate[%d]=%02X is not supported by STA!\n", __func__, i, pmlmeinfo->network.SupportedRates[i]);
1113                 } else {
1114                         /*  the rate is supported by STA */
1115                         bssrate[index++] = pmlmeinfo->network.SupportedRates[i];
1116                 }
1117         }
1118
1119         bssrate_len = index;
1120         DBG_88E("bssrate_len=%d\n", bssrate_len);
1121
1122         if (bssrate_len == 0) {
1123                 rtw_free_xmitbuf(pxmitpriv, pmgntframe->pxmitbuf);
1124                 rtw_free_xmitframe(pxmitpriv, pmgntframe);
1125                 goto exit; /* don't connect to AP if no joint supported rate */
1126         }
1127
1128
1129         if (bssrate_len > 8) {
1130                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_ , 8, bssrate, &(pattrib->pktlen));
1131                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_ , (bssrate_len - 8), (bssrate + 8), &(pattrib->pktlen));
1132         } else {
1133                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_ , bssrate_len , bssrate, &(pattrib->pktlen));
1134         }
1135
1136         /* RSN */
1137         p = rtw_get_ie((pmlmeinfo->network.IEs + sizeof(struct ndis_802_11_fixed_ie)), _RSN_IE_2_, &ie_len, (pmlmeinfo->network.IELength - sizeof(struct ndis_802_11_fixed_ie)));
1138         if (p != NULL)
1139                 pframe = rtw_set_ie(pframe, _RSN_IE_2_, ie_len, (p + 2), &(pattrib->pktlen));
1140
1141         /* HT caps */
1142         if (padapter->mlmepriv.htpriv.ht_option) {
1143                 p = rtw_get_ie((pmlmeinfo->network.IEs + sizeof(struct ndis_802_11_fixed_ie)), _HT_CAPABILITY_IE_, &ie_len, (pmlmeinfo->network.IELength - sizeof(struct ndis_802_11_fixed_ie)));
1144                 if ((p != NULL) && (!(is_ap_in_tkip(padapter)))) {
1145                         memcpy(&(pmlmeinfo->HT_caps), (p + 2), sizeof(struct HT_caps_element));
1146
1147                         /* to disable 40M Hz support while gd_bw_40MHz_en = 0 */
1148                         if (pregpriv->cbw40_enable == 0)
1149                                 pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info &= cpu_to_le16(~(BIT(6) | BIT(1)));
1150                         else
1151                                 pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info |= cpu_to_le16(BIT(1));
1152
1153                         /* todo: disable SM power save mode */
1154                         pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info |= cpu_to_le16(0x000c);
1155
1156                         rtw_hal_get_hwreg(padapter, HW_VAR_RF_TYPE, (u8 *)(&rf_type));
1157                         switch (rf_type) {
1158                         case RF_1T1R:
1159                                 if (pregpriv->rx_stbc)
1160                                         pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info |= cpu_to_le16(0x0100);/* RX STBC One spatial stream */
1161                                 memcpy(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_rate_1R, 16);
1162                                 break;
1163                         case RF_2T2R:
1164                         case RF_1T2R:
1165                         default:
1166                                 if ((pregpriv->rx_stbc == 0x3) ||/* enable for 2.4/5 GHz */
1167                                     ((pmlmeext->cur_wireless_mode & WIRELESS_11_24N) && (pregpriv->rx_stbc == 0x1)) || /* enable for 2.4GHz */
1168                                     (pregpriv->wifi_spec == 1)) {
1169                                         DBG_88E("declare supporting RX STBC\n");
1170                                         pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info |= cpu_to_le16(0x0200);/* RX STBC two spatial stream */
1171                                 }
1172                                 memcpy(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_rate_2R, 16);
1173                                 break;
1174                         }
1175                         pframe = rtw_set_ie(pframe, _HT_CAPABILITY_IE_, ie_len , (u8 *)(&(pmlmeinfo->HT_caps)), &(pattrib->pktlen));
1176                 }
1177         }
1178
1179         /* vendor specific IE, such as WPA, WMM, WPS */
1180         for (i = sizeof(struct ndis_802_11_fixed_ie); i < pmlmeinfo->network.IELength; i += (pIE->Length + 2)) {
1181                 pIE = (struct ndis_802_11_var_ie *)(pmlmeinfo->network.IEs + i);
1182
1183                 switch (pIE->ElementID) {
1184                 case _VENDOR_SPECIFIC_IE_:
1185                         if ((!memcmp(pIE->data, RTW_WPA_OUI, 4)) ||
1186                             (!memcmp(pIE->data, WMM_OUI, 4)) ||
1187                             (!memcmp(pIE->data, WPS_OUI, 4))) {
1188                                 if (!padapter->registrypriv.wifi_spec) {
1189                                         /* Commented by Kurt 20110629 */
1190                                         /* In some older APs, WPS handshake */
1191                                         /* would be fail if we append vender extensions informations to AP */
1192                                         if (!memcmp(pIE->data, WPS_OUI, 4))
1193                                                 pIE->Length = 14;
1194                                 }
1195                                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, pIE->Length, pIE->data, &(pattrib->pktlen));
1196                         }
1197                         break;
1198                 default:
1199                         break;
1200                 }
1201         }
1202
1203         if (pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_REALTEK)
1204                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, 6 , REALTEK_96B_IE, &(pattrib->pktlen));
1205
1206         pattrib->last_txcmdsz = pattrib->pktlen;
1207         dump_mgntframe(padapter, pmgntframe);
1208
1209         ret = _SUCCESS;
1210
1211 exit:
1212         if (ret == _SUCCESS)
1213                 rtw_buf_update(&pmlmepriv->assoc_req, &pmlmepriv->assoc_req_len, (u8 *)pwlanhdr, pattrib->pktlen);
1214         else
1215                 rtw_buf_free(&pmlmepriv->assoc_req, &pmlmepriv->assoc_req_len);
1216
1217         return;
1218 }
1219
1220 /* when wait_ack is true, this function should be called at process context */
1221 static int _issue_nulldata(struct adapter *padapter, unsigned char *da, unsigned int power_mode, int wait_ack)
1222 {
1223         int ret = _FAIL;
1224         struct xmit_frame                       *pmgntframe;
1225         struct pkt_attrib                       *pattrib;
1226         unsigned char                                   *pframe;
1227         struct rtw_ieee80211_hdr        *pwlanhdr;
1228         __le16 *fctrl;
1229         struct xmit_priv        *pxmitpriv;
1230         struct mlme_ext_priv    *pmlmeext;
1231         struct mlme_ext_info    *pmlmeinfo;
1232         struct wlan_bssid_ex    *pnetwork;
1233
1234         if (!padapter)
1235                 goto exit;
1236
1237         pxmitpriv = &(padapter->xmitpriv);
1238         pmlmeext = &(padapter->mlmeextpriv);
1239         pmlmeinfo = &(pmlmeext->mlmext_info);
1240         pnetwork = &(pmlmeinfo->network);
1241
1242         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1243         if (pmgntframe == NULL)
1244                 goto exit;
1245
1246         /* update attribute */
1247         pattrib = &pmgntframe->attrib;
1248         update_mgntframe_attrib(padapter, pattrib);
1249         pattrib->retry_ctrl = false;
1250
1251         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1252
1253         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1254         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1255
1256         fctrl = &(pwlanhdr->frame_ctl);
1257         *(fctrl) = 0;
1258
1259         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
1260                 SetFrDs(fctrl);
1261         else if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
1262                 SetToDs(fctrl);
1263
1264         if (power_mode)
1265                 SetPwrMgt(fctrl);
1266
1267         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
1268         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1269         memcpy(pwlanhdr->addr3, pnetwork->MacAddress, ETH_ALEN);
1270
1271         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1272         pmlmeext->mgnt_seq++;
1273         SetFrameSubType(pframe, WIFI_DATA_NULL);
1274
1275         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1276         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1277
1278         pattrib->last_txcmdsz = pattrib->pktlen;
1279
1280         if (wait_ack) {
1281                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
1282         } else {
1283                 dump_mgntframe(padapter, pmgntframe);
1284                 ret = _SUCCESS;
1285         }
1286
1287 exit:
1288         return ret;
1289 }
1290
1291
1292 /* when wait_ms > 0 , this function should be called at process context */
1293 /* da == NULL for station mode */
1294 int issue_nulldata(struct adapter *padapter, unsigned char *da, unsigned int power_mode, int try_cnt, int wait_ms)
1295 {
1296         int ret;
1297         int i = 0;
1298         unsigned long start = jiffies;
1299         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1300         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1301         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
1302
1303         /* da == NULL, assume it's null data for sta to ap*/
1304         if (da == NULL)
1305                 da = pnetwork->MacAddress;
1306
1307         do {
1308                 ret = _issue_nulldata(padapter, da, power_mode, wait_ms > 0 ? true : false);
1309
1310                 i++;
1311
1312                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
1313                         break;
1314
1315                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
1316                         msleep(wait_ms);
1317         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
1318
1319         if (ret != _FAIL) {
1320                 ret = _SUCCESS;
1321                 goto exit;
1322         }
1323
1324         if (try_cnt && wait_ms) {
1325                 if (da)
1326                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
1327                                 FUNC_ADPT_ARG(padapter), da, rtw_get_oper_ch(padapter),
1328                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt,
1329                                 jiffies_to_msecs(jiffies - start));
1330                 else
1331                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
1332                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
1333                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt,
1334                                 jiffies_to_msecs(jiffies - start));
1335         }
1336 exit:
1337         return ret;
1338 }
1339
1340 /* when wait_ack is true, this function should be called at process context */
1341 static int _issue_qos_nulldata(struct adapter *padapter, unsigned char *da, u16 tid, int wait_ack)
1342 {
1343         int ret = _FAIL;
1344         struct xmit_frame                       *pmgntframe;
1345         struct pkt_attrib                       *pattrib;
1346         unsigned char                                   *pframe;
1347         struct rtw_ieee80211_hdr        *pwlanhdr;
1348         __le16 *fctrl;
1349         unsigned short *qc;
1350         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
1351         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1352         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1353         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
1354
1355         DBG_88E("%s\n", __func__);
1356
1357         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1358         if (pmgntframe == NULL)
1359                 goto exit;
1360
1361         /* update attribute */
1362         pattrib = &pmgntframe->attrib;
1363         update_mgntframe_attrib(padapter, pattrib);
1364
1365         pattrib->hdrlen += 2;
1366         pattrib->qos_en = true;
1367         pattrib->eosp = 1;
1368         pattrib->ack_policy = 0;
1369         pattrib->mdata = 0;
1370
1371         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1372
1373         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1374         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1375
1376         fctrl = &(pwlanhdr->frame_ctl);
1377         *(fctrl) = 0;
1378
1379         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
1380                 SetFrDs(fctrl);
1381         else if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
1382                 SetToDs(fctrl);
1383
1384         if (pattrib->mdata)
1385                 SetMData(fctrl);
1386
1387         qc = (unsigned short *)(pframe + pattrib->hdrlen - 2);
1388
1389         SetPriority(qc, tid);
1390
1391         SetEOSP(qc, pattrib->eosp);
1392
1393         SetAckpolicy(qc, pattrib->ack_policy);
1394
1395         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
1396         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1397         memcpy(pwlanhdr->addr3, pnetwork->MacAddress, ETH_ALEN);
1398
1399         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1400         pmlmeext->mgnt_seq++;
1401         SetFrameSubType(pframe, WIFI_QOS_DATA_NULL);
1402
1403         pframe += sizeof(struct rtw_ieee80211_hdr_3addr_qos);
1404         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr_qos);
1405
1406         pattrib->last_txcmdsz = pattrib->pktlen;
1407
1408         if (wait_ack) {
1409                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
1410         } else {
1411                 dump_mgntframe(padapter, pmgntframe);
1412                 ret = _SUCCESS;
1413         }
1414
1415 exit:
1416         return ret;
1417 }
1418
1419 /* when wait_ms > 0 , this function should be called at process context */
1420 /* da == NULL for station mode */
1421 int issue_qos_nulldata(struct adapter *padapter, unsigned char *da, u16 tid, int try_cnt, int wait_ms)
1422 {
1423         int ret;
1424         int i = 0;
1425         unsigned long start = jiffies;
1426         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1427         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1428         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
1429
1430         /* da == NULL, assume it's null data for sta to ap*/
1431         if (da == NULL)
1432                 da = pnetwork->MacAddress;
1433
1434         do {
1435                 ret = _issue_qos_nulldata(padapter, da, tid, wait_ms > 0 ? true : false);
1436
1437                 i++;
1438
1439                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
1440                         break;
1441
1442                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
1443                         msleep(wait_ms);
1444         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
1445
1446         if (ret != _FAIL) {
1447                 ret = _SUCCESS;
1448                 goto exit;
1449         }
1450
1451         if (try_cnt && wait_ms) {
1452                 if (da)
1453                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
1454                                 FUNC_ADPT_ARG(padapter), da, rtw_get_oper_ch(padapter),
1455                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt,
1456                                 jiffies_to_msecs(jiffies - start));
1457                 else
1458                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
1459                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
1460                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt,
1461                                 jiffies_to_msecs(jiffies - start));
1462         }
1463 exit:
1464         return ret;
1465 }
1466
1467 static int _issue_deauth(struct adapter *padapter, unsigned char *da, unsigned short reason, u8 wait_ack)
1468 {
1469         struct xmit_frame                       *pmgntframe;
1470         struct pkt_attrib                       *pattrib;
1471         unsigned char                                   *pframe;
1472         struct rtw_ieee80211_hdr        *pwlanhdr;
1473         __le16 *fctrl;
1474         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
1475         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1476         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1477         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
1478         int ret = _FAIL;
1479         __le16 le_tmp;
1480
1481         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1482         if (pmgntframe == NULL)
1483                 goto exit;
1484
1485         /* update attribute */
1486         pattrib = &pmgntframe->attrib;
1487         update_mgntframe_attrib(padapter, pattrib);
1488         pattrib->retry_ctrl = false;
1489
1490         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1491
1492         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1493         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1494
1495         fctrl = &(pwlanhdr->frame_ctl);
1496         *(fctrl) = 0;
1497
1498         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
1499         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1500         memcpy(pwlanhdr->addr3, pnetwork->MacAddress, ETH_ALEN);
1501
1502         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1503         pmlmeext->mgnt_seq++;
1504         SetFrameSubType(pframe, WIFI_DEAUTH);
1505
1506         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1507         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1508
1509         le_tmp = cpu_to_le16(reason);
1510         pframe = rtw_set_fixed_ie(pframe, _RSON_CODE_, &le_tmp,
1511                                   &pattrib->pktlen);
1512
1513         pattrib->last_txcmdsz = pattrib->pktlen;
1514
1515
1516         if (wait_ack) {
1517                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
1518         } else {
1519                 dump_mgntframe(padapter, pmgntframe);
1520                 ret = _SUCCESS;
1521         }
1522
1523 exit:
1524         return ret;
1525 }
1526
1527 int issue_deauth(struct adapter *padapter, unsigned char *da, unsigned short reason)
1528 {
1529         DBG_88E("%s to %pM\n", __func__, da);
1530         return _issue_deauth(padapter, da, reason, false);
1531 }
1532
1533 static int issue_deauth_ex(struct adapter *padapter, u8 *da,
1534                            unsigned short reason, int try_cnt,
1535                            int wait_ms)
1536 {
1537         int ret;
1538         int i = 0;
1539         unsigned long start = jiffies;
1540
1541         do {
1542                 ret = _issue_deauth(padapter, da, reason, wait_ms > 0 ? true : false);
1543
1544                 i++;
1545
1546                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
1547                         break;
1548
1549                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
1550                         msleep(wait_ms);
1551         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
1552
1553         if (ret != _FAIL) {
1554                 ret = _SUCCESS;
1555                 goto exit;
1556         }
1557
1558         if (try_cnt && wait_ms) {
1559                 if (da)
1560                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
1561                                 FUNC_ADPT_ARG(padapter), da, rtw_get_oper_ch(padapter),
1562                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt,
1563                                 jiffies_to_msecs(jiffies - start));
1564                 else
1565                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
1566                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
1567                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt,
1568                                 jiffies_to_msecs(jiffies - start));
1569         }
1570 exit:
1571         return ret;
1572 }
1573
1574 void issue_action_spct_ch_switch(struct adapter *padapter, u8 *ra, u8 new_ch, u8 ch_offset)
1575 {
1576         struct xmit_frame                       *pmgntframe;
1577         struct pkt_attrib                       *pattrib;
1578         unsigned char                           *pframe;
1579         struct rtw_ieee80211_hdr        *pwlanhdr;
1580         __le16 *fctrl;
1581         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
1582         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1583
1584
1585         DBG_88E(FUNC_NDEV_FMT" ra =%pM, ch:%u, offset:%u\n",
1586                 FUNC_NDEV_ARG(padapter->pnetdev), ra, new_ch, ch_offset);
1587
1588         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1589         if (pmgntframe == NULL)
1590                 return;
1591
1592         /* update attribute */
1593         pattrib = &pmgntframe->attrib;
1594         update_mgntframe_attrib(padapter, pattrib);
1595
1596         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1597
1598         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1599         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1600
1601         fctrl = &(pwlanhdr->frame_ctl);
1602         *(fctrl) = 0;
1603
1604         memcpy(pwlanhdr->addr1, ra, ETH_ALEN); /* RA */
1605         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN); /* TA */
1606         memcpy(pwlanhdr->addr3, ra, ETH_ALEN); /* DA = RA */
1607
1608         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1609         pmlmeext->mgnt_seq++;
1610         SetFrameSubType(pframe, WIFI_ACTION);
1611
1612         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1613         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1614
1615         /* category, action */
1616         {
1617                 u8 category, action;
1618                 category = RTW_WLAN_CATEGORY_SPECTRUM_MGMT;
1619                 action = RTW_WLAN_ACTION_SPCT_CHL_SWITCH;
1620
1621                 pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
1622                 pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
1623         }
1624
1625         pframe = rtw_set_ie_ch_switch(pframe, &(pattrib->pktlen), 0, new_ch, 0);
1626         pframe = rtw_set_ie_secondary_ch_offset(pframe, &(pattrib->pktlen),
1627                 hal_ch_offset_to_secondary_ch_offset(ch_offset));
1628
1629         pattrib->last_txcmdsz = pattrib->pktlen;
1630
1631         dump_mgntframe(padapter, pmgntframe);
1632 }
1633
1634 static void issue_action_BA(struct adapter *padapter, unsigned char *raddr,
1635                             unsigned char action, unsigned short status)
1636 {
1637         u8 category = RTW_WLAN_CATEGORY_BACK;
1638         u16 start_seq;
1639         u16 BA_para_set;
1640         u16 reason_code;
1641         u16 BA_timeout_value;
1642         __le16  le_tmp;
1643         u16 BA_starting_seqctrl = 0;
1644         enum ht_cap_ampdu_factor max_rx_ampdu_factor;
1645         struct xmit_frame *pmgntframe;
1646         struct pkt_attrib *pattrib;
1647         u8 *pframe;
1648         struct rtw_ieee80211_hdr *pwlanhdr;
1649         __le16 *fctrl;
1650         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
1651         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
1652         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1653         struct sta_info *psta;
1654         struct sta_priv *pstapriv = &padapter->stapriv;
1655         struct registry_priv *pregpriv = &padapter->registrypriv;
1656         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
1657
1658         DBG_88E("%s, category=%d, action=%d, status=%d\n", __func__, category, action, status);
1659
1660         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1661         if (pmgntframe == NULL)
1662                 return;
1663
1664         /* update attribute */
1665         pattrib = &pmgntframe->attrib;
1666         update_mgntframe_attrib(padapter, pattrib);
1667
1668         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1669
1670         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1671         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1672
1673         fctrl = &(pwlanhdr->frame_ctl);
1674         *(fctrl) = 0;
1675
1676         /* memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); */
1677         memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
1678         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1679         memcpy(pwlanhdr->addr3, pnetwork->MacAddress, ETH_ALEN);
1680
1681         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1682         pmlmeext->mgnt_seq++;
1683         SetFrameSubType(pframe, WIFI_ACTION);
1684
1685         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1686         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1687
1688         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
1689         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
1690
1691         if (category == 3) {
1692                 switch (action) {
1693                 case 0: /* ADDBA req */
1694                         do {
1695                                 pmlmeinfo->dialogToken++;
1696                         } while (pmlmeinfo->dialogToken == 0);
1697                         pframe = rtw_set_fixed_ie(pframe, 1, &(pmlmeinfo->dialogToken), &(pattrib->pktlen));
1698
1699                         BA_para_set = 0x1002 | ((status & 0xf) << 2); /* immediate ack & 64 buffer size */
1700                         le_tmp = cpu_to_le16(BA_para_set);
1701                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1702                                                   &pattrib->pktlen);
1703
1704                         BA_timeout_value = 5000;/*  5ms */
1705                         le_tmp = cpu_to_le16(BA_timeout_value);
1706                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1707                                                   &pattrib->pktlen);
1708
1709                         psta = rtw_get_stainfo(pstapriv, raddr);
1710                         if (psta != NULL) {
1711                                 start_seq = (psta->sta_xmitpriv.txseq_tid[status & 0x07]&0xfff) + 1;
1712
1713                                 DBG_88E("BA_starting_seqctrl=%d for TID=%d\n", start_seq, status & 0x07);
1714
1715                                 psta->BA_starting_seqctrl[status & 0x07] = start_seq;
1716
1717                                 BA_starting_seqctrl = start_seq << 4;
1718                         }
1719                         le_tmp = cpu_to_le16(BA_starting_seqctrl);
1720                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1721                                                   &pattrib->pktlen);
1722                         break;
1723                 case 1: /* ADDBA rsp */
1724                 {
1725                         struct ADDBA_request *ADDBA_req = &pmlmeinfo->ADDBA_req;
1726
1727                         pframe = rtw_set_fixed_ie(pframe, 1,
1728                                                   &ADDBA_req->dialog_token,
1729                                                   &pattrib->pktlen);
1730                         pframe = rtw_set_fixed_ie(pframe, 2, &status,
1731                                                   &pattrib->pktlen);
1732
1733                         BA_para_set = le16_to_cpu(ADDBA_req->BA_para_set) &
1734                                       0x3f;
1735                         rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor);
1736                         switch (max_rx_ampdu_factor) {
1737                         case MAX_AMPDU_FACTOR_64K:
1738                                 BA_para_set |= 0x1000; /* 64 buffer size */
1739                                 break;
1740                         case MAX_AMPDU_FACTOR_32K:
1741                                 BA_para_set |= 0x0800; /* 32 buffer size */
1742                                 break;
1743                         case MAX_AMPDU_FACTOR_16K:
1744                                 BA_para_set |= 0x0400; /* 16 buffer size */
1745                                 break;
1746                         case MAX_AMPDU_FACTOR_8K:
1747                                 BA_para_set |= 0x0200; /* 8 buffer size */
1748                                 break;
1749                         default:
1750                                 BA_para_set |= 0x1000; /* 64 buffer size */
1751                                 break;
1752                         }
1753
1754                         if (pregpriv->ampdu_amsdu == 0)/* disabled */
1755                                 BA_para_set = BA_para_set & ~BIT(0);
1756                         else if (pregpriv->ampdu_amsdu == 1)/* enabled */
1757                                 BA_para_set = BA_para_set | BIT(0);
1758                         le_tmp = cpu_to_le16(BA_para_set);
1759
1760                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1761                                                   &pattrib->pktlen);
1762                         pframe = rtw_set_fixed_ie(pframe, 2,
1763                                                   &ADDBA_req->BA_timeout_value,
1764                                                   &pattrib->pktlen);
1765                         break;
1766                 }
1767                 case 2:/* DELBA */
1768                         BA_para_set = (status & 0x1F) << 3;
1769                         le_tmp = cpu_to_le16(BA_para_set);
1770                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1771                                                   &pattrib->pktlen);
1772
1773                         reason_code = 37;/* Requested from peer STA as it does not want to use the mechanism */
1774                         le_tmp = cpu_to_le16(reason_code);
1775                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1776                                                   &pattrib->pktlen);
1777                         break;
1778                 default:
1779                         break;
1780                 }
1781         }
1782
1783         pattrib->last_txcmdsz = pattrib->pktlen;
1784
1785         dump_mgntframe(padapter, pmgntframe);
1786 }
1787
1788 static void issue_action_BSSCoexistPacket(struct adapter *padapter)
1789 {
1790         struct list_head *plist, *phead;
1791         unsigned char category, action;
1792         struct xmit_frame                       *pmgntframe;
1793         struct pkt_attrib                       *pattrib;
1794         unsigned char                           *pframe;
1795         struct rtw_ieee80211_hdr        *pwlanhdr;
1796         __le16 *fctrl;
1797         struct  wlan_network    *pnetwork = NULL;
1798         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
1799         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1800         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1801         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1802         struct __queue *queue   = &(pmlmepriv->scanned_queue);
1803         u8 InfoContent[16] = {0};
1804         u8 ICS[8][15];
1805         struct wlan_bssid_ex  *cur_network   = &(pmlmeinfo->network);
1806
1807         if ((pmlmepriv->num_FortyMHzIntolerant == 0) || (pmlmepriv->num_sta_no_ht == 0))
1808                 return;
1809
1810         if (pmlmeinfo->bwmode_updated)
1811                 return;
1812
1813
1814         DBG_88E("%s\n", __func__);
1815
1816
1817         category = RTW_WLAN_CATEGORY_PUBLIC;
1818         action = ACT_PUBLIC_BSSCOEXIST;
1819
1820         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1821         if (pmgntframe == NULL)
1822                 return;
1823
1824         /* update attribute */
1825         pattrib = &pmgntframe->attrib;
1826         update_mgntframe_attrib(padapter, pattrib);
1827
1828         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1829
1830         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1831         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1832
1833         fctrl = &(pwlanhdr->frame_ctl);
1834         *(fctrl) = 0;
1835
1836         memcpy(pwlanhdr->addr1, cur_network->MacAddress, ETH_ALEN);
1837         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1838         memcpy(pwlanhdr->addr3, cur_network->MacAddress, ETH_ALEN);
1839
1840         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1841         pmlmeext->mgnt_seq++;
1842         SetFrameSubType(pframe, WIFI_ACTION);
1843
1844         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1845         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1846
1847         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
1848         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
1849
1850
1851         /*  */
1852         if (pmlmepriv->num_FortyMHzIntolerant > 0) {
1853                 u8 iedata = 0;
1854
1855                 iedata |= BIT(2);/* 20 MHz BSS Width Request */
1856
1857                 pframe = rtw_set_ie(pframe, EID_BSSCoexistence,  1, &iedata, &(pattrib->pktlen));
1858         }
1859
1860
1861         /*  */
1862         memset(ICS, 0, sizeof(ICS));
1863         if (pmlmepriv->num_sta_no_ht > 0) {
1864                 int i;
1865
1866                 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1867
1868                 phead = get_list_head(queue);
1869                 plist = phead->next;
1870
1871                 while (phead != plist) {
1872                         int len;
1873                         u8 *p;
1874                         struct wlan_bssid_ex *pbss_network;
1875
1876                         pnetwork = container_of(plist, struct wlan_network, list);
1877
1878                         plist = plist->next;
1879
1880                         pbss_network = (struct wlan_bssid_ex *)&pnetwork->network;
1881
1882                         p = rtw_get_ie(pbss_network->IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, pbss_network->IELength - _FIXED_IE_LENGTH_);
1883                         if ((p == NULL) || (len == 0)) { /* non-HT */
1884                                 if ((pbss_network->Configuration.DSConfig <= 0) || (pbss_network->Configuration.DSConfig > 14))
1885                                         continue;
1886
1887                                 ICS[0][pbss_network->Configuration.DSConfig] = 1;
1888
1889                                 if (ICS[0][0] == 0)
1890                                         ICS[0][0] = 1;
1891                         }
1892                 }
1893                 spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1894
1895                 for (i = 0; i < 8; i++) {
1896                         if (ICS[i][0] == 1) {
1897                                 int j, k = 0;
1898
1899                                 InfoContent[k] = i;
1900                                 /* SET_BSS_INTOLERANT_ELE_REG_CLASS(InfoContent, i); */
1901                                 k++;
1902
1903                                 for (j = 1; j <= 14; j++) {
1904                                         if (ICS[i][j] == 1) {
1905                                                 if (k < 16) {
1906                                                         InfoContent[k] = j; /* channel number */
1907                                                         /* SET_BSS_INTOLERANT_ELE_CHANNEL(InfoContent+k, j); */
1908                                                         k++;
1909                                                 }
1910                                         }
1911                                 }
1912
1913                                 pframe = rtw_set_ie(pframe, EID_BSSIntolerantChlReport, k, InfoContent, &(pattrib->pktlen));
1914                         }
1915                 }
1916         }
1917
1918
1919         pattrib->last_txcmdsz = pattrib->pktlen;
1920
1921         dump_mgntframe(padapter, pmgntframe);
1922 }
1923
1924 unsigned int send_delba(struct adapter *padapter, u8 initiator, u8 *addr)
1925 {
1926         struct sta_priv *pstapriv = &padapter->stapriv;
1927         struct sta_info *psta = NULL;
1928         /* struct recv_reorder_ctrl *preorder_ctrl; */
1929         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1930         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1931         u16 tid;
1932
1933         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
1934                 if (!(pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS))
1935                         return _SUCCESS;
1936
1937         psta = rtw_get_stainfo(pstapriv, addr);
1938         if (psta == NULL)
1939                 return _SUCCESS;
1940
1941         if (initiator == 0) { /*  recipient */
1942                 for (tid = 0; tid < MAXTID; tid++) {
1943                         if (psta->recvreorder_ctrl[tid].enable) {
1944                                 DBG_88E("rx agg disable tid(%d)\n", tid);
1945                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_DELBA, (((tid << 1) | initiator)&0x1F));
1946                                 psta->recvreorder_ctrl[tid].enable = false;
1947                                 psta->recvreorder_ctrl[tid].indicate_seq = 0xffff;
1948                         }
1949                 }
1950         } else if (initiator == 1) { /*  originator */
1951                 for (tid = 0; tid < MAXTID; tid++) {
1952                         if (psta->htpriv.agg_enable_bitmap & BIT(tid)) {
1953                                 DBG_88E("tx agg disable tid(%d)\n", tid);
1954                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_DELBA, (((tid << 1) | initiator)&0x1F));
1955                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
1956                                 psta->htpriv.candidate_tid_bitmap &= ~BIT(tid);
1957                         }
1958                 }
1959         }
1960
1961         return _SUCCESS;
1962 }
1963
1964 unsigned int send_beacon(struct adapter *padapter)
1965 {
1966         u8 bxmitok = false;
1967         int     issue = 0;
1968         int poll = 0;
1969
1970         unsigned long start = jiffies;
1971
1972         rtw_hal_set_hwreg(padapter, HW_VAR_BCN_VALID, NULL);
1973         do {
1974                 issue_beacon(padapter, 100);
1975                 issue++;
1976                 do {
1977                         yield();
1978                         rtw_hal_get_hwreg(padapter, HW_VAR_BCN_VALID, (u8 *)(&bxmitok));
1979                         poll++;
1980                 } while ((poll%10) != 0 && !bxmitok && !padapter->bSurpriseRemoved && !padapter->bDriverStopped);
1981         } while (!bxmitok && issue < 100 && !padapter->bSurpriseRemoved && !padapter->bDriverStopped);
1982
1983         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
1984                 return _FAIL;
1985         if (!bxmitok) {
1986                 DBG_88E("%s fail! %u ms\n", __func__,
1987                         jiffies_to_msecs(jiffies - start));
1988                 return _FAIL;
1989         } else {
1990                 u32 passing_time = jiffies_to_msecs(jiffies - start);
1991
1992                 if (passing_time > 100 || issue > 3)
1993                         DBG_88E("%s success, issue:%d, poll:%d, %u ms\n",
1994                                 __func__, issue, poll,
1995                                 jiffies_to_msecs(jiffies - start));
1996                 return _SUCCESS;
1997         }
1998 }
1999
2000 /****************************************************************************
2001
2002 Following are some utility functions for WiFi MLME
2003
2004 *****************************************************************************/
2005
2006 static void site_survey(struct adapter *padapter)
2007 {
2008         unsigned char           survey_channel = 0, val8;
2009         enum rt_scan_type ScanType = SCAN_PASSIVE;
2010         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2011         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2012         u32 initialgain = 0;
2013         struct rtw_ieee80211_channel *ch;
2014
2015         if (pmlmeext->sitesurvey_res.channel_idx < pmlmeext->sitesurvey_res.ch_num) {
2016                 ch = &pmlmeext->sitesurvey_res.ch[pmlmeext->sitesurvey_res.channel_idx];
2017                 survey_channel = ch->hw_value;
2018                 ScanType = (ch->flags & RTW_IEEE80211_CHAN_PASSIVE_SCAN) ? SCAN_PASSIVE : SCAN_ACTIVE;
2019         }
2020
2021
2022         if (survey_channel != 0) {
2023                 /* PAUSE 4-AC Queue when site_survey */
2024                 /* rtw_hal_get_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
2025                 /* val8 |= 0x0f; */
2026                 /* rtw_hal_set_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
2027                 if (pmlmeext->sitesurvey_res.channel_idx == 0)
2028                         set_channel_bwmode(padapter, survey_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
2029                 else
2030                         SelectChannel(padapter, survey_channel);
2031
2032                 if (ScanType == SCAN_ACTIVE) { /* obey the channel plan setting... */
2033                         int i;
2034                         for (i = 0; i < RTW_SSID_SCAN_AMOUNT; i++) {
2035                                 if (pmlmeext->sitesurvey_res.ssid[i].SsidLength) {
2036                                         /* todo: to issue two probe req??? */
2037                                         issue_probereq(padapter,
2038                                         &(pmlmeext->sitesurvey_res.ssid[i]),
2039                                                                 NULL, false);
2040                                         /* msleep(SURVEY_TO>>1); */
2041                                         issue_probereq(padapter,
2042                                         &(pmlmeext->sitesurvey_res.ssid[i]),
2043                                                                 NULL, false);
2044                                 }
2045                         }
2046
2047                         if (pmlmeext->sitesurvey_res.scan_mode == SCAN_ACTIVE) {
2048                                 /* todo: to issue two probe req??? */
2049                                 issue_probereq(padapter, NULL, NULL, false);
2050                                 /* msleep(SURVEY_TO>>1); */
2051                                 issue_probereq(padapter, NULL, NULL, false);
2052                         }
2053
2054                         if (pmlmeext->sitesurvey_res.scan_mode == SCAN_ACTIVE) {
2055                                 /* todo: to issue two probe req??? */
2056                                 issue_probereq(padapter, NULL, NULL, false);
2057                                 /* msleep(SURVEY_TO>>1); */
2058                                 issue_probereq(padapter, NULL, NULL, false);
2059                         }
2060                 }
2061
2062                 set_survey_timer(pmlmeext, pmlmeext->chan_scan_time);
2063         } else {
2064
2065                 /*  20100721:Interrupt scan operation here. */
2066                 /*  For SW antenna diversity before link, it needs to switch to another antenna and scan again. */
2067                 /*  It compares the scan result and select better one to do connection. */
2068                 if (rtw_hal_antdiv_before_linked(padapter)) {
2069                         pmlmeext->sitesurvey_res.bss_cnt = 0;
2070                         pmlmeext->sitesurvey_res.channel_idx = -1;
2071                         pmlmeext->chan_scan_time = SURVEY_TO / 2;
2072                         set_survey_timer(pmlmeext, pmlmeext->chan_scan_time);
2073                         return;
2074                 }
2075
2076                 pmlmeext->sitesurvey_res.state = SCAN_COMPLETE;
2077
2078                 /* switch back to the original channel */
2079
2080                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
2081
2082                 /* flush 4-AC Queue after site_survey */
2083                 /* val8 = 0; */
2084                 /* rtw_hal_set_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
2085
2086                 /* config MSR */
2087                 Set_MSR(padapter, (pmlmeinfo->state & 0x3));
2088
2089                 initialgain = 0xff; /* restore RX GAIN */
2090                 rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
2091                 /* turn on dynamic functions */
2092                 Restore_DM_Func_Flag(padapter);
2093                 /* Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true); */
2094
2095                 if (is_client_associated_to_ap(padapter))
2096                         issue_nulldata(padapter, NULL, 0, 3, 500);
2097
2098                 val8 = 0; /* survey done */
2099                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_SITESURVEY, (u8 *)(&val8));
2100
2101                 report_surveydone_event(padapter);
2102
2103                 pmlmeext->chan_scan_time = SURVEY_TO;
2104                 pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
2105
2106                 issue_action_BSSCoexistPacket(padapter);
2107                 issue_action_BSSCoexistPacket(padapter);
2108                 issue_action_BSSCoexistPacket(padapter);
2109         }
2110         return;
2111 }
2112
2113 /* collect bss info from Beacon and Probe request/response frames. */
2114 static u8 collect_bss_info(struct adapter *padapter,
2115                            struct recv_frame *precv_frame,
2116                            struct wlan_bssid_ex *bssid)
2117 {
2118         int     i;
2119         u32     len;
2120         u8 *p;
2121         u16 val16, subtype;
2122         u8 *pframe = precv_frame->rx_data;
2123         u32     packet_len = precv_frame->len;
2124         u8 ie_offset;
2125         struct registry_priv    *pregistrypriv = &padapter->registrypriv;
2126         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2127         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2128
2129         len = packet_len - sizeof(struct rtw_ieee80211_hdr_3addr);
2130
2131         if (len > MAX_IE_SZ)
2132                 return _FAIL;
2133
2134         memset(bssid, 0, sizeof(struct wlan_bssid_ex));
2135
2136         subtype = GetFrameSubType(pframe);
2137
2138         if (subtype == WIFI_BEACON) {
2139                 bssid->Reserved[0] = 1;
2140                 ie_offset = _BEACON_IE_OFFSET_;
2141         } else {
2142                 /*  FIXME : more type */
2143                 if (subtype == WIFI_PROBEREQ) {
2144                         ie_offset = _PROBEREQ_IE_OFFSET_;
2145                         bssid->Reserved[0] = 2;
2146                 } else if (subtype == WIFI_PROBERSP) {
2147                         ie_offset = _PROBERSP_IE_OFFSET_;
2148                         bssid->Reserved[0] = 3;
2149                 } else {
2150                         bssid->Reserved[0] = 0;
2151                         ie_offset = _FIXED_IE_LENGTH_;
2152                 }
2153         }
2154
2155         bssid->Length = sizeof(struct wlan_bssid_ex) - MAX_IE_SZ + len;
2156
2157         /* below is to copy the information element */
2158         bssid->IELength = len;
2159         memcpy(bssid->IEs, (pframe + sizeof(struct rtw_ieee80211_hdr_3addr)), bssid->IELength);
2160
2161         /* get the signal strength in dBM.raw data */
2162         bssid->Rssi = precv_frame->attrib.phy_info.recvpower;
2163         bssid->PhyInfo.SignalQuality = precv_frame->attrib.phy_info.SignalQuality;/* in percentage */
2164         bssid->PhyInfo.SignalStrength = precv_frame->attrib.phy_info.SignalStrength;/* in percentage */
2165         rtw_hal_get_def_var(padapter, HAL_DEF_CURRENT_ANTENNA,  &bssid->PhyInfo.Optimum_antenna);
2166
2167         /*  checking SSID */
2168         p = rtw_get_ie(bssid->IEs + ie_offset, _SSID_IE_, &len, bssid->IELength - ie_offset);
2169         if (p == NULL) {
2170                 DBG_88E("marc: cannot find SSID for survey event\n");
2171                 return _FAIL;
2172         }
2173
2174         if (len) {
2175                 if (len > NDIS_802_11_LENGTH_SSID) {
2176                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
2177                         return _FAIL;
2178                 }
2179                 memcpy(bssid->Ssid.Ssid, (p + 2), len);
2180                 bssid->Ssid.SsidLength = len;
2181         } else {
2182                 bssid->Ssid.SsidLength = 0;
2183         }
2184
2185         memset(bssid->SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
2186
2187         /* checking rate info... */
2188         i = 0;
2189         p = rtw_get_ie(bssid->IEs + ie_offset, _SUPPORTEDRATES_IE_, &len, bssid->IELength - ie_offset);
2190         if (p != NULL) {
2191                 if (len > NDIS_802_11_LENGTH_RATES_EX) {
2192                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
2193                         return _FAIL;
2194                 }
2195                 memcpy(bssid->SupportedRates, (p + 2), len);
2196                 i = len;
2197         }
2198
2199         p = rtw_get_ie(bssid->IEs + ie_offset, _EXT_SUPPORTEDRATES_IE_, &len, bssid->IELength - ie_offset);
2200         if (p != NULL) {
2201                 if (len > (NDIS_802_11_LENGTH_RATES_EX-i)) {
2202                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
2203                         return _FAIL;
2204                 }
2205                 memcpy(bssid->SupportedRates + i, (p + 2), len);
2206         }
2207
2208         /* todo: */
2209         bssid->NetworkTypeInUse = Ndis802_11OFDM24;
2210
2211         if (bssid->IELength < 12)
2212                 return _FAIL;
2213
2214         /*  Checking for DSConfig */
2215         p = rtw_get_ie(bssid->IEs + ie_offset, _DSSET_IE_, &len, bssid->IELength - ie_offset);
2216
2217         bssid->Configuration.DSConfig = 0;
2218         bssid->Configuration.Length = 0;
2219
2220         if (p) {
2221                 bssid->Configuration.DSConfig = *(p + 2);
2222         } else {/*  In 5G, some ap do not have DSSET IE */
2223                 /*  checking HT info for channel */
2224                 p = rtw_get_ie(bssid->IEs + ie_offset, _HT_ADD_INFO_IE_, &len, bssid->IELength - ie_offset);
2225                 if (p) {
2226                         struct HT_info_element *HT_info = (struct HT_info_element *)(p + 2);
2227                         bssid->Configuration.DSConfig = HT_info->primary_channel;
2228                 } else { /*  use current channel */
2229                         bssid->Configuration.DSConfig = rtw_get_oper_ch(padapter);
2230                 }
2231         }
2232
2233         if (subtype == WIFI_PROBEREQ) {
2234                 /*  FIXME */
2235                 bssid->InfrastructureMode = Ndis802_11Infrastructure;
2236                 memcpy(bssid->MacAddress, GetAddr2Ptr(pframe), ETH_ALEN);
2237                 bssid->Privacy = 1;
2238                 return _SUCCESS;
2239         }
2240
2241         bssid->Configuration.BeaconPeriod =
2242                 get_unaligned_le16(rtw_get_beacon_interval_from_ie(bssid->IEs));
2243
2244         val16 = rtw_get_capability((struct wlan_bssid_ex *)bssid);
2245
2246         if (val16 & BIT(0)) {
2247                 bssid->InfrastructureMode = Ndis802_11Infrastructure;
2248                 memcpy(bssid->MacAddress, GetAddr2Ptr(pframe), ETH_ALEN);
2249         } else {
2250                 bssid->InfrastructureMode = Ndis802_11IBSS;
2251                 memcpy(bssid->MacAddress, GetAddr3Ptr(pframe), ETH_ALEN);
2252         }
2253
2254         if (val16 & BIT(4))
2255                 bssid->Privacy = 1;
2256         else
2257                 bssid->Privacy = 0;
2258
2259         bssid->Configuration.ATIMWindow = 0;
2260
2261         /* 20/40 BSS Coexistence check */
2262         if ((pregistrypriv->wifi_spec == 1) && (!pmlmeinfo->bwmode_updated)) {
2263                 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2264                 p = rtw_get_ie(bssid->IEs + ie_offset, _HT_CAPABILITY_IE_, &len, bssid->IELength - ie_offset);
2265                 if (p && len > 0) {
2266                         struct HT_caps_element  *pHT_caps;
2267                         pHT_caps = (struct HT_caps_element *)(p + 2);
2268
2269                         if (le16_to_cpu(pHT_caps->u.HT_cap_element.HT_caps_info)&BIT(14))
2270                                 pmlmepriv->num_FortyMHzIntolerant++;
2271                 } else {
2272                         pmlmepriv->num_sta_no_ht++;
2273                 }
2274         }
2275
2276         /*  mark bss info receiving from nearby channel as SignalQuality 101 */
2277         if (bssid->Configuration.DSConfig != rtw_get_oper_ch(padapter))
2278                 bssid->PhyInfo.SignalQuality = 101;
2279         return _SUCCESS;
2280 }
2281
2282 static void start_create_ibss(struct adapter *padapter)
2283 {
2284         unsigned short  caps;
2285         u8 val8;
2286         u8 join_type;
2287         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2288         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2289         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
2290         pmlmeext->cur_channel = (u8)pnetwork->Configuration.DSConfig;
2291         pmlmeinfo->bcn_interval = get_beacon_interval(pnetwork);
2292
2293         /* update wireless mode */
2294         update_wireless_mode(padapter);
2295
2296         /* update capability */
2297         caps = rtw_get_capability((struct wlan_bssid_ex *)pnetwork);
2298         update_capinfo(padapter, caps);
2299         if (caps&cap_IBSS) {/* adhoc master */
2300                 val8 = 0xcf;
2301                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
2302
2303                 /* switch channel */
2304                 /* SelectChannel(padapter, pmlmeext->cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE); */
2305                 set_channel_bwmode(padapter, pmlmeext->cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
2306
2307                 beacon_timing_control(padapter);
2308
2309                 /* set msr to WIFI_FW_ADHOC_STATE */
2310                 pmlmeinfo->state = WIFI_FW_ADHOC_STATE;
2311                 Set_MSR(padapter, (pmlmeinfo->state & 0x3));
2312
2313                 /* issue beacon */
2314                 if (send_beacon(padapter) == _FAIL) {
2315                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("issuing beacon frame fail....\n"));
2316
2317                         report_join_res(padapter, -1);
2318                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
2319                 } else {
2320                         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, padapter->registrypriv.dev_network.MacAddress);
2321                         join_type = 0;
2322                         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
2323
2324                         report_join_res(padapter, 1);
2325                         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
2326                 }
2327         } else {
2328                 DBG_88E("start_create_ibss, invalid cap:%x\n", caps);
2329                 return;
2330         }
2331 }
2332
2333 static void start_clnt_join(struct adapter *padapter)
2334 {
2335         unsigned short  caps;
2336         u8 val8;
2337         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2338         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2339         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
2340         int beacon_timeout;
2341
2342         pmlmeext->cur_channel = (u8)pnetwork->Configuration.DSConfig;
2343         pmlmeinfo->bcn_interval = get_beacon_interval(pnetwork);
2344
2345         /* update wireless mode */
2346         update_wireless_mode(padapter);
2347
2348         /* update capability */
2349         caps = rtw_get_capability((struct wlan_bssid_ex *)pnetwork);
2350         update_capinfo(padapter, caps);
2351         if (caps&cap_ESS) {
2352                 Set_MSR(padapter, WIFI_FW_STATION_STATE);
2353
2354                 val8 = (pmlmeinfo->auth_algo == dot11AuthAlgrthm_8021X) ? 0xcc : 0xcf;
2355
2356                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
2357
2358                 /* switch channel */
2359                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
2360
2361                 /* here wait for receiving the beacon to start auth */
2362                 /* and enable a timer */
2363                 beacon_timeout = decide_wait_for_beacon_timeout(pmlmeinfo->bcn_interval);
2364                 set_link_timer(pmlmeext, beacon_timeout);
2365                 mod_timer(&padapter->mlmepriv.assoc_timer, jiffies +
2366                           msecs_to_jiffies((REAUTH_TO * REAUTH_LIMIT) + (REASSOC_TO * REASSOC_LIMIT) + beacon_timeout));
2367
2368                 pmlmeinfo->state = WIFI_FW_AUTH_NULL | WIFI_FW_STATION_STATE;
2369         } else if (caps&cap_IBSS) { /* adhoc client */
2370                 Set_MSR(padapter, WIFI_FW_ADHOC_STATE);
2371
2372                 val8 = 0xcf;
2373                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
2374
2375                 /* switch channel */
2376                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
2377
2378                 beacon_timing_control(padapter);
2379
2380                 pmlmeinfo->state = WIFI_FW_ADHOC_STATE;
2381
2382                 report_join_res(padapter, 1);
2383         } else {
2384                 return;
2385         }
2386 }
2387
2388 static void start_clnt_auth(struct adapter *padapter)
2389 {
2390         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2391         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2392
2393         del_timer_sync(&pmlmeext->link_timer);
2394
2395         pmlmeinfo->state &= (~WIFI_FW_AUTH_NULL);
2396         pmlmeinfo->state |= WIFI_FW_AUTH_STATE;
2397
2398         pmlmeinfo->auth_seq = 1;
2399         pmlmeinfo->reauth_count = 0;
2400         pmlmeinfo->reassoc_count = 0;
2401         pmlmeinfo->link_count = 0;
2402         pmlmeext->retry = 0;
2403
2404
2405         /*  Because of AP's not receiving deauth before */
2406         /*  AP may: 1)not response auth or 2)deauth us after link is complete */
2407         /*  issue deauth before issuing auth to deal with the situation */
2408         /*      Commented by Albert 2012/07/21 */
2409         /*      For the Win8 P2P connection, it will be hard to have a successful connection if this Wi-Fi doesn't connect to it. */
2410         issue_deauth(padapter, (&(pmlmeinfo->network))->MacAddress, WLAN_REASON_DEAUTH_LEAVING);
2411
2412         DBG_88E_LEVEL(_drv_info_, "start auth\n");
2413         issue_auth(padapter, NULL, 0);
2414
2415         set_link_timer(pmlmeext, REAUTH_TO);
2416 }
2417
2418
2419 static void start_clnt_assoc(struct adapter *padapter)
2420 {
2421         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2422         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2423
2424         del_timer_sync(&pmlmeext->link_timer);
2425
2426         pmlmeinfo->state &= (~(WIFI_FW_AUTH_NULL | WIFI_FW_AUTH_STATE));
2427         pmlmeinfo->state |= (WIFI_FW_AUTH_SUCCESS | WIFI_FW_ASSOC_STATE);
2428
2429         issue_assocreq(padapter);
2430
2431         set_link_timer(pmlmeext, REASSOC_TO);
2432 }
2433
2434 static unsigned int receive_disconnect(struct adapter *padapter,
2435                                        unsigned char *MacAddr,
2436                                        unsigned short reason)
2437 {
2438         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2439         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2440         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
2441
2442         /* check A3 */
2443         if (memcmp(MacAddr, pnetwork->MacAddress, ETH_ALEN))
2444                 return _SUCCESS;
2445
2446         DBG_88E("%s\n", __func__);
2447
2448         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
2449                 if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {
2450                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
2451                         report_del_sta_event(padapter, MacAddr, reason);
2452                 } else if (pmlmeinfo->state & WIFI_FW_LINKING_STATE) {
2453                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
2454                         report_join_res(padapter, -2);
2455                 }
2456         }
2457         return _SUCCESS;
2458 }
2459
2460 static void process_80211d(struct adapter *padapter, struct wlan_bssid_ex *bssid)
2461 {
2462         struct registry_priv *pregistrypriv;
2463         struct mlme_ext_priv *pmlmeext;
2464         struct rt_channel_info *chplan_new;
2465         u8 channel;
2466         u8 i;
2467
2468         pregistrypriv = &padapter->registrypriv;
2469         pmlmeext = &padapter->mlmeextpriv;
2470
2471         /*  Adjust channel plan by AP Country IE */
2472         if (pregistrypriv->enable80211d &&
2473             (!pmlmeext->update_channel_plan_by_ap_done)) {
2474                 u8 *ie, *p;
2475                 u32 len;
2476                 struct rt_channel_plan chplan_ap;
2477                 struct rt_channel_info chplan_sta[MAX_CHANNEL_NUM];
2478                 u8 country[4];
2479                 u8 fcn; /*  first channel number */
2480                 u8 noc; /*  number of channel */
2481                 u8 j, k;
2482
2483                 ie = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _COUNTRY_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
2484                 if (!ie)
2485                         return;
2486                 if (len < 6)
2487                         return;
2488                 ie += 2;
2489                 p = ie;
2490                 ie += len;
2491
2492                 memset(country, 0, 4);
2493                 memcpy(country, p, 3);
2494                 p += 3;
2495                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
2496                          ("%s: 802.11d country =%s\n", __func__, country));
2497
2498                 i = 0;
2499                 while ((ie - p) >= 3) {
2500                         fcn = *(p++);
2501                         noc = *(p++);
2502                         p++;
2503
2504                         for (j = 0; j < noc; j++) {
2505                                 if (fcn <= 14)
2506                                         channel = fcn + j; /*  2.4 GHz */
2507                                 else
2508                                         channel = fcn + j*4; /*  5 GHz */
2509
2510                                 chplan_ap.Channel[i++] = channel;
2511                         }
2512                 }
2513                 chplan_ap.Len = i;
2514
2515                 memcpy(chplan_sta, pmlmeext->channel_set, sizeof(chplan_sta));
2516
2517                 memset(pmlmeext->channel_set, 0, sizeof(pmlmeext->channel_set));
2518                 chplan_new = pmlmeext->channel_set;
2519
2520                 i = 0;
2521                 j = 0;
2522                 k = 0;
2523                 if (pregistrypriv->wireless_mode & WIRELESS_11G) {
2524                         do {
2525                                 if ((i == MAX_CHANNEL_NUM) ||
2526                                     (chplan_sta[i].ChannelNum == 0) ||
2527                                     (chplan_sta[i].ChannelNum > 14))
2528                                         break;
2529
2530                                 if ((j == chplan_ap.Len) || (chplan_ap.Channel[j] > 14))
2531                                         break;
2532
2533                                 if (chplan_sta[i].ChannelNum == chplan_ap.Channel[j]) {
2534                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
2535                                         chplan_new[k].ScanType = SCAN_ACTIVE;
2536                                         i++;
2537                                         j++;
2538                                         k++;
2539                                 } else if (chplan_sta[i].ChannelNum < chplan_ap.Channel[j]) {
2540                                         chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
2541                                         chplan_new[k].ScanType = SCAN_PASSIVE;
2542                                         i++;
2543                                         k++;
2544                                 } else if (chplan_sta[i].ChannelNum > chplan_ap.Channel[j]) {
2545                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
2546                                         chplan_new[k].ScanType = SCAN_ACTIVE;
2547                                         j++;
2548                                         k++;
2549                                 }
2550                         } while (1);
2551
2552                         /*  change AP not support channel to Passive scan */
2553                         while ((i < MAX_CHANNEL_NUM) &&
2554                                (chplan_sta[i].ChannelNum != 0) &&
2555                                (chplan_sta[i].ChannelNum <= 14)) {
2556                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
2557                                 chplan_new[k].ScanType = SCAN_PASSIVE;
2558                                 i++;
2559                                 k++;
2560                         }
2561
2562                         /*  add channel AP supported */
2563                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] <= 14)) {
2564                                 chplan_new[k].ChannelNum = chplan_ap.Channel[j];
2565                                 chplan_new[k].ScanType = SCAN_ACTIVE;
2566                                 j++;
2567                                 k++;
2568                         }
2569                 } else {
2570                         /*  keep original STA 2.4G channel plan */
2571                         while ((i < MAX_CHANNEL_NUM) &&
2572                                (chplan_sta[i].ChannelNum != 0) &&
2573                                (chplan_sta[i].ChannelNum <= 14)) {
2574                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
2575                                 chplan_new[k].ScanType = chplan_sta[i].ScanType;
2576                                 i++;
2577                                 k++;
2578                         }
2579
2580                         /*  skip AP 2.4G channel plan */
2581                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] <= 14))
2582                                 j++;
2583                 }
2584
2585                 /*  keep original STA 5G channel plan */
2586                 while ((i < MAX_CHANNEL_NUM) && (chplan_sta[i].ChannelNum != 0)) {
2587                         chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
2588                         chplan_new[k].ScanType = chplan_sta[i].ScanType;
2589                         i++;
2590                         k++;
2591                 }
2592
2593                 pmlmeext->update_channel_plan_by_ap_done = 1;
2594         }
2595
2596         /*  If channel is used by AP, set channel scan type to active */
2597         channel = bssid->Configuration.DSConfig;
2598         chplan_new = pmlmeext->channel_set;
2599         i = 0;
2600         while ((i < MAX_CHANNEL_NUM) && (chplan_new[i].ChannelNum != 0)) {
2601                 if (chplan_new[i].ChannelNum == channel) {
2602                         if (chplan_new[i].ScanType == SCAN_PASSIVE) {
2603                                 chplan_new[i].ScanType = SCAN_ACTIVE;
2604                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
2605                                          ("%s: change channel %d scan type from passive to active\n",
2606                                          __func__, channel));
2607                         }
2608                         break;
2609                 }
2610                 i++;
2611         }
2612 }
2613
2614 /****************************************************************************
2615
2616 Following are the callback functions for each subtype of the management frames
2617
2618 *****************************************************************************/
2619
2620 static unsigned int OnProbeReq(struct adapter *padapter,
2621                                struct recv_frame *precv_frame)
2622 {
2623         unsigned int    ielen;
2624         unsigned char   *p;
2625         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2626         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2627         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2628         struct wlan_bssid_ex *cur = &(pmlmeinfo->network);
2629         u8 *pframe = precv_frame->rx_data;
2630         uint len = precv_frame->len;
2631
2632         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
2633                 return _SUCCESS;
2634
2635         if (!check_fwstate(pmlmepriv, _FW_LINKED) &&
2636             !check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_AP_STATE))
2637                 return _SUCCESS;
2638
2639         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _PROBEREQ_IE_OFFSET_, _SSID_IE_, (int *)&ielen,
2640                         len - WLAN_HDR_A3_LEN - _PROBEREQ_IE_OFFSET_);
2641
2642         /* check (wildcard) SSID */
2643         if (p != NULL) {
2644                 if ((ielen != 0 && memcmp((void *)(p+2), (void *)cur->Ssid.Ssid, cur->Ssid.SsidLength)) ||
2645                     (ielen == 0 && pmlmeinfo->hidden_ssid_mode))
2646                         return _SUCCESS;
2647
2648                 if (check_fwstate(pmlmepriv, _FW_LINKED) &&
2649                     pmlmepriv->cur_network.join_res)
2650                         issue_probersp(padapter, get_sa(pframe));
2651         }
2652         return _SUCCESS;
2653 }
2654
2655 static unsigned int OnProbeRsp(struct adapter *padapter,
2656                                struct recv_frame *precv_frame)
2657 {
2658         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2659
2660         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
2661                 report_survey_event(padapter, precv_frame);
2662                 return _SUCCESS;
2663         }
2664
2665         return _SUCCESS;
2666 }
2667
2668 static unsigned int OnBeacon(struct adapter *padapter,
2669                              struct recv_frame *precv_frame)
2670 {
2671         int cam_idx;
2672         struct sta_info *psta;
2673         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2674         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2675         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2676         struct sta_priv *pstapriv = &padapter->stapriv;
2677         u8 *pframe = precv_frame->rx_data;
2678         uint len = precv_frame->len;
2679         struct wlan_bssid_ex *pbss;
2680         int ret = _SUCCESS;
2681         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
2682
2683         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
2684                 report_survey_event(padapter, precv_frame);
2685                 return _SUCCESS;
2686         }
2687
2688         if (!memcmp(GetAddr3Ptr(pframe), pnetwork->MacAddress, ETH_ALEN)) {
2689                 if (pmlmeinfo->state & WIFI_FW_AUTH_NULL) {
2690                         /* we should update current network before auth, or some IE is wrong */
2691                         pbss = (struct wlan_bssid_ex *)rtw_malloc(sizeof(struct wlan_bssid_ex));
2692                         if (pbss) {
2693                                 if (collect_bss_info(padapter, precv_frame, pbss) == _SUCCESS) {
2694                                         update_network(&(pmlmepriv->cur_network.network), pbss, padapter, true);
2695                                         rtw_get_bcn_info(&(pmlmepriv->cur_network));
2696                                 }
2697                                 kfree(pbss);
2698                         }
2699
2700                         /* check the vendor of the assoc AP */
2701                         pmlmeinfo->assoc_AP_vendor = check_assoc_AP(pframe+sizeof(struct rtw_ieee80211_hdr_3addr), len-sizeof(struct rtw_ieee80211_hdr_3addr));
2702
2703                         /* update TSF Value */
2704                         update_TSF(pmlmeext, pframe, len);
2705
2706                         /* start auth */
2707                         start_clnt_auth(padapter);
2708
2709                         return _SUCCESS;
2710                 }
2711
2712                 if (((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) && (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)) {
2713                         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
2714                         if (psta != NULL) {
2715                                 ret = rtw_check_bcn_info(padapter, pframe, len);
2716                                 if (!ret) {
2717                                                 DBG_88E_LEVEL(_drv_info_, "ap has changed, disconnect now\n ");
2718                                                 receive_disconnect(padapter, pmlmeinfo->network.MacAddress , 65535);
2719                                                 return _SUCCESS;
2720                                 }
2721                                 /* update WMM, ERP in the beacon */
2722                                 /* todo: the timer is used instead of the number of the beacon received */
2723                                 if ((sta_rx_pkts(psta) & 0xf) == 0)
2724                                         update_beacon_info(padapter, pframe, len, psta);
2725                         }
2726                 } else if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
2727                         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
2728                         if (psta != NULL) {
2729                                 /* update WMM, ERP in the beacon */
2730                                 /* todo: the timer is used instead of the number of the beacon received */
2731                                 if ((sta_rx_pkts(psta) & 0xf) == 0)
2732                                         update_beacon_info(padapter, pframe, len, psta);
2733                         } else {
2734                                 /* allocate a new CAM entry for IBSS station */
2735                                 cam_idx = allocate_fw_sta_entry(padapter);
2736                                 if (cam_idx == NUM_STA)
2737                                         goto _END_ONBEACON_;
2738
2739                                 /* get supported rate */
2740                                 if (update_sta_support_rate(padapter, (pframe + WLAN_HDR_A3_LEN + _BEACON_IE_OFFSET_), (len - WLAN_HDR_A3_LEN - _BEACON_IE_OFFSET_), cam_idx) == _FAIL) {
2741                                         pmlmeinfo->FW_sta_info[cam_idx].status = 0;
2742                                         goto _END_ONBEACON_;
2743                                 }
2744
2745                                 /* update TSF Value */
2746                                 update_TSF(pmlmeext, pframe, len);
2747
2748                                 /* report sta add event */
2749                                 report_add_sta_event(padapter, GetAddr2Ptr(pframe), cam_idx);
2750                         }
2751                 }
2752         }
2753
2754 _END_ONBEACON_:
2755
2756         return _SUCCESS;
2757 }
2758
2759 #ifdef CONFIG_88EU_AP_MODE
2760 static unsigned int OnAuth(struct adapter *padapter,
2761                            struct recv_frame *precv_frame)
2762 {
2763         unsigned int    auth_mode, ie_len;
2764         u16 seq;
2765         unsigned char   *sa, *p;
2766         u16 algorithm;
2767         int     status;
2768         static struct sta_info stat;
2769         struct  sta_info        *pstat = NULL;
2770         struct  sta_priv *pstapriv = &padapter->stapriv;
2771         struct security_priv *psecuritypriv = &padapter->securitypriv;
2772         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2773         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2774         u8 *pframe = precv_frame->rx_data;
2775         uint len = precv_frame->len;
2776
2777         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
2778                 return _FAIL;
2779
2780         DBG_88E("+OnAuth\n");
2781
2782         sa = GetAddr2Ptr(pframe);
2783
2784         auth_mode = psecuritypriv->dot11AuthAlgrthm;
2785         seq = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + 2));
2786         algorithm = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN));
2787
2788         DBG_88E("auth alg=%x, seq=%X\n", algorithm, seq);
2789
2790         if (auth_mode == 2 && psecuritypriv->dot11PrivacyAlgrthm != _WEP40_ &&
2791             psecuritypriv->dot11PrivacyAlgrthm != _WEP104_)
2792                 auth_mode = 0;
2793
2794         if ((algorithm > 0 && auth_mode == 0) ||        /*  rx a shared-key auth but shared not enabled */
2795             (algorithm == 0 && auth_mode == 1)) {       /*  rx a open-system auth but shared-key is enabled */
2796                 DBG_88E("auth rejected due to bad alg [alg=%d, auth_mib=%d] %02X%02X%02X%02X%02X%02X\n",
2797                         algorithm, auth_mode, sa[0], sa[1], sa[2], sa[3], sa[4], sa[5]);
2798
2799                 status = _STATS_NO_SUPP_ALG_;
2800
2801                 goto auth_fail;
2802         }
2803
2804         if (!rtw_access_ctrl(padapter, sa)) {
2805                 status = _STATS_UNABLE_HANDLE_STA_;
2806                 goto auth_fail;
2807         }
2808
2809         pstat = rtw_get_stainfo(pstapriv, sa);
2810         if (pstat == NULL) {
2811                 /*  allocate a new one */
2812                 DBG_88E("going to alloc stainfo for sa=%pM\n", sa);
2813                 pstat = rtw_alloc_stainfo(pstapriv, sa);
2814                 if (pstat == NULL) {
2815                         DBG_88E(" Exceed the upper limit of supported clients...\n");
2816                         status = _STATS_UNABLE_HANDLE_STA_;
2817                         goto auth_fail;
2818                 }
2819
2820                 pstat->state = WIFI_FW_AUTH_NULL;
2821                 pstat->auth_seq = 0;
2822         } else {
2823                 spin_lock_bh(&pstapriv->asoc_list_lock);
2824                 if (!list_empty(&pstat->asoc_list)) {
2825                         list_del_init(&pstat->asoc_list);
2826                         pstapriv->asoc_list_cnt--;
2827                 }
2828                 spin_unlock_bh(&pstapriv->asoc_list_lock);
2829
2830                 if (seq == 1) {
2831                         /* TODO: STA re_auth and auth timeout */
2832                 }
2833         }
2834
2835         spin_lock_bh(&pstapriv->auth_list_lock);
2836         if (list_empty(&pstat->auth_list)) {
2837                 list_add_tail(&pstat->auth_list, &pstapriv->auth_list);
2838                 pstapriv->auth_list_cnt++;
2839         }
2840         spin_unlock_bh(&pstapriv->auth_list_lock);
2841
2842         if (pstat->auth_seq == 0)
2843                 pstat->expire_to = pstapriv->auth_to;
2844
2845         if ((pstat->auth_seq + 1) != seq) {
2846                 DBG_88E("(1)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
2847                         seq, pstat->auth_seq+1);
2848                 status = _STATS_OUT_OF_AUTH_SEQ_;
2849                 goto auth_fail;
2850         }
2851
2852         if (algorithm == 0 && (auth_mode == 0 || auth_mode == 2)) {
2853                 if (seq == 1) {
2854                         pstat->state &= ~WIFI_FW_AUTH_NULL;
2855                         pstat->state |= WIFI_FW_AUTH_SUCCESS;
2856                         pstat->expire_to = pstapriv->assoc_to;
2857                         pstat->authalg = algorithm;
2858                 } else {
2859                         DBG_88E("(2)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
2860                                 seq, pstat->auth_seq+1);
2861                         status = _STATS_OUT_OF_AUTH_SEQ_;
2862                         goto auth_fail;
2863                 }
2864         } else { /*  shared system or auto authentication */
2865                 if (seq == 1) {
2866                         /* prepare for the challenging txt... */
2867
2868                         pstat->state &= ~WIFI_FW_AUTH_NULL;
2869                         pstat->state |= WIFI_FW_AUTH_STATE;
2870                         pstat->authalg = algorithm;
2871                         pstat->auth_seq = 2;
2872                 } else if (seq == 3) {
2873                         /* checking for challenging txt... */
2874                         DBG_88E("checking for challenging txt...\n");
2875
2876                         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + 4 + _AUTH_IE_OFFSET_ , _CHLGETXT_IE_, (int *)&ie_len,
2877                                         len - WLAN_HDR_A3_LEN - _AUTH_IE_OFFSET_ - 4);
2878
2879                         if ((p == NULL) || (ie_len <= 0)) {
2880                                 DBG_88E("auth rejected because challenge failure!(1)\n");
2881                                 status = _STATS_CHALLENGE_FAIL_;
2882                                 goto auth_fail;
2883                         }
2884
2885                         if (!memcmp((void *)(p + 2), pstat->chg_txt, 128)) {
2886                                 pstat->state &= (~WIFI_FW_AUTH_STATE);
2887                                 pstat->state |= WIFI_FW_AUTH_SUCCESS;
2888                                 /*  challenging txt is correct... */
2889                                 pstat->expire_to =  pstapriv->assoc_to;
2890                         } else {
2891                                 DBG_88E("auth rejected because challenge failure!\n");
2892                                 status = _STATS_CHALLENGE_FAIL_;
2893                                 goto auth_fail;
2894                         }
2895                 } else {
2896                         DBG_88E("(3)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
2897                                 seq, pstat->auth_seq+1);
2898                         status = _STATS_OUT_OF_AUTH_SEQ_;
2899                         goto auth_fail;
2900                 }
2901         }
2902
2903         /*  Now, we are going to issue_auth... */
2904         pstat->auth_seq = seq + 1;
2905
2906         issue_auth(padapter, pstat, (unsigned short)(_STATS_SUCCESSFUL_));
2907
2908         if (pstat->state & WIFI_FW_AUTH_SUCCESS)
2909                 pstat->auth_seq = 0;
2910
2911         return _SUCCESS;
2912
2913 auth_fail:
2914
2915         if (pstat)
2916                 rtw_free_stainfo(padapter, pstat);
2917
2918         pstat = &stat;
2919         memset((char *)pstat, '\0', sizeof(stat));
2920         pstat->auth_seq = 2;
2921         memcpy(pstat->hwaddr, sa, 6);
2922
2923         issue_auth(padapter, pstat, (unsigned short)status);
2924
2925         return _FAIL;
2926 }
2927 #endif /* CONFIG_88EU_AP_MODE */
2928
2929 static unsigned int OnAuthClient(struct adapter *padapter,
2930                                  struct recv_frame *precv_frame)
2931 {
2932         unsigned int    seq, len, status, offset;
2933         unsigned char   *p;
2934         unsigned int    go2asoc = 0;
2935         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2936         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2937         u8 *pframe = precv_frame->rx_data;
2938         uint pkt_len = precv_frame->len;
2939
2940         DBG_88E("%s\n", __func__);
2941
2942         /* check A1 matches or not */
2943         if (memcmp(myid(&(padapter->eeprompriv)), get_da(pframe), ETH_ALEN))
2944                 return _SUCCESS;
2945
2946         if (!(pmlmeinfo->state & WIFI_FW_AUTH_STATE))
2947                 return _SUCCESS;
2948
2949         offset = (GetPrivacy(pframe)) ? 4 : 0;
2950
2951         seq     = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + offset + 2));
2952         status  = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + offset + 4));
2953
2954         if (status != 0) {
2955                 DBG_88E("clnt auth fail, status: %d\n", status);
2956                 if (status == 13) { /*  pmlmeinfo->auth_algo == dot11AuthAlgrthm_Auto) */
2957                         if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared)
2958                                 pmlmeinfo->auth_algo = dot11AuthAlgrthm_Open;
2959                         else
2960                                 pmlmeinfo->auth_algo = dot11AuthAlgrthm_Shared;
2961                 }
2962
2963                 set_link_timer(pmlmeext, 1);
2964                 goto authclnt_fail;
2965         }
2966
2967         if (seq == 2) {
2968                 if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared) {
2969                         /*  legendary shared system */
2970                         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _AUTH_IE_OFFSET_, _CHLGETXT_IE_, (int *)&len,
2971                                 pkt_len - WLAN_HDR_A3_LEN - _AUTH_IE_OFFSET_);
2972
2973                         if (p == NULL)
2974                                 goto authclnt_fail;
2975
2976                         memcpy((void *)(pmlmeinfo->chg_txt), (void *)(p + 2), len);
2977                         pmlmeinfo->auth_seq = 3;
2978                         issue_auth(padapter, NULL, 0);
2979                         set_link_timer(pmlmeext, REAUTH_TO);
2980
2981                         return _SUCCESS;
2982                 } else {
2983                         /*  open system */
2984                         go2asoc = 1;
2985                 }
2986         } else if (seq == 4) {
2987                 if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared)
2988                         go2asoc = 1;
2989                 else
2990                         goto authclnt_fail;
2991         } else {
2992                 /*  this is also illegal */
2993                 goto authclnt_fail;
2994         }
2995
2996         if (go2asoc) {
2997                 DBG_88E_LEVEL(_drv_info_, "auth success, start assoc\n");
2998                 start_clnt_assoc(padapter);
2999                 return _SUCCESS;
3000         }
3001 authclnt_fail:
3002         return _FAIL;
3003 }
3004
3005 static unsigned int OnAssocReq(struct adapter *padapter,
3006                                struct recv_frame *precv_frame)
3007 {
3008 #ifdef CONFIG_88EU_AP_MODE
3009         u16 capab_info;
3010         struct rtw_ieee802_11_elems elems;
3011         struct sta_info *pstat;
3012         unsigned char           reassoc, *p, *pos, *wpa_ie;
3013         unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
3014         int             i, ie_len, wpa_ie_len, left;
3015         unsigned char           supportRate[16];
3016         int                                     supportRateNum;
3017         unsigned short          status = _STATS_SUCCESSFUL_;
3018         unsigned short          frame_type, ie_offset = 0;
3019         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3020         struct security_priv *psecuritypriv = &padapter->securitypriv;
3021         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3022         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3023         struct wlan_bssid_ex *cur = &(pmlmeinfo->network);
3024         struct sta_priv *pstapriv = &padapter->stapriv;
3025         u8 *pframe = precv_frame->rx_data;
3026         uint pkt_len = precv_frame->len;
3027
3028         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
3029                 return _FAIL;
3030
3031         frame_type = GetFrameSubType(pframe);
3032         if (frame_type == WIFI_ASSOCREQ) {
3033                 reassoc = 0;
3034                 ie_offset = _ASOCREQ_IE_OFFSET_;
3035         } else { /*  WIFI_REASSOCREQ */
3036                 reassoc = 1;
3037                 ie_offset = _REASOCREQ_IE_OFFSET_;
3038         }
3039
3040
3041         if (pkt_len < IEEE80211_3ADDR_LEN + ie_offset) {
3042                 DBG_88E("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
3043                        "\n", reassoc, (unsigned long)pkt_len);
3044                 return _FAIL;
3045         }
3046
3047         pstat = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
3048         if (pstat == NULL) {
3049                 status = _RSON_CLS2_;
3050                 goto asoc_class2_error;
3051         }
3052
3053         capab_info = get_unaligned_le16(pframe + WLAN_HDR_A3_LEN);
3054
3055         left = pkt_len - (IEEE80211_3ADDR_LEN + ie_offset);
3056         pos = pframe + (IEEE80211_3ADDR_LEN + ie_offset);
3057
3058
3059         DBG_88E("%s\n", __func__);
3060
3061         /*  check if this stat has been successfully authenticated/assocated */
3062         if (!((pstat->state) & WIFI_FW_AUTH_SUCCESS)) {
3063                 if (!((pstat->state) & WIFI_FW_ASSOC_SUCCESS)) {
3064                         status = _RSON_CLS2_;
3065                         goto asoc_class2_error;
3066                 } else {
3067                         pstat->state &= (~WIFI_FW_ASSOC_SUCCESS);
3068                         pstat->state |= WIFI_FW_ASSOC_STATE;
3069                 }
3070         } else {
3071                 pstat->state &= (~WIFI_FW_AUTH_SUCCESS);
3072                 pstat->state |= WIFI_FW_ASSOC_STATE;
3073         }
3074         pstat->capability = capab_info;
3075         /* now parse all ieee802_11 ie to point to elems */
3076         if (rtw_ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
3077             !elems.ssid) {
3078                 DBG_88E("STA %pM sent invalid association request\n",
3079                         pstat->hwaddr);
3080                 status = _STATS_FAILURE_;
3081                 goto OnAssocReqFail;
3082         }
3083
3084
3085         /*  now we should check all the fields... */
3086         /*  checking SSID */
3087         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _SSID_IE_, &ie_len,
3088                 pkt_len - WLAN_HDR_A3_LEN - ie_offset);
3089         if (p == NULL)
3090                 status = _STATS_FAILURE_;
3091
3092         if (ie_len == 0) { /*  broadcast ssid, however it is not allowed in assocreq */
3093                 status = _STATS_FAILURE_;
3094         } else {
3095                 /*  check if ssid match */
3096                 if (memcmp((void *)(p+2), cur->Ssid.Ssid, cur->Ssid.SsidLength))
3097                         status = _STATS_FAILURE_;
3098
3099                 if (ie_len != cur->Ssid.SsidLength)
3100                         status = _STATS_FAILURE_;
3101         }
3102
3103         if (_STATS_SUCCESSFUL_ != status)
3104                 goto OnAssocReqFail;
3105
3106         /*  check if the supported rate is ok */
3107         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _SUPPORTEDRATES_IE_, &ie_len, pkt_len - WLAN_HDR_A3_LEN - ie_offset);
3108         if (p == NULL) {
3109                 DBG_88E("Rx a sta assoc-req which supported rate is empty!\n");
3110                 /*  use our own rate set as statoin used */
3111                 /* memcpy(supportRate, AP_BSSRATE, AP_BSSRATE_LEN); */
3112                 /* supportRateNum = AP_BSSRATE_LEN; */
3113
3114                 status = _STATS_FAILURE_;
3115                 goto OnAssocReqFail;
3116         } else {
3117                 memcpy(supportRate, p+2, ie_len);
3118                 supportRateNum = ie_len;
3119
3120                 p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _EXT_SUPPORTEDRATES_IE_ , &ie_len,
3121                                 pkt_len - WLAN_HDR_A3_LEN - ie_offset);
3122                 if (p !=  NULL) {
3123                         if (supportRateNum <= sizeof(supportRate)) {
3124                                 memcpy(supportRate+supportRateNum, p+2, ie_len);
3125                                 supportRateNum += ie_len;
3126                         }
3127                 }
3128         }
3129
3130         /* todo: mask supportRate between AP & STA -> move to update raid */
3131         /* get_matched_rate(pmlmeext, supportRate, &supportRateNum, 0); */
3132
3133         /* update station supportRate */
3134         pstat->bssratelen = supportRateNum;
3135         memcpy(pstat->bssrateset, supportRate, supportRateNum);
3136         UpdateBrateTblForSoftAP(pstat->bssrateset, pstat->bssratelen);
3137
3138         /* check RSN/WPA/WPS */
3139         pstat->dot8021xalg = 0;
3140         pstat->wpa_psk = 0;
3141         pstat->wpa_group_cipher = 0;
3142         pstat->wpa2_group_cipher = 0;
3143         pstat->wpa_pairwise_cipher = 0;
3144         pstat->wpa2_pairwise_cipher = 0;
3145         memset(pstat->wpa_ie, 0, sizeof(pstat->wpa_ie));
3146         if ((psecuritypriv->wpa_psk & BIT(1)) && elems.rsn_ie) {
3147                 int group_cipher = 0, pairwise_cipher = 0;
3148
3149                 wpa_ie = elems.rsn_ie;
3150                 wpa_ie_len = elems.rsn_ie_len;
3151
3152                 if (rtw_parse_wpa2_ie(wpa_ie-2, wpa_ie_len+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
3153                         pstat->dot8021xalg = 1;/* psk,  todo:802.1x */
3154                         pstat->wpa_psk |= BIT(1);
3155
3156                         pstat->wpa2_group_cipher = group_cipher&psecuritypriv->wpa2_group_cipher;
3157                         pstat->wpa2_pairwise_cipher = pairwise_cipher&psecuritypriv->wpa2_pairwise_cipher;
3158
3159                         if (!pstat->wpa2_group_cipher)
3160                                 status = WLAN_STATUS_INVALID_GROUP_CIPHER;
3161
3162                         if (!pstat->wpa2_pairwise_cipher)
3163                                 status = WLAN_STATUS_INVALID_PAIRWISE_CIPHER;
3164                 } else {
3165                         status = WLAN_STATUS_INVALID_IE;
3166                 }
3167         } else if ((psecuritypriv->wpa_psk & BIT(0)) && elems.wpa_ie) {
3168                 int group_cipher = 0, pairwise_cipher = 0;
3169
3170                 wpa_ie = elems.wpa_ie;
3171                 wpa_ie_len = elems.wpa_ie_len;
3172
3173                 if (rtw_parse_wpa_ie(wpa_ie-2, wpa_ie_len+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
3174                         pstat->dot8021xalg = 1;/* psk,  todo:802.1x */
3175                         pstat->wpa_psk |= BIT(0);
3176
3177                         pstat->wpa_group_cipher = group_cipher&psecuritypriv->wpa_group_cipher;
3178                         pstat->wpa_pairwise_cipher = pairwise_cipher&psecuritypriv->wpa_pairwise_cipher;
3179
3180                         if (!pstat->wpa_group_cipher)
3181                                 status = WLAN_STATUS_INVALID_GROUP_CIPHER;
3182
3183                         if (!pstat->wpa_pairwise_cipher)
3184                                 status = WLAN_STATUS_INVALID_PAIRWISE_CIPHER;
3185                 } else {
3186                         status = WLAN_STATUS_INVALID_IE;
3187                 }
3188         } else {
3189                 wpa_ie = NULL;
3190                 wpa_ie_len = 0;
3191         }
3192
3193         if (_STATS_SUCCESSFUL_ != status)
3194                 goto OnAssocReqFail;
3195
3196         pstat->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
3197         if (wpa_ie == NULL) {
3198                 if (elems.wps_ie) {
3199                         DBG_88E("STA included WPS IE in "
3200                                    "(Re)Association Request - assume WPS is "
3201                                    "used\n");
3202                         pstat->flags |= WLAN_STA_WPS;
3203                         /* wpabuf_free(sta->wps_ie); */
3204                         /* sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4, */
3205                         /*                              elems.wps_ie_len - 4); */
3206                 } else {
3207                         DBG_88E("STA did not include WPA/RSN IE "
3208                                    "in (Re)Association Request - possible WPS "
3209                                    "use\n");
3210                         pstat->flags |= WLAN_STA_MAYBE_WPS;
3211                 }
3212
3213
3214                 /*  AP support WPA/RSN, and sta is going to do WPS, but AP is not ready */
3215                 /*  that the selected registrar of AP is _FLASE */
3216                 if ((psecuritypriv->wpa_psk > 0) && (pstat->flags & (WLAN_STA_WPS|WLAN_STA_MAYBE_WPS))) {
3217                         if (pmlmepriv->wps_beacon_ie) {
3218                                 u8 selected_registrar = 0;
3219
3220                                 rtw_get_wps_attr_content(pmlmepriv->wps_beacon_ie, pmlmepriv->wps_beacon_ie_len, WPS_ATTR_SELECTED_REGISTRAR , &selected_registrar, NULL);
3221
3222                                 if (!selected_registrar) {
3223                                         DBG_88E("selected_registrar is false , or AP is not ready to do WPS\n");
3224
3225                                         status = _STATS_UNABLE_HANDLE_STA_;
3226
3227                                         goto OnAssocReqFail;
3228                                 }
3229                         }
3230                 }
3231         } else {
3232                 int copy_len;
3233
3234                 if (psecuritypriv->wpa_psk == 0) {
3235                         DBG_88E("STA %pM: WPA/RSN IE in association "
3236                         "request, but AP don't support WPA/RSN\n", pstat->hwaddr);
3237
3238                         status = WLAN_STATUS_INVALID_IE;
3239
3240                         goto OnAssocReqFail;
3241                 }
3242
3243                 if (elems.wps_ie) {
3244                         DBG_88E("STA included WPS IE in "
3245                                    "(Re)Association Request - WPS is "
3246                                    "used\n");
3247                         pstat->flags |= WLAN_STA_WPS;
3248                         copy_len = 0;
3249                 } else {
3250                         copy_len = min_t(int, wpa_ie_len + 2, sizeof(pstat->wpa_ie));
3251                 }
3252                 if (copy_len > 0)
3253                         memcpy(pstat->wpa_ie, wpa_ie-2, copy_len);
3254         }
3255         /*  check if there is WMM IE & support WWM-PS */
3256         pstat->flags &= ~WLAN_STA_WME;
3257         pstat->qos_option = 0;
3258         pstat->qos_info = 0;
3259         pstat->has_legacy_ac = true;
3260         pstat->uapsd_vo = 0;
3261         pstat->uapsd_vi = 0;
3262         pstat->uapsd_be = 0;
3263         pstat->uapsd_bk = 0;
3264         if (pmlmepriv->qospriv.qos_option) {
3265                 p = pframe + WLAN_HDR_A3_LEN + ie_offset; ie_len = 0;
3266                 for (;;) {
3267                         p = rtw_get_ie(p, _VENDOR_SPECIFIC_IE_, &ie_len, pkt_len - WLAN_HDR_A3_LEN - ie_offset);
3268                         if (p != NULL) {
3269                                 if (!memcmp(p+2, WMM_IE, 6)) {
3270                                         pstat->flags |= WLAN_STA_WME;
3271
3272                                         pstat->qos_option = 1;
3273                                         pstat->qos_info = *(p+8);
3274
3275                                         pstat->max_sp_len = (pstat->qos_info>>5)&0x3;
3276
3277                                         if ((pstat->qos_info&0xf) != 0xf)
3278                                                 pstat->has_legacy_ac = true;
3279                                         else
3280                                                 pstat->has_legacy_ac = false;
3281
3282                                         if (pstat->qos_info&0xf) {
3283                                                 if (pstat->qos_info&BIT(0))
3284                                                         pstat->uapsd_vo = BIT(0)|BIT(1);
3285                                                 else
3286                                                         pstat->uapsd_vo = 0;
3287
3288                                                 if (pstat->qos_info&BIT(1))
3289                                                         pstat->uapsd_vi = BIT(0)|BIT(1);
3290                                                 else
3291                                                         pstat->uapsd_vi = 0;
3292
3293                                                 if (pstat->qos_info&BIT(2))
3294                                                         pstat->uapsd_bk = BIT(0)|BIT(1);
3295                                                 else
3296                                                         pstat->uapsd_bk = 0;
3297
3298                                                 if (pstat->qos_info&BIT(3))
3299                                                         pstat->uapsd_be = BIT(0)|BIT(1);
3300                                                 else
3301                                                         pstat->uapsd_be = 0;
3302                                         }
3303                                         break;
3304                                 }
3305                         } else {
3306                                 break;
3307                         }
3308                         p = p + ie_len + 2;
3309                 }
3310         }
3311
3312         /* save HT capabilities in the sta object */
3313         memset(&pstat->htpriv.ht_cap, 0, sizeof(struct rtw_ieee80211_ht_cap));
3314         if (elems.ht_capabilities && elems.ht_capabilities_len >= sizeof(struct rtw_ieee80211_ht_cap)) {
3315                 pstat->flags |= WLAN_STA_HT;
3316
3317                 pstat->flags |= WLAN_STA_WME;
3318
3319                 memcpy(&pstat->htpriv.ht_cap, elems.ht_capabilities, sizeof(struct rtw_ieee80211_ht_cap));
3320         } else {
3321                 pstat->flags &= ~WLAN_STA_HT;
3322         }
3323         if ((!pmlmepriv->htpriv.ht_option) && (pstat->flags&WLAN_STA_HT)) {
3324                 status = _STATS_FAILURE_;
3325                 goto OnAssocReqFail;
3326         }
3327
3328         if ((pstat->flags & WLAN_STA_HT) &&
3329             ((pstat->wpa2_pairwise_cipher&WPA_CIPHER_TKIP) ||
3330             (pstat->wpa_pairwise_cipher&WPA_CIPHER_TKIP))) {
3331                 DBG_88E("HT: %pM tried to "
3332                         "use TKIP with HT association\n", pstat->hwaddr);
3333
3334                 /* status = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY; */
3335                 /* goto OnAssocReqFail; */
3336         }
3337
3338         pstat->flags |= WLAN_STA_NONERP;
3339         for (i = 0; i < pstat->bssratelen; i++) {
3340                 if ((pstat->bssrateset[i] & 0x7f) > 22) {
3341                         pstat->flags &= ~WLAN_STA_NONERP;
3342                         break;
3343                 }
3344         }
3345
3346         if (pstat->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3347                 pstat->flags |= WLAN_STA_SHORT_PREAMBLE;
3348         else
3349                 pstat->flags &= ~WLAN_STA_SHORT_PREAMBLE;
3350
3351
3352
3353         if (status != _STATS_SUCCESSFUL_)
3354                 goto OnAssocReqFail;
3355
3356         /* TODO: identify_proprietary_vendor_ie(); */
3357         /*  Realtek proprietary IE */
3358         /*  identify if this is Broadcom sta */
3359         /*  identify if this is ralink sta */
3360         /*  Customer proprietary IE */
3361
3362         /* get a unique AID */
3363         if (pstat->aid > 0) {
3364                 DBG_88E("  old AID %d\n", pstat->aid);
3365         } else {
3366                 for (pstat->aid = 1; pstat->aid <= NUM_STA; pstat->aid++)
3367                         if (pstapriv->sta_aid[pstat->aid - 1] == NULL)
3368                                 break;
3369
3370                 /* if (pstat->aid > NUM_STA) { */
3371                 if (pstat->aid > pstapriv->max_num_sta) {
3372                         pstat->aid = 0;
3373
3374                         DBG_88E("  no room for more AIDs\n");
3375
3376                         status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
3377
3378                         goto OnAssocReqFail;
3379                 } else {
3380                         pstapriv->sta_aid[pstat->aid - 1] = pstat;
3381                         DBG_88E("allocate new AID=(%d)\n", pstat->aid);
3382                 }
3383         }
3384
3385         pstat->state &= (~WIFI_FW_ASSOC_STATE);
3386         pstat->state |= WIFI_FW_ASSOC_SUCCESS;
3387
3388         spin_lock_bh(&pstapriv->auth_list_lock);
3389         if (!list_empty(&pstat->auth_list)) {
3390                 list_del_init(&pstat->auth_list);
3391                 pstapriv->auth_list_cnt--;
3392         }
3393         spin_unlock_bh(&pstapriv->auth_list_lock);
3394
3395         spin_lock_bh(&pstapriv->asoc_list_lock);
3396         if (list_empty(&pstat->asoc_list)) {
3397                 pstat->expire_to = pstapriv->expire_to;
3398                 list_add_tail(&pstat->asoc_list, &pstapriv->asoc_list);
3399                 pstapriv->asoc_list_cnt++;
3400         }
3401         spin_unlock_bh(&pstapriv->asoc_list_lock);
3402
3403         /*  now the station is qualified to join our BSS... */
3404         if ((pstat->state & WIFI_FW_ASSOC_SUCCESS) && (_STATS_SUCCESSFUL_ == status)) {
3405                 /* 1 bss_cap_update & sta_info_update */
3406                 bss_cap_update_on_sta_join(padapter, pstat);
3407                 sta_info_update(padapter, pstat);
3408
3409                 /* issue assoc rsp before notify station join event. */
3410                 if (frame_type == WIFI_ASSOCREQ)
3411                         issue_asocrsp(padapter, status, pstat, WIFI_ASSOCRSP);
3412                 else
3413                         issue_asocrsp(padapter, status, pstat, WIFI_REASSOCRSP);
3414
3415                 /* 2 - report to upper layer */
3416                 DBG_88E("indicate_sta_join_event to upper layer - hostapd\n");
3417                 rtw_indicate_sta_assoc_event(padapter, pstat);
3418
3419                 /* 3-(1) report sta add event */
3420                 report_add_sta_event(padapter, pstat->hwaddr, pstat->aid);
3421         }
3422
3423         return _SUCCESS;
3424
3425 asoc_class2_error:
3426
3427         issue_deauth(padapter, (void *)GetAddr2Ptr(pframe), status);
3428
3429         return _FAIL;
3430
3431 OnAssocReqFail:
3432
3433         pstat->aid = 0;
3434         if (frame_type == WIFI_ASSOCREQ)
3435                 issue_asocrsp(padapter, status, pstat, WIFI_ASSOCRSP);
3436         else
3437                 issue_asocrsp(padapter, status, pstat, WIFI_REASSOCRSP);
3438
3439 #endif /* CONFIG_88EU_AP_MODE */
3440
3441         return _FAIL;
3442 }
3443
3444 static unsigned int OnAssocRsp(struct adapter *padapter,
3445                                struct recv_frame *precv_frame)
3446 {
3447         uint i;
3448         int res;
3449         unsigned short  status;
3450         struct ndis_802_11_var_ie *pIE;
3451         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3452         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3453         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3454         /* struct wlan_bssid_ex *cur_network = &(pmlmeinfo->network); */
3455         u8 *pframe = precv_frame->rx_data;
3456         uint pkt_len = precv_frame->len;
3457
3458         DBG_88E("%s\n", __func__);
3459
3460         /* check A1 matches or not */
3461         if (memcmp(myid(&(padapter->eeprompriv)), get_da(pframe), ETH_ALEN))
3462                 return _SUCCESS;
3463
3464         if (!(pmlmeinfo->state & (WIFI_FW_AUTH_SUCCESS | WIFI_FW_ASSOC_STATE)))
3465                 return _SUCCESS;
3466
3467         if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)
3468                 return _SUCCESS;
3469
3470         del_timer_sync(&pmlmeext->link_timer);
3471
3472         /* status */
3473         status = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 2));
3474         if (status > 0) {
3475                 DBG_88E("assoc reject, status code: %d\n", status);
3476                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
3477                 res = -4;
3478                 goto report_assoc_result;
3479         }
3480
3481         /* get capabilities */
3482         pmlmeinfo->capability = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
3483
3484         /* set slot time */
3485         pmlmeinfo->slotTime = (pmlmeinfo->capability & BIT(10)) ? 9 : 20;
3486
3487         /* AID */
3488         pmlmeinfo->aid = (int)(le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 4))&0x3fff);
3489         res = pmlmeinfo->aid;
3490
3491         /* following are moved to join event callback function */
3492         /* to handle HT, WMM, rate adaptive, update MAC reg */
3493         /* for not to handle the synchronous IO in the tasklet */
3494         for (i = (6 + WLAN_HDR_A3_LEN); i < pkt_len;) {
3495                 pIE = (struct ndis_802_11_var_ie *)(pframe + i);
3496
3497                 switch (pIE->ElementID) {
3498                 case _VENDOR_SPECIFIC_IE_:
3499                         if (!memcmp(pIE->data, WMM_PARA_OUI, 6)) /* WMM */
3500                                 WMM_param_handler(padapter, pIE);
3501                         break;
3502                 case _HT_CAPABILITY_IE_:        /* HT caps */
3503                         HT_caps_handler(padapter, pIE);
3504                         break;
3505                 case _HT_EXTRA_INFO_IE_:        /* HT info */
3506                         HT_info_handler(padapter, pIE);
3507                         break;
3508                 case _ERPINFO_IE_:
3509                         ERP_IE_handler(padapter, pIE);
3510                 default:
3511                         break;
3512                 }
3513
3514                 i += (pIE->Length + 2);
3515         }
3516
3517         pmlmeinfo->state &= (~WIFI_FW_ASSOC_STATE);
3518         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
3519
3520         UpdateBrateTbl(padapter, pmlmeinfo->network.SupportedRates);
3521
3522 report_assoc_result:
3523         if (res > 0) {
3524                 rtw_buf_update(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len, pframe, pkt_len);
3525         } else {
3526                 rtw_buf_free(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len);
3527         }
3528
3529         report_join_res(padapter, res);
3530
3531         return _SUCCESS;
3532 }
3533
3534 static unsigned int OnDeAuth(struct adapter *padapter,
3535                              struct recv_frame *precv_frame)
3536 {
3537         unsigned short  reason;
3538         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3539         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3540         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3541         u8 *pframe = precv_frame->rx_data;
3542         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
3543
3544         /* check A3 */
3545         if (memcmp(GetAddr3Ptr(pframe), pnetwork->MacAddress, ETH_ALEN))
3546                 return _SUCCESS;
3547
3548         reason = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
3549
3550         DBG_88E("%s Reason code(%d)\n", __func__, reason);
3551
3552 #ifdef CONFIG_88EU_AP_MODE
3553         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
3554                 struct sta_info *psta;
3555                 struct sta_priv *pstapriv = &padapter->stapriv;
3556
3557                 DBG_88E_LEVEL(_drv_always_, "ap recv deauth reason code(%d) sta:%pM\n",
3558                               reason, GetAddr2Ptr(pframe));
3559
3560                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
3561                 if (psta) {
3562                         u8 updated = 0;
3563
3564                         spin_lock_bh(&pstapriv->asoc_list_lock);
3565                         if (!list_empty(&psta->asoc_list)) {
3566                                 list_del_init(&psta->asoc_list);
3567                                 pstapriv->asoc_list_cnt--;
3568                                 updated = ap_free_sta(padapter, psta, false, reason);
3569                         }
3570                         spin_unlock_bh(&pstapriv->asoc_list_lock);
3571
3572                         associated_clients_update(padapter, updated);
3573                 }
3574
3575
3576                 return _SUCCESS;
3577         } else
3578 #endif
3579         {
3580                 DBG_88E_LEVEL(_drv_always_, "sta recv deauth reason code(%d) sta:%pM\n",
3581                               reason, GetAddr3Ptr(pframe));
3582
3583                 receive_disconnect(padapter, GetAddr3Ptr(pframe) , reason);
3584         }
3585         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
3586         return _SUCCESS;
3587 }
3588
3589 static unsigned int OnDisassoc(struct adapter *padapter,
3590                                struct recv_frame *precv_frame)
3591 {
3592         u16 reason;
3593         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3594         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3595         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3596         u8 *pframe = precv_frame->rx_data;
3597         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
3598
3599         /* check A3 */
3600         if (memcmp(GetAddr3Ptr(pframe), pnetwork->MacAddress, ETH_ALEN))
3601                 return _SUCCESS;
3602
3603         reason = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
3604
3605         DBG_88E("%s Reason code(%d)\n", __func__, reason);
3606
3607 #ifdef CONFIG_88EU_AP_MODE
3608         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
3609                 struct sta_info *psta;
3610                 struct sta_priv *pstapriv = &padapter->stapriv;
3611
3612                 DBG_88E_LEVEL(_drv_always_, "ap recv disassoc reason code(%d) sta:%pM\n",
3613                               reason, GetAddr2Ptr(pframe));
3614
3615                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
3616                 if (psta) {
3617                         u8 updated = 0;
3618
3619                         spin_lock_bh(&pstapriv->asoc_list_lock);
3620                         if (!list_empty(&psta->asoc_list)) {
3621                                 list_del_init(&psta->asoc_list);
3622                                 pstapriv->asoc_list_cnt--;
3623                                 updated = ap_free_sta(padapter, psta, false, reason);
3624                         }
3625                         spin_unlock_bh(&pstapriv->asoc_list_lock);
3626
3627                         associated_clients_update(padapter, updated);
3628                 }
3629
3630                 return _SUCCESS;
3631         } else
3632 #endif
3633         {
3634                 DBG_88E_LEVEL(_drv_always_, "ap recv disassoc reason code(%d) sta:%pM\n",
3635                               reason, GetAddr3Ptr(pframe));
3636
3637                 receive_disconnect(padapter, GetAddr3Ptr(pframe), reason);
3638         }
3639         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
3640         return _SUCCESS;
3641 }
3642
3643 static unsigned int OnAtim(struct adapter *padapter,
3644                            struct recv_frame *precv_frame)
3645 {
3646         DBG_88E("%s\n", __func__);
3647         return _SUCCESS;
3648 }
3649
3650 static unsigned int on_action_spct(struct adapter *padapter,
3651                                    struct recv_frame *precv_frame)
3652 {
3653         struct sta_info *psta = NULL;
3654         struct sta_priv *pstapriv = &padapter->stapriv;
3655         u8 *pframe = precv_frame->rx_data;
3656         u8 *frame_body = (u8 *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
3657         u8 category;
3658         u8 action;
3659
3660         DBG_88E(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(padapter->pnetdev));
3661
3662         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
3663
3664         if (!psta)
3665                 goto exit;
3666
3667         category = frame_body[0];
3668         if (category != RTW_WLAN_CATEGORY_SPECTRUM_MGMT)
3669                 goto exit;
3670
3671         action = frame_body[1];
3672         switch (action) {
3673         case RTW_WLAN_ACTION_SPCT_MSR_REQ:
3674         case RTW_WLAN_ACTION_SPCT_MSR_RPRT:
3675         case RTW_WLAN_ACTION_SPCT_TPC_REQ:
3676         case RTW_WLAN_ACTION_SPCT_TPC_RPRT:
3677                 break;
3678         case RTW_WLAN_ACTION_SPCT_CHL_SWITCH:
3679                 break;
3680         default:
3681                 break;
3682         }
3683
3684 exit:
3685         return _FAIL;
3686 }
3687
3688 static unsigned int OnAction_qos(struct adapter *padapter,
3689                                  struct recv_frame *precv_frame)
3690 {
3691         return _SUCCESS;
3692 }
3693
3694 static unsigned int OnAction_dls(struct adapter *padapter,
3695                                  struct recv_frame *precv_frame)
3696 {
3697         return _SUCCESS;
3698 }
3699
3700 static unsigned int OnAction_back(struct adapter *padapter,
3701                                   struct recv_frame *precv_frame)
3702 {
3703         u8 *addr;
3704         struct sta_info *psta = NULL;
3705         struct recv_reorder_ctrl *preorder_ctrl;
3706         unsigned char           *frame_body;
3707         unsigned char           category, action;
3708         unsigned short  tid, status, reason_code = 0;
3709         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3710         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3711         u8 *pframe = precv_frame->rx_data;
3712         struct sta_priv *pstapriv = &padapter->stapriv;
3713         /* check RA matches or not */
3714         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe),
3715                    ETH_ALEN))/* for if1, sta/ap mode */
3716                 return _SUCCESS;
3717
3718         DBG_88E("%s\n", __func__);
3719
3720         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
3721                 if (!(pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS))
3722                         return _SUCCESS;
3723
3724         addr = GetAddr2Ptr(pframe);
3725         psta = rtw_get_stainfo(pstapriv, addr);
3726
3727         if (psta == NULL)
3728                 return _SUCCESS;
3729
3730         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
3731
3732         category = frame_body[0];
3733         if (category == RTW_WLAN_CATEGORY_BACK) { /*  representing Block Ack */
3734                 if (!pmlmeinfo->HT_enable)
3735                         return _SUCCESS;
3736                 action = frame_body[1];
3737                 DBG_88E("%s, action=%d\n", __func__, action);
3738                 switch (action) {
3739                 case RTW_WLAN_ACTION_ADDBA_REQ: /* ADDBA request */
3740                         memcpy(&(pmlmeinfo->ADDBA_req), &(frame_body[2]), sizeof(struct ADDBA_request));
3741                         process_addba_req(padapter, (u8 *)&(pmlmeinfo->ADDBA_req), addr);
3742
3743                         if (pmlmeinfo->bAcceptAddbaReq)
3744                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_ADDBA_RESP, 0);
3745                         else
3746                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_ADDBA_RESP, 37);/* reject ADDBA Req */
3747                         break;
3748                 case RTW_WLAN_ACTION_ADDBA_RESP: /* ADDBA response */
3749                         status = get_unaligned_le16(&frame_body[3]);
3750                         tid = (frame_body[5] >> 2) & 0x7;
3751                         if (status == 0) {      /* successful */
3752                                 DBG_88E("agg_enable for TID=%d\n", tid);
3753                                 psta->htpriv.agg_enable_bitmap |= 1 << tid;
3754                                 psta->htpriv.candidate_tid_bitmap &= ~BIT(tid);
3755                         } else {
3756                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
3757                         }
3758                         break;
3759                 case RTW_WLAN_ACTION_DELBA: /* DELBA */
3760                         if ((frame_body[3] & BIT(3)) == 0) {
3761                                 psta->htpriv.agg_enable_bitmap &= ~(1 << ((frame_body[3] >> 4) & 0xf));
3762                                 psta->htpriv.candidate_tid_bitmap &= ~(1 << ((frame_body[3] >> 4) & 0xf));
3763                                 reason_code = get_unaligned_le16(&frame_body[4]);
3764                         } else if ((frame_body[3] & BIT(3)) == BIT(3)) {
3765                                 tid = (frame_body[3] >> 4) & 0x0F;
3766                                 preorder_ctrl =  &psta->recvreorder_ctrl[tid];
3767                                 preorder_ctrl->enable = false;
3768                                 preorder_ctrl->indicate_seq = 0xffff;
3769                         }
3770                         DBG_88E("%s(): DELBA: %x(%x)\n", __func__, pmlmeinfo->agg_enable_bitmap, reason_code);
3771                         /* todo: how to notify the host while receiving DELETE BA */
3772                         break;
3773                 default:
3774                         break;
3775                 }
3776         }
3777         return _SUCCESS;
3778 }
3779
3780 static s32 rtw_action_public_decache(struct recv_frame *recv_frame, s32 token)
3781 {
3782         struct adapter *adapter = recv_frame->adapter;
3783         struct mlme_ext_priv *mlmeext = &(adapter->mlmeextpriv);
3784         u8 *frame = recv_frame->rx_data;
3785         u16 seq_ctrl = ((recv_frame->attrib.seq_num&0xffff) << 4) |
3786                 (recv_frame->attrib.frag_num & 0xf);
3787
3788         if (GetRetry(frame)) {
3789                 if (token >= 0) {
3790                         if ((seq_ctrl == mlmeext->action_public_rxseq) && (token == mlmeext->action_public_dialog_token)) {
3791                                 DBG_88E(FUNC_ADPT_FMT" seq_ctrl = 0x%x, rxseq = 0x%x, token:%d\n",
3792                                         FUNC_ADPT_ARG(adapter), seq_ctrl, mlmeext->action_public_rxseq, token);
3793                                 return _FAIL;
3794                         }
3795                 } else {
3796                         if (seq_ctrl == mlmeext->action_public_rxseq) {
3797                                 DBG_88E(FUNC_ADPT_FMT" seq_ctrl = 0x%x, rxseq = 0x%x\n",
3798                                         FUNC_ADPT_ARG(adapter), seq_ctrl, mlmeext->action_public_rxseq);
3799                                 return _FAIL;
3800                         }
3801                 }
3802         }
3803
3804         mlmeext->action_public_rxseq = seq_ctrl;
3805
3806         if (token >= 0)
3807                 mlmeext->action_public_dialog_token = token;
3808
3809         return _SUCCESS;
3810 }
3811
3812 static unsigned int on_action_public_p2p(struct recv_frame *precv_frame)
3813 {
3814         u8 *pframe = precv_frame->rx_data;
3815         u8 *frame_body;
3816         u8 dialogToken = 0;
3817         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
3818
3819         dialogToken = frame_body[7];
3820
3821         if (rtw_action_public_decache(precv_frame, dialogToken) == _FAIL)
3822                 return _FAIL;
3823
3824         return _SUCCESS;
3825 }
3826
3827 static unsigned int on_action_public_vendor(struct recv_frame *precv_frame)
3828 {
3829         unsigned int ret = _FAIL;
3830         u8 *pframe = precv_frame->rx_data;
3831         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
3832
3833         if (!memcmp(frame_body + 2, P2P_OUI, 4))
3834                 ret = on_action_public_p2p(precv_frame);
3835
3836         return ret;
3837 }
3838
3839 static unsigned int on_action_public_default(struct recv_frame *precv_frame, u8 action)
3840 {
3841         unsigned int ret = _FAIL;
3842         u8 *pframe = precv_frame->rx_data;
3843         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
3844         u8 token;
3845
3846         token = frame_body[2];
3847
3848         if (rtw_action_public_decache(precv_frame, token) == _FAIL)
3849                 goto exit;
3850
3851         ret = _SUCCESS;
3852
3853 exit:
3854         return ret;
3855 }
3856
3857 static unsigned int on_action_public(struct adapter *padapter,
3858                                      struct recv_frame *precv_frame)
3859 {
3860         unsigned int ret = _FAIL;
3861         u8 *pframe = precv_frame->rx_data;
3862         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
3863         u8 category, action;
3864
3865         /* check RA matches or not */
3866         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe), ETH_ALEN))
3867                 goto exit;
3868
3869         category = frame_body[0];
3870         if (category != RTW_WLAN_CATEGORY_PUBLIC)
3871                 goto exit;
3872
3873         action = frame_body[1];
3874         switch (action) {
3875         case ACT_PUBLIC_VENDOR:
3876                 ret = on_action_public_vendor(precv_frame);
3877                 break;
3878         default:
3879                 ret = on_action_public_default(precv_frame, action);
3880                 break;
3881         }
3882
3883 exit:
3884         return ret;
3885 }
3886
3887 static unsigned int OnAction_ht(struct adapter *padapter,
3888                                 struct recv_frame *precv_frame)
3889 {
3890         return _SUCCESS;
3891 }
3892
3893 static unsigned int OnAction_wmm(struct adapter *padapter,
3894                                  struct recv_frame *precv_frame)
3895 {
3896         return _SUCCESS;
3897 }
3898
3899 static unsigned int OnAction_p2p(struct adapter *padapter,
3900                                  struct recv_frame *precv_frame)
3901 {
3902         return _SUCCESS;
3903 }
3904
3905 static unsigned int DoReserved(struct adapter *padapter,
3906                                struct recv_frame *precv_frame)
3907 {
3908         return _SUCCESS;
3909 }
3910
3911 static struct action_handler OnAction_tbl[] = {
3912         {RTW_WLAN_CATEGORY_SPECTRUM_MGMT,        "ACTION_SPECTRUM_MGMT", on_action_spct},
3913         {RTW_WLAN_CATEGORY_QOS, "ACTION_QOS", &OnAction_qos},
3914         {RTW_WLAN_CATEGORY_DLS, "ACTION_DLS", &OnAction_dls},
3915         {RTW_WLAN_CATEGORY_BACK, "ACTION_BACK", &OnAction_back},
3916         {RTW_WLAN_CATEGORY_PUBLIC, "ACTION_PUBLIC", on_action_public},
3917         {RTW_WLAN_CATEGORY_RADIO_MEASUREMENT, "ACTION_RADIO_MEASUREMENT", &DoReserved},
3918         {RTW_WLAN_CATEGORY_FT, "ACTION_FT",     &DoReserved},
3919         {RTW_WLAN_CATEGORY_HT,  "ACTION_HT",    &OnAction_ht},
3920         {RTW_WLAN_CATEGORY_SA_QUERY, "ACTION_SA_QUERY", &DoReserved},
3921         {RTW_WLAN_CATEGORY_WMM, "ACTION_WMM", &OnAction_wmm},
3922         {RTW_WLAN_CATEGORY_P2P, "ACTION_P2P", &OnAction_p2p},
3923 };
3924
3925 static unsigned int OnAction(struct adapter *padapter,
3926                              struct recv_frame *precv_frame)
3927 {
3928         int i;
3929         unsigned char   category;
3930         struct action_handler *ptable;
3931         unsigned char   *frame_body;
3932         u8 *pframe = precv_frame->rx_data;
3933
3934         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
3935
3936         category = frame_body[0];
3937
3938         for (i = 0; i < ARRAY_SIZE(OnAction_tbl); i++) {
3939                 ptable = &OnAction_tbl[i];
3940                 if (category == ptable->num)
3941                         ptable->func(padapter, precv_frame);
3942         }
3943         return _SUCCESS;
3944 }
3945
3946 /****************************************************************************
3947
3948 Following are the initialization functions for WiFi MLME
3949
3950 *****************************************************************************/
3951
3952 static struct mlme_handler mlme_sta_tbl[] = {
3953         {WIFI_ASSOCREQ,         "OnAssocReq",   &OnAssocReq},
3954         {WIFI_ASSOCRSP,         "OnAssocRsp",   &OnAssocRsp},
3955         {WIFI_REASSOCREQ,       "OnReAssocReq", &OnAssocReq},
3956         {WIFI_REASSOCRSP,       "OnReAssocRsp", &OnAssocRsp},
3957         {WIFI_PROBEREQ,         "OnProbeReq",   &OnProbeReq},
3958         {WIFI_PROBERSP,         "OnProbeRsp",           &OnProbeRsp},
3959
3960         /*----------------------------------------------------------
3961                                         below 2 are reserved
3962         -----------------------------------------------------------*/
3963         {0,                                     "DoReserved",           &DoReserved},
3964         {0,                                     "DoReserved",           &DoReserved},
3965         {WIFI_BEACON,           "OnBeacon",             &OnBeacon},
3966         {WIFI_ATIM,                     "OnATIM",               &OnAtim},
3967         {WIFI_DISASSOC,         "OnDisassoc",           &OnDisassoc},
3968         {WIFI_AUTH,                     "OnAuth",               &OnAuthClient},
3969         {WIFI_DEAUTH,           "OnDeAuth",             &OnDeAuth},
3970         {WIFI_ACTION,           "OnAction",             &OnAction},
3971 };
3972
3973 int init_hw_mlme_ext(struct adapter *padapter)
3974 {
3975         struct  mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3976
3977         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
3978         return _SUCCESS;
3979 }
3980
3981 static void init_mlme_ext_priv_value(struct adapter *padapter)
3982 {
3983         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3984         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3985         unsigned char   mixed_datarate[NumRates] = {
3986                 _1M_RATE_, _2M_RATE_, _5M_RATE_, _11M_RATE_, _6M_RATE_,
3987                 _9M_RATE_, _12M_RATE_, _18M_RATE_, _24M_RATE_, _36M_RATE_,
3988                  _48M_RATE_, _54M_RATE_, 0xff
3989         };
3990         unsigned char   mixed_basicrate[NumRates] = {
3991                 _1M_RATE_, _2M_RATE_, _5M_RATE_, _11M_RATE_, _6M_RATE_,
3992                 _12M_RATE_, _24M_RATE_, 0xff,
3993         };
3994
3995         atomic_set(&pmlmeext->event_seq, 0);
3996         pmlmeext->mgnt_seq = 0;/* reset to zero when disconnect at client mode */
3997
3998         pmlmeext->cur_channel = padapter->registrypriv.channel;
3999         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
4000         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
4001         pmlmeext->oper_channel = pmlmeext->cur_channel;
4002         pmlmeext->oper_bwmode = pmlmeext->cur_bwmode;
4003         pmlmeext->oper_ch_offset = pmlmeext->cur_ch_offset;
4004         pmlmeext->retry = 0;
4005
4006         pmlmeext->cur_wireless_mode = padapter->registrypriv.wireless_mode;
4007
4008         memcpy(pmlmeext->datarate, mixed_datarate, NumRates);
4009         memcpy(pmlmeext->basicrate, mixed_basicrate, NumRates);
4010
4011         pmlmeext->tx_rate = IEEE80211_CCK_RATE_1MB;
4012
4013         pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
4014         pmlmeext->sitesurvey_res.channel_idx = 0;
4015         pmlmeext->sitesurvey_res.bss_cnt = 0;
4016         pmlmeext->scan_abort = false;
4017
4018         pmlmeinfo->state = WIFI_FW_NULL_STATE;
4019         pmlmeinfo->reauth_count = 0;
4020         pmlmeinfo->reassoc_count = 0;
4021         pmlmeinfo->link_count = 0;
4022         pmlmeinfo->auth_seq = 0;
4023         pmlmeinfo->auth_algo = dot11AuthAlgrthm_Open;
4024         pmlmeinfo->key_index = 0;
4025         pmlmeinfo->iv = 0;
4026
4027         pmlmeinfo->enc_algo = _NO_PRIVACY_;
4028         pmlmeinfo->authModeToggle = 0;
4029
4030         memset(pmlmeinfo->chg_txt, 0, 128);
4031
4032         pmlmeinfo->slotTime = SHORT_SLOT_TIME;
4033         pmlmeinfo->preamble_mode = PREAMBLE_AUTO;
4034
4035         pmlmeinfo->dialogToken = 0;
4036
4037         pmlmeext->action_public_rxseq = 0xffff;
4038         pmlmeext->action_public_dialog_token = 0xff;
4039 }
4040
4041 static int has_channel(struct rt_channel_info *channel_set,
4042                                            u8 chanset_size,
4043                                            u8 chan) {
4044         int i;
4045
4046         for (i = 0; i < chanset_size; i++) {
4047                 if (channel_set[i].ChannelNum == chan)
4048                         return 1;
4049         }
4050         return 0;
4051 }
4052
4053 static void init_channel_list(struct adapter *padapter, struct rt_channel_info *channel_set,
4054                                                           u8 chanset_size,
4055                                                           struct p2p_channels *channel_list) {
4056         struct p2p_oper_class_map op_class[] = {
4057                 { IEEE80211G,  81,   1,  13,  1, BW20 },
4058                 { IEEE80211G,  82,  14,  14,  1, BW20 },
4059                 { -1, 0, 0, 0, 0, BW20 }
4060         };
4061
4062         int cla, op;
4063
4064         cla = 0;
4065
4066         for (op = 0; op_class[op].op_class; op++) {
4067                 u8 ch;
4068                 struct p2p_oper_class_map *o = &op_class[op];
4069                 struct p2p_reg_class *reg = NULL;
4070
4071                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4072                         if (!has_channel(channel_set, chanset_size, ch)) {
4073                                 continue;
4074                         }
4075
4076                         if ((0 == padapter->registrypriv.ht_enable) && (8 == o->inc))
4077                                 continue;
4078
4079                         if ((0 == (padapter->registrypriv.cbw40_enable & BIT(1))) &&
4080                             ((BW40MINUS == o->bw) || (BW40PLUS == o->bw)))
4081                                 continue;
4082
4083                         if (reg == NULL) {
4084                                 reg = &channel_list->reg_class[cla];
4085                                 cla++;
4086                                 reg->reg_class = o->op_class;
4087                                 reg->channels = 0;
4088                         }
4089                         reg->channel[reg->channels] = ch;
4090                         reg->channels++;
4091                 }
4092         }
4093         channel_list->reg_classes = cla;
4094 }
4095
4096 static u8 init_channel_set(struct adapter *padapter, u8 ChannelPlan, struct rt_channel_info *channel_set)
4097 {
4098         u8 index, chanset_size = 0;
4099         u8 b2_4GBand = false;
4100         u8 Index2G = 0;
4101
4102         memset(channel_set, 0, sizeof(struct rt_channel_info) * MAX_CHANNEL_NUM);
4103
4104         if (ChannelPlan >= RT_CHANNEL_DOMAIN_MAX && ChannelPlan != RT_CHANNEL_DOMAIN_REALTEK_DEFINE) {
4105                 DBG_88E("ChannelPlan ID %x error !!!!!\n", ChannelPlan);
4106                 return chanset_size;
4107         }
4108
4109         if (padapter->registrypriv.wireless_mode & WIRELESS_11G) {
4110                 b2_4GBand = true;
4111                 if (RT_CHANNEL_DOMAIN_REALTEK_DEFINE == ChannelPlan)
4112                         Index2G = RTW_CHANNEL_PLAN_MAP_REALTEK_DEFINE.Index2G;
4113                 else
4114                         Index2G = RTW_ChannelPlanMap[ChannelPlan].Index2G;
4115         }
4116
4117         if (b2_4GBand) {
4118                 for (index = 0; index < RTW_ChannelPlan2G[Index2G].Len; index++) {
4119                         channel_set[chanset_size].ChannelNum = RTW_ChannelPlan2G[Index2G].Channel[index];
4120
4121                         if ((RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN == ChannelPlan) ||/* Channel 1~11 is active, and 12~14 is passive */
4122                             (RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN_2G == ChannelPlan)) {
4123                                 if (channel_set[chanset_size].ChannelNum >= 1 && channel_set[chanset_size].ChannelNum <= 11)
4124                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
4125                                 else if ((channel_set[chanset_size].ChannelNum  >= 12 && channel_set[chanset_size].ChannelNum  <= 14))
4126                                         channel_set[chanset_size].ScanType  = SCAN_PASSIVE;
4127                         } else if (RT_CHANNEL_DOMAIN_WORLD_WIDE_13 == ChannelPlan ||
4128                                    RT_CHANNEL_DOMAIN_2G_WORLD == Index2G) {/*  channel 12~13, passive scan */
4129                                 if (channel_set[chanset_size].ChannelNum <= 11)
4130                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
4131                                 else
4132                                         channel_set[chanset_size].ScanType = SCAN_PASSIVE;
4133                         } else {
4134                                 channel_set[chanset_size].ScanType = SCAN_ACTIVE;
4135                         }
4136
4137                         chanset_size++;
4138                 }
4139         }
4140         return chanset_size;
4141 }
4142
4143 int     init_mlme_ext_priv(struct adapter *padapter)
4144 {
4145         struct registry_priv *pregistrypriv = &padapter->registrypriv;
4146         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4147         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
4148         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4149
4150         pmlmeext->padapter = padapter;
4151
4152         init_mlme_ext_priv_value(padapter);
4153         pmlmeinfo->bAcceptAddbaReq = pregistrypriv->bAcceptAddbaReq;
4154
4155         init_mlme_ext_timer(padapter);
4156
4157 #ifdef CONFIG_88EU_AP_MODE
4158         init_mlme_ap_info(padapter);
4159 #endif
4160
4161         pmlmeext->max_chan_nums = init_channel_set(padapter, pmlmepriv->ChannelPlan, pmlmeext->channel_set);
4162         init_channel_list(padapter, pmlmeext->channel_set, pmlmeext->max_chan_nums, &pmlmeext->channel_list);
4163
4164         pmlmeext->chan_scan_time = SURVEY_TO;
4165         pmlmeext->mlmeext_init = true;
4166
4167
4168         pmlmeext->active_keep_alive_check = true;
4169
4170         return _SUCCESS;
4171 }
4172
4173 void free_mlme_ext_priv(struct mlme_ext_priv *pmlmeext)
4174 {
4175         struct adapter *padapter = pmlmeext->padapter;
4176
4177         if (!padapter)
4178                 return;
4179
4180         if (padapter->bDriverStopped) {
4181                 del_timer_sync(&pmlmeext->survey_timer);
4182                 del_timer_sync(&pmlmeext->link_timer);
4183         }
4184 }
4185
4186 static void _mgt_dispatcher(struct adapter *padapter, struct mlme_handler *ptable, struct recv_frame *precv_frame)
4187 {
4188         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
4189         u8 *pframe = precv_frame->rx_data;
4190
4191         if (ptable->func) {
4192                 /* receive the frames that ra(a1) is my address or ra(a1) is bc address. */
4193                 if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN) &&
4194                     memcmp(GetAddr1Ptr(pframe), bc_addr, ETH_ALEN))
4195                         return;
4196                 ptable->func(padapter, precv_frame);
4197         }
4198 }
4199
4200 void mgt_dispatcher(struct adapter *padapter, struct recv_frame *precv_frame)
4201 {
4202         int index;
4203         struct mlme_handler *ptable;
4204 #ifdef CONFIG_88EU_AP_MODE
4205         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
4206 #endif /* CONFIG_88EU_AP_MODE */
4207         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
4208         u8 *pframe = precv_frame->rx_data;
4209         struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(pframe));
4210
4211         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
4212                  ("+mgt_dispatcher: type(0x%x) subtype(0x%x)\n",
4213                   (unsigned int)GetFrameType(pframe),
4214                   (unsigned int)GetFrameSubType(pframe)));
4215
4216         if (GetFrameType(pframe) != WIFI_MGT_TYPE) {
4217                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
4218                          ("mgt_dispatcher: type(0x%x) error!\n",
4219                           (unsigned int)GetFrameType(pframe)));
4220                 return;
4221         }
4222
4223         /* receive the frames that ra(a1) is my address or ra(a1) is bc address. */
4224         if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN) &&
4225             memcmp(GetAddr1Ptr(pframe), bc_addr, ETH_ALEN))
4226                 return;
4227
4228         ptable = mlme_sta_tbl;
4229
4230         index = GetFrameSubType(pframe) >> 4;
4231
4232         if (index > 13) {
4233                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Currently we do not support reserved sub-fr-type=%d\n", index));
4234                 return;
4235         }
4236         ptable += index;
4237
4238         if (psta != NULL) {
4239                 if (GetRetry(pframe)) {
4240                         if (precv_frame->attrib.seq_num ==
4241                             psta->RxMgmtFrameSeqNum) {
4242                                 /* drop the duplicate management frame */
4243                                 DBG_88E("Drop duplicate management frame with seq_num=%d.\n",
4244                                         precv_frame->attrib.seq_num);
4245                                 return;
4246                         }
4247                 }
4248                 psta->RxMgmtFrameSeqNum = precv_frame->attrib.seq_num;
4249         }
4250
4251 #ifdef CONFIG_88EU_AP_MODE
4252         switch (GetFrameSubType(pframe)) {
4253         case WIFI_AUTH:
4254                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
4255                         ptable->func = &OnAuth;
4256                 else
4257                         ptable->func = &OnAuthClient;
4258                 /* fall through */
4259         case WIFI_ASSOCREQ:
4260         case WIFI_REASSOCREQ:
4261         case WIFI_PROBEREQ:
4262         case WIFI_BEACON:
4263         case WIFI_ACTION:
4264                 _mgt_dispatcher(padapter, ptable, precv_frame);
4265                 break;
4266         default:
4267                 _mgt_dispatcher(padapter, ptable, precv_frame);
4268                 break;
4269         }
4270 #else
4271         _mgt_dispatcher(padapter, ptable, precv_frame);
4272 #endif
4273 }
4274
4275 /****************************************************************************
4276
4277 Following are the functions to report events
4278
4279 *****************************************************************************/
4280
4281 void report_survey_event(struct adapter *padapter,
4282                          struct recv_frame *precv_frame)
4283 {
4284         struct cmd_obj *pcmd_obj;
4285         u8 *pevtcmd;
4286         u32 cmdsz;
4287         struct survey_event     *psurvey_evt;
4288         struct C2HEvent_Header *pc2h_evt_hdr;
4289         struct mlme_ext_priv *pmlmeext;
4290         struct cmd_priv *pcmdpriv;
4291
4292         if (!padapter)
4293                 return;
4294
4295         pmlmeext = &padapter->mlmeextpriv;
4296         pcmdpriv = &padapter->cmdpriv;
4297
4298
4299         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
4300         if (pcmd_obj == NULL)
4301                 return;
4302
4303         cmdsz = sizeof(struct survey_event) + sizeof(struct C2HEvent_Header);
4304         pevtcmd = kzalloc(cmdsz, GFP_ATOMIC);
4305         if (pevtcmd == NULL) {
4306                 kfree(pcmd_obj);
4307                 return;
4308         }
4309
4310         INIT_LIST_HEAD(&pcmd_obj->list);
4311
4312         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4313         pcmd_obj->cmdsz = cmdsz;
4314         pcmd_obj->parmbuf = pevtcmd;
4315
4316         pcmd_obj->rsp = NULL;
4317         pcmd_obj->rspsz  = 0;
4318
4319         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4320         pc2h_evt_hdr->len = sizeof(struct survey_event);
4321         pc2h_evt_hdr->ID = GEN_EVT_CODE(_Survey);
4322         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4323
4324         psurvey_evt = (struct survey_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4325
4326         if (collect_bss_info(padapter, precv_frame, (struct wlan_bssid_ex *)&psurvey_evt->bss) == _FAIL) {
4327                 kfree(pcmd_obj);
4328                 kfree(pevtcmd);
4329                 return;
4330         }
4331
4332         process_80211d(padapter, &psurvey_evt->bss);
4333
4334         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4335
4336         pmlmeext->sitesurvey_res.bss_cnt++;
4337
4338         return;
4339 }
4340
4341 void report_surveydone_event(struct adapter *padapter)
4342 {
4343         struct cmd_obj *pcmd_obj;
4344         u8 *pevtcmd;
4345         u32 cmdsz;
4346         struct surveydone_event *psurveydone_evt;
4347         struct C2HEvent_Header  *pc2h_evt_hdr;
4348         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4349         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
4350
4351         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
4352         if (pcmd_obj == NULL)
4353                 return;
4354
4355         cmdsz = sizeof(struct surveydone_event) + sizeof(struct C2HEvent_Header);
4356         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
4357         if (pevtcmd == NULL) {
4358                 kfree(pcmd_obj);
4359                 return;
4360         }
4361
4362         INIT_LIST_HEAD(&pcmd_obj->list);
4363
4364         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4365         pcmd_obj->cmdsz = cmdsz;
4366         pcmd_obj->parmbuf = pevtcmd;
4367
4368         pcmd_obj->rsp = NULL;
4369         pcmd_obj->rspsz  = 0;
4370
4371         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4372         pc2h_evt_hdr->len = sizeof(struct surveydone_event);
4373         pc2h_evt_hdr->ID = GEN_EVT_CODE(_SurveyDone);
4374         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4375
4376         psurveydone_evt = (struct surveydone_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4377         psurveydone_evt->bss_cnt = pmlmeext->sitesurvey_res.bss_cnt;
4378
4379         DBG_88E("survey done event(%x)\n", psurveydone_evt->bss_cnt);
4380
4381         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4382
4383         return;
4384 }
4385
4386 void report_join_res(struct adapter *padapter, int res)
4387 {
4388         struct cmd_obj *pcmd_obj;
4389         u8 *pevtcmd;
4390         u32 cmdsz;
4391         struct joinbss_event            *pjoinbss_evt;
4392         struct C2HEvent_Header  *pc2h_evt_hdr;
4393         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4394         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4395         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
4396
4397         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
4398         if (pcmd_obj == NULL)
4399                 return;
4400
4401         cmdsz = sizeof(struct joinbss_event) + sizeof(struct C2HEvent_Header);
4402         pevtcmd = kzalloc(cmdsz, GFP_ATOMIC);
4403         if (pevtcmd == NULL) {
4404                 kfree(pcmd_obj);
4405                 return;
4406         }
4407
4408         INIT_LIST_HEAD(&pcmd_obj->list);
4409
4410         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4411         pcmd_obj->cmdsz = cmdsz;
4412         pcmd_obj->parmbuf = pevtcmd;
4413
4414         pcmd_obj->rsp = NULL;
4415         pcmd_obj->rspsz  = 0;
4416
4417         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4418         pc2h_evt_hdr->len = sizeof(struct joinbss_event);
4419         pc2h_evt_hdr->ID = GEN_EVT_CODE(_JoinBss);
4420         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4421
4422         pjoinbss_evt = (struct joinbss_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4423         memcpy((unsigned char *)(&(pjoinbss_evt->network.network)), &(pmlmeinfo->network), sizeof(struct wlan_bssid_ex));
4424         pjoinbss_evt->network.join_res  = res;
4425         pjoinbss_evt->network.aid = res;
4426
4427         DBG_88E("report_join_res(%d)\n", res);
4428
4429
4430         rtw_joinbss_event_prehandle(padapter, (u8 *)&pjoinbss_evt->network);
4431
4432
4433         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4434
4435         return;
4436 }
4437
4438 void report_del_sta_event(struct adapter *padapter, unsigned char *MacAddr, unsigned short reason)
4439 {
4440         struct cmd_obj *pcmd_obj;
4441         u8 *pevtcmd;
4442         u32 cmdsz;
4443         struct sta_info *psta;
4444         int     mac_id;
4445         struct stadel_event                     *pdel_sta_evt;
4446         struct C2HEvent_Header  *pc2h_evt_hdr;
4447         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4448         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
4449
4450         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
4451         if (pcmd_obj == NULL)
4452                 return;
4453
4454         cmdsz = sizeof(struct stadel_event) + sizeof(struct C2HEvent_Header);
4455         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
4456         if (pevtcmd == NULL) {
4457                 kfree(pcmd_obj);
4458                 return;
4459         }
4460
4461         INIT_LIST_HEAD(&pcmd_obj->list);
4462
4463         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4464         pcmd_obj->cmdsz = cmdsz;
4465         pcmd_obj->parmbuf = pevtcmd;
4466
4467         pcmd_obj->rsp = NULL;
4468         pcmd_obj->rspsz  = 0;
4469
4470         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4471         pc2h_evt_hdr->len = sizeof(struct stadel_event);
4472         pc2h_evt_hdr->ID = GEN_EVT_CODE(_DelSTA);
4473         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4474
4475         pdel_sta_evt = (struct stadel_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4476         memcpy((unsigned char *)(&(pdel_sta_evt->macaddr)), MacAddr, ETH_ALEN);
4477         memcpy((unsigned char *)(pdel_sta_evt->rsvd), (unsigned char *)(&reason), 2);
4478
4479
4480         psta = rtw_get_stainfo(&padapter->stapriv, MacAddr);
4481         if (psta)
4482                 mac_id = (int)psta->mac_id;
4483         else
4484                 mac_id = -1;
4485
4486         pdel_sta_evt->mac_id = mac_id;
4487
4488         DBG_88E("report_del_sta_event: delete STA, mac_id =%d\n", mac_id);
4489
4490         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4491
4492         return;
4493 }
4494
4495 void report_add_sta_event(struct adapter *padapter, unsigned char *MacAddr, int cam_idx)
4496 {
4497         struct cmd_obj *pcmd_obj;
4498         u8 *pevtcmd;
4499         u32 cmdsz;
4500         struct stassoc_event            *padd_sta_evt;
4501         struct C2HEvent_Header  *pc2h_evt_hdr;
4502         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4503         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
4504
4505         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
4506         if (pcmd_obj == NULL)
4507                 return;
4508
4509         cmdsz = sizeof(struct stassoc_event) + sizeof(struct C2HEvent_Header);
4510         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
4511         if (pevtcmd == NULL) {
4512                 kfree(pcmd_obj);
4513                 return;
4514         }
4515
4516         INIT_LIST_HEAD(&pcmd_obj->list);
4517
4518         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4519         pcmd_obj->cmdsz = cmdsz;
4520         pcmd_obj->parmbuf = pevtcmd;
4521
4522         pcmd_obj->rsp = NULL;
4523         pcmd_obj->rspsz  = 0;
4524
4525         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4526         pc2h_evt_hdr->len = sizeof(struct stassoc_event);
4527         pc2h_evt_hdr->ID = GEN_EVT_CODE(_AddSTA);
4528         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4529
4530         padd_sta_evt = (struct stassoc_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4531         memcpy((unsigned char *)(&(padd_sta_evt->macaddr)), MacAddr, ETH_ALEN);
4532         padd_sta_evt->cam_id = cam_idx;
4533
4534         DBG_88E("report_add_sta_event: add STA\n");
4535
4536         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4537
4538         return;
4539 }
4540
4541
4542 /****************************************************************************
4543
4544 Following are the event callback functions
4545
4546 *****************************************************************************/
4547
4548 /* for sta/adhoc mode */
4549 void update_sta_info(struct adapter *padapter, struct sta_info *psta)
4550 {
4551         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
4552         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4553         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4554
4555         /* ERP */
4556         VCS_update(padapter, psta);
4557
4558         /* HT */
4559         if (pmlmepriv->htpriv.ht_option) {
4560                 psta->htpriv.ht_option = true;
4561
4562                 psta->htpriv.ampdu_enable = pmlmepriv->htpriv.ampdu_enable;
4563
4564                 if (support_short_GI(padapter, &(pmlmeinfo->HT_caps)))
4565                         psta->htpriv.sgi = true;
4566
4567                 psta->qos_option = true;
4568         } else {
4569                 psta->htpriv.ht_option = false;
4570
4571                 psta->htpriv.ampdu_enable = false;
4572
4573                 psta->htpriv.sgi = false;
4574                 psta->qos_option = false;
4575         }
4576         psta->htpriv.bwmode = pmlmeext->cur_bwmode;
4577         psta->htpriv.ch_offset = pmlmeext->cur_ch_offset;
4578
4579         psta->htpriv.agg_enable_bitmap = 0x0;/* reset */
4580         psta->htpriv.candidate_tid_bitmap = 0x0;/* reset */
4581
4582         /* QoS */
4583         if (pmlmepriv->qospriv.qos_option)
4584                 psta->qos_option = true;
4585
4586
4587         psta->state = _FW_LINKED;
4588 }
4589
4590 void mlmeext_joinbss_event_callback(struct adapter *padapter, int join_res)
4591 {
4592         struct sta_info         *psta, *psta_bmc;
4593         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4594         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4595         struct wlan_bssid_ex *cur_network = &(pmlmeinfo->network);
4596         struct sta_priv         *pstapriv = &padapter->stapriv;
4597         u8 join_type;
4598         u16 media_status;
4599
4600         if (join_res < 0) {
4601                 join_type = 1;
4602                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
4603                 rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
4604
4605                 /* restore to initial setting. */
4606                 update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
4607
4608                 goto exit_mlmeext_joinbss_event_callback;
4609         }
4610
4611         if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
4612                 /* for bc/mc */
4613                 psta_bmc = rtw_get_bcmc_stainfo(padapter);
4614                 if (psta_bmc) {
4615                         pmlmeinfo->FW_sta_info[psta_bmc->mac_id].psta = psta_bmc;
4616                         update_bmc_sta_support_rate(padapter, psta_bmc->mac_id);
4617                         Update_RA_Entry(padapter, psta_bmc->mac_id);
4618                 }
4619         }
4620
4621
4622         /* turn on dynamic functions */
4623         Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true);
4624
4625         /*  update IOT-related issue */
4626         update_IOT_info(padapter);
4627
4628         rtw_hal_set_hwreg(padapter, HW_VAR_BASIC_RATE, cur_network->SupportedRates);
4629
4630         /* BCN interval */
4631         rtw_hal_set_hwreg(padapter, HW_VAR_BEACON_INTERVAL, (u8 *)(&pmlmeinfo->bcn_interval));
4632
4633         /* update capability */
4634         update_capinfo(padapter, pmlmeinfo->capability);
4635
4636         /* WMM, Update EDCA param */
4637         WMMOnAssocRsp(padapter);
4638
4639         /* HT */
4640         HTOnAssocRsp(padapter);
4641
4642         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
4643
4644         psta = rtw_get_stainfo(pstapriv, cur_network->MacAddress);
4645         if (psta) { /* only for infra. mode */
4646                 pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
4647
4648                 psta->wireless_mode = pmlmeext->cur_wireless_mode;
4649
4650                 /* set per sta rate after updating HT cap. */
4651                 set_sta_rate(padapter, psta);
4652                 rtw_hal_set_hwreg(padapter, HW_VAR_TX_RPT_MAX_MACID, (u8 *)&psta->mac_id);
4653                 media_status = (psta->mac_id<<8)|1; /*   MACID|OPMODE: 1 means connect */
4654                 rtw_hal_set_hwreg(padapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status);
4655         }
4656
4657         join_type = 2;
4658         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
4659
4660         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
4661                 /*  correcting TSF */
4662                 correct_TSF(padapter, pmlmeext);
4663         }
4664         rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_CONNECT, 0);
4665
4666 exit_mlmeext_joinbss_event_callback:
4667
4668         DBG_88E("=>%s\n", __func__);
4669 }
4670
4671 void mlmeext_sta_add_event_callback(struct adapter *padapter, struct sta_info *psta)
4672 {
4673         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
4674         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4675         u8 join_type;
4676
4677         DBG_88E("%s\n", __func__);
4678
4679         if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
4680                 if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {/* adhoc master or sta_count>1 */
4681                         /* nothing to do */
4682                 } else { /* adhoc client */
4683                         /*  correcting TSF */
4684                         correct_TSF(padapter, pmlmeext);
4685
4686                         /* start beacon */
4687                         if (send_beacon(padapter) == _FAIL) {
4688                                 pmlmeinfo->FW_sta_info[psta->mac_id].status = 0;
4689                                 pmlmeinfo->state ^= WIFI_FW_ADHOC_STATE;
4690                                 return;
4691                         }
4692                         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
4693                 }
4694
4695                 join_type = 2;
4696                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
4697         }
4698
4699         pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
4700
4701         /* rate radaptive */
4702         Update_RA_Entry(padapter, psta->mac_id);
4703
4704         /* update adhoc sta_info */
4705         update_sta_info(padapter, psta);
4706 }
4707
4708 void mlmeext_sta_del_event_callback(struct adapter *padapter)
4709 {
4710         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4711         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4712
4713         if (is_client_associated_to_ap(padapter) || is_IBSS_empty(padapter)) {
4714                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
4715                 rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
4716
4717                 /* restore to initial setting. */
4718                 update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
4719
4720                 /* switch to the 20M Hz mode after disconnect */
4721                 pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
4722                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
4723
4724                 /* SelectChannel(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset); */
4725                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
4726
4727
4728                 flush_all_cam_entry(padapter);
4729
4730                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
4731
4732                 /* set MSR to no link state -> infra. mode */
4733                 Set_MSR(padapter, _HW_STATE_STATION_);
4734
4735                 del_timer_sync(&pmlmeext->link_timer);
4736         }
4737 }
4738
4739 /****************************************************************************
4740
4741 Following are the functions for the timer handlers
4742
4743 *****************************************************************************/
4744 void _linked_rx_signal_strehgth_display(struct adapter *padapter);
4745 void _linked_rx_signal_strehgth_display(struct adapter *padapter)
4746 {
4747         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4748       struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4749         u8 mac_id;
4750         int UndecoratedSmoothedPWDB;
4751         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
4752                 mac_id = 0;
4753         else if ((pmlmeinfo->state&0x03) == _HW_STATE_AP_)
4754                 mac_id = 2;
4755
4756         rtw_hal_get_def_var(padapter, HW_DEF_RA_INFO_DUMP, &mac_id);
4757
4758         rtw_hal_get_def_var(padapter, HAL_DEF_UNDERCORATEDSMOOTHEDPWDB, &UndecoratedSmoothedPWDB);
4759         DBG_88E("UndecoratedSmoothedPWDB:%d\n", UndecoratedSmoothedPWDB);
4760 }
4761
4762 static u8 chk_ap_is_alive(struct adapter *padapter, struct sta_info *psta)
4763 {
4764         u8 ret = false;
4765
4766         if ((sta_rx_data_pkts(psta) == sta_last_rx_data_pkts(psta)) &&
4767             sta_rx_beacon_pkts(psta) == sta_last_rx_beacon_pkts(psta) &&
4768             sta_rx_probersp_pkts(psta) == sta_last_rx_probersp_pkts(psta))
4769                 ret = false;
4770         else
4771                 ret = true;
4772
4773         sta_update_last_rx_pkts(psta);
4774
4775         return ret;
4776 }
4777
4778 void linked_status_chk(struct adapter *padapter)
4779 {
4780         u32     i;
4781         struct sta_info         *psta;
4782         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
4783         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4784         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4785         struct sta_priv         *pstapriv = &padapter->stapriv;
4786
4787         if (padapter->bRxRSSIDisplay)
4788                 _linked_rx_signal_strehgth_display(padapter);
4789
4790         if (is_client_associated_to_ap(padapter)) {
4791                 /* linked infrastructure client mode */
4792
4793                 int tx_chk = _SUCCESS, rx_chk = _SUCCESS;
4794                 int rx_chk_limit;
4795
4796                 rx_chk_limit = 4;
4797                 psta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.MacAddress);
4798                 if (psta != NULL) {
4799                         bool is_p2p_enable = false;
4800
4801                         if (!chk_ap_is_alive(padapter, psta))
4802                                 rx_chk = _FAIL;
4803
4804                         if (pxmitpriv->last_tx_pkts == pxmitpriv->tx_pkts)
4805                                 tx_chk = _FAIL;
4806
4807                         if (pmlmeext->active_keep_alive_check && (rx_chk == _FAIL || tx_chk == _FAIL)) {
4808                                 u8 backup_oper_channel = 0;
4809
4810                                 /* switch to correct channel of current network  before issue keep-alive frames */
4811                                 if (rtw_get_oper_ch(padapter) != pmlmeext->cur_channel) {
4812                                         backup_oper_channel = rtw_get_oper_ch(padapter);
4813                                         SelectChannel(padapter, pmlmeext->cur_channel);
4814                                 }
4815
4816                                 if (rx_chk != _SUCCESS)
4817                                         issue_probereq_ex(padapter, &pmlmeinfo->network.Ssid, psta->hwaddr, 3, 1);
4818
4819                                 if ((tx_chk != _SUCCESS && pmlmeinfo->link_count++ == 0xf) || rx_chk != _SUCCESS) {
4820                                         tx_chk = issue_nulldata(padapter, psta->hwaddr, 0, 3, 1);
4821                                         /* if tx acked and p2p disabled, set rx_chk _SUCCESS to reset retry count */
4822                                         if (tx_chk == _SUCCESS && !is_p2p_enable)
4823                                                 rx_chk = _SUCCESS;
4824                                 }
4825
4826                                 /* back to the original operation channel */
4827                                 if (backup_oper_channel > 0)
4828                                         SelectChannel(padapter, backup_oper_channel);
4829                         } else {
4830                                 if (rx_chk != _SUCCESS) {
4831                                         if (pmlmeext->retry == 0) {
4832                                                 issue_probereq(padapter,
4833                                                 &pmlmeinfo->network.Ssid,
4834                                                 pmlmeinfo->network.MacAddress,
4835                                                                         false);
4836                                                 issue_probereq(padapter,
4837                                                 &pmlmeinfo->network.Ssid,
4838                                                 pmlmeinfo->network.MacAddress,
4839                                                                         false);
4840                                                 issue_probereq(padapter,
4841                                                 &pmlmeinfo->network.Ssid,
4842                                                 pmlmeinfo->network.MacAddress,
4843                                                                         false);
4844                                         }
4845                                 }
4846
4847                                 if (tx_chk != _SUCCESS && pmlmeinfo->link_count++ == 0xf) {
4848                                         tx_chk = issue_nulldata(padapter, NULL, 0, 1, 0);
4849                                 }
4850                         }
4851
4852                         if (rx_chk == _FAIL) {
4853                                 pmlmeext->retry++;
4854                                 if (pmlmeext->retry > rx_chk_limit) {
4855                                         DBG_88E_LEVEL(_drv_always_, FUNC_ADPT_FMT" disconnect or roaming\n",
4856                                                       FUNC_ADPT_ARG(padapter));
4857                                         receive_disconnect(padapter, pmlmeinfo->network.MacAddress,
4858                                                            WLAN_REASON_EXPIRATION_CHK);
4859                                         return;
4860                                 }
4861                         } else {
4862                                 pmlmeext->retry = 0;
4863                         }
4864
4865                         if (tx_chk == _FAIL) {
4866                                 pmlmeinfo->link_count &= 0xf;
4867                         } else {
4868                                 pxmitpriv->last_tx_pkts = pxmitpriv->tx_pkts;
4869                                 pmlmeinfo->link_count = 0;
4870                         }
4871                 } /* end of if ((psta = rtw_get_stainfo(pstapriv, passoc_res->network.MacAddress)) != NULL) */
4872         } else if (is_client_associated_to_ibss(padapter)) {
4873                 /* linked IBSS mode */
4874                 /* for each assoc list entry to check the rx pkt counter */
4875                 for (i = IBSS_START_MAC_ID; i < NUM_STA; i++) {
4876                         if (pmlmeinfo->FW_sta_info[i].status == 1) {
4877                                 psta = pmlmeinfo->FW_sta_info[i].psta;
4878
4879                                 if (NULL == psta)
4880                                         continue;
4881                                 if (pmlmeinfo->FW_sta_info[i].rx_pkt == sta_rx_pkts(psta)) {
4882                                         if (pmlmeinfo->FW_sta_info[i].retry < 3) {
4883                                                 pmlmeinfo->FW_sta_info[i].retry++;
4884                                         } else {
4885                                                 pmlmeinfo->FW_sta_info[i].retry = 0;
4886                                                 pmlmeinfo->FW_sta_info[i].status = 0;
4887                                                 report_del_sta_event(padapter, psta->hwaddr
4888                                                         , 65535/*  indicate disconnect caused by no rx */
4889                                         );
4890                                         }
4891                                 } else {
4892                                         pmlmeinfo->FW_sta_info[i].retry = 0;
4893                                         pmlmeinfo->FW_sta_info[i].rx_pkt = (u32)sta_rx_pkts(psta);
4894                                 }
4895                         }
4896                 }
4897         }
4898 }
4899
4900 void survey_timer_hdl(unsigned long data)
4901 {
4902         struct adapter *padapter = (struct adapter *)data;
4903         struct cmd_obj  *ph2c;
4904         struct sitesurvey_parm  *psurveyPara;
4905         struct cmd_priv                                 *pcmdpriv = &padapter->cmdpriv;
4906         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4907
4908         /* issue rtw_sitesurvey_cmd */
4909         if (pmlmeext->sitesurvey_res.state > SCAN_START) {
4910                 if (pmlmeext->sitesurvey_res.state ==  SCAN_PROCESS)
4911                         pmlmeext->sitesurvey_res.channel_idx++;
4912
4913                 if (pmlmeext->scan_abort) {
4914                         pmlmeext->sitesurvey_res.channel_idx = pmlmeext->sitesurvey_res.ch_num;
4915                         DBG_88E("%s idx:%d\n", __func__
4916                                 , pmlmeext->sitesurvey_res.channel_idx);
4917
4918                         pmlmeext->scan_abort = false;/* reset */
4919                 }
4920
4921                 ph2c = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
4922                 if (ph2c == NULL)
4923                         goto exit_survey_timer_hdl;
4924
4925                 psurveyPara = kzalloc(sizeof(struct sitesurvey_parm), GFP_ATOMIC);
4926                 if (psurveyPara == NULL) {
4927                         kfree(ph2c);
4928                         goto exit_survey_timer_hdl;
4929                 }
4930
4931                 init_h2fwcmd_w_parm_no_rsp(ph2c, psurveyPara, GEN_CMD_CODE(_SiteSurvey));
4932                 rtw_enqueue_cmd(pcmdpriv, ph2c);
4933         }
4934
4935
4936 exit_survey_timer_hdl:
4937         return;
4938 }
4939
4940 void link_timer_hdl(unsigned long data)
4941 {
4942         struct adapter *padapter = (struct adapter *)data;
4943         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4944         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4945
4946         if (pmlmeinfo->state & WIFI_FW_AUTH_NULL) {
4947                 DBG_88E("link_timer_hdl:no beacon while connecting\n");
4948                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
4949                 report_join_res(padapter, -3);
4950         } else if (pmlmeinfo->state & WIFI_FW_AUTH_STATE) {
4951                 /* re-auth timer */
4952                 if (++pmlmeinfo->reauth_count > REAUTH_LIMIT) {
4953                         pmlmeinfo->state = 0;
4954                         report_join_res(padapter, -1);
4955                         return;
4956                 }
4957
4958                 DBG_88E("link_timer_hdl: auth timeout and try again\n");
4959                 pmlmeinfo->auth_seq = 1;
4960                 issue_auth(padapter, NULL, 0);
4961                 set_link_timer(pmlmeext, REAUTH_TO);
4962         } else if (pmlmeinfo->state & WIFI_FW_ASSOC_STATE) {
4963                 /* re-assoc timer */
4964                 if (++pmlmeinfo->reassoc_count > REASSOC_LIMIT) {
4965                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
4966                         report_join_res(padapter, -2);
4967                         return;
4968                 }
4969
4970                 DBG_88E("link_timer_hdl: assoc timeout and try again\n");
4971                 issue_assocreq(padapter);
4972                 set_link_timer(pmlmeext, REASSOC_TO);
4973         }
4974         return;
4975 }
4976
4977 void addba_timer_hdl(unsigned long data)
4978 {
4979         struct sta_info *psta = (struct sta_info *)data;
4980         struct ht_priv  *phtpriv;
4981
4982         if (!psta)
4983                 return;
4984
4985         phtpriv = &psta->htpriv;
4986
4987         if ((phtpriv->ht_option) && (phtpriv->ampdu_enable)) {
4988                 if (phtpriv->candidate_tid_bitmap)
4989                         phtpriv->candidate_tid_bitmap = 0x0;
4990         }
4991 }
4992
4993 u8 setopmode_hdl(struct adapter *padapter, u8 *pbuf)
4994 {
4995         u8 type;
4996         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4997         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4998         struct setopmode_parm *psetop = (struct setopmode_parm *)pbuf;
4999
5000         if (psetop->mode == Ndis802_11APMode) {
5001                 pmlmeinfo->state = WIFI_FW_AP_STATE;
5002                 type = _HW_STATE_AP_;
5003         } else if (psetop->mode == Ndis802_11Infrastructure) {
5004                 pmlmeinfo->state &= ~(BIT(0)|BIT(1));/*  clear state */
5005                 pmlmeinfo->state |= WIFI_FW_STATION_STATE;/* set to     STATION_STATE */
5006                 type = _HW_STATE_STATION_;
5007         } else if (psetop->mode == Ndis802_11IBSS) {
5008                 type = _HW_STATE_ADHOC_;
5009         } else {
5010                 type = _HW_STATE_NOLINK_;
5011         }
5012
5013         rtw_hal_set_hwreg(padapter, HW_VAR_SET_OPMODE, (u8 *)(&type));
5014         /* Set_NETYPE0_MSR(padapter, type); */
5015
5016         return H2C_SUCCESS;
5017 }
5018
5019 u8 createbss_hdl(struct adapter *padapter, u8 *pbuf)
5020 {
5021         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5022         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5023         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
5024         struct wlan_bssid_ex *pparm = (struct wlan_bssid_ex *)pbuf;
5025         /* u32  initialgain; */
5026
5027
5028         if (pparm->InfrastructureMode == Ndis802_11APMode) {
5029 #ifdef CONFIG_88EU_AP_MODE
5030
5031                 if (pmlmeinfo->state == WIFI_FW_AP_STATE) {
5032                         /* todo: */
5033                         return H2C_SUCCESS;
5034                 }
5035 #endif
5036         }
5037
5038         /* below is for ad-hoc master */
5039         if (pparm->InfrastructureMode == Ndis802_11IBSS) {
5040                 rtw_joinbss_reset(padapter);
5041
5042                 pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
5043                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5044                 pmlmeinfo->ERP_enable = 0;
5045                 pmlmeinfo->WMM_enable = 0;
5046                 pmlmeinfo->HT_enable = 0;
5047                 pmlmeinfo->HT_caps_enable = 0;
5048                 pmlmeinfo->HT_info_enable = 0;
5049                 pmlmeinfo->agg_enable_bitmap = 0;
5050                 pmlmeinfo->candidate_tid_bitmap = 0;
5051
5052                 /* disable dynamic functions, such as high power, DIG */
5053                 Save_DM_Func_Flag(padapter);
5054                 Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false);
5055
5056                 /* config the initial gain under linking, need to write the BB registers */
5057                 /* initialgain = 0x1E; */
5058                 /* rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain)); */
5059
5060                 /* cancel link timer */
5061                 del_timer_sync(&pmlmeext->link_timer);
5062
5063                 /* clear CAM */
5064                 flush_all_cam_entry(padapter);
5065
5066                 memcpy(pnetwork, pbuf, FIELD_OFFSET(struct wlan_bssid_ex, IELength));
5067                 pnetwork->IELength = ((struct wlan_bssid_ex *)pbuf)->IELength;
5068
5069                 if (pnetwork->IELength > MAX_IE_SZ)/* Check pbuf->IELength */
5070                         return H2C_PARAMETERS_ERROR;
5071
5072                 memcpy(pnetwork->IEs, ((struct wlan_bssid_ex *)pbuf)->IEs, pnetwork->IELength);
5073
5074                 start_create_ibss(padapter);
5075         }
5076
5077         return H2C_SUCCESS;
5078 }
5079
5080 u8 join_cmd_hdl(struct adapter *padapter, u8 *pbuf)
5081 {
5082         u8 join_type;
5083         struct ndis_802_11_var_ie *pIE;
5084         struct registry_priv    *pregpriv = &padapter->registrypriv;
5085         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5086         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5087         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
5088         struct wlan_bssid_ex *pparm = (struct wlan_bssid_ex *)pbuf;
5089         u32 i;
5090
5091         /* check already connecting to AP or not */
5092         if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {
5093                 if (pmlmeinfo->state & WIFI_FW_STATION_STATE)
5094                         issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, 5, 100);
5095
5096                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
5097
5098                 /* clear CAM */
5099                 flush_all_cam_entry(padapter);
5100
5101                 del_timer_sync(&pmlmeext->link_timer);
5102
5103                 /* set MSR to nolink -> infra. mode */
5104                 Set_MSR(padapter, _HW_STATE_STATION_);
5105
5106
5107                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
5108         }
5109
5110         rtw_antenna_select_cmd(padapter, pparm->PhyInfo.Optimum_antenna, false);
5111
5112         rtw_joinbss_reset(padapter);
5113
5114         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
5115         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5116         pmlmeinfo->ERP_enable = 0;
5117         pmlmeinfo->WMM_enable = 0;
5118         pmlmeinfo->HT_enable = 0;
5119         pmlmeinfo->HT_caps_enable = 0;
5120         pmlmeinfo->HT_info_enable = 0;
5121         pmlmeinfo->agg_enable_bitmap = 0;
5122         pmlmeinfo->candidate_tid_bitmap = 0;
5123         pmlmeinfo->bwmode_updated = false;
5124
5125         memcpy(pnetwork, pbuf, FIELD_OFFSET(struct wlan_bssid_ex, IELength));
5126         pnetwork->IELength = ((struct wlan_bssid_ex *)pbuf)->IELength;
5127
5128         if (pnetwork->IELength > MAX_IE_SZ)/* Check pbuf->IELength */
5129                 return H2C_PARAMETERS_ERROR;
5130
5131         memcpy(pnetwork->IEs, ((struct wlan_bssid_ex *)pbuf)->IEs, pnetwork->IELength);
5132
5133         /* Check AP vendor to move rtw_joinbss_cmd() */
5134
5135         for (i = sizeof(struct ndis_802_11_fixed_ie); i < pnetwork->IELength;) {
5136                 pIE = (struct ndis_802_11_var_ie *)(pnetwork->IEs + i);
5137
5138                 switch (pIE->ElementID) {
5139                 case _VENDOR_SPECIFIC_IE_:/* Get WMM IE. */
5140                         if (!memcmp(pIE->data, WMM_OUI, 4))
5141                                 pmlmeinfo->WMM_enable = 1;
5142                         break;
5143                 case _HT_CAPABILITY_IE_:        /* Get HT Cap IE. */
5144                         pmlmeinfo->HT_caps_enable = 1;
5145                         break;
5146                 case _HT_EXTRA_INFO_IE_:        /* Get HT Info IE. */
5147                         pmlmeinfo->HT_info_enable = 1;
5148
5149                         /* spec case only for cisco's ap because cisco's ap issue assoc rsp using mcs rate @40MHz or @20MHz */
5150                         {
5151                                 struct HT_info_element *pht_info = (struct HT_info_element *)(pIE->data);
5152
5153                                 if ((pregpriv->cbw40_enable) &&  (pht_info->infos[0] & BIT(2))) {
5154                                         /* switch to the 40M Hz mode according to the AP */
5155                                         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_40;
5156                                         switch (pht_info->infos[0] & 0x3) {
5157                                         case 1:
5158                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
5159                                                 break;
5160                                         case 3:
5161                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
5162                                                 break;
5163                                         default:
5164                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5165                                                 break;
5166                                 }
5167
5168                                         DBG_88E("set ch/bw before connected\n");
5169                                 }
5170                         }
5171                         break;
5172                 default:
5173                         break;
5174                 }
5175
5176                 i += (pIE->Length + 2);
5177         }
5178         /* disable dynamic functions, such as high power, DIG */
5179
5180         /* config the initial gain under linking, need to write the BB registers */
5181
5182         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, pmlmeinfo->network.MacAddress);
5183         join_type = 0;
5184         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
5185
5186         /* cancel link timer */
5187         del_timer_sync(&pmlmeext->link_timer);
5188
5189         start_clnt_join(padapter);
5190
5191         return H2C_SUCCESS;
5192 }
5193
5194 u8 disconnect_hdl(struct adapter *padapter, unsigned char *pbuf)
5195 {
5196         struct disconnect_parm *param = (struct disconnect_parm *)pbuf;
5197         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5198         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5199         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
5200         u8 val8;
5201
5202         if (is_client_associated_to_ap(padapter))
5203                 issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, param->deauth_timeout_ms/100, 100);
5204
5205         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
5206         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
5207
5208         /* restore to initial setting. */
5209         update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
5210
5211         if (((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) || ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)) {
5212                 /* Stop BCN */
5213                 val8 = 0;
5214                 rtw_hal_set_hwreg(padapter, HW_VAR_BCN_FUNC, (u8 *)(&val8));
5215         }
5216
5217
5218         /* set MSR to no link state -> infra. mode */
5219         Set_MSR(padapter, _HW_STATE_STATION_);
5220
5221         pmlmeinfo->state = WIFI_FW_NULL_STATE;
5222
5223         /* switch to the 20M Hz mode after disconnect */
5224         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
5225         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5226
5227         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
5228
5229         flush_all_cam_entry(padapter);
5230
5231         del_timer_sync(&pmlmeext->link_timer);
5232
5233         rtw_free_uc_swdec_pending_queue(padapter);
5234
5235         return  H2C_SUCCESS;
5236 }
5237
5238 static int rtw_scan_ch_decision(struct adapter *padapter, struct rtw_ieee80211_channel *out,
5239         u32 out_num, struct rtw_ieee80211_channel *in, u32 in_num)
5240 {
5241         int i, j;
5242         int set_idx;
5243         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5244
5245         /* clear out first */
5246         memset(out, 0, sizeof(struct rtw_ieee80211_channel)*out_num);
5247
5248         /* acquire channels from in */
5249         j = 0;
5250         for (i = 0; i < in_num; i++) {
5251                 set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, in[i].hw_value);
5252                 if (in[i].hw_value && !(in[i].flags & RTW_IEEE80211_CHAN_DISABLED) &&
5253                     set_idx >= 0) {
5254                         out[j] = in[i];
5255
5256                         if (pmlmeext->channel_set[set_idx].ScanType == SCAN_PASSIVE)
5257                                 out[j].flags &= RTW_IEEE80211_CHAN_PASSIVE_SCAN;
5258
5259                         j++;
5260                 }
5261                 if (j >= out_num)
5262                         break;
5263         }
5264
5265         /* if out is empty, use channel_set as default */
5266         if (j == 0) {
5267                 for (i = 0; i < pmlmeext->max_chan_nums; i++) {
5268                         out[i].hw_value = pmlmeext->channel_set[i].ChannelNum;
5269
5270                         if (pmlmeext->channel_set[i].ScanType == SCAN_PASSIVE)
5271                                 out[i].flags &= RTW_IEEE80211_CHAN_PASSIVE_SCAN;
5272
5273                         j++;
5274                 }
5275         }
5276
5277         return j;
5278 }
5279
5280 u8 sitesurvey_cmd_hdl(struct adapter *padapter, u8 *pbuf)
5281 {
5282         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5283         struct sitesurvey_parm  *pparm = (struct sitesurvey_parm *)pbuf;
5284         u8 bdelayscan = false;
5285         u8 val8;
5286         u32     initialgain;
5287         u32     i;
5288
5289         if (pmlmeext->sitesurvey_res.state == SCAN_DISABLE) {
5290                 /* for first time sitesurvey_cmd */
5291                 rtw_hal_set_hwreg(padapter, HW_VAR_CHECK_TXBUF, NULL);
5292
5293                 pmlmeext->sitesurvey_res.state = SCAN_START;
5294                 pmlmeext->sitesurvey_res.bss_cnt = 0;
5295                 pmlmeext->sitesurvey_res.channel_idx = 0;
5296
5297                 for (i = 0; i < RTW_SSID_SCAN_AMOUNT; i++) {
5298                         if (pparm->ssid[i].SsidLength) {
5299                                 memcpy(pmlmeext->sitesurvey_res.ssid[i].Ssid, pparm->ssid[i].Ssid, IW_ESSID_MAX_SIZE);
5300                                 pmlmeext->sitesurvey_res.ssid[i].SsidLength = pparm->ssid[i].SsidLength;
5301                         } else {
5302                                 pmlmeext->sitesurvey_res.ssid[i].SsidLength = 0;
5303                         }
5304                 }
5305
5306                 pmlmeext->sitesurvey_res.ch_num = rtw_scan_ch_decision(padapter
5307                         , pmlmeext->sitesurvey_res.ch, RTW_CHANNEL_SCAN_AMOUNT
5308                         , pparm->ch, pparm->ch_num
5309         );
5310
5311                 pmlmeext->sitesurvey_res.scan_mode = pparm->scan_mode;
5312
5313                 /* issue null data if associating to the AP */
5314                 if (is_client_associated_to_ap(padapter)) {
5315                         pmlmeext->sitesurvey_res.state = SCAN_TXNULL;
5316
5317                         issue_nulldata(padapter, NULL, 1, 3, 500);
5318
5319                         bdelayscan = true;
5320                 }
5321                 if (bdelayscan) {
5322                         /* delay 50ms to protect nulldata(1). */
5323                         set_survey_timer(pmlmeext, 50);
5324                         return H2C_SUCCESS;
5325                 }
5326         }
5327
5328         if ((pmlmeext->sitesurvey_res.state == SCAN_START) || (pmlmeext->sitesurvey_res.state == SCAN_TXNULL)) {
5329                 /* disable dynamic functions, such as high power, DIG */
5330                 Save_DM_Func_Flag(padapter);
5331                 Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false);
5332
5333                 /* config the initial gain under scanning, need to write the BB registers */
5334                 initialgain = 0x1E;
5335
5336                 rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
5337
5338                 /* set MSR to no link state */
5339                 Set_MSR(padapter, _HW_STATE_NOLINK_);
5340
5341                 val8 = 1; /* under site survey */
5342                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_SITESURVEY, (u8 *)(&val8));
5343
5344                 pmlmeext->sitesurvey_res.state = SCAN_PROCESS;
5345         }
5346
5347         site_survey(padapter);
5348
5349         return H2C_SUCCESS;
5350 }
5351
5352 u8 setauth_hdl(struct adapter *padapter, unsigned char *pbuf)
5353 {
5354         struct setauth_parm             *pparm = (struct setauth_parm *)pbuf;
5355         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5356         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5357
5358         if (pparm->mode < 4)
5359                 pmlmeinfo->auth_algo = pparm->mode;
5360         return  H2C_SUCCESS;
5361 }
5362
5363 u8 setkey_hdl(struct adapter *padapter, u8 *pbuf)
5364 {
5365         unsigned short                          ctrl;
5366         struct setkey_parm              *pparm = (struct setkey_parm *)pbuf;
5367         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5368         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5369         unsigned char                                   null_sta[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
5370
5371         /* main tx key for wep. */
5372         if (pparm->set_tx)
5373                 pmlmeinfo->key_index = pparm->keyid;
5374
5375         /* write cam */
5376         ctrl = BIT(15) | ((pparm->algorithm) << 2) | pparm->keyid;
5377
5378         DBG_88E_LEVEL(_drv_info_, "set group key to hw: alg:%d(WEP40-1 WEP104-5 TKIP-2 AES-4) "
5379                         "keyid:%d\n", pparm->algorithm, pparm->keyid);
5380         write_cam(padapter, pparm->keyid, ctrl, null_sta, pparm->key);
5381
5382         return H2C_SUCCESS;
5383 }
5384
5385 u8 set_stakey_hdl(struct adapter *padapter, u8 *pbuf)
5386 {
5387         u16 ctrl = 0;
5388         u8 cam_id;/* cam_entry */
5389         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5390         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5391         struct set_stakey_parm  *pparm = (struct set_stakey_parm *)pbuf;
5392
5393         /* cam_entry: */
5394         /* 0~3 for default key */
5395
5396         /* for concurrent mode (ap+sta): */
5397         /* default key is disable, using sw encrypt/decrypt */
5398         /* cam_entry = 4 for sta mode (macid = 0) */
5399         /* cam_entry(macid+3) = 5 ~ N for ap mode (aid = 1~N, macid = 2 ~N) */
5400
5401         /* for concurrent mode (sta+sta): */
5402         /* default key is disable, using sw encrypt/decrypt */
5403         /* cam_entry = 4 mapping to macid = 0 */
5404         /* cam_entry = 5 mapping to macid = 2 */
5405
5406         cam_id = 4;
5407
5408         DBG_88E_LEVEL(_drv_info_, "set pairwise key to hw: alg:%d(WEP40-1 WEP104-5 TKIP-2 AES-4) camid:%d\n",
5409                       pparm->algorithm, cam_id);
5410         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
5411                 struct sta_info *psta;
5412                 struct sta_priv *pstapriv = &padapter->stapriv;
5413
5414                 if (pparm->algorithm == _NO_PRIVACY_)   /*  clear cam entry */ {
5415                         clear_cam_entry(padapter, pparm->id);
5416                         return H2C_SUCCESS_RSP;
5417                 }
5418
5419                 psta = rtw_get_stainfo(pstapriv, pparm->addr);
5420                 if (psta) {
5421                         ctrl = BIT(15) | ((pparm->algorithm) << 2);
5422
5423                         DBG_88E("r871x_set_stakey_hdl(): enc_algorithm=%d\n", pparm->algorithm);
5424
5425                         if ((psta->mac_id < 1) || (psta->mac_id > (NUM_STA-4))) {
5426                                 DBG_88E("r871x_set_stakey_hdl():set_stakey failed, mac_id(aid)=%d\n", psta->mac_id);
5427                                 return H2C_REJECTED;
5428                         }
5429
5430                         cam_id = psta->mac_id + 3;/* 0~3 for default key, cmd_id = macid + 3, macid = aid+1; */
5431
5432                         DBG_88E("Write CAM, mac_addr =%pM, cam_entry=%d\n",
5433                                 pparm->addr, cam_id);
5434
5435                         write_cam(padapter, cam_id, ctrl, pparm->addr, pparm->key);
5436
5437                         return H2C_SUCCESS_RSP;
5438                 } else {
5439                         DBG_88E("r871x_set_stakey_hdl(): sta has been free\n");
5440                         return H2C_REJECTED;
5441                 }
5442         }
5443
5444         /* below for sta mode */
5445
5446         if (pparm->algorithm == _NO_PRIVACY_) { /*  clear cam entry */
5447                 clear_cam_entry(padapter, pparm->id);
5448                 return H2C_SUCCESS;
5449         }
5450         ctrl = BIT(15) | ((pparm->algorithm) << 2);
5451         write_cam(padapter, cam_id, ctrl, pparm->addr, pparm->key);
5452         pmlmeinfo->enc_algo = pparm->algorithm;
5453         return H2C_SUCCESS;
5454 }
5455
5456 u8 add_ba_hdl(struct adapter *padapter, unsigned char *pbuf)
5457 {
5458         struct addBaReq_parm    *pparm = (struct addBaReq_parm *)pbuf;
5459         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5460         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5461
5462         struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, pparm->addr);
5463
5464         if (!psta)
5465                 return  H2C_SUCCESS;
5466
5467         if (((pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) && (pmlmeinfo->HT_enable)) ||
5468             ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)) {
5469                 issue_action_BA(padapter, pparm->addr, RTW_WLAN_ACTION_ADDBA_REQ, (u16)pparm->tid);
5470                 mod_timer(&psta->addba_retry_timer,
5471                           jiffies + msecs_to_jiffies(ADDBA_TO));
5472         } else {
5473                 psta->htpriv.candidate_tid_bitmap &= ~BIT(pparm->tid);
5474         }
5475         return  H2C_SUCCESS;
5476 }
5477
5478 u8 set_tx_beacon_cmd(struct adapter *padapter)
5479 {
5480         struct cmd_obj  *ph2c;
5481         struct wlan_bssid_ex    *ptxBeacon_parm;
5482         struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
5483         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5484         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5485         u8 res = _SUCCESS;
5486         int len_diff = 0;
5487
5488
5489         ph2c = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
5490         if (ph2c == NULL) {
5491                 res = _FAIL;
5492                 goto exit;
5493         }
5494
5495         ptxBeacon_parm = kmemdup(&(pmlmeinfo->network),
5496                                 sizeof(struct wlan_bssid_ex), GFP_KERNEL);
5497         if (ptxBeacon_parm == NULL) {
5498                 kfree(ph2c);
5499                 res = _FAIL;
5500                 goto exit;
5501         }
5502
5503         len_diff = update_hidden_ssid(ptxBeacon_parm->IEs+_BEACON_IE_OFFSET_,
5504                                       ptxBeacon_parm->IELength-_BEACON_IE_OFFSET_,
5505                                       pmlmeinfo->hidden_ssid_mode);
5506         ptxBeacon_parm->IELength += len_diff;
5507
5508         init_h2fwcmd_w_parm_no_rsp(ph2c, ptxBeacon_parm, GEN_CMD_CODE(_TX_Beacon));
5509
5510         res = rtw_enqueue_cmd(pcmdpriv, ph2c);
5511
5512
5513 exit:
5514
5515
5516         return res;
5517 }
5518
5519 u8 mlme_evt_hdl(struct adapter *padapter, unsigned char *pbuf)
5520 {
5521         u8 evt_code;
5522         u16 evt_sz;
5523         uint    *peventbuf;
5524         void (*event_callback)(struct adapter *dev, u8 *pbuf);
5525
5526         peventbuf = (uint *)pbuf;
5527         evt_sz = (u16)(*peventbuf&0xffff);
5528         evt_code = (u8)((*peventbuf>>16)&0xff);
5529
5530         /*  checking if event code is valid */
5531         if (evt_code >= MAX_C2HEVT) {
5532                 RT_TRACE(_module_rtl871x_cmd_c_, _drv_err_, ("\nEvent Code(%d) mismatch!\n", evt_code));
5533                 goto _abort_event_;
5534         }
5535
5536         /*  checking if event size match the event parm size */
5537         if ((wlanevents[evt_code].parmsize != 0) &&
5538             (wlanevents[evt_code].parmsize != evt_sz)) {
5539                 RT_TRACE(_module_rtl871x_cmd_c_, _drv_err_,
5540                          ("\nEvent(%d) Parm Size mismatch (%d vs %d)!\n",
5541                          evt_code, wlanevents[evt_code].parmsize, evt_sz));
5542                 goto _abort_event_;
5543         }
5544
5545         peventbuf += 2;
5546
5547         if (peventbuf) {
5548                 event_callback = wlanevents[evt_code].event_callback;
5549                 event_callback(padapter, (u8 *)peventbuf);
5550
5551         }
5552
5553 _abort_event_:
5554         return H2C_SUCCESS;
5555 }
5556
5557 u8 tx_beacon_hdl(struct adapter *padapter, unsigned char *pbuf)
5558 {
5559         if (send_beacon(padapter) == _FAIL) {
5560                 DBG_88E("issue_beacon, fail!\n");
5561                 return H2C_PARAMETERS_ERROR;
5562         }
5563 #ifdef CONFIG_88EU_AP_MODE
5564         else { /* tx bc/mc frames after update TIM */
5565                 struct sta_info *psta_bmc;
5566                 struct list_head *xmitframe_plist, *xmitframe_phead;
5567                 struct xmit_frame *pxmitframe = NULL;
5568                 struct sta_priv  *pstapriv = &padapter->stapriv;
5569
5570                 /* for BC/MC Frames */
5571                 psta_bmc = rtw_get_bcmc_stainfo(padapter);
5572                 if (!psta_bmc)
5573                         return H2C_SUCCESS;
5574
5575                 if ((pstapriv->tim_bitmap&BIT(0)) && (psta_bmc->sleepq_len > 0)) {
5576                         msleep(10);/*  10ms, ATIM(HIQ) Windows */
5577                         spin_lock_bh(&psta_bmc->sleep_q.lock);
5578
5579                         xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
5580                         xmitframe_plist = xmitframe_phead->next;
5581
5582                         while (xmitframe_phead != xmitframe_plist) {
5583                                 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
5584
5585                                 xmitframe_plist = xmitframe_plist->next;
5586
5587                                 list_del_init(&pxmitframe->list);
5588
5589                                 psta_bmc->sleepq_len--;
5590                                 if (psta_bmc->sleepq_len > 0)
5591                                         pxmitframe->attrib.mdata = 1;
5592                                 else
5593                                         pxmitframe->attrib.mdata = 0;
5594
5595                                 pxmitframe->attrib.triggered = 1;
5596
5597                                 pxmitframe->attrib.qsel = 0x11;/* HIQ */
5598
5599                                 spin_unlock_bh(&psta_bmc->sleep_q.lock);
5600                                 if (rtw_hal_xmit(padapter, pxmitframe))
5601                                         rtw_os_xmit_complete(padapter, pxmitframe);
5602                                 spin_lock_bh(&psta_bmc->sleep_q.lock);
5603                         }
5604                         spin_unlock_bh(&psta_bmc->sleep_q.lock);
5605                 }
5606         }
5607 #endif
5608         return H2C_SUCCESS;
5609 }
5610
5611 u8 set_ch_hdl(struct adapter *padapter, u8 *pbuf)
5612 {
5613         struct set_ch_parm *set_ch_parm;
5614         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5615
5616         if (!pbuf)
5617                 return H2C_PARAMETERS_ERROR;
5618
5619         set_ch_parm = (struct set_ch_parm *)pbuf;
5620
5621         DBG_88E(FUNC_NDEV_FMT" ch:%u, bw:%u, ch_offset:%u\n",
5622                 FUNC_NDEV_ARG(padapter->pnetdev),
5623                 set_ch_parm->ch, set_ch_parm->bw, set_ch_parm->ch_offset);
5624
5625         pmlmeext->cur_channel = set_ch_parm->ch;
5626         pmlmeext->cur_ch_offset = set_ch_parm->ch_offset;
5627         pmlmeext->cur_bwmode = set_ch_parm->bw;
5628
5629         set_channel_bwmode(padapter, set_ch_parm->ch, set_ch_parm->ch_offset, set_ch_parm->bw);
5630
5631         return  H2C_SUCCESS;
5632 }
5633
5634 u8 set_chplan_hdl(struct adapter *padapter, unsigned char *pbuf)
5635 {
5636         struct SetChannelPlan_param *setChannelPlan_param;
5637         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5638
5639         if (!pbuf)
5640                 return H2C_PARAMETERS_ERROR;
5641
5642         setChannelPlan_param = (struct SetChannelPlan_param *)pbuf;
5643
5644         pmlmeext->max_chan_nums = init_channel_set(padapter, setChannelPlan_param->channel_plan, pmlmeext->channel_set);
5645         init_channel_list(padapter, pmlmeext->channel_set, pmlmeext->max_chan_nums, &pmlmeext->channel_list);
5646
5647         return  H2C_SUCCESS;
5648 }