96e5c6dc5b7c5169e6c622199b736d97bb96e8f6
[platform/kernel/linux-rpi.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, raddr, ETH_ALEN);
1677         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1678         memcpy(pwlanhdr->addr3, pnetwork->MacAddress, ETH_ALEN);
1679
1680         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1681         pmlmeext->mgnt_seq++;
1682         SetFrameSubType(pframe, WIFI_ACTION);
1683
1684         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1685         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1686
1687         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
1688         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
1689
1690         if (category == 3) {
1691                 switch (action) {
1692                 case 0: /* ADDBA req */
1693                         do {
1694                                 pmlmeinfo->dialogToken++;
1695                         } while (pmlmeinfo->dialogToken == 0);
1696                         pframe = rtw_set_fixed_ie(pframe, 1, &(pmlmeinfo->dialogToken), &(pattrib->pktlen));
1697
1698                         BA_para_set = 0x1002 | ((status & 0xf) << 2); /* immediate ack & 64 buffer size */
1699                         le_tmp = cpu_to_le16(BA_para_set);
1700                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1701                                                   &pattrib->pktlen);
1702
1703                         BA_timeout_value = 5000;/*  5ms */
1704                         le_tmp = cpu_to_le16(BA_timeout_value);
1705                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1706                                                   &pattrib->pktlen);
1707
1708                         psta = rtw_get_stainfo(pstapriv, raddr);
1709                         if (psta != NULL) {
1710                                 start_seq = (psta->sta_xmitpriv.txseq_tid[status & 0x07]&0xfff) + 1;
1711
1712                                 DBG_88E("BA_starting_seqctrl=%d for TID=%d\n", start_seq, status & 0x07);
1713
1714                                 psta->BA_starting_seqctrl[status & 0x07] = start_seq;
1715
1716                                 BA_starting_seqctrl = start_seq << 4;
1717                         }
1718                         le_tmp = cpu_to_le16(BA_starting_seqctrl);
1719                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1720                                                   &pattrib->pktlen);
1721                         break;
1722                 case 1: /* ADDBA rsp */
1723                 {
1724                         struct ADDBA_request *ADDBA_req = &pmlmeinfo->ADDBA_req;
1725
1726                         pframe = rtw_set_fixed_ie(pframe, 1,
1727                                                   &ADDBA_req->dialog_token,
1728                                                   &pattrib->pktlen);
1729                         pframe = rtw_set_fixed_ie(pframe, 2, &status,
1730                                                   &pattrib->pktlen);
1731
1732                         BA_para_set = le16_to_cpu(ADDBA_req->BA_para_set) &
1733                                       0x3f;
1734                         rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor);
1735                         switch (max_rx_ampdu_factor) {
1736                         case MAX_AMPDU_FACTOR_64K:
1737                                 BA_para_set |= 0x1000; /* 64 buffer size */
1738                                 break;
1739                         case MAX_AMPDU_FACTOR_32K:
1740                                 BA_para_set |= 0x0800; /* 32 buffer size */
1741                                 break;
1742                         case MAX_AMPDU_FACTOR_16K:
1743                                 BA_para_set |= 0x0400; /* 16 buffer size */
1744                                 break;
1745                         case MAX_AMPDU_FACTOR_8K:
1746                                 BA_para_set |= 0x0200; /* 8 buffer size */
1747                                 break;
1748                         default:
1749                                 BA_para_set |= 0x1000; /* 64 buffer size */
1750                                 break;
1751                         }
1752
1753                         if (pregpriv->ampdu_amsdu == 0)/* disabled */
1754                                 BA_para_set = BA_para_set & ~BIT(0);
1755                         else if (pregpriv->ampdu_amsdu == 1)/* enabled */
1756                                 BA_para_set = BA_para_set | BIT(0);
1757                         le_tmp = cpu_to_le16(BA_para_set);
1758
1759                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1760                                                   &pattrib->pktlen);
1761                         pframe = rtw_set_fixed_ie(pframe, 2,
1762                                                   &ADDBA_req->BA_timeout_value,
1763                                                   &pattrib->pktlen);
1764                         break;
1765                 }
1766                 case 2:/* DELBA */
1767                         BA_para_set = (status & 0x1F) << 3;
1768                         le_tmp = cpu_to_le16(BA_para_set);
1769                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1770                                                   &pattrib->pktlen);
1771
1772                         reason_code = 37;/* Requested from peer STA as it does not want to use the mechanism */
1773                         le_tmp = cpu_to_le16(reason_code);
1774                         pframe = rtw_set_fixed_ie(pframe, 2, &(le_tmp),
1775                                                   &pattrib->pktlen);
1776                         break;
1777                 default:
1778                         break;
1779                 }
1780         }
1781
1782         pattrib->last_txcmdsz = pattrib->pktlen;
1783
1784         dump_mgntframe(padapter, pmgntframe);
1785 }
1786
1787 static void issue_action_BSSCoexistPacket(struct adapter *padapter)
1788 {
1789         struct list_head *plist, *phead;
1790         unsigned char category, action;
1791         struct xmit_frame                       *pmgntframe;
1792         struct pkt_attrib                       *pattrib;
1793         unsigned char                           *pframe;
1794         struct rtw_ieee80211_hdr        *pwlanhdr;
1795         __le16 *fctrl;
1796         struct  wlan_network    *pnetwork = NULL;
1797         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
1798         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1799         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1800         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1801         struct __queue *queue   = &(pmlmepriv->scanned_queue);
1802         u8 InfoContent[16] = {0};
1803         u8 ICS[8][15];
1804         struct wlan_bssid_ex  *cur_network   = &(pmlmeinfo->network);
1805
1806         if ((pmlmepriv->num_FortyMHzIntolerant == 0) || (pmlmepriv->num_sta_no_ht == 0))
1807                 return;
1808
1809         if (pmlmeinfo->bwmode_updated)
1810                 return;
1811
1812
1813         DBG_88E("%s\n", __func__);
1814
1815
1816         category = RTW_WLAN_CATEGORY_PUBLIC;
1817         action = ACT_PUBLIC_BSSCOEXIST;
1818
1819         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1820         if (pmgntframe == NULL)
1821                 return;
1822
1823         /* update attribute */
1824         pattrib = &pmgntframe->attrib;
1825         update_mgntframe_attrib(padapter, pattrib);
1826
1827         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1828
1829         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1830         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1831
1832         fctrl = &(pwlanhdr->frame_ctl);
1833         *(fctrl) = 0;
1834
1835         memcpy(pwlanhdr->addr1, cur_network->MacAddress, ETH_ALEN);
1836         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1837         memcpy(pwlanhdr->addr3, cur_network->MacAddress, ETH_ALEN);
1838
1839         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1840         pmlmeext->mgnt_seq++;
1841         SetFrameSubType(pframe, WIFI_ACTION);
1842
1843         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1844         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1845
1846         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
1847         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
1848
1849
1850         /*  */
1851         if (pmlmepriv->num_FortyMHzIntolerant > 0) {
1852                 u8 iedata = 0;
1853
1854                 iedata |= BIT(2);/* 20 MHz BSS Width Request */
1855
1856                 pframe = rtw_set_ie(pframe, EID_BSSCoexistence,  1, &iedata, &(pattrib->pktlen));
1857         }
1858
1859
1860         /*  */
1861         memset(ICS, 0, sizeof(ICS));
1862         if (pmlmepriv->num_sta_no_ht > 0) {
1863                 int i;
1864
1865                 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1866
1867                 phead = get_list_head(queue);
1868                 plist = phead->next;
1869
1870                 while (phead != plist) {
1871                         int len;
1872                         u8 *p;
1873                         struct wlan_bssid_ex *pbss_network;
1874
1875                         pnetwork = container_of(plist, struct wlan_network, list);
1876
1877                         plist = plist->next;
1878
1879                         pbss_network = (struct wlan_bssid_ex *)&pnetwork->network;
1880
1881                         p = rtw_get_ie(pbss_network->IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, pbss_network->IELength - _FIXED_IE_LENGTH_);
1882                         if ((p == NULL) || (len == 0)) { /* non-HT */
1883                                 if ((pbss_network->Configuration.DSConfig <= 0) || (pbss_network->Configuration.DSConfig > 14))
1884                                         continue;
1885
1886                                 ICS[0][pbss_network->Configuration.DSConfig] = 1;
1887
1888                                 if (ICS[0][0] == 0)
1889                                         ICS[0][0] = 1;
1890                         }
1891                 }
1892                 spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1893
1894                 for (i = 0; i < 8; i++) {
1895                         if (ICS[i][0] == 1) {
1896                                 int j, k = 0;
1897
1898                                 InfoContent[k] = i;
1899                                 /* SET_BSS_INTOLERANT_ELE_REG_CLASS(InfoContent, i); */
1900                                 k++;
1901
1902                                 for (j = 1; j <= 14; j++) {
1903                                         if (ICS[i][j] == 1) {
1904                                                 if (k < 16) {
1905                                                         InfoContent[k] = j; /* channel number */
1906                                                         /* SET_BSS_INTOLERANT_ELE_CHANNEL(InfoContent+k, j); */
1907                                                         k++;
1908                                                 }
1909                                         }
1910                                 }
1911
1912                                 pframe = rtw_set_ie(pframe, EID_BSSIntolerantChlReport, k, InfoContent, &(pattrib->pktlen));
1913                         }
1914                 }
1915         }
1916
1917
1918         pattrib->last_txcmdsz = pattrib->pktlen;
1919
1920         dump_mgntframe(padapter, pmgntframe);
1921 }
1922
1923 unsigned int send_delba(struct adapter *padapter, u8 initiator, u8 *addr)
1924 {
1925         struct sta_priv *pstapriv = &padapter->stapriv;
1926         struct sta_info *psta = NULL;
1927         /* struct recv_reorder_ctrl *preorder_ctrl; */
1928         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1929         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1930         u16 tid;
1931
1932         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
1933                 if (!(pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS))
1934                         return _SUCCESS;
1935
1936         psta = rtw_get_stainfo(pstapriv, addr);
1937         if (psta == NULL)
1938                 return _SUCCESS;
1939
1940         if (initiator == 0) { /*  recipient */
1941                 for (tid = 0; tid < MAXTID; tid++) {
1942                         if (psta->recvreorder_ctrl[tid].enable) {
1943                                 DBG_88E("rx agg disable tid(%d)\n", tid);
1944                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_DELBA, (((tid << 1) | initiator)&0x1F));
1945                                 psta->recvreorder_ctrl[tid].enable = false;
1946                                 psta->recvreorder_ctrl[tid].indicate_seq = 0xffff;
1947                         }
1948                 }
1949         } else if (initiator == 1) { /*  originator */
1950                 for (tid = 0; tid < MAXTID; tid++) {
1951                         if (psta->htpriv.agg_enable_bitmap & BIT(tid)) {
1952                                 DBG_88E("tx agg disable tid(%d)\n", tid);
1953                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_DELBA, (((tid << 1) | initiator)&0x1F));
1954                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
1955                                 psta->htpriv.candidate_tid_bitmap &= ~BIT(tid);
1956                         }
1957                 }
1958         }
1959
1960         return _SUCCESS;
1961 }
1962
1963 unsigned int send_beacon(struct adapter *padapter)
1964 {
1965         u8 bxmitok = false;
1966         int     issue = 0;
1967         int poll = 0;
1968
1969         unsigned long start = jiffies;
1970
1971         rtw_hal_set_hwreg(padapter, HW_VAR_BCN_VALID, NULL);
1972         do {
1973                 issue_beacon(padapter, 100);
1974                 issue++;
1975                 do {
1976                         yield();
1977                         rtw_hal_get_hwreg(padapter, HW_VAR_BCN_VALID, (u8 *)(&bxmitok));
1978                         poll++;
1979                 } while ((poll%10) != 0 && !bxmitok && !padapter->bSurpriseRemoved && !padapter->bDriverStopped);
1980         } while (!bxmitok && issue < 100 && !padapter->bSurpriseRemoved && !padapter->bDriverStopped);
1981
1982         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
1983                 return _FAIL;
1984         if (!bxmitok) {
1985                 DBG_88E("%s fail! %u ms\n", __func__,
1986                         jiffies_to_msecs(jiffies - start));
1987                 return _FAIL;
1988         } else {
1989                 u32 passing_time = jiffies_to_msecs(jiffies - start);
1990
1991                 if (passing_time > 100 || issue > 3)
1992                         DBG_88E("%s success, issue:%d, poll:%d, %u ms\n",
1993                                 __func__, issue, poll,
1994                                 jiffies_to_msecs(jiffies - start));
1995                 return _SUCCESS;
1996         }
1997 }
1998
1999 /****************************************************************************
2000
2001 Following are some utility functions for WiFi MLME
2002
2003 *****************************************************************************/
2004
2005 static void site_survey(struct adapter *padapter)
2006 {
2007         unsigned char           survey_channel = 0, val8;
2008         enum rt_scan_type ScanType = SCAN_PASSIVE;
2009         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2010         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2011         u32 initialgain = 0;
2012         struct rtw_ieee80211_channel *ch;
2013
2014         if (pmlmeext->sitesurvey_res.channel_idx < pmlmeext->sitesurvey_res.ch_num) {
2015                 ch = &pmlmeext->sitesurvey_res.ch[pmlmeext->sitesurvey_res.channel_idx];
2016                 survey_channel = ch->hw_value;
2017                 ScanType = (ch->flags & RTW_IEEE80211_CHAN_PASSIVE_SCAN) ? SCAN_PASSIVE : SCAN_ACTIVE;
2018         }
2019
2020
2021         if (survey_channel != 0) {
2022                 /* PAUSE 4-AC Queue when site_survey */
2023                 /* rtw_hal_get_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
2024                 /* val8 |= 0x0f; */
2025                 /* rtw_hal_set_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
2026                 if (pmlmeext->sitesurvey_res.channel_idx == 0)
2027                         set_channel_bwmode(padapter, survey_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
2028                 else
2029                         SelectChannel(padapter, survey_channel);
2030
2031                 if (ScanType == SCAN_ACTIVE) { /* obey the channel plan setting... */
2032                         int i;
2033                         for (i = 0; i < RTW_SSID_SCAN_AMOUNT; i++) {
2034                                 if (pmlmeext->sitesurvey_res.ssid[i].SsidLength) {
2035                                         /* todo: to issue two probe req??? */
2036                                         issue_probereq(padapter,
2037                                         &(pmlmeext->sitesurvey_res.ssid[i]),
2038                                                                 NULL, false);
2039                                         /* msleep(SURVEY_TO>>1); */
2040                                         issue_probereq(padapter,
2041                                         &(pmlmeext->sitesurvey_res.ssid[i]),
2042                                                                 NULL, false);
2043                                 }
2044                         }
2045
2046                         if (pmlmeext->sitesurvey_res.scan_mode == SCAN_ACTIVE) {
2047                                 /* todo: to issue two probe req??? */
2048                                 issue_probereq(padapter, NULL, NULL, false);
2049                                 /* msleep(SURVEY_TO>>1); */
2050                                 issue_probereq(padapter, NULL, NULL, false);
2051                         }
2052
2053                         if (pmlmeext->sitesurvey_res.scan_mode == SCAN_ACTIVE) {
2054                                 /* todo: to issue two probe req??? */
2055                                 issue_probereq(padapter, NULL, NULL, false);
2056                                 /* msleep(SURVEY_TO>>1); */
2057                                 issue_probereq(padapter, NULL, NULL, false);
2058                         }
2059                 }
2060
2061                 set_survey_timer(pmlmeext, pmlmeext->chan_scan_time);
2062         } else {
2063
2064                 /*  20100721:Interrupt scan operation here. */
2065                 /*  For SW antenna diversity before link, it needs to switch to another antenna and scan again. */
2066                 /*  It compares the scan result and select better one to do connection. */
2067                 if (rtw_hal_antdiv_before_linked(padapter)) {
2068                         pmlmeext->sitesurvey_res.bss_cnt = 0;
2069                         pmlmeext->sitesurvey_res.channel_idx = -1;
2070                         pmlmeext->chan_scan_time = SURVEY_TO / 2;
2071                         set_survey_timer(pmlmeext, pmlmeext->chan_scan_time);
2072                         return;
2073                 }
2074
2075                 pmlmeext->sitesurvey_res.state = SCAN_COMPLETE;
2076
2077                 /* switch back to the original channel */
2078
2079                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
2080
2081                 /* flush 4-AC Queue after site_survey */
2082                 /* val8 = 0; */
2083                 /* rtw_hal_set_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
2084
2085                 /* config MSR */
2086                 Set_MSR(padapter, (pmlmeinfo->state & 0x3));
2087
2088                 initialgain = 0xff; /* restore RX GAIN */
2089                 rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
2090                 /* turn on dynamic functions */
2091                 Restore_DM_Func_Flag(padapter);
2092                 /* Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true); */
2093
2094                 if (is_client_associated_to_ap(padapter))
2095                         issue_nulldata(padapter, NULL, 0, 3, 500);
2096
2097                 val8 = 0; /* survey done */
2098                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_SITESURVEY, (u8 *)(&val8));
2099
2100                 report_surveydone_event(padapter);
2101
2102                 pmlmeext->chan_scan_time = SURVEY_TO;
2103                 pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
2104
2105                 issue_action_BSSCoexistPacket(padapter);
2106                 issue_action_BSSCoexistPacket(padapter);
2107                 issue_action_BSSCoexistPacket(padapter);
2108         }
2109         return;
2110 }
2111
2112 /* collect bss info from Beacon and Probe request/response frames. */
2113 static u8 collect_bss_info(struct adapter *padapter,
2114                            struct recv_frame *precv_frame,
2115                            struct wlan_bssid_ex *bssid)
2116 {
2117         int     i;
2118         u32     len;
2119         u8 *p;
2120         u16 val16, subtype;
2121         u8 *pframe = precv_frame->rx_data;
2122         u32     packet_len = precv_frame->len;
2123         u8 ie_offset;
2124         struct registry_priv    *pregistrypriv = &padapter->registrypriv;
2125         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2126         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2127
2128         len = packet_len - sizeof(struct rtw_ieee80211_hdr_3addr);
2129
2130         if (len > MAX_IE_SZ)
2131                 return _FAIL;
2132
2133         memset(bssid, 0, sizeof(struct wlan_bssid_ex));
2134
2135         subtype = GetFrameSubType(pframe);
2136
2137         if (subtype == WIFI_BEACON) {
2138                 bssid->Reserved[0] = 1;
2139                 ie_offset = _BEACON_IE_OFFSET_;
2140         } else {
2141                 /*  FIXME : more type */
2142                 if (subtype == WIFI_PROBEREQ) {
2143                         ie_offset = _PROBEREQ_IE_OFFSET_;
2144                         bssid->Reserved[0] = 2;
2145                 } else if (subtype == WIFI_PROBERSP) {
2146                         ie_offset = _PROBERSP_IE_OFFSET_;
2147                         bssid->Reserved[0] = 3;
2148                 } else {
2149                         bssid->Reserved[0] = 0;
2150                         ie_offset = _FIXED_IE_LENGTH_;
2151                 }
2152         }
2153
2154         bssid->Length = sizeof(struct wlan_bssid_ex) - MAX_IE_SZ + len;
2155
2156         /* below is to copy the information element */
2157         bssid->IELength = len;
2158         memcpy(bssid->IEs, (pframe + sizeof(struct rtw_ieee80211_hdr_3addr)), bssid->IELength);
2159
2160         /* get the signal strength in dBM.raw data */
2161         bssid->Rssi = precv_frame->attrib.phy_info.recvpower;
2162         bssid->PhyInfo.SignalQuality = precv_frame->attrib.phy_info.SignalQuality;/* in percentage */
2163         bssid->PhyInfo.SignalStrength = precv_frame->attrib.phy_info.SignalStrength;/* in percentage */
2164         rtw_hal_get_def_var(padapter, HAL_DEF_CURRENT_ANTENNA,  &bssid->PhyInfo.Optimum_antenna);
2165
2166         /*  checking SSID */
2167         p = rtw_get_ie(bssid->IEs + ie_offset, _SSID_IE_, &len, bssid->IELength - ie_offset);
2168         if (p == NULL) {
2169                 DBG_88E("marc: cannot find SSID for survey event\n");
2170                 return _FAIL;
2171         }
2172
2173         if (len) {
2174                 if (len > NDIS_802_11_LENGTH_SSID) {
2175                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
2176                         return _FAIL;
2177                 }
2178                 memcpy(bssid->Ssid.Ssid, (p + 2), len);
2179                 bssid->Ssid.SsidLength = len;
2180         } else {
2181                 bssid->Ssid.SsidLength = 0;
2182         }
2183
2184         memset(bssid->SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
2185
2186         /* checking rate info... */
2187         i = 0;
2188         p = rtw_get_ie(bssid->IEs + ie_offset, _SUPPORTEDRATES_IE_, &len, bssid->IELength - ie_offset);
2189         if (p != NULL) {
2190                 if (len > NDIS_802_11_LENGTH_RATES_EX) {
2191                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
2192                         return _FAIL;
2193                 }
2194                 memcpy(bssid->SupportedRates, (p + 2), len);
2195                 i = len;
2196         }
2197
2198         p = rtw_get_ie(bssid->IEs + ie_offset, _EXT_SUPPORTEDRATES_IE_, &len, bssid->IELength - ie_offset);
2199         if (p != NULL) {
2200                 if (len > (NDIS_802_11_LENGTH_RATES_EX-i)) {
2201                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
2202                         return _FAIL;
2203                 }
2204                 memcpy(bssid->SupportedRates + i, (p + 2), len);
2205         }
2206
2207         /* todo: */
2208         bssid->NetworkTypeInUse = Ndis802_11OFDM24;
2209
2210         if (bssid->IELength < 12)
2211                 return _FAIL;
2212
2213         /*  Checking for DSConfig */
2214         p = rtw_get_ie(bssid->IEs + ie_offset, _DSSET_IE_, &len, bssid->IELength - ie_offset);
2215
2216         bssid->Configuration.DSConfig = 0;
2217         bssid->Configuration.Length = 0;
2218
2219         if (p) {
2220                 bssid->Configuration.DSConfig = *(p + 2);
2221         } else {/*  In 5G, some ap do not have DSSET IE */
2222                 /*  checking HT info for channel */
2223                 p = rtw_get_ie(bssid->IEs + ie_offset, _HT_ADD_INFO_IE_, &len, bssid->IELength - ie_offset);
2224                 if (p) {
2225                         struct HT_info_element *HT_info = (struct HT_info_element *)(p + 2);
2226                         bssid->Configuration.DSConfig = HT_info->primary_channel;
2227                 } else { /*  use current channel */
2228                         bssid->Configuration.DSConfig = rtw_get_oper_ch(padapter);
2229                 }
2230         }
2231
2232         if (subtype == WIFI_PROBEREQ) {
2233                 /*  FIXME */
2234                 bssid->InfrastructureMode = Ndis802_11Infrastructure;
2235                 memcpy(bssid->MacAddress, GetAddr2Ptr(pframe), ETH_ALEN);
2236                 bssid->Privacy = 1;
2237                 return _SUCCESS;
2238         }
2239
2240         bssid->Configuration.BeaconPeriod =
2241                 get_unaligned_le16(rtw_get_beacon_interval_from_ie(bssid->IEs));
2242
2243         val16 = rtw_get_capability((struct wlan_bssid_ex *)bssid);
2244
2245         if (val16 & BIT(0)) {
2246                 bssid->InfrastructureMode = Ndis802_11Infrastructure;
2247                 memcpy(bssid->MacAddress, GetAddr2Ptr(pframe), ETH_ALEN);
2248         } else {
2249                 bssid->InfrastructureMode = Ndis802_11IBSS;
2250                 memcpy(bssid->MacAddress, GetAddr3Ptr(pframe), ETH_ALEN);
2251         }
2252
2253         if (val16 & BIT(4))
2254                 bssid->Privacy = 1;
2255         else
2256                 bssid->Privacy = 0;
2257
2258         bssid->Configuration.ATIMWindow = 0;
2259
2260         /* 20/40 BSS Coexistence check */
2261         if ((pregistrypriv->wifi_spec == 1) && (!pmlmeinfo->bwmode_updated)) {
2262                 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2263                 p = rtw_get_ie(bssid->IEs + ie_offset, _HT_CAPABILITY_IE_, &len, bssid->IELength - ie_offset);
2264                 if (p && len > 0) {
2265                         struct HT_caps_element  *pHT_caps;
2266                         pHT_caps = (struct HT_caps_element *)(p + 2);
2267
2268                         if (le16_to_cpu(pHT_caps->u.HT_cap_element.HT_caps_info)&BIT(14))
2269                                 pmlmepriv->num_FortyMHzIntolerant++;
2270                 } else {
2271                         pmlmepriv->num_sta_no_ht++;
2272                 }
2273         }
2274
2275         /*  mark bss info receiving from nearby channel as SignalQuality 101 */
2276         if (bssid->Configuration.DSConfig != rtw_get_oper_ch(padapter))
2277                 bssid->PhyInfo.SignalQuality = 101;
2278         return _SUCCESS;
2279 }
2280
2281 static void start_create_ibss(struct adapter *padapter)
2282 {
2283         unsigned short  caps;
2284         u8 val8;
2285         u8 join_type;
2286         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2287         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2288         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
2289         pmlmeext->cur_channel = (u8)pnetwork->Configuration.DSConfig;
2290         pmlmeinfo->bcn_interval = get_beacon_interval(pnetwork);
2291
2292         /* update wireless mode */
2293         update_wireless_mode(padapter);
2294
2295         /* update capability */
2296         caps = rtw_get_capability((struct wlan_bssid_ex *)pnetwork);
2297         update_capinfo(padapter, caps);
2298         if (caps&cap_IBSS) {/* adhoc master */
2299                 val8 = 0xcf;
2300                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
2301
2302                 /* switch channel */
2303                 /* SelectChannel(padapter, pmlmeext->cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE); */
2304                 set_channel_bwmode(padapter, pmlmeext->cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
2305
2306                 beacon_timing_control(padapter);
2307
2308                 /* set msr to WIFI_FW_ADHOC_STATE */
2309                 pmlmeinfo->state = WIFI_FW_ADHOC_STATE;
2310                 Set_MSR(padapter, (pmlmeinfo->state & 0x3));
2311
2312                 /* issue beacon */
2313                 if (send_beacon(padapter) == _FAIL) {
2314                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("issuing beacon frame fail....\n"));
2315
2316                         report_join_res(padapter, -1);
2317                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
2318                 } else {
2319                         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, padapter->registrypriv.dev_network.MacAddress);
2320                         join_type = 0;
2321                         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
2322
2323                         report_join_res(padapter, 1);
2324                         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
2325                 }
2326         } else {
2327                 DBG_88E("start_create_ibss, invalid cap:%x\n", caps);
2328                 return;
2329         }
2330 }
2331
2332 static void start_clnt_join(struct adapter *padapter)
2333 {
2334         unsigned short  caps;
2335         u8 val8;
2336         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2337         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2338         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
2339         int beacon_timeout;
2340
2341         pmlmeext->cur_channel = (u8)pnetwork->Configuration.DSConfig;
2342         pmlmeinfo->bcn_interval = get_beacon_interval(pnetwork);
2343
2344         /* update wireless mode */
2345         update_wireless_mode(padapter);
2346
2347         /* update capability */
2348         caps = rtw_get_capability((struct wlan_bssid_ex *)pnetwork);
2349         update_capinfo(padapter, caps);
2350         if (caps&cap_ESS) {
2351                 Set_MSR(padapter, WIFI_FW_STATION_STATE);
2352
2353                 val8 = (pmlmeinfo->auth_algo == dot11AuthAlgrthm_8021X) ? 0xcc : 0xcf;
2354
2355                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
2356
2357                 /* switch channel */
2358                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
2359
2360                 /* here wait for receiving the beacon to start auth */
2361                 /* and enable a timer */
2362                 beacon_timeout = decide_wait_for_beacon_timeout(pmlmeinfo->bcn_interval);
2363                 set_link_timer(pmlmeext, beacon_timeout);
2364                 mod_timer(&padapter->mlmepriv.assoc_timer, jiffies +
2365                           msecs_to_jiffies((REAUTH_TO * REAUTH_LIMIT) + (REASSOC_TO * REASSOC_LIMIT) + beacon_timeout));
2366
2367                 pmlmeinfo->state = WIFI_FW_AUTH_NULL | WIFI_FW_STATION_STATE;
2368         } else if (caps&cap_IBSS) { /* adhoc client */
2369                 Set_MSR(padapter, WIFI_FW_ADHOC_STATE);
2370
2371                 val8 = 0xcf;
2372                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
2373
2374                 /* switch channel */
2375                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
2376
2377                 beacon_timing_control(padapter);
2378
2379                 pmlmeinfo->state = WIFI_FW_ADHOC_STATE;
2380
2381                 report_join_res(padapter, 1);
2382         } else {
2383                 return;
2384         }
2385 }
2386
2387 static void start_clnt_auth(struct adapter *padapter)
2388 {
2389         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2390         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2391
2392         del_timer_sync(&pmlmeext->link_timer);
2393
2394         pmlmeinfo->state &= (~WIFI_FW_AUTH_NULL);
2395         pmlmeinfo->state |= WIFI_FW_AUTH_STATE;
2396
2397         pmlmeinfo->auth_seq = 1;
2398         pmlmeinfo->reauth_count = 0;
2399         pmlmeinfo->reassoc_count = 0;
2400         pmlmeinfo->link_count = 0;
2401         pmlmeext->retry = 0;
2402
2403
2404         /*  Because of AP's not receiving deauth before */
2405         /*  AP may: 1)not response auth or 2)deauth us after link is complete */
2406         /*  issue deauth before issuing auth to deal with the situation */
2407         /*      Commented by Albert 2012/07/21 */
2408         /*      For the Win8 P2P connection, it will be hard to have a successful connection if this Wi-Fi doesn't connect to it. */
2409         issue_deauth(padapter, (&(pmlmeinfo->network))->MacAddress, WLAN_REASON_DEAUTH_LEAVING);
2410
2411         DBG_88E_LEVEL(_drv_info_, "start auth\n");
2412         issue_auth(padapter, NULL, 0);
2413
2414         set_link_timer(pmlmeext, REAUTH_TO);
2415 }
2416
2417
2418 static void start_clnt_assoc(struct adapter *padapter)
2419 {
2420         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2421         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2422
2423         del_timer_sync(&pmlmeext->link_timer);
2424
2425         pmlmeinfo->state &= (~(WIFI_FW_AUTH_NULL | WIFI_FW_AUTH_STATE));
2426         pmlmeinfo->state |= (WIFI_FW_AUTH_SUCCESS | WIFI_FW_ASSOC_STATE);
2427
2428         issue_assocreq(padapter);
2429
2430         set_link_timer(pmlmeext, REASSOC_TO);
2431 }
2432
2433 static unsigned int receive_disconnect(struct adapter *padapter,
2434                                        unsigned char *MacAddr,
2435                                        unsigned short reason)
2436 {
2437         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2438         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2439         struct wlan_bssid_ex    *pnetwork = &(pmlmeinfo->network);
2440
2441         /* check A3 */
2442         if (memcmp(MacAddr, pnetwork->MacAddress, ETH_ALEN))
2443                 return _SUCCESS;
2444
2445         DBG_88E("%s\n", __func__);
2446
2447         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
2448                 if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {
2449                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
2450                         report_del_sta_event(padapter, MacAddr, reason);
2451                 } else if (pmlmeinfo->state & WIFI_FW_LINKING_STATE) {
2452                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
2453                         report_join_res(padapter, -2);
2454                 }
2455         }
2456         return _SUCCESS;
2457 }
2458
2459 static void process_80211d(struct adapter *padapter, struct wlan_bssid_ex *bssid)
2460 {
2461         struct registry_priv *pregistrypriv;
2462         struct mlme_ext_priv *pmlmeext;
2463         struct rt_channel_info *chplan_new;
2464         u8 channel;
2465         u8 i;
2466
2467         pregistrypriv = &padapter->registrypriv;
2468         pmlmeext = &padapter->mlmeextpriv;
2469
2470         /*  Adjust channel plan by AP Country IE */
2471         if (pregistrypriv->enable80211d &&
2472             (!pmlmeext->update_channel_plan_by_ap_done)) {
2473                 u8 *ie, *p;
2474                 u32 len;
2475                 struct rt_channel_plan chplan_ap;
2476                 struct rt_channel_info chplan_sta[MAX_CHANNEL_NUM];
2477                 u8 country[4];
2478                 u8 fcn; /*  first channel number */
2479                 u8 noc; /*  number of channel */
2480                 u8 j, k;
2481
2482                 ie = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _COUNTRY_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
2483                 if (!ie)
2484                         return;
2485                 if (len < 6)
2486                         return;
2487                 ie += 2;
2488                 p = ie;
2489                 ie += len;
2490
2491                 memset(country, 0, 4);
2492                 memcpy(country, p, 3);
2493                 p += 3;
2494                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
2495                          ("%s: 802.11d country =%s\n", __func__, country));
2496
2497                 i = 0;
2498                 while ((ie - p) >= 3) {
2499                         fcn = *(p++);
2500                         noc = *(p++);
2501                         p++;
2502
2503                         for (j = 0; j < noc; j++) {
2504                                 if (fcn <= 14)
2505                                         channel = fcn + j; /*  2.4 GHz */
2506                                 else
2507                                         channel = fcn + j*4; /*  5 GHz */
2508
2509                                 chplan_ap.Channel[i++] = channel;
2510                         }
2511                 }
2512                 chplan_ap.Len = i;
2513
2514                 memcpy(chplan_sta, pmlmeext->channel_set, sizeof(chplan_sta));
2515
2516                 memset(pmlmeext->channel_set, 0, sizeof(pmlmeext->channel_set));
2517                 chplan_new = pmlmeext->channel_set;
2518
2519                 i = 0;
2520                 j = 0;
2521                 k = 0;
2522                 if (pregistrypriv->wireless_mode & WIRELESS_11G) {
2523                         do {
2524                                 if ((i == MAX_CHANNEL_NUM) ||
2525                                     (chplan_sta[i].ChannelNum == 0) ||
2526                                     (chplan_sta[i].ChannelNum > 14))
2527                                         break;
2528
2529                                 if ((j == chplan_ap.Len) || (chplan_ap.Channel[j] > 14))
2530                                         break;
2531
2532                                 if (chplan_sta[i].ChannelNum == chplan_ap.Channel[j]) {
2533                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
2534                                         chplan_new[k].ScanType = SCAN_ACTIVE;
2535                                         i++;
2536                                         j++;
2537                                         k++;
2538                                 } else if (chplan_sta[i].ChannelNum < chplan_ap.Channel[j]) {
2539                                         chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
2540                                         chplan_new[k].ScanType = SCAN_PASSIVE;
2541                                         i++;
2542                                         k++;
2543                                 } else if (chplan_sta[i].ChannelNum > chplan_ap.Channel[j]) {
2544                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
2545                                         chplan_new[k].ScanType = SCAN_ACTIVE;
2546                                         j++;
2547                                         k++;
2548                                 }
2549                         } while (1);
2550
2551                         /*  change AP not support channel to Passive scan */
2552                         while ((i < MAX_CHANNEL_NUM) &&
2553                                (chplan_sta[i].ChannelNum != 0) &&
2554                                (chplan_sta[i].ChannelNum <= 14)) {
2555                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
2556                                 chplan_new[k].ScanType = SCAN_PASSIVE;
2557                                 i++;
2558                                 k++;
2559                         }
2560
2561                         /*  add channel AP supported */
2562                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] <= 14)) {
2563                                 chplan_new[k].ChannelNum = chplan_ap.Channel[j];
2564                                 chplan_new[k].ScanType = SCAN_ACTIVE;
2565                                 j++;
2566                                 k++;
2567                         }
2568                 } else {
2569                         /*  keep original STA 2.4G channel plan */
2570                         while ((i < MAX_CHANNEL_NUM) &&
2571                                (chplan_sta[i].ChannelNum != 0) &&
2572                                (chplan_sta[i].ChannelNum <= 14)) {
2573                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
2574                                 chplan_new[k].ScanType = chplan_sta[i].ScanType;
2575                                 i++;
2576                                 k++;
2577                         }
2578
2579                         /*  skip AP 2.4G channel plan */
2580                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] <= 14))
2581                                 j++;
2582                 }
2583
2584                 /*  keep original STA 5G channel plan */
2585                 while ((i < MAX_CHANNEL_NUM) && (chplan_sta[i].ChannelNum != 0)) {
2586                         chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
2587                         chplan_new[k].ScanType = chplan_sta[i].ScanType;
2588                         i++;
2589                         k++;
2590                 }
2591
2592                 pmlmeext->update_channel_plan_by_ap_done = 1;
2593         }
2594
2595         /*  If channel is used by AP, set channel scan type to active */
2596         channel = bssid->Configuration.DSConfig;
2597         chplan_new = pmlmeext->channel_set;
2598         i = 0;
2599         while ((i < MAX_CHANNEL_NUM) && (chplan_new[i].ChannelNum != 0)) {
2600                 if (chplan_new[i].ChannelNum == channel) {
2601                         if (chplan_new[i].ScanType == SCAN_PASSIVE) {
2602                                 chplan_new[i].ScanType = SCAN_ACTIVE;
2603                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
2604                                          ("%s: change channel %d scan type from passive to active\n",
2605                                          __func__, channel));
2606                         }
2607                         break;
2608                 }
2609                 i++;
2610         }
2611 }
2612
2613 /****************************************************************************
2614
2615 Following are the callback functions for each subtype of the management frames
2616
2617 *****************************************************************************/
2618
2619 static unsigned int OnProbeReq(struct adapter *padapter,
2620                                struct recv_frame *precv_frame)
2621 {
2622         unsigned int    ielen;
2623         unsigned char   *p;
2624         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2625         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
2626         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2627         struct wlan_bssid_ex *cur = &(pmlmeinfo->network);
2628         u8 *pframe = precv_frame->rx_data;
2629         uint len = precv_frame->len;
2630
2631         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
2632                 return _SUCCESS;
2633
2634         if (!check_fwstate(pmlmepriv, _FW_LINKED) &&
2635             !check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_AP_STATE))
2636                 return _SUCCESS;
2637
2638         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _PROBEREQ_IE_OFFSET_, _SSID_IE_, (int *)&ielen,
2639                         len - WLAN_HDR_A3_LEN - _PROBEREQ_IE_OFFSET_);
2640
2641         /* check (wildcard) SSID */
2642         if (p != NULL) {
2643                 if ((ielen != 0 && memcmp((void *)(p+2), (void *)cur->Ssid.Ssid, cur->Ssid.SsidLength)) ||
2644                     (ielen == 0 && pmlmeinfo->hidden_ssid_mode))
2645                         return _SUCCESS;
2646
2647                 if (check_fwstate(pmlmepriv, _FW_LINKED) &&
2648                     pmlmepriv->cur_network.join_res)
2649                         issue_probersp(padapter, get_sa(pframe));
2650         }
2651         return _SUCCESS;
2652 }
2653
2654 static unsigned int OnProbeRsp(struct adapter *padapter,
2655                                struct recv_frame *precv_frame)
2656 {
2657         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2658
2659         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
2660                 report_survey_event(padapter, precv_frame);
2661                 return _SUCCESS;
2662         }
2663
2664         return _SUCCESS;
2665 }
2666
2667 static unsigned int OnBeacon(struct adapter *padapter,
2668                              struct recv_frame *precv_frame)
2669 {
2670         int cam_idx;
2671         struct sta_info *psta;
2672         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2673         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2674         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2675         struct sta_priv *pstapriv = &padapter->stapriv;
2676         u8 *pframe = precv_frame->rx_data;
2677         uint len = precv_frame->len;
2678         struct wlan_bssid_ex *pbss;
2679         int ret = _SUCCESS;
2680         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
2681
2682         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
2683                 report_survey_event(padapter, precv_frame);
2684                 return _SUCCESS;
2685         }
2686
2687         if (!memcmp(GetAddr3Ptr(pframe), pnetwork->MacAddress, ETH_ALEN)) {
2688                 if (pmlmeinfo->state & WIFI_FW_AUTH_NULL) {
2689                         /* we should update current network before auth, or some IE is wrong */
2690                         pbss = (struct wlan_bssid_ex *)rtw_malloc(sizeof(struct wlan_bssid_ex));
2691                         if (pbss) {
2692                                 if (collect_bss_info(padapter, precv_frame, pbss) == _SUCCESS) {
2693                                         update_network(&(pmlmepriv->cur_network.network), pbss, padapter, true);
2694                                         rtw_get_bcn_info(&(pmlmepriv->cur_network));
2695                                 }
2696                                 kfree(pbss);
2697                         }
2698
2699                         /* check the vendor of the assoc AP */
2700                         pmlmeinfo->assoc_AP_vendor = check_assoc_AP(pframe+sizeof(struct rtw_ieee80211_hdr_3addr), len-sizeof(struct rtw_ieee80211_hdr_3addr));
2701
2702                         /* update TSF Value */
2703                         update_TSF(pmlmeext, pframe, len);
2704
2705                         /* start auth */
2706                         start_clnt_auth(padapter);
2707
2708                         return _SUCCESS;
2709                 }
2710
2711                 if (((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) && (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)) {
2712                         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
2713                         if (psta != NULL) {
2714                                 ret = rtw_check_bcn_info(padapter, pframe, len);
2715                                 if (!ret) {
2716                                                 DBG_88E_LEVEL(_drv_info_, "ap has changed, disconnect now\n ");
2717                                                 receive_disconnect(padapter, pmlmeinfo->network.MacAddress , 65535);
2718                                                 return _SUCCESS;
2719                                 }
2720                                 /* update WMM, ERP in the beacon */
2721                                 /* todo: the timer is used instead of the number of the beacon received */
2722                                 if ((sta_rx_pkts(psta) & 0xf) == 0)
2723                                         update_beacon_info(padapter, pframe, len, psta);
2724                         }
2725                 } else if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
2726                         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
2727                         if (psta != NULL) {
2728                                 /* update WMM, ERP in the beacon */
2729                                 /* todo: the timer is used instead of the number of the beacon received */
2730                                 if ((sta_rx_pkts(psta) & 0xf) == 0)
2731                                         update_beacon_info(padapter, pframe, len, psta);
2732                         } else {
2733                                 /* allocate a new CAM entry for IBSS station */
2734                                 cam_idx = allocate_fw_sta_entry(padapter);
2735                                 if (cam_idx == NUM_STA)
2736                                         goto _END_ONBEACON_;
2737
2738                                 /* get supported rate */
2739                                 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) {
2740                                         pmlmeinfo->FW_sta_info[cam_idx].status = 0;
2741                                         goto _END_ONBEACON_;
2742                                 }
2743
2744                                 /* update TSF Value */
2745                                 update_TSF(pmlmeext, pframe, len);
2746
2747                                 /* report sta add event */
2748                                 report_add_sta_event(padapter, GetAddr2Ptr(pframe), cam_idx);
2749                         }
2750                 }
2751         }
2752
2753 _END_ONBEACON_:
2754
2755         return _SUCCESS;
2756 }
2757
2758 #ifdef CONFIG_88EU_AP_MODE
2759 static unsigned int OnAuth(struct adapter *padapter,
2760                            struct recv_frame *precv_frame)
2761 {
2762         unsigned int    auth_mode, ie_len;
2763         u16 seq;
2764         unsigned char   *sa, *p;
2765         u16 algorithm;
2766         int     status;
2767         static struct sta_info stat;
2768         struct  sta_info        *pstat = NULL;
2769         struct  sta_priv *pstapriv = &padapter->stapriv;
2770         struct security_priv *psecuritypriv = &padapter->securitypriv;
2771         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2772         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2773         u8 *pframe = precv_frame->rx_data;
2774         uint len = precv_frame->len;
2775
2776         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
2777                 return _FAIL;
2778
2779         DBG_88E("+OnAuth\n");
2780
2781         sa = GetAddr2Ptr(pframe);
2782
2783         auth_mode = psecuritypriv->dot11AuthAlgrthm;
2784         seq = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + 2));
2785         algorithm = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN));
2786
2787         DBG_88E("auth alg=%x, seq=%X\n", algorithm, seq);
2788
2789         if (auth_mode == 2 && psecuritypriv->dot11PrivacyAlgrthm != _WEP40_ &&
2790             psecuritypriv->dot11PrivacyAlgrthm != _WEP104_)
2791                 auth_mode = 0;
2792
2793         if ((algorithm > 0 && auth_mode == 0) ||        /*  rx a shared-key auth but shared not enabled */
2794             (algorithm == 0 && auth_mode == 1)) {       /*  rx a open-system auth but shared-key is enabled */
2795                 DBG_88E("auth rejected due to bad alg [alg=%d, auth_mib=%d] %02X%02X%02X%02X%02X%02X\n",
2796                         algorithm, auth_mode, sa[0], sa[1], sa[2], sa[3], sa[4], sa[5]);
2797
2798                 status = _STATS_NO_SUPP_ALG_;
2799
2800                 goto auth_fail;
2801         }
2802
2803         if (!rtw_access_ctrl(padapter, sa)) {
2804                 status = _STATS_UNABLE_HANDLE_STA_;
2805                 goto auth_fail;
2806         }
2807
2808         pstat = rtw_get_stainfo(pstapriv, sa);
2809         if (pstat == NULL) {
2810                 /*  allocate a new one */
2811                 DBG_88E("going to alloc stainfo for sa=%pM\n", sa);
2812                 pstat = rtw_alloc_stainfo(pstapriv, sa);
2813                 if (pstat == NULL) {
2814                         DBG_88E(" Exceed the upper limit of supported clients...\n");
2815                         status = _STATS_UNABLE_HANDLE_STA_;
2816                         goto auth_fail;
2817                 }
2818
2819                 pstat->state = WIFI_FW_AUTH_NULL;
2820                 pstat->auth_seq = 0;
2821         } else {
2822                 spin_lock_bh(&pstapriv->asoc_list_lock);
2823                 if (!list_empty(&pstat->asoc_list)) {
2824                         list_del_init(&pstat->asoc_list);
2825                         pstapriv->asoc_list_cnt--;
2826                 }
2827                 spin_unlock_bh(&pstapriv->asoc_list_lock);
2828
2829                 if (seq == 1) {
2830                         /* TODO: STA re_auth and auth timeout */
2831                 }
2832         }
2833
2834         spin_lock_bh(&pstapriv->auth_list_lock);
2835         if (list_empty(&pstat->auth_list)) {
2836                 list_add_tail(&pstat->auth_list, &pstapriv->auth_list);
2837                 pstapriv->auth_list_cnt++;
2838         }
2839         spin_unlock_bh(&pstapriv->auth_list_lock);
2840
2841         if (pstat->auth_seq == 0)
2842                 pstat->expire_to = pstapriv->auth_to;
2843
2844         if ((pstat->auth_seq + 1) != seq) {
2845                 DBG_88E("(1)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
2846                         seq, pstat->auth_seq+1);
2847                 status = _STATS_OUT_OF_AUTH_SEQ_;
2848                 goto auth_fail;
2849         }
2850
2851         if (algorithm == 0 && (auth_mode == 0 || auth_mode == 2)) {
2852                 if (seq == 1) {
2853                         pstat->state &= ~WIFI_FW_AUTH_NULL;
2854                         pstat->state |= WIFI_FW_AUTH_SUCCESS;
2855                         pstat->expire_to = pstapriv->assoc_to;
2856                         pstat->authalg = algorithm;
2857                 } else {
2858                         DBG_88E("(2)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
2859                                 seq, pstat->auth_seq+1);
2860                         status = _STATS_OUT_OF_AUTH_SEQ_;
2861                         goto auth_fail;
2862                 }
2863         } else { /*  shared system or auto authentication */
2864                 if (seq == 1) {
2865                         /* prepare for the challenging txt... */
2866
2867                         pstat->state &= ~WIFI_FW_AUTH_NULL;
2868                         pstat->state |= WIFI_FW_AUTH_STATE;
2869                         pstat->authalg = algorithm;
2870                         pstat->auth_seq = 2;
2871                 } else if (seq == 3) {
2872                         /* checking for challenging txt... */
2873                         DBG_88E("checking for challenging txt...\n");
2874
2875                         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + 4 + _AUTH_IE_OFFSET_ , _CHLGETXT_IE_, (int *)&ie_len,
2876                                         len - WLAN_HDR_A3_LEN - _AUTH_IE_OFFSET_ - 4);
2877
2878                         if ((p == NULL) || (ie_len <= 0)) {
2879                                 DBG_88E("auth rejected because challenge failure!(1)\n");
2880                                 status = _STATS_CHALLENGE_FAIL_;
2881                                 goto auth_fail;
2882                         }
2883
2884                         if (!memcmp((void *)(p + 2), pstat->chg_txt, 128)) {
2885                                 pstat->state &= (~WIFI_FW_AUTH_STATE);
2886                                 pstat->state |= WIFI_FW_AUTH_SUCCESS;
2887                                 /*  challenging txt is correct... */
2888                                 pstat->expire_to =  pstapriv->assoc_to;
2889                         } else {
2890                                 DBG_88E("auth rejected because challenge failure!\n");
2891                                 status = _STATS_CHALLENGE_FAIL_;
2892                                 goto auth_fail;
2893                         }
2894                 } else {
2895                         DBG_88E("(3)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
2896                                 seq, pstat->auth_seq+1);
2897                         status = _STATS_OUT_OF_AUTH_SEQ_;
2898                         goto auth_fail;
2899                 }
2900         }
2901
2902         /*  Now, we are going to issue_auth... */
2903         pstat->auth_seq = seq + 1;
2904
2905         issue_auth(padapter, pstat, (unsigned short)(_STATS_SUCCESSFUL_));
2906
2907         if (pstat->state & WIFI_FW_AUTH_SUCCESS)
2908                 pstat->auth_seq = 0;
2909
2910         return _SUCCESS;
2911
2912 auth_fail:
2913
2914         if (pstat)
2915                 rtw_free_stainfo(padapter, pstat);
2916
2917         pstat = &stat;
2918         memset((char *)pstat, '\0', sizeof(stat));
2919         pstat->auth_seq = 2;
2920         memcpy(pstat->hwaddr, sa, 6);
2921
2922         issue_auth(padapter, pstat, (unsigned short)status);
2923
2924         return _FAIL;
2925 }
2926 #endif /* CONFIG_88EU_AP_MODE */
2927
2928 static unsigned int OnAuthClient(struct adapter *padapter,
2929                                  struct recv_frame *precv_frame)
2930 {
2931         unsigned int    seq, len, status, offset;
2932         unsigned char   *p;
2933         unsigned int    go2asoc = 0;
2934         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2935         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
2936         u8 *pframe = precv_frame->rx_data;
2937         uint pkt_len = precv_frame->len;
2938
2939         DBG_88E("%s\n", __func__);
2940
2941         /* check A1 matches or not */
2942         if (memcmp(myid(&(padapter->eeprompriv)), get_da(pframe), ETH_ALEN))
2943                 return _SUCCESS;
2944
2945         if (!(pmlmeinfo->state & WIFI_FW_AUTH_STATE))
2946                 return _SUCCESS;
2947
2948         offset = (GetPrivacy(pframe)) ? 4 : 0;
2949
2950         seq     = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + offset + 2));
2951         status  = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + offset + 4));
2952
2953         if (status != 0) {
2954                 DBG_88E("clnt auth fail, status: %d\n", status);
2955                 if (status == 13) { /*  pmlmeinfo->auth_algo == dot11AuthAlgrthm_Auto) */
2956                         if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared)
2957                                 pmlmeinfo->auth_algo = dot11AuthAlgrthm_Open;
2958                         else
2959                                 pmlmeinfo->auth_algo = dot11AuthAlgrthm_Shared;
2960                 }
2961
2962                 set_link_timer(pmlmeext, 1);
2963                 goto authclnt_fail;
2964         }
2965
2966         if (seq == 2) {
2967                 if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared) {
2968                         /*  legendary shared system */
2969                         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _AUTH_IE_OFFSET_, _CHLGETXT_IE_, (int *)&len,
2970                                 pkt_len - WLAN_HDR_A3_LEN - _AUTH_IE_OFFSET_);
2971
2972                         if (p == NULL)
2973                                 goto authclnt_fail;
2974
2975                         memcpy((void *)(pmlmeinfo->chg_txt), (void *)(p + 2), len);
2976                         pmlmeinfo->auth_seq = 3;
2977                         issue_auth(padapter, NULL, 0);
2978                         set_link_timer(pmlmeext, REAUTH_TO);
2979
2980                         return _SUCCESS;
2981                 } else {
2982                         /*  open system */
2983                         go2asoc = 1;
2984                 }
2985         } else if (seq == 4) {
2986                 if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared)
2987                         go2asoc = 1;
2988                 else
2989                         goto authclnt_fail;
2990         } else {
2991                 /*  this is also illegal */
2992                 goto authclnt_fail;
2993         }
2994
2995         if (go2asoc) {
2996                 DBG_88E_LEVEL(_drv_info_, "auth success, start assoc\n");
2997                 start_clnt_assoc(padapter);
2998                 return _SUCCESS;
2999         }
3000 authclnt_fail:
3001         return _FAIL;
3002 }
3003
3004 static unsigned int OnAssocReq(struct adapter *padapter,
3005                                struct recv_frame *precv_frame)
3006 {
3007 #ifdef CONFIG_88EU_AP_MODE
3008         u16 capab_info;
3009         struct rtw_ieee802_11_elems elems;
3010         struct sta_info *pstat;
3011         unsigned char           reassoc, *p, *pos, *wpa_ie;
3012         unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
3013         int             i, ie_len, wpa_ie_len, left;
3014         unsigned char           supportRate[16];
3015         int                                     supportRateNum;
3016         unsigned short          status = _STATS_SUCCESSFUL_;
3017         unsigned short          frame_type, ie_offset = 0;
3018         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3019         struct security_priv *psecuritypriv = &padapter->securitypriv;
3020         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3021         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3022         struct wlan_bssid_ex *cur = &(pmlmeinfo->network);
3023         struct sta_priv *pstapriv = &padapter->stapriv;
3024         u8 *pframe = precv_frame->rx_data;
3025         uint pkt_len = precv_frame->len;
3026
3027         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
3028                 return _FAIL;
3029
3030         frame_type = GetFrameSubType(pframe);
3031         if (frame_type == WIFI_ASSOCREQ) {
3032                 reassoc = 0;
3033                 ie_offset = _ASOCREQ_IE_OFFSET_;
3034         } else { /*  WIFI_REASSOCREQ */
3035                 reassoc = 1;
3036                 ie_offset = _REASOCREQ_IE_OFFSET_;
3037         }
3038
3039
3040         if (pkt_len < IEEE80211_3ADDR_LEN + ie_offset) {
3041                 DBG_88E("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
3042                        "\n", reassoc, (unsigned long)pkt_len);
3043                 return _FAIL;
3044         }
3045
3046         pstat = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
3047         if (pstat == NULL) {
3048                 status = _RSON_CLS2_;
3049                 goto asoc_class2_error;
3050         }
3051
3052         capab_info = get_unaligned_le16(pframe + WLAN_HDR_A3_LEN);
3053
3054         left = pkt_len - (IEEE80211_3ADDR_LEN + ie_offset);
3055         pos = pframe + (IEEE80211_3ADDR_LEN + ie_offset);
3056
3057
3058         DBG_88E("%s\n", __func__);
3059
3060         /*  check if this stat has been successfully authenticated/assocated */
3061         if (!((pstat->state) & WIFI_FW_AUTH_SUCCESS)) {
3062                 if (!((pstat->state) & WIFI_FW_ASSOC_SUCCESS)) {
3063                         status = _RSON_CLS2_;
3064                         goto asoc_class2_error;
3065                 } else {
3066                         pstat->state &= (~WIFI_FW_ASSOC_SUCCESS);
3067                         pstat->state |= WIFI_FW_ASSOC_STATE;
3068                 }
3069         } else {
3070                 pstat->state &= (~WIFI_FW_AUTH_SUCCESS);
3071                 pstat->state |= WIFI_FW_ASSOC_STATE;
3072         }
3073         pstat->capability = capab_info;
3074         /* now parse all ieee802_11 ie to point to elems */
3075         if (rtw_ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
3076             !elems.ssid) {
3077                 DBG_88E("STA %pM sent invalid association request\n",
3078                         pstat->hwaddr);
3079                 status = _STATS_FAILURE_;
3080                 goto OnAssocReqFail;
3081         }
3082
3083
3084         /*  now we should check all the fields... */
3085         /*  checking SSID */
3086         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _SSID_IE_, &ie_len,
3087                 pkt_len - WLAN_HDR_A3_LEN - ie_offset);
3088         if (p == NULL)
3089                 status = _STATS_FAILURE_;
3090
3091         if (ie_len == 0) { /*  broadcast ssid, however it is not allowed in assocreq */
3092                 status = _STATS_FAILURE_;
3093         } else {
3094                 /*  check if ssid match */
3095                 if (memcmp((void *)(p+2), cur->Ssid.Ssid, cur->Ssid.SsidLength))
3096                         status = _STATS_FAILURE_;
3097
3098                 if (ie_len != cur->Ssid.SsidLength)
3099                         status = _STATS_FAILURE_;
3100         }
3101
3102         if (_STATS_SUCCESSFUL_ != status)
3103                 goto OnAssocReqFail;
3104
3105         /*  check if the supported rate is ok */
3106         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _SUPPORTEDRATES_IE_, &ie_len, pkt_len - WLAN_HDR_A3_LEN - ie_offset);
3107         if (p == NULL) {
3108                 DBG_88E("Rx a sta assoc-req which supported rate is empty!\n");
3109                 /*  use our own rate set as statoin used */
3110                 /* memcpy(supportRate, AP_BSSRATE, AP_BSSRATE_LEN); */
3111                 /* supportRateNum = AP_BSSRATE_LEN; */
3112
3113                 status = _STATS_FAILURE_;
3114                 goto OnAssocReqFail;
3115         } else {
3116                 memcpy(supportRate, p+2, ie_len);
3117                 supportRateNum = ie_len;
3118
3119                 p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _EXT_SUPPORTEDRATES_IE_ , &ie_len,
3120                                 pkt_len - WLAN_HDR_A3_LEN - ie_offset);
3121                 if (p !=  NULL) {
3122                         if (supportRateNum <= sizeof(supportRate)) {
3123                                 memcpy(supportRate+supportRateNum, p+2, ie_len);
3124                                 supportRateNum += ie_len;
3125                         }
3126                 }
3127         }
3128
3129         /* todo: mask supportRate between AP & STA -> move to update raid */
3130         /* get_matched_rate(pmlmeext, supportRate, &supportRateNum, 0); */
3131
3132         /* update station supportRate */
3133         pstat->bssratelen = supportRateNum;
3134         memcpy(pstat->bssrateset, supportRate, supportRateNum);
3135         UpdateBrateTblForSoftAP(pstat->bssrateset, pstat->bssratelen);
3136
3137         /* check RSN/WPA/WPS */
3138         pstat->dot8021xalg = 0;
3139         pstat->wpa_psk = 0;
3140         pstat->wpa_group_cipher = 0;
3141         pstat->wpa2_group_cipher = 0;
3142         pstat->wpa_pairwise_cipher = 0;
3143         pstat->wpa2_pairwise_cipher = 0;
3144         memset(pstat->wpa_ie, 0, sizeof(pstat->wpa_ie));
3145         if ((psecuritypriv->wpa_psk & BIT(1)) && elems.rsn_ie) {
3146                 int group_cipher = 0, pairwise_cipher = 0;
3147
3148                 wpa_ie = elems.rsn_ie;
3149                 wpa_ie_len = elems.rsn_ie_len;
3150
3151                 if (rtw_parse_wpa2_ie(wpa_ie-2, wpa_ie_len+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
3152                         pstat->dot8021xalg = 1;/* psk,  todo:802.1x */
3153                         pstat->wpa_psk |= BIT(1);
3154
3155                         pstat->wpa2_group_cipher = group_cipher&psecuritypriv->wpa2_group_cipher;
3156                         pstat->wpa2_pairwise_cipher = pairwise_cipher&psecuritypriv->wpa2_pairwise_cipher;
3157
3158                         if (!pstat->wpa2_group_cipher)
3159                                 status = WLAN_STATUS_INVALID_GROUP_CIPHER;
3160
3161                         if (!pstat->wpa2_pairwise_cipher)
3162                                 status = WLAN_STATUS_INVALID_PAIRWISE_CIPHER;
3163                 } else {
3164                         status = WLAN_STATUS_INVALID_IE;
3165                 }
3166         } else if ((psecuritypriv->wpa_psk & BIT(0)) && elems.wpa_ie) {
3167                 int group_cipher = 0, pairwise_cipher = 0;
3168
3169                 wpa_ie = elems.wpa_ie;
3170                 wpa_ie_len = elems.wpa_ie_len;
3171
3172                 if (rtw_parse_wpa_ie(wpa_ie-2, wpa_ie_len+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
3173                         pstat->dot8021xalg = 1;/* psk,  todo:802.1x */
3174                         pstat->wpa_psk |= BIT(0);
3175
3176                         pstat->wpa_group_cipher = group_cipher&psecuritypriv->wpa_group_cipher;
3177                         pstat->wpa_pairwise_cipher = pairwise_cipher&psecuritypriv->wpa_pairwise_cipher;
3178
3179                         if (!pstat->wpa_group_cipher)
3180                                 status = WLAN_STATUS_INVALID_GROUP_CIPHER;
3181
3182                         if (!pstat->wpa_pairwise_cipher)
3183                                 status = WLAN_STATUS_INVALID_PAIRWISE_CIPHER;
3184                 } else {
3185                         status = WLAN_STATUS_INVALID_IE;
3186                 }
3187         } else {
3188                 wpa_ie = NULL;
3189                 wpa_ie_len = 0;
3190         }
3191
3192         if (_STATS_SUCCESSFUL_ != status)
3193                 goto OnAssocReqFail;
3194
3195         pstat->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
3196         if (wpa_ie == NULL) {
3197                 if (elems.wps_ie) {
3198                         DBG_88E("STA included WPS IE in "
3199                                    "(Re)Association Request - assume WPS is "
3200                                    "used\n");
3201                         pstat->flags |= WLAN_STA_WPS;
3202                         /* wpabuf_free(sta->wps_ie); */
3203                         /* sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4, */
3204                         /*                              elems.wps_ie_len - 4); */
3205                 } else {
3206                         DBG_88E("STA did not include WPA/RSN IE "
3207                                    "in (Re)Association Request - possible WPS "
3208                                    "use\n");
3209                         pstat->flags |= WLAN_STA_MAYBE_WPS;
3210                 }
3211
3212
3213                 /*  AP support WPA/RSN, and sta is going to do WPS, but AP is not ready */
3214                 /*  that the selected registrar of AP is _FLASE */
3215                 if ((psecuritypriv->wpa_psk > 0) && (pstat->flags & (WLAN_STA_WPS|WLAN_STA_MAYBE_WPS))) {
3216                         if (pmlmepriv->wps_beacon_ie) {
3217                                 u8 selected_registrar = 0;
3218
3219                                 rtw_get_wps_attr_content(pmlmepriv->wps_beacon_ie, pmlmepriv->wps_beacon_ie_len, WPS_ATTR_SELECTED_REGISTRAR , &selected_registrar, NULL);
3220
3221                                 if (!selected_registrar) {
3222                                         DBG_88E("selected_registrar is false , or AP is not ready to do WPS\n");
3223
3224                                         status = _STATS_UNABLE_HANDLE_STA_;
3225
3226                                         goto OnAssocReqFail;
3227                                 }
3228                         }
3229                 }
3230         } else {
3231                 int copy_len;
3232
3233                 if (psecuritypriv->wpa_psk == 0) {
3234                         DBG_88E("STA %pM: WPA/RSN IE in association "
3235                         "request, but AP don't support WPA/RSN\n", pstat->hwaddr);
3236
3237                         status = WLAN_STATUS_INVALID_IE;
3238
3239                         goto OnAssocReqFail;
3240                 }
3241
3242                 if (elems.wps_ie) {
3243                         DBG_88E("STA included WPS IE in "
3244                                    "(Re)Association Request - WPS is "
3245                                    "used\n");
3246                         pstat->flags |= WLAN_STA_WPS;
3247                         copy_len = 0;
3248                 } else {
3249                         copy_len = min_t(int, wpa_ie_len + 2, sizeof(pstat->wpa_ie));
3250                 }
3251                 if (copy_len > 0)
3252                         memcpy(pstat->wpa_ie, wpa_ie-2, copy_len);
3253         }
3254         /*  check if there is WMM IE & support WWM-PS */
3255         pstat->flags &= ~WLAN_STA_WME;
3256         pstat->qos_option = 0;
3257         pstat->qos_info = 0;
3258         pstat->has_legacy_ac = true;
3259         pstat->uapsd_vo = 0;
3260         pstat->uapsd_vi = 0;
3261         pstat->uapsd_be = 0;
3262         pstat->uapsd_bk = 0;
3263         if (pmlmepriv->qospriv.qos_option) {
3264                 p = pframe + WLAN_HDR_A3_LEN + ie_offset; ie_len = 0;
3265                 for (;;) {
3266                         p = rtw_get_ie(p, _VENDOR_SPECIFIC_IE_, &ie_len, pkt_len - WLAN_HDR_A3_LEN - ie_offset);
3267                         if (p != NULL) {
3268                                 if (!memcmp(p+2, WMM_IE, 6)) {
3269                                         pstat->flags |= WLAN_STA_WME;
3270
3271                                         pstat->qos_option = 1;
3272                                         pstat->qos_info = *(p+8);
3273
3274                                         pstat->max_sp_len = (pstat->qos_info>>5)&0x3;
3275
3276                                         if ((pstat->qos_info&0xf) != 0xf)
3277                                                 pstat->has_legacy_ac = true;
3278                                         else
3279                                                 pstat->has_legacy_ac = false;
3280
3281                                         if (pstat->qos_info&0xf) {
3282                                                 if (pstat->qos_info&BIT(0))
3283                                                         pstat->uapsd_vo = BIT(0)|BIT(1);
3284                                                 else
3285                                                         pstat->uapsd_vo = 0;
3286
3287                                                 if (pstat->qos_info&BIT(1))
3288                                                         pstat->uapsd_vi = BIT(0)|BIT(1);
3289                                                 else
3290                                                         pstat->uapsd_vi = 0;
3291
3292                                                 if (pstat->qos_info&BIT(2))
3293                                                         pstat->uapsd_bk = BIT(0)|BIT(1);
3294                                                 else
3295                                                         pstat->uapsd_bk = 0;
3296
3297                                                 if (pstat->qos_info&BIT(3))
3298                                                         pstat->uapsd_be = BIT(0)|BIT(1);
3299                                                 else
3300                                                         pstat->uapsd_be = 0;
3301                                         }
3302                                         break;
3303                                 }
3304                         } else {
3305                                 break;
3306                         }
3307                         p = p + ie_len + 2;
3308                 }
3309         }
3310
3311         /* save HT capabilities in the sta object */
3312         memset(&pstat->htpriv.ht_cap, 0, sizeof(struct rtw_ieee80211_ht_cap));
3313         if (elems.ht_capabilities && elems.ht_capabilities_len >= sizeof(struct rtw_ieee80211_ht_cap)) {
3314                 pstat->flags |= WLAN_STA_HT;
3315
3316                 pstat->flags |= WLAN_STA_WME;
3317
3318                 memcpy(&pstat->htpriv.ht_cap, elems.ht_capabilities, sizeof(struct rtw_ieee80211_ht_cap));
3319         } else {
3320                 pstat->flags &= ~WLAN_STA_HT;
3321         }
3322         if ((!pmlmepriv->htpriv.ht_option) && (pstat->flags&WLAN_STA_HT)) {
3323                 status = _STATS_FAILURE_;
3324                 goto OnAssocReqFail;
3325         }
3326
3327         if ((pstat->flags & WLAN_STA_HT) &&
3328             ((pstat->wpa2_pairwise_cipher&WPA_CIPHER_TKIP) ||
3329             (pstat->wpa_pairwise_cipher&WPA_CIPHER_TKIP))) {
3330                 DBG_88E("HT: %pM tried to "
3331                         "use TKIP with HT association\n", pstat->hwaddr);
3332
3333                 /* status = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY; */
3334                 /* goto OnAssocReqFail; */
3335         }
3336
3337         pstat->flags |= WLAN_STA_NONERP;
3338         for (i = 0; i < pstat->bssratelen; i++) {
3339                 if ((pstat->bssrateset[i] & 0x7f) > 22) {
3340                         pstat->flags &= ~WLAN_STA_NONERP;
3341                         break;
3342                 }
3343         }
3344
3345         if (pstat->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3346                 pstat->flags |= WLAN_STA_SHORT_PREAMBLE;
3347         else
3348                 pstat->flags &= ~WLAN_STA_SHORT_PREAMBLE;
3349
3350
3351
3352         if (status != _STATS_SUCCESSFUL_)
3353                 goto OnAssocReqFail;
3354
3355         /* TODO: identify_proprietary_vendor_ie(); */
3356         /*  Realtek proprietary IE */
3357         /*  identify if this is Broadcom sta */
3358         /*  identify if this is ralink sta */
3359         /*  Customer proprietary IE */
3360
3361         /* get a unique AID */
3362         if (pstat->aid > 0) {
3363                 DBG_88E("  old AID %d\n", pstat->aid);
3364         } else {
3365                 for (pstat->aid = 1; pstat->aid <= NUM_STA; pstat->aid++)
3366                         if (pstapriv->sta_aid[pstat->aid - 1] == NULL)
3367                                 break;
3368
3369                 /* if (pstat->aid > NUM_STA) { */
3370                 if (pstat->aid > pstapriv->max_num_sta) {
3371                         pstat->aid = 0;
3372
3373                         DBG_88E("  no room for more AIDs\n");
3374
3375                         status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
3376
3377                         goto OnAssocReqFail;
3378                 } else {
3379                         pstapriv->sta_aid[pstat->aid - 1] = pstat;
3380                         DBG_88E("allocate new AID=(%d)\n", pstat->aid);
3381                 }
3382         }
3383
3384         pstat->state &= (~WIFI_FW_ASSOC_STATE);
3385         pstat->state |= WIFI_FW_ASSOC_SUCCESS;
3386
3387         spin_lock_bh(&pstapriv->auth_list_lock);
3388         if (!list_empty(&pstat->auth_list)) {
3389                 list_del_init(&pstat->auth_list);
3390                 pstapriv->auth_list_cnt--;
3391         }
3392         spin_unlock_bh(&pstapriv->auth_list_lock);
3393
3394         spin_lock_bh(&pstapriv->asoc_list_lock);
3395         if (list_empty(&pstat->asoc_list)) {
3396                 pstat->expire_to = pstapriv->expire_to;
3397                 list_add_tail(&pstat->asoc_list, &pstapriv->asoc_list);
3398                 pstapriv->asoc_list_cnt++;
3399         }
3400         spin_unlock_bh(&pstapriv->asoc_list_lock);
3401
3402         /*  now the station is qualified to join our BSS... */
3403         if ((pstat->state & WIFI_FW_ASSOC_SUCCESS) && (_STATS_SUCCESSFUL_ == status)) {
3404                 /* 1 bss_cap_update & sta_info_update */
3405                 bss_cap_update_on_sta_join(padapter, pstat);
3406                 sta_info_update(padapter, pstat);
3407
3408                 /* issue assoc rsp before notify station join event. */
3409                 if (frame_type == WIFI_ASSOCREQ)
3410                         issue_asocrsp(padapter, status, pstat, WIFI_ASSOCRSP);
3411                 else
3412                         issue_asocrsp(padapter, status, pstat, WIFI_REASSOCRSP);
3413
3414                 /* 2 - report to upper layer */
3415                 DBG_88E("indicate_sta_join_event to upper layer - hostapd\n");
3416                 rtw_indicate_sta_assoc_event(padapter, pstat);
3417
3418                 /* 3-(1) report sta add event */
3419                 report_add_sta_event(padapter, pstat->hwaddr, pstat->aid);
3420         }
3421
3422         return _SUCCESS;
3423
3424 asoc_class2_error:
3425
3426         issue_deauth(padapter, (void *)GetAddr2Ptr(pframe), status);
3427
3428         return _FAIL;
3429
3430 OnAssocReqFail:
3431
3432         pstat->aid = 0;
3433         if (frame_type == WIFI_ASSOCREQ)
3434                 issue_asocrsp(padapter, status, pstat, WIFI_ASSOCRSP);
3435         else
3436                 issue_asocrsp(padapter, status, pstat, WIFI_REASSOCRSP);
3437
3438 #endif /* CONFIG_88EU_AP_MODE */
3439
3440         return _FAIL;
3441 }
3442
3443 static unsigned int OnAssocRsp(struct adapter *padapter,
3444                                struct recv_frame *precv_frame)
3445 {
3446         uint i;
3447         int res;
3448         unsigned short  status;
3449         struct ndis_802_11_var_ie *pIE;
3450         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3451         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3452         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3453         /* struct wlan_bssid_ex *cur_network = &(pmlmeinfo->network); */
3454         u8 *pframe = precv_frame->rx_data;
3455         uint pkt_len = precv_frame->len;
3456
3457         DBG_88E("%s\n", __func__);
3458
3459         /* check A1 matches or not */
3460         if (memcmp(myid(&(padapter->eeprompriv)), get_da(pframe), ETH_ALEN))
3461                 return _SUCCESS;
3462
3463         if (!(pmlmeinfo->state & (WIFI_FW_AUTH_SUCCESS | WIFI_FW_ASSOC_STATE)))
3464                 return _SUCCESS;
3465
3466         if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)
3467                 return _SUCCESS;
3468
3469         del_timer_sync(&pmlmeext->link_timer);
3470
3471         /* status */
3472         status = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 2));
3473         if (status > 0) {
3474                 DBG_88E("assoc reject, status code: %d\n", status);
3475                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
3476                 res = -4;
3477                 goto report_assoc_result;
3478         }
3479
3480         /* get capabilities */
3481         pmlmeinfo->capability = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
3482
3483         /* set slot time */
3484         pmlmeinfo->slotTime = (pmlmeinfo->capability & BIT(10)) ? 9 : 20;
3485
3486         /* AID */
3487         pmlmeinfo->aid = (int)(le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 4))&0x3fff);
3488         res = pmlmeinfo->aid;
3489
3490         /* following are moved to join event callback function */
3491         /* to handle HT, WMM, rate adaptive, update MAC reg */
3492         /* for not to handle the synchronous IO in the tasklet */
3493         for (i = (6 + WLAN_HDR_A3_LEN); i < pkt_len;) {
3494                 pIE = (struct ndis_802_11_var_ie *)(pframe + i);
3495
3496                 switch (pIE->ElementID) {
3497                 case _VENDOR_SPECIFIC_IE_:
3498                         if (!memcmp(pIE->data, WMM_PARA_OUI, 6)) /* WMM */
3499                                 WMM_param_handler(padapter, pIE);
3500                         break;
3501                 case _HT_CAPABILITY_IE_:        /* HT caps */
3502                         HT_caps_handler(padapter, pIE);
3503                         break;
3504                 case _HT_EXTRA_INFO_IE_:        /* HT info */
3505                         HT_info_handler(padapter, pIE);
3506                         break;
3507                 case _ERPINFO_IE_:
3508                         ERP_IE_handler(padapter, pIE);
3509                 default:
3510                         break;
3511                 }
3512
3513                 i += (pIE->Length + 2);
3514         }
3515
3516         pmlmeinfo->state &= (~WIFI_FW_ASSOC_STATE);
3517         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
3518
3519         UpdateBrateTbl(padapter, pmlmeinfo->network.SupportedRates);
3520
3521 report_assoc_result:
3522         if (res > 0) {
3523                 rtw_buf_update(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len, pframe, pkt_len);
3524         } else {
3525                 rtw_buf_free(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len);
3526         }
3527
3528         report_join_res(padapter, res);
3529
3530         return _SUCCESS;
3531 }
3532
3533 static unsigned int OnDeAuth(struct adapter *padapter,
3534                              struct recv_frame *precv_frame)
3535 {
3536         unsigned short  reason;
3537         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3538         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3539         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3540         u8 *pframe = precv_frame->rx_data;
3541         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
3542
3543         /* check A3 */
3544         if (memcmp(GetAddr3Ptr(pframe), pnetwork->MacAddress, ETH_ALEN))
3545                 return _SUCCESS;
3546
3547         reason = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
3548
3549         DBG_88E("%s Reason code(%d)\n", __func__, reason);
3550
3551 #ifdef CONFIG_88EU_AP_MODE
3552         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
3553                 struct sta_info *psta;
3554                 struct sta_priv *pstapriv = &padapter->stapriv;
3555
3556                 DBG_88E_LEVEL(_drv_always_, "ap recv deauth reason code(%d) sta:%pM\n",
3557                               reason, GetAddr2Ptr(pframe));
3558
3559                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
3560                 if (psta) {
3561                         u8 updated = 0;
3562
3563                         spin_lock_bh(&pstapriv->asoc_list_lock);
3564                         if (!list_empty(&psta->asoc_list)) {
3565                                 list_del_init(&psta->asoc_list);
3566                                 pstapriv->asoc_list_cnt--;
3567                                 updated = ap_free_sta(padapter, psta, false, reason);
3568                         }
3569                         spin_unlock_bh(&pstapriv->asoc_list_lock);
3570
3571                         associated_clients_update(padapter, updated);
3572                 }
3573
3574
3575                 return _SUCCESS;
3576         } else
3577 #endif
3578         {
3579                 DBG_88E_LEVEL(_drv_always_, "sta recv deauth reason code(%d) sta:%pM\n",
3580                               reason, GetAddr3Ptr(pframe));
3581
3582                 receive_disconnect(padapter, GetAddr3Ptr(pframe) , reason);
3583         }
3584         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
3585         return _SUCCESS;
3586 }
3587
3588 static unsigned int OnDisassoc(struct adapter *padapter,
3589                                struct recv_frame *precv_frame)
3590 {
3591         u16 reason;
3592         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3593         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3594         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3595         u8 *pframe = precv_frame->rx_data;
3596         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
3597
3598         /* check A3 */
3599         if (memcmp(GetAddr3Ptr(pframe), pnetwork->MacAddress, ETH_ALEN))
3600                 return _SUCCESS;
3601
3602         reason = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
3603
3604         DBG_88E("%s Reason code(%d)\n", __func__, reason);
3605
3606 #ifdef CONFIG_88EU_AP_MODE
3607         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
3608                 struct sta_info *psta;
3609                 struct sta_priv *pstapriv = &padapter->stapriv;
3610
3611                 DBG_88E_LEVEL(_drv_always_, "ap recv disassoc reason code(%d) sta:%pM\n",
3612                               reason, GetAddr2Ptr(pframe));
3613
3614                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
3615                 if (psta) {
3616                         u8 updated = 0;
3617
3618                         spin_lock_bh(&pstapriv->asoc_list_lock);
3619                         if (!list_empty(&psta->asoc_list)) {
3620                                 list_del_init(&psta->asoc_list);
3621                                 pstapriv->asoc_list_cnt--;
3622                                 updated = ap_free_sta(padapter, psta, false, reason);
3623                         }
3624                         spin_unlock_bh(&pstapriv->asoc_list_lock);
3625
3626                         associated_clients_update(padapter, updated);
3627                 }
3628
3629                 return _SUCCESS;
3630         } else
3631 #endif
3632         {
3633                 DBG_88E_LEVEL(_drv_always_, "ap recv disassoc reason code(%d) sta:%pM\n",
3634                               reason, GetAddr3Ptr(pframe));
3635
3636                 receive_disconnect(padapter, GetAddr3Ptr(pframe), reason);
3637         }
3638         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
3639         return _SUCCESS;
3640 }
3641
3642 static unsigned int OnAtim(struct adapter *padapter,
3643                            struct recv_frame *precv_frame)
3644 {
3645         DBG_88E("%s\n", __func__);
3646         return _SUCCESS;
3647 }
3648
3649 static unsigned int on_action_spct(struct adapter *padapter,
3650                                    struct recv_frame *precv_frame)
3651 {
3652         struct sta_info *psta = NULL;
3653         struct sta_priv *pstapriv = &padapter->stapriv;
3654         u8 *pframe = precv_frame->rx_data;
3655         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
3656         u8 category;
3657         u8 action;
3658
3659         DBG_88E(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(padapter->pnetdev));
3660
3661         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
3662
3663         if (!psta)
3664                 goto exit;
3665
3666         category = frame_body[0];
3667         if (category != RTW_WLAN_CATEGORY_SPECTRUM_MGMT)
3668                 goto exit;
3669
3670         action = frame_body[1];
3671         switch (action) {
3672         case RTW_WLAN_ACTION_SPCT_MSR_REQ:
3673         case RTW_WLAN_ACTION_SPCT_MSR_RPRT:
3674         case RTW_WLAN_ACTION_SPCT_TPC_REQ:
3675         case RTW_WLAN_ACTION_SPCT_TPC_RPRT:
3676                 break;
3677         case RTW_WLAN_ACTION_SPCT_CHL_SWITCH:
3678                 break;
3679         default:
3680                 break;
3681         }
3682
3683 exit:
3684         return _FAIL;
3685 }
3686
3687 static unsigned int OnAction_qos(struct adapter *padapter,
3688                                  struct recv_frame *precv_frame)
3689 {
3690         return _SUCCESS;
3691 }
3692
3693 static unsigned int OnAction_dls(struct adapter *padapter,
3694                                  struct recv_frame *precv_frame)
3695 {
3696         return _SUCCESS;
3697 }
3698
3699 static unsigned int OnAction_back(struct adapter *padapter,
3700                                   struct recv_frame *precv_frame)
3701 {
3702         u8 *addr;
3703         struct sta_info *psta = NULL;
3704         struct recv_reorder_ctrl *preorder_ctrl;
3705         unsigned char           *frame_body;
3706         unsigned char           category, action;
3707         unsigned short  tid, status, reason_code = 0;
3708         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3709         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3710         u8 *pframe = precv_frame->rx_data;
3711         struct sta_priv *pstapriv = &padapter->stapriv;
3712         /* check RA matches or not */
3713         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe),
3714                    ETH_ALEN))/* for if1, sta/ap mode */
3715                 return _SUCCESS;
3716
3717         DBG_88E("%s\n", __func__);
3718
3719         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
3720                 if (!(pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS))
3721                         return _SUCCESS;
3722
3723         addr = GetAddr2Ptr(pframe);
3724         psta = rtw_get_stainfo(pstapriv, addr);
3725
3726         if (psta == NULL)
3727                 return _SUCCESS;
3728
3729         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
3730
3731         category = frame_body[0];
3732         if (category == RTW_WLAN_CATEGORY_BACK) { /*  representing Block Ack */
3733                 if (!pmlmeinfo->HT_enable)
3734                         return _SUCCESS;
3735                 action = frame_body[1];
3736                 DBG_88E("%s, action=%d\n", __func__, action);
3737                 switch (action) {
3738                 case RTW_WLAN_ACTION_ADDBA_REQ: /* ADDBA request */
3739                         memcpy(&(pmlmeinfo->ADDBA_req), &(frame_body[2]), sizeof(struct ADDBA_request));
3740                         process_addba_req(padapter, (u8 *)&(pmlmeinfo->ADDBA_req), addr);
3741
3742                         /* 37 = reject ADDBA Req */
3743                         issue_action_BA(padapter, addr,
3744                                         RTW_WLAN_ACTION_ADDBA_RESP,
3745                                         pmlmeinfo->accept_addba_req ? 0 : 37);
3746                         break;
3747                 case RTW_WLAN_ACTION_ADDBA_RESP: /* ADDBA response */
3748                         status = get_unaligned_le16(&frame_body[3]);
3749                         tid = (frame_body[5] >> 2) & 0x7;
3750                         if (status == 0) {      /* successful */
3751                                 DBG_88E("agg_enable for TID=%d\n", tid);
3752                                 psta->htpriv.agg_enable_bitmap |= 1 << tid;
3753                                 psta->htpriv.candidate_tid_bitmap &= ~BIT(tid);
3754                         } else {
3755                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
3756                         }
3757                         break;
3758                 case RTW_WLAN_ACTION_DELBA: /* DELBA */
3759                         if ((frame_body[3] & BIT(3)) == 0) {
3760                                 psta->htpriv.agg_enable_bitmap &= ~(1 << ((frame_body[3] >> 4) & 0xf));
3761                                 psta->htpriv.candidate_tid_bitmap &= ~(1 << ((frame_body[3] >> 4) & 0xf));
3762                                 reason_code = get_unaligned_le16(&frame_body[4]);
3763                         } else if ((frame_body[3] & BIT(3)) == BIT(3)) {
3764                                 tid = (frame_body[3] >> 4) & 0x0F;
3765                                 preorder_ctrl =  &psta->recvreorder_ctrl[tid];
3766                                 preorder_ctrl->enable = false;
3767                                 preorder_ctrl->indicate_seq = 0xffff;
3768                         }
3769                         DBG_88E("%s(): DELBA: %x(%x)\n", __func__, pmlmeinfo->agg_enable_bitmap, reason_code);
3770                         /* todo: how to notify the host while receiving DELETE BA */
3771                         break;
3772                 default:
3773                         break;
3774                 }
3775         }
3776         return _SUCCESS;
3777 }
3778
3779 static s32 rtw_action_public_decache(struct recv_frame *recv_frame, s32 token)
3780 {
3781         struct adapter *adapter = recv_frame->adapter;
3782         struct mlme_ext_priv *mlmeext = &(adapter->mlmeextpriv);
3783         u8 *frame = recv_frame->rx_data;
3784         u16 seq_ctrl = ((recv_frame->attrib.seq_num&0xffff) << 4) |
3785                 (recv_frame->attrib.frag_num & 0xf);
3786
3787         if (GetRetry(frame)) {
3788                 if (token >= 0) {
3789                         if ((seq_ctrl == mlmeext->action_public_rxseq) && (token == mlmeext->action_public_dialog_token)) {
3790                                 DBG_88E(FUNC_ADPT_FMT" seq_ctrl = 0x%x, rxseq = 0x%x, token:%d\n",
3791                                         FUNC_ADPT_ARG(adapter), seq_ctrl, mlmeext->action_public_rxseq, token);
3792                                 return _FAIL;
3793                         }
3794                 } else {
3795                         if (seq_ctrl == mlmeext->action_public_rxseq) {
3796                                 DBG_88E(FUNC_ADPT_FMT" seq_ctrl = 0x%x, rxseq = 0x%x\n",
3797                                         FUNC_ADPT_ARG(adapter), seq_ctrl, mlmeext->action_public_rxseq);
3798                                 return _FAIL;
3799                         }
3800                 }
3801         }
3802
3803         mlmeext->action_public_rxseq = seq_ctrl;
3804
3805         if (token >= 0)
3806                 mlmeext->action_public_dialog_token = token;
3807
3808         return _SUCCESS;
3809 }
3810
3811 static unsigned int on_action_public_p2p(struct recv_frame *precv_frame)
3812 {
3813         u8 *pframe = precv_frame->rx_data;
3814         u8 *frame_body;
3815         u8 dialogToken = 0;
3816         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
3817
3818         dialogToken = frame_body[7];
3819
3820         if (rtw_action_public_decache(precv_frame, dialogToken) == _FAIL)
3821                 return _FAIL;
3822
3823         return _SUCCESS;
3824 }
3825
3826 static unsigned int on_action_public_vendor(struct recv_frame *precv_frame)
3827 {
3828         unsigned int ret = _FAIL;
3829         u8 *pframe = precv_frame->rx_data;
3830         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
3831
3832         if (!memcmp(frame_body + 2, P2P_OUI, 4))
3833                 ret = on_action_public_p2p(precv_frame);
3834
3835         return ret;
3836 }
3837
3838 static unsigned int on_action_public_default(struct recv_frame *precv_frame, u8 action)
3839 {
3840         unsigned int ret = _FAIL;
3841         u8 *pframe = precv_frame->rx_data;
3842         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
3843         u8 token;
3844
3845         token = frame_body[2];
3846
3847         if (rtw_action_public_decache(precv_frame, token) == _FAIL)
3848                 goto exit;
3849
3850         ret = _SUCCESS;
3851
3852 exit:
3853         return ret;
3854 }
3855
3856 static unsigned int on_action_public(struct adapter *padapter,
3857                                      struct recv_frame *precv_frame)
3858 {
3859         unsigned int ret = _FAIL;
3860         u8 *pframe = precv_frame->rx_data;
3861         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
3862         u8 category, action;
3863
3864         /* check RA matches or not */
3865         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe), ETH_ALEN))
3866                 goto exit;
3867
3868         category = frame_body[0];
3869         if (category != RTW_WLAN_CATEGORY_PUBLIC)
3870                 goto exit;
3871
3872         action = frame_body[1];
3873         switch (action) {
3874         case ACT_PUBLIC_VENDOR:
3875                 ret = on_action_public_vendor(precv_frame);
3876                 break;
3877         default:
3878                 ret = on_action_public_default(precv_frame, action);
3879                 break;
3880         }
3881
3882 exit:
3883         return ret;
3884 }
3885
3886 static unsigned int OnAction_ht(struct adapter *padapter,
3887                                 struct recv_frame *precv_frame)
3888 {
3889         return _SUCCESS;
3890 }
3891
3892 static unsigned int OnAction_wmm(struct adapter *padapter,
3893                                  struct recv_frame *precv_frame)
3894 {
3895         return _SUCCESS;
3896 }
3897
3898 static unsigned int OnAction_p2p(struct adapter *padapter,
3899                                  struct recv_frame *precv_frame)
3900 {
3901         return _SUCCESS;
3902 }
3903
3904 static unsigned int DoReserved(struct adapter *padapter,
3905                                struct recv_frame *precv_frame)
3906 {
3907         return _SUCCESS;
3908 }
3909
3910 static struct action_handler OnAction_tbl[] = {
3911         {RTW_WLAN_CATEGORY_SPECTRUM_MGMT,        "ACTION_SPECTRUM_MGMT", on_action_spct},
3912         {RTW_WLAN_CATEGORY_QOS, "ACTION_QOS", &OnAction_qos},
3913         {RTW_WLAN_CATEGORY_DLS, "ACTION_DLS", &OnAction_dls},
3914         {RTW_WLAN_CATEGORY_BACK, "ACTION_BACK", &OnAction_back},
3915         {RTW_WLAN_CATEGORY_PUBLIC, "ACTION_PUBLIC", on_action_public},
3916         {RTW_WLAN_CATEGORY_RADIO_MEASUREMENT, "ACTION_RADIO_MEASUREMENT", &DoReserved},
3917         {RTW_WLAN_CATEGORY_FT, "ACTION_FT",     &DoReserved},
3918         {RTW_WLAN_CATEGORY_HT,  "ACTION_HT",    &OnAction_ht},
3919         {RTW_WLAN_CATEGORY_SA_QUERY, "ACTION_SA_QUERY", &DoReserved},
3920         {RTW_WLAN_CATEGORY_WMM, "ACTION_WMM", &OnAction_wmm},
3921         {RTW_WLAN_CATEGORY_P2P, "ACTION_P2P", &OnAction_p2p},
3922 };
3923
3924 static unsigned int OnAction(struct adapter *padapter,
3925                              struct recv_frame *precv_frame)
3926 {
3927         int i;
3928         unsigned char   category;
3929         struct action_handler *ptable;
3930         unsigned char   *frame_body;
3931         u8 *pframe = precv_frame->rx_data;
3932
3933         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
3934
3935         category = frame_body[0];
3936
3937         for (i = 0; i < ARRAY_SIZE(OnAction_tbl); i++) {
3938                 ptable = &OnAction_tbl[i];
3939                 if (category == ptable->num)
3940                         ptable->func(padapter, precv_frame);
3941         }
3942         return _SUCCESS;
3943 }
3944
3945 /****************************************************************************
3946
3947 Following are the initialization functions for WiFi MLME
3948
3949 *****************************************************************************/
3950
3951 static struct mlme_handler mlme_sta_tbl[] = {
3952         {WIFI_ASSOCREQ,         "OnAssocReq",   &OnAssocReq},
3953         {WIFI_ASSOCRSP,         "OnAssocRsp",   &OnAssocRsp},
3954         {WIFI_REASSOCREQ,       "OnReAssocReq", &OnAssocReq},
3955         {WIFI_REASSOCRSP,       "OnReAssocRsp", &OnAssocRsp},
3956         {WIFI_PROBEREQ,         "OnProbeReq",   &OnProbeReq},
3957         {WIFI_PROBERSP,         "OnProbeRsp",           &OnProbeRsp},
3958
3959         /*----------------------------------------------------------
3960                                         below 2 are reserved
3961         -----------------------------------------------------------*/
3962         {0,                                     "DoReserved",           &DoReserved},
3963         {0,                                     "DoReserved",           &DoReserved},
3964         {WIFI_BEACON,           "OnBeacon",             &OnBeacon},
3965         {WIFI_ATIM,                     "OnATIM",               &OnAtim},
3966         {WIFI_DISASSOC,         "OnDisassoc",           &OnDisassoc},
3967         {WIFI_AUTH,                     "OnAuth",               &OnAuthClient},
3968         {WIFI_DEAUTH,           "OnDeAuth",             &OnDeAuth},
3969         {WIFI_ACTION,           "OnAction",             &OnAction},
3970 };
3971
3972 int init_hw_mlme_ext(struct adapter *padapter)
3973 {
3974         struct  mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3975
3976         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
3977         return _SUCCESS;
3978 }
3979
3980 static void init_mlme_ext_priv_value(struct adapter *padapter)
3981 {
3982         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3983         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
3984         unsigned char   mixed_datarate[NumRates] = {
3985                 _1M_RATE_, _2M_RATE_, _5M_RATE_, _11M_RATE_, _6M_RATE_,
3986                 _9M_RATE_, _12M_RATE_, _18M_RATE_, _24M_RATE_, _36M_RATE_,
3987                  _48M_RATE_, _54M_RATE_, 0xff
3988         };
3989         unsigned char   mixed_basicrate[NumRates] = {
3990                 _1M_RATE_, _2M_RATE_, _5M_RATE_, _11M_RATE_, _6M_RATE_,
3991                 _12M_RATE_, _24M_RATE_, 0xff,
3992         };
3993
3994         atomic_set(&pmlmeext->event_seq, 0);
3995         pmlmeext->mgnt_seq = 0;/* reset to zero when disconnect at client mode */
3996
3997         pmlmeext->cur_channel = padapter->registrypriv.channel;
3998         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
3999         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
4000         pmlmeext->oper_channel = pmlmeext->cur_channel;
4001         pmlmeext->oper_bwmode = pmlmeext->cur_bwmode;
4002         pmlmeext->oper_ch_offset = pmlmeext->cur_ch_offset;
4003         pmlmeext->retry = 0;
4004
4005         pmlmeext->cur_wireless_mode = padapter->registrypriv.wireless_mode;
4006
4007         memcpy(pmlmeext->datarate, mixed_datarate, NumRates);
4008         memcpy(pmlmeext->basicrate, mixed_basicrate, NumRates);
4009
4010         pmlmeext->tx_rate = IEEE80211_CCK_RATE_1MB;
4011
4012         pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
4013         pmlmeext->sitesurvey_res.channel_idx = 0;
4014         pmlmeext->sitesurvey_res.bss_cnt = 0;
4015         pmlmeext->scan_abort = false;
4016
4017         pmlmeinfo->state = WIFI_FW_NULL_STATE;
4018         pmlmeinfo->reauth_count = 0;
4019         pmlmeinfo->reassoc_count = 0;
4020         pmlmeinfo->link_count = 0;
4021         pmlmeinfo->auth_seq = 0;
4022         pmlmeinfo->auth_algo = dot11AuthAlgrthm_Open;
4023         pmlmeinfo->key_index = 0;
4024         pmlmeinfo->iv = 0;
4025
4026         pmlmeinfo->enc_algo = _NO_PRIVACY_;
4027         pmlmeinfo->authModeToggle = 0;
4028
4029         memset(pmlmeinfo->chg_txt, 0, 128);
4030
4031         pmlmeinfo->slotTime = SHORT_SLOT_TIME;
4032         pmlmeinfo->preamble_mode = PREAMBLE_AUTO;
4033
4034         pmlmeinfo->dialogToken = 0;
4035
4036         pmlmeext->action_public_rxseq = 0xffff;
4037         pmlmeext->action_public_dialog_token = 0xff;
4038 }
4039
4040 static int has_channel(struct rt_channel_info *channel_set,
4041                                            u8 chanset_size,
4042                                            u8 chan) {
4043         int i;
4044
4045         for (i = 0; i < chanset_size; i++) {
4046                 if (channel_set[i].ChannelNum == chan)
4047                         return 1;
4048         }
4049         return 0;
4050 }
4051
4052 static void init_channel_list(struct adapter *padapter, struct rt_channel_info *channel_set,
4053                                                           u8 chanset_size,
4054                                                           struct p2p_channels *channel_list) {
4055         struct p2p_oper_class_map op_class[] = {
4056                 { IEEE80211G,  81,   1,  13,  1, BW20 },
4057                 { IEEE80211G,  82,  14,  14,  1, BW20 },
4058                 { -1, 0, 0, 0, 0, BW20 }
4059         };
4060
4061         int cla, op;
4062
4063         cla = 0;
4064
4065         for (op = 0; op_class[op].op_class; op++) {
4066                 u8 ch;
4067                 struct p2p_oper_class_map *o = &op_class[op];
4068                 struct p2p_reg_class *reg = NULL;
4069
4070                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4071                         if (!has_channel(channel_set, chanset_size, ch)) {
4072                                 continue;
4073                         }
4074
4075                         if ((0 == padapter->registrypriv.ht_enable) && (8 == o->inc))
4076                                 continue;
4077
4078                         if ((0 == (padapter->registrypriv.cbw40_enable & BIT(1))) &&
4079                             ((BW40MINUS == o->bw) || (BW40PLUS == o->bw)))
4080                                 continue;
4081
4082                         if (reg == NULL) {
4083                                 reg = &channel_list->reg_class[cla];
4084                                 cla++;
4085                                 reg->reg_class = o->op_class;
4086                                 reg->channels = 0;
4087                         }
4088                         reg->channel[reg->channels] = ch;
4089                         reg->channels++;
4090                 }
4091         }
4092         channel_list->reg_classes = cla;
4093 }
4094
4095 static u8 init_channel_set(struct adapter *padapter, u8 ChannelPlan, struct rt_channel_info *channel_set)
4096 {
4097         u8 index, chanset_size = 0;
4098         u8 b2_4GBand = false;
4099         u8 Index2G = 0;
4100
4101         memset(channel_set, 0, sizeof(struct rt_channel_info) * MAX_CHANNEL_NUM);
4102
4103         if (ChannelPlan >= RT_CHANNEL_DOMAIN_MAX && ChannelPlan != RT_CHANNEL_DOMAIN_REALTEK_DEFINE) {
4104                 DBG_88E("ChannelPlan ID %x error !!!!!\n", ChannelPlan);
4105                 return chanset_size;
4106         }
4107
4108         if (padapter->registrypriv.wireless_mode & WIRELESS_11G) {
4109                 b2_4GBand = true;
4110                 if (RT_CHANNEL_DOMAIN_REALTEK_DEFINE == ChannelPlan)
4111                         Index2G = RTW_CHANNEL_PLAN_MAP_REALTEK_DEFINE.Index2G;
4112                 else
4113                         Index2G = RTW_ChannelPlanMap[ChannelPlan].Index2G;
4114         }
4115
4116         if (b2_4GBand) {
4117                 for (index = 0; index < RTW_ChannelPlan2G[Index2G].Len; index++) {
4118                         channel_set[chanset_size].ChannelNum = RTW_ChannelPlan2G[Index2G].Channel[index];
4119
4120                         if ((RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN == ChannelPlan) ||/* Channel 1~11 is active, and 12~14 is passive */
4121                             (RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN_2G == ChannelPlan)) {
4122                                 if (channel_set[chanset_size].ChannelNum >= 1 && channel_set[chanset_size].ChannelNum <= 11)
4123                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
4124                                 else if ((channel_set[chanset_size].ChannelNum  >= 12 && channel_set[chanset_size].ChannelNum  <= 14))
4125                                         channel_set[chanset_size].ScanType  = SCAN_PASSIVE;
4126                         } else if (RT_CHANNEL_DOMAIN_WORLD_WIDE_13 == ChannelPlan ||
4127                                    RT_CHANNEL_DOMAIN_2G_WORLD == Index2G) {/*  channel 12~13, passive scan */
4128                                 if (channel_set[chanset_size].ChannelNum <= 11)
4129                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
4130                                 else
4131                                         channel_set[chanset_size].ScanType = SCAN_PASSIVE;
4132                         } else {
4133                                 channel_set[chanset_size].ScanType = SCAN_ACTIVE;
4134                         }
4135
4136                         chanset_size++;
4137                 }
4138         }
4139         return chanset_size;
4140 }
4141
4142 int     init_mlme_ext_priv(struct adapter *padapter)
4143 {
4144         struct registry_priv *pregistrypriv = &padapter->registrypriv;
4145         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4146         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
4147         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4148
4149         pmlmeext->padapter = padapter;
4150
4151         init_mlme_ext_priv_value(padapter);
4152         pmlmeinfo->accept_addba_req = pregistrypriv->accept_addba_req;
4153
4154         init_mlme_ext_timer(padapter);
4155
4156 #ifdef CONFIG_88EU_AP_MODE
4157         init_mlme_ap_info(padapter);
4158 #endif
4159
4160         pmlmeext->max_chan_nums = init_channel_set(padapter, pmlmepriv->ChannelPlan, pmlmeext->channel_set);
4161         init_channel_list(padapter, pmlmeext->channel_set, pmlmeext->max_chan_nums, &pmlmeext->channel_list);
4162
4163         pmlmeext->chan_scan_time = SURVEY_TO;
4164         pmlmeext->mlmeext_init = true;
4165
4166
4167         pmlmeext->active_keep_alive_check = true;
4168
4169         return _SUCCESS;
4170 }
4171
4172 void free_mlme_ext_priv(struct mlme_ext_priv *pmlmeext)
4173 {
4174         struct adapter *padapter = pmlmeext->padapter;
4175
4176         if (!padapter)
4177                 return;
4178
4179         if (padapter->bDriverStopped) {
4180                 del_timer_sync(&pmlmeext->survey_timer);
4181                 del_timer_sync(&pmlmeext->link_timer);
4182         }
4183 }
4184
4185 static void _mgt_dispatcher(struct adapter *padapter, struct mlme_handler *ptable, struct recv_frame *precv_frame)
4186 {
4187         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
4188         u8 *pframe = precv_frame->rx_data;
4189
4190         if (ptable->func) {
4191                 /* receive the frames that ra(a1) is my address or ra(a1) is bc address. */
4192                 if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN) &&
4193                     memcmp(GetAddr1Ptr(pframe), bc_addr, ETH_ALEN))
4194                         return;
4195                 ptable->func(padapter, precv_frame);
4196         }
4197 }
4198
4199 void mgt_dispatcher(struct adapter *padapter, struct recv_frame *precv_frame)
4200 {
4201         int index;
4202         struct mlme_handler *ptable;
4203 #ifdef CONFIG_88EU_AP_MODE
4204         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
4205 #endif /* CONFIG_88EU_AP_MODE */
4206         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
4207         u8 *pframe = precv_frame->rx_data;
4208         struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(pframe));
4209
4210         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
4211                  ("+mgt_dispatcher: type(0x%x) subtype(0x%x)\n",
4212                   (unsigned int)GetFrameType(pframe),
4213                   (unsigned int)GetFrameSubType(pframe)));
4214
4215         if (GetFrameType(pframe) != WIFI_MGT_TYPE) {
4216                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
4217                          ("mgt_dispatcher: type(0x%x) error!\n",
4218                           (unsigned int)GetFrameType(pframe)));
4219                 return;
4220         }
4221
4222         /* receive the frames that ra(a1) is my address or ra(a1) is bc address. */
4223         if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN) &&
4224             memcmp(GetAddr1Ptr(pframe), bc_addr, ETH_ALEN))
4225                 return;
4226
4227         ptable = mlme_sta_tbl;
4228
4229         index = GetFrameSubType(pframe) >> 4;
4230
4231         if (index > 13) {
4232                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Currently we do not support reserved sub-fr-type=%d\n", index));
4233                 return;
4234         }
4235         ptable += index;
4236
4237         if (psta != NULL) {
4238                 if (GetRetry(pframe)) {
4239                         if (precv_frame->attrib.seq_num ==
4240                             psta->RxMgmtFrameSeqNum) {
4241                                 /* drop the duplicate management frame */
4242                                 DBG_88E("Drop duplicate management frame with seq_num=%d.\n",
4243                                         precv_frame->attrib.seq_num);
4244                                 return;
4245                         }
4246                 }
4247                 psta->RxMgmtFrameSeqNum = precv_frame->attrib.seq_num;
4248         }
4249
4250 #ifdef CONFIG_88EU_AP_MODE
4251         switch (GetFrameSubType(pframe)) {
4252         case WIFI_AUTH:
4253                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
4254                         ptable->func = &OnAuth;
4255                 else
4256                         ptable->func = &OnAuthClient;
4257                 /* fall through */
4258         case WIFI_ASSOCREQ:
4259         case WIFI_REASSOCREQ:
4260         case WIFI_PROBEREQ:
4261         case WIFI_BEACON:
4262         case WIFI_ACTION:
4263                 _mgt_dispatcher(padapter, ptable, precv_frame);
4264                 break;
4265         default:
4266                 _mgt_dispatcher(padapter, ptable, precv_frame);
4267                 break;
4268         }
4269 #else
4270         _mgt_dispatcher(padapter, ptable, precv_frame);
4271 #endif
4272 }
4273
4274 /****************************************************************************
4275
4276 Following are the functions to report events
4277
4278 *****************************************************************************/
4279
4280 void report_survey_event(struct adapter *padapter,
4281                          struct recv_frame *precv_frame)
4282 {
4283         struct cmd_obj *pcmd_obj;
4284         u8 *pevtcmd;
4285         u32 cmdsz;
4286         struct survey_event     *psurvey_evt;
4287         struct C2HEvent_Header *pc2h_evt_hdr;
4288         struct mlme_ext_priv *pmlmeext;
4289         struct cmd_priv *pcmdpriv;
4290
4291         if (!padapter)
4292                 return;
4293
4294         pmlmeext = &padapter->mlmeextpriv;
4295         pcmdpriv = &padapter->cmdpriv;
4296
4297
4298         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
4299         if (pcmd_obj == NULL)
4300                 return;
4301
4302         cmdsz = sizeof(struct survey_event) + sizeof(struct C2HEvent_Header);
4303         pevtcmd = kzalloc(cmdsz, GFP_ATOMIC);
4304         if (pevtcmd == NULL) {
4305                 kfree(pcmd_obj);
4306                 return;
4307         }
4308
4309         INIT_LIST_HEAD(&pcmd_obj->list);
4310
4311         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4312         pcmd_obj->cmdsz = cmdsz;
4313         pcmd_obj->parmbuf = pevtcmd;
4314
4315         pcmd_obj->rsp = NULL;
4316         pcmd_obj->rspsz  = 0;
4317
4318         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4319         pc2h_evt_hdr->len = sizeof(struct survey_event);
4320         pc2h_evt_hdr->ID = GEN_EVT_CODE(_Survey);
4321         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4322
4323         psurvey_evt = (struct survey_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4324
4325         if (collect_bss_info(padapter, precv_frame, (struct wlan_bssid_ex *)&psurvey_evt->bss) == _FAIL) {
4326                 kfree(pcmd_obj);
4327                 kfree(pevtcmd);
4328                 return;
4329         }
4330
4331         process_80211d(padapter, &psurvey_evt->bss);
4332
4333         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4334
4335         pmlmeext->sitesurvey_res.bss_cnt++;
4336
4337         return;
4338 }
4339
4340 void report_surveydone_event(struct adapter *padapter)
4341 {
4342         struct cmd_obj *pcmd_obj;
4343         u8 *pevtcmd;
4344         u32 cmdsz;
4345         struct surveydone_event *psurveydone_evt;
4346         struct C2HEvent_Header  *pc2h_evt_hdr;
4347         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4348         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
4349
4350         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
4351         if (pcmd_obj == NULL)
4352                 return;
4353
4354         cmdsz = sizeof(struct surveydone_event) + sizeof(struct C2HEvent_Header);
4355         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
4356         if (pevtcmd == NULL) {
4357                 kfree(pcmd_obj);
4358                 return;
4359         }
4360
4361         INIT_LIST_HEAD(&pcmd_obj->list);
4362
4363         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4364         pcmd_obj->cmdsz = cmdsz;
4365         pcmd_obj->parmbuf = pevtcmd;
4366
4367         pcmd_obj->rsp = NULL;
4368         pcmd_obj->rspsz  = 0;
4369
4370         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4371         pc2h_evt_hdr->len = sizeof(struct surveydone_event);
4372         pc2h_evt_hdr->ID = GEN_EVT_CODE(_SurveyDone);
4373         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4374
4375         psurveydone_evt = (struct surveydone_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4376         psurveydone_evt->bss_cnt = pmlmeext->sitesurvey_res.bss_cnt;
4377
4378         DBG_88E("survey done event(%x)\n", psurveydone_evt->bss_cnt);
4379
4380         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4381
4382         return;
4383 }
4384
4385 void report_join_res(struct adapter *padapter, int res)
4386 {
4387         struct cmd_obj *pcmd_obj;
4388         u8 *pevtcmd;
4389         u32 cmdsz;
4390         struct joinbss_event            *pjoinbss_evt;
4391         struct C2HEvent_Header  *pc2h_evt_hdr;
4392         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4393         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4394         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
4395
4396         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
4397         if (pcmd_obj == NULL)
4398                 return;
4399
4400         cmdsz = sizeof(struct joinbss_event) + sizeof(struct C2HEvent_Header);
4401         pevtcmd = kzalloc(cmdsz, GFP_ATOMIC);
4402         if (pevtcmd == NULL) {
4403                 kfree(pcmd_obj);
4404                 return;
4405         }
4406
4407         INIT_LIST_HEAD(&pcmd_obj->list);
4408
4409         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4410         pcmd_obj->cmdsz = cmdsz;
4411         pcmd_obj->parmbuf = pevtcmd;
4412
4413         pcmd_obj->rsp = NULL;
4414         pcmd_obj->rspsz  = 0;
4415
4416         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4417         pc2h_evt_hdr->len = sizeof(struct joinbss_event);
4418         pc2h_evt_hdr->ID = GEN_EVT_CODE(_JoinBss);
4419         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4420
4421         pjoinbss_evt = (struct joinbss_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4422         memcpy((unsigned char *)(&(pjoinbss_evt->network.network)), &(pmlmeinfo->network), sizeof(struct wlan_bssid_ex));
4423         pjoinbss_evt->network.join_res  = res;
4424         pjoinbss_evt->network.aid = res;
4425
4426         DBG_88E("report_join_res(%d)\n", res);
4427
4428
4429         rtw_joinbss_event_prehandle(padapter, (u8 *)&pjoinbss_evt->network);
4430
4431
4432         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4433
4434         return;
4435 }
4436
4437 void report_del_sta_event(struct adapter *padapter, unsigned char *MacAddr, unsigned short reason)
4438 {
4439         struct cmd_obj *pcmd_obj;
4440         u8 *pevtcmd;
4441         u32 cmdsz;
4442         struct sta_info *psta;
4443         int     mac_id;
4444         struct stadel_event                     *pdel_sta_evt;
4445         struct C2HEvent_Header  *pc2h_evt_hdr;
4446         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4447         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
4448
4449         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
4450         if (pcmd_obj == NULL)
4451                 return;
4452
4453         cmdsz = sizeof(struct stadel_event) + sizeof(struct C2HEvent_Header);
4454         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
4455         if (pevtcmd == NULL) {
4456                 kfree(pcmd_obj);
4457                 return;
4458         }
4459
4460         INIT_LIST_HEAD(&pcmd_obj->list);
4461
4462         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4463         pcmd_obj->cmdsz = cmdsz;
4464         pcmd_obj->parmbuf = pevtcmd;
4465
4466         pcmd_obj->rsp = NULL;
4467         pcmd_obj->rspsz  = 0;
4468
4469         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4470         pc2h_evt_hdr->len = sizeof(struct stadel_event);
4471         pc2h_evt_hdr->ID = GEN_EVT_CODE(_DelSTA);
4472         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4473
4474         pdel_sta_evt = (struct stadel_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4475         memcpy((unsigned char *)(&(pdel_sta_evt->macaddr)), MacAddr, ETH_ALEN);
4476         memcpy((unsigned char *)(pdel_sta_evt->rsvd), (unsigned char *)(&reason), 2);
4477
4478
4479         psta = rtw_get_stainfo(&padapter->stapriv, MacAddr);
4480         if (psta)
4481                 mac_id = (int)psta->mac_id;
4482         else
4483                 mac_id = -1;
4484
4485         pdel_sta_evt->mac_id = mac_id;
4486
4487         DBG_88E("report_del_sta_event: delete STA, mac_id =%d\n", mac_id);
4488
4489         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4490
4491         return;
4492 }
4493
4494 void report_add_sta_event(struct adapter *padapter, unsigned char *MacAddr, int cam_idx)
4495 {
4496         struct cmd_obj *pcmd_obj;
4497         u8 *pevtcmd;
4498         u32 cmdsz;
4499         struct stassoc_event            *padd_sta_evt;
4500         struct C2HEvent_Header  *pc2h_evt_hdr;
4501         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4502         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
4503
4504         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
4505         if (pcmd_obj == NULL)
4506                 return;
4507
4508         cmdsz = sizeof(struct stassoc_event) + sizeof(struct C2HEvent_Header);
4509         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
4510         if (pevtcmd == NULL) {
4511                 kfree(pcmd_obj);
4512                 return;
4513         }
4514
4515         INIT_LIST_HEAD(&pcmd_obj->list);
4516
4517         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
4518         pcmd_obj->cmdsz = cmdsz;
4519         pcmd_obj->parmbuf = pevtcmd;
4520
4521         pcmd_obj->rsp = NULL;
4522         pcmd_obj->rspsz  = 0;
4523
4524         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
4525         pc2h_evt_hdr->len = sizeof(struct stassoc_event);
4526         pc2h_evt_hdr->ID = GEN_EVT_CODE(_AddSTA);
4527         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
4528
4529         padd_sta_evt = (struct stassoc_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
4530         memcpy((unsigned char *)(&(padd_sta_evt->macaddr)), MacAddr, ETH_ALEN);
4531         padd_sta_evt->cam_id = cam_idx;
4532
4533         DBG_88E("report_add_sta_event: add STA\n");
4534
4535         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
4536
4537         return;
4538 }
4539
4540
4541 /****************************************************************************
4542
4543 Following are the event callback functions
4544
4545 *****************************************************************************/
4546
4547 /* for sta/adhoc mode */
4548 void update_sta_info(struct adapter *padapter, struct sta_info *psta)
4549 {
4550         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
4551         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4552         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4553
4554         /* ERP */
4555         VCS_update(padapter, psta);
4556
4557         /* HT */
4558         if (pmlmepriv->htpriv.ht_option) {
4559                 psta->htpriv.ht_option = true;
4560
4561                 psta->htpriv.ampdu_enable = pmlmepriv->htpriv.ampdu_enable;
4562
4563                 if (support_short_GI(padapter, &(pmlmeinfo->HT_caps)))
4564                         psta->htpriv.sgi = true;
4565
4566                 psta->qos_option = true;
4567         } else {
4568                 psta->htpriv.ht_option = false;
4569
4570                 psta->htpriv.ampdu_enable = false;
4571
4572                 psta->htpriv.sgi = false;
4573                 psta->qos_option = false;
4574         }
4575         psta->htpriv.bwmode = pmlmeext->cur_bwmode;
4576         psta->htpriv.ch_offset = pmlmeext->cur_ch_offset;
4577
4578         psta->htpriv.agg_enable_bitmap = 0x0;/* reset */
4579         psta->htpriv.candidate_tid_bitmap = 0x0;/* reset */
4580
4581         /* QoS */
4582         if (pmlmepriv->qospriv.qos_option)
4583                 psta->qos_option = true;
4584
4585
4586         psta->state = _FW_LINKED;
4587 }
4588
4589 void mlmeext_joinbss_event_callback(struct adapter *padapter, int join_res)
4590 {
4591         struct sta_info         *psta, *psta_bmc;
4592         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4593         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4594         struct wlan_bssid_ex *cur_network = &(pmlmeinfo->network);
4595         struct sta_priv         *pstapriv = &padapter->stapriv;
4596         u8 join_type;
4597         u16 media_status;
4598
4599         if (join_res < 0) {
4600                 join_type = 1;
4601                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
4602                 rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
4603
4604                 /* restore to initial setting. */
4605                 update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
4606
4607                 goto exit_mlmeext_joinbss_event_callback;
4608         }
4609
4610         if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
4611                 /* for bc/mc */
4612                 psta_bmc = rtw_get_bcmc_stainfo(padapter);
4613                 if (psta_bmc) {
4614                         pmlmeinfo->FW_sta_info[psta_bmc->mac_id].psta = psta_bmc;
4615                         update_bmc_sta_support_rate(padapter, psta_bmc->mac_id);
4616                         Update_RA_Entry(padapter, psta_bmc->mac_id);
4617                 }
4618         }
4619
4620
4621         /* turn on dynamic functions */
4622         Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true);
4623
4624         /*  update IOT-related issue */
4625         update_IOT_info(padapter);
4626
4627         rtw_hal_set_hwreg(padapter, HW_VAR_BASIC_RATE, cur_network->SupportedRates);
4628
4629         /* BCN interval */
4630         rtw_hal_set_hwreg(padapter, HW_VAR_BEACON_INTERVAL, (u8 *)(&pmlmeinfo->bcn_interval));
4631
4632         /* update capability */
4633         update_capinfo(padapter, pmlmeinfo->capability);
4634
4635         /* WMM, Update EDCA param */
4636         WMMOnAssocRsp(padapter);
4637
4638         /* HT */
4639         HTOnAssocRsp(padapter);
4640
4641         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
4642
4643         psta = rtw_get_stainfo(pstapriv, cur_network->MacAddress);
4644         if (psta) { /* only for infra. mode */
4645                 pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
4646
4647                 psta->wireless_mode = pmlmeext->cur_wireless_mode;
4648
4649                 /* set per sta rate after updating HT cap. */
4650                 set_sta_rate(padapter, psta);
4651                 rtw_hal_set_hwreg(padapter, HW_VAR_TX_RPT_MAX_MACID, (u8 *)&psta->mac_id);
4652                 media_status = (psta->mac_id<<8)|1; /*   MACID|OPMODE: 1 means connect */
4653                 rtw_hal_set_hwreg(padapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status);
4654         }
4655
4656         join_type = 2;
4657         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
4658
4659         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
4660                 /*  correcting TSF */
4661                 correct_TSF(padapter, pmlmeext);
4662         }
4663         rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_CONNECT, 0);
4664
4665 exit_mlmeext_joinbss_event_callback:
4666
4667         DBG_88E("=>%s\n", __func__);
4668 }
4669
4670 void mlmeext_sta_add_event_callback(struct adapter *padapter, struct sta_info *psta)
4671 {
4672         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
4673         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4674         u8 join_type;
4675
4676         DBG_88E("%s\n", __func__);
4677
4678         if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
4679                 if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {/* adhoc master or sta_count>1 */
4680                         /* nothing to do */
4681                 } else { /* adhoc client */
4682                         /*  correcting TSF */
4683                         correct_TSF(padapter, pmlmeext);
4684
4685                         /* start beacon */
4686                         if (send_beacon(padapter) == _FAIL) {
4687                                 pmlmeinfo->FW_sta_info[psta->mac_id].status = 0;
4688                                 pmlmeinfo->state ^= WIFI_FW_ADHOC_STATE;
4689                                 return;
4690                         }
4691                         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
4692                 }
4693
4694                 join_type = 2;
4695                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
4696         }
4697
4698         pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
4699
4700         /* rate radaptive */
4701         Update_RA_Entry(padapter, psta->mac_id);
4702
4703         /* update adhoc sta_info */
4704         update_sta_info(padapter, psta);
4705 }
4706
4707 void mlmeext_sta_del_event_callback(struct adapter *padapter)
4708 {
4709         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4710         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4711
4712         if (is_client_associated_to_ap(padapter) || is_IBSS_empty(padapter)) {
4713                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
4714                 rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
4715
4716                 /* restore to initial setting. */
4717                 update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
4718
4719                 /* switch to the 20M Hz mode after disconnect */
4720                 pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
4721                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
4722
4723                 /* SelectChannel(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset); */
4724                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
4725
4726
4727                 flush_all_cam_entry(padapter);
4728
4729                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
4730
4731                 /* set MSR to no link state -> infra. mode */
4732                 Set_MSR(padapter, _HW_STATE_STATION_);
4733
4734                 del_timer_sync(&pmlmeext->link_timer);
4735         }
4736 }
4737
4738 /****************************************************************************
4739
4740 Following are the functions for the timer handlers
4741
4742 *****************************************************************************/
4743 void _linked_rx_signal_strehgth_display(struct adapter *padapter);
4744 void _linked_rx_signal_strehgth_display(struct adapter *padapter)
4745 {
4746         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4747       struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4748         u8 mac_id;
4749         int UndecoratedSmoothedPWDB;
4750         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
4751                 mac_id = 0;
4752         else if ((pmlmeinfo->state&0x03) == _HW_STATE_AP_)
4753                 mac_id = 2;
4754
4755         rtw_hal_get_def_var(padapter, HW_DEF_RA_INFO_DUMP, &mac_id);
4756
4757         rtw_hal_get_def_var(padapter, HAL_DEF_UNDERCORATEDSMOOTHEDPWDB, &UndecoratedSmoothedPWDB);
4758         DBG_88E("UndecoratedSmoothedPWDB:%d\n", UndecoratedSmoothedPWDB);
4759 }
4760
4761 static u8 chk_ap_is_alive(struct adapter *padapter, struct sta_info *psta)
4762 {
4763         u8 ret = false;
4764
4765         if ((sta_rx_data_pkts(psta) == sta_last_rx_data_pkts(psta)) &&
4766             sta_rx_beacon_pkts(psta) == sta_last_rx_beacon_pkts(psta) &&
4767             sta_rx_probersp_pkts(psta) == sta_last_rx_probersp_pkts(psta))
4768                 ret = false;
4769         else
4770                 ret = true;
4771
4772         sta_update_last_rx_pkts(psta);
4773
4774         return ret;
4775 }
4776
4777 void linked_status_chk(struct adapter *padapter)
4778 {
4779         u32     i;
4780         struct sta_info         *psta;
4781         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
4782         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4783         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4784         struct sta_priv         *pstapriv = &padapter->stapriv;
4785
4786         if (padapter->bRxRSSIDisplay)
4787                 _linked_rx_signal_strehgth_display(padapter);
4788
4789         if (is_client_associated_to_ap(padapter)) {
4790                 /* linked infrastructure client mode */
4791
4792                 int tx_chk = _SUCCESS, rx_chk = _SUCCESS;
4793                 int rx_chk_limit;
4794
4795                 rx_chk_limit = 4;
4796                 psta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.MacAddress);
4797                 if (psta != NULL) {
4798                         bool is_p2p_enable = false;
4799
4800                         if (!chk_ap_is_alive(padapter, psta))
4801                                 rx_chk = _FAIL;
4802
4803                         if (pxmitpriv->last_tx_pkts == pxmitpriv->tx_pkts)
4804                                 tx_chk = _FAIL;
4805
4806                         if (pmlmeext->active_keep_alive_check && (rx_chk == _FAIL || tx_chk == _FAIL)) {
4807                                 u8 backup_oper_channel = 0;
4808
4809                                 /* switch to correct channel of current network  before issue keep-alive frames */
4810                                 if (rtw_get_oper_ch(padapter) != pmlmeext->cur_channel) {
4811                                         backup_oper_channel = rtw_get_oper_ch(padapter);
4812                                         SelectChannel(padapter, pmlmeext->cur_channel);
4813                                 }
4814
4815                                 if (rx_chk != _SUCCESS)
4816                                         issue_probereq_ex(padapter, &pmlmeinfo->network.Ssid, psta->hwaddr, 3, 1);
4817
4818                                 if ((tx_chk != _SUCCESS && pmlmeinfo->link_count++ == 0xf) || rx_chk != _SUCCESS) {
4819                                         tx_chk = issue_nulldata(padapter, psta->hwaddr, 0, 3, 1);
4820                                         /* if tx acked and p2p disabled, set rx_chk _SUCCESS to reset retry count */
4821                                         if (tx_chk == _SUCCESS && !is_p2p_enable)
4822                                                 rx_chk = _SUCCESS;
4823                                 }
4824
4825                                 /* back to the original operation channel */
4826                                 if (backup_oper_channel > 0)
4827                                         SelectChannel(padapter, backup_oper_channel);
4828                         } else {
4829                                 if (rx_chk != _SUCCESS) {
4830                                         if (pmlmeext->retry == 0) {
4831                                                 issue_probereq(padapter,
4832                                                 &pmlmeinfo->network.Ssid,
4833                                                 pmlmeinfo->network.MacAddress,
4834                                                                         false);
4835                                                 issue_probereq(padapter,
4836                                                 &pmlmeinfo->network.Ssid,
4837                                                 pmlmeinfo->network.MacAddress,
4838                                                                         false);
4839                                                 issue_probereq(padapter,
4840                                                 &pmlmeinfo->network.Ssid,
4841                                                 pmlmeinfo->network.MacAddress,
4842                                                                         false);
4843                                         }
4844                                 }
4845
4846                                 if (tx_chk != _SUCCESS && pmlmeinfo->link_count++ == 0xf) {
4847                                         tx_chk = issue_nulldata(padapter, NULL, 0, 1, 0);
4848                                 }
4849                         }
4850
4851                         if (rx_chk == _FAIL) {
4852                                 pmlmeext->retry++;
4853                                 if (pmlmeext->retry > rx_chk_limit) {
4854                                         DBG_88E_LEVEL(_drv_always_, FUNC_ADPT_FMT" disconnect or roaming\n",
4855                                                       FUNC_ADPT_ARG(padapter));
4856                                         receive_disconnect(padapter, pmlmeinfo->network.MacAddress,
4857                                                            WLAN_REASON_EXPIRATION_CHK);
4858                                         return;
4859                                 }
4860                         } else {
4861                                 pmlmeext->retry = 0;
4862                         }
4863
4864                         if (tx_chk == _FAIL) {
4865                                 pmlmeinfo->link_count &= 0xf;
4866                         } else {
4867                                 pxmitpriv->last_tx_pkts = pxmitpriv->tx_pkts;
4868                                 pmlmeinfo->link_count = 0;
4869                         }
4870                 } /* end of if ((psta = rtw_get_stainfo(pstapriv, passoc_res->network.MacAddress)) != NULL) */
4871         } else if (is_client_associated_to_ibss(padapter)) {
4872                 /* linked IBSS mode */
4873                 /* for each assoc list entry to check the rx pkt counter */
4874                 for (i = IBSS_START_MAC_ID; i < NUM_STA; i++) {
4875                         if (pmlmeinfo->FW_sta_info[i].status == 1) {
4876                                 psta = pmlmeinfo->FW_sta_info[i].psta;
4877
4878                                 if (NULL == psta)
4879                                         continue;
4880                                 if (pmlmeinfo->FW_sta_info[i].rx_pkt == sta_rx_pkts(psta)) {
4881                                         if (pmlmeinfo->FW_sta_info[i].retry < 3) {
4882                                                 pmlmeinfo->FW_sta_info[i].retry++;
4883                                         } else {
4884                                                 pmlmeinfo->FW_sta_info[i].retry = 0;
4885                                                 pmlmeinfo->FW_sta_info[i].status = 0;
4886                                                 report_del_sta_event(padapter, psta->hwaddr
4887                                                         , 65535/*  indicate disconnect caused by no rx */
4888                                         );
4889                                         }
4890                                 } else {
4891                                         pmlmeinfo->FW_sta_info[i].retry = 0;
4892                                         pmlmeinfo->FW_sta_info[i].rx_pkt = (u32)sta_rx_pkts(psta);
4893                                 }
4894                         }
4895                 }
4896         }
4897 }
4898
4899 void survey_timer_hdl(unsigned long data)
4900 {
4901         struct adapter *padapter = (struct adapter *)data;
4902         struct cmd_obj  *ph2c;
4903         struct sitesurvey_parm  *psurveyPara;
4904         struct cmd_priv                                 *pcmdpriv = &padapter->cmdpriv;
4905         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
4906
4907         /* issue rtw_sitesurvey_cmd */
4908         if (pmlmeext->sitesurvey_res.state > SCAN_START) {
4909                 if (pmlmeext->sitesurvey_res.state ==  SCAN_PROCESS)
4910                         pmlmeext->sitesurvey_res.channel_idx++;
4911
4912                 if (pmlmeext->scan_abort) {
4913                         pmlmeext->sitesurvey_res.channel_idx = pmlmeext->sitesurvey_res.ch_num;
4914                         DBG_88E("%s idx:%d\n", __func__
4915                                 , pmlmeext->sitesurvey_res.channel_idx);
4916
4917                         pmlmeext->scan_abort = false;/* reset */
4918                 }
4919
4920                 ph2c = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
4921                 if (ph2c == NULL)
4922                         goto exit_survey_timer_hdl;
4923
4924                 psurveyPara = kzalloc(sizeof(struct sitesurvey_parm), GFP_ATOMIC);
4925                 if (psurveyPara == NULL) {
4926                         kfree(ph2c);
4927                         goto exit_survey_timer_hdl;
4928                 }
4929
4930                 init_h2fwcmd_w_parm_no_rsp(ph2c, psurveyPara, GEN_CMD_CODE(_SiteSurvey));
4931                 rtw_enqueue_cmd(pcmdpriv, ph2c);
4932         }
4933
4934
4935 exit_survey_timer_hdl:
4936         return;
4937 }
4938
4939 void link_timer_hdl(unsigned long data)
4940 {
4941         struct adapter *padapter = (struct adapter *)data;
4942         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4943         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4944
4945         if (pmlmeinfo->state & WIFI_FW_AUTH_NULL) {
4946                 DBG_88E("link_timer_hdl:no beacon while connecting\n");
4947                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
4948                 report_join_res(padapter, -3);
4949         } else if (pmlmeinfo->state & WIFI_FW_AUTH_STATE) {
4950                 /* re-auth timer */
4951                 if (++pmlmeinfo->reauth_count > REAUTH_LIMIT) {
4952                         pmlmeinfo->state = 0;
4953                         report_join_res(padapter, -1);
4954                         return;
4955                 }
4956
4957                 DBG_88E("link_timer_hdl: auth timeout and try again\n");
4958                 pmlmeinfo->auth_seq = 1;
4959                 issue_auth(padapter, NULL, 0);
4960                 set_link_timer(pmlmeext, REAUTH_TO);
4961         } else if (pmlmeinfo->state & WIFI_FW_ASSOC_STATE) {
4962                 /* re-assoc timer */
4963                 if (++pmlmeinfo->reassoc_count > REASSOC_LIMIT) {
4964                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
4965                         report_join_res(padapter, -2);
4966                         return;
4967                 }
4968
4969                 DBG_88E("link_timer_hdl: assoc timeout and try again\n");
4970                 issue_assocreq(padapter);
4971                 set_link_timer(pmlmeext, REASSOC_TO);
4972         }
4973         return;
4974 }
4975
4976 void addba_timer_hdl(unsigned long data)
4977 {
4978         struct sta_info *psta = (struct sta_info *)data;
4979         struct ht_priv  *phtpriv;
4980
4981         if (!psta)
4982                 return;
4983
4984         phtpriv = &psta->htpriv;
4985
4986         if ((phtpriv->ht_option) && (phtpriv->ampdu_enable)) {
4987                 if (phtpriv->candidate_tid_bitmap)
4988                         phtpriv->candidate_tid_bitmap = 0x0;
4989         }
4990 }
4991
4992 u8 setopmode_hdl(struct adapter *padapter, u8 *pbuf)
4993 {
4994         u8 type;
4995         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
4996         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4997         struct setopmode_parm *psetop = (struct setopmode_parm *)pbuf;
4998
4999         if (psetop->mode == Ndis802_11APMode) {
5000                 pmlmeinfo->state = WIFI_FW_AP_STATE;
5001                 type = _HW_STATE_AP_;
5002         } else if (psetop->mode == Ndis802_11Infrastructure) {
5003                 pmlmeinfo->state &= ~(BIT(0)|BIT(1));/*  clear state */
5004                 pmlmeinfo->state |= WIFI_FW_STATION_STATE;/* set to     STATION_STATE */
5005                 type = _HW_STATE_STATION_;
5006         } else if (psetop->mode == Ndis802_11IBSS) {
5007                 type = _HW_STATE_ADHOC_;
5008         } else {
5009                 type = _HW_STATE_NOLINK_;
5010         }
5011
5012         rtw_hal_set_hwreg(padapter, HW_VAR_SET_OPMODE, (u8 *)(&type));
5013         /* Set_NETYPE0_MSR(padapter, type); */
5014
5015         return H2C_SUCCESS;
5016 }
5017
5018 u8 createbss_hdl(struct adapter *padapter, u8 *pbuf)
5019 {
5020         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5021         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5022         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
5023         struct wlan_bssid_ex *pparm = (struct wlan_bssid_ex *)pbuf;
5024         /* u32  initialgain; */
5025
5026
5027         if (pparm->InfrastructureMode == Ndis802_11APMode) {
5028 #ifdef CONFIG_88EU_AP_MODE
5029
5030                 if (pmlmeinfo->state == WIFI_FW_AP_STATE) {
5031                         /* todo: */
5032                         return H2C_SUCCESS;
5033                 }
5034 #endif
5035         }
5036
5037         /* below is for ad-hoc master */
5038         if (pparm->InfrastructureMode == Ndis802_11IBSS) {
5039                 rtw_joinbss_reset(padapter);
5040
5041                 pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
5042                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5043                 pmlmeinfo->ERP_enable = 0;
5044                 pmlmeinfo->WMM_enable = 0;
5045                 pmlmeinfo->HT_enable = 0;
5046                 pmlmeinfo->HT_caps_enable = 0;
5047                 pmlmeinfo->HT_info_enable = 0;
5048                 pmlmeinfo->agg_enable_bitmap = 0;
5049                 pmlmeinfo->candidate_tid_bitmap = 0;
5050
5051                 /* disable dynamic functions, such as high power, DIG */
5052                 Save_DM_Func_Flag(padapter);
5053                 Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false);
5054
5055                 /* config the initial gain under linking, need to write the BB registers */
5056                 /* initialgain = 0x1E; */
5057                 /* rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain)); */
5058
5059                 /* cancel link timer */
5060                 del_timer_sync(&pmlmeext->link_timer);
5061
5062                 /* clear CAM */
5063                 flush_all_cam_entry(padapter);
5064
5065                 memcpy(pnetwork, pbuf, FIELD_OFFSET(struct wlan_bssid_ex, IELength));
5066                 pnetwork->IELength = ((struct wlan_bssid_ex *)pbuf)->IELength;
5067
5068                 if (pnetwork->IELength > MAX_IE_SZ)/* Check pbuf->IELength */
5069                         return H2C_PARAMETERS_ERROR;
5070
5071                 memcpy(pnetwork->IEs, ((struct wlan_bssid_ex *)pbuf)->IEs, pnetwork->IELength);
5072
5073                 start_create_ibss(padapter);
5074         }
5075
5076         return H2C_SUCCESS;
5077 }
5078
5079 u8 join_cmd_hdl(struct adapter *padapter, u8 *pbuf)
5080 {
5081         u8 join_type;
5082         struct ndis_802_11_var_ie *pIE;
5083         struct registry_priv    *pregpriv = &padapter->registrypriv;
5084         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5085         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5086         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
5087         struct wlan_bssid_ex *pparm = (struct wlan_bssid_ex *)pbuf;
5088         u32 i;
5089
5090         /* check already connecting to AP or not */
5091         if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {
5092                 if (pmlmeinfo->state & WIFI_FW_STATION_STATE)
5093                         issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, 5, 100);
5094
5095                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
5096
5097                 /* clear CAM */
5098                 flush_all_cam_entry(padapter);
5099
5100                 del_timer_sync(&pmlmeext->link_timer);
5101
5102                 /* set MSR to nolink -> infra. mode */
5103                 Set_MSR(padapter, _HW_STATE_STATION_);
5104
5105
5106                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
5107         }
5108
5109         rtw_antenna_select_cmd(padapter, pparm->PhyInfo.Optimum_antenna, false);
5110
5111         rtw_joinbss_reset(padapter);
5112
5113         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
5114         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5115         pmlmeinfo->ERP_enable = 0;
5116         pmlmeinfo->WMM_enable = 0;
5117         pmlmeinfo->HT_enable = 0;
5118         pmlmeinfo->HT_caps_enable = 0;
5119         pmlmeinfo->HT_info_enable = 0;
5120         pmlmeinfo->agg_enable_bitmap = 0;
5121         pmlmeinfo->candidate_tid_bitmap = 0;
5122         pmlmeinfo->bwmode_updated = false;
5123
5124         memcpy(pnetwork, pbuf, FIELD_OFFSET(struct wlan_bssid_ex, IELength));
5125         pnetwork->IELength = ((struct wlan_bssid_ex *)pbuf)->IELength;
5126
5127         if (pnetwork->IELength > MAX_IE_SZ)/* Check pbuf->IELength */
5128                 return H2C_PARAMETERS_ERROR;
5129
5130         memcpy(pnetwork->IEs, ((struct wlan_bssid_ex *)pbuf)->IEs, pnetwork->IELength);
5131
5132         /* Check AP vendor to move rtw_joinbss_cmd() */
5133
5134         for (i = sizeof(struct ndis_802_11_fixed_ie); i < pnetwork->IELength;) {
5135                 pIE = (struct ndis_802_11_var_ie *)(pnetwork->IEs + i);
5136
5137                 switch (pIE->ElementID) {
5138                 case _VENDOR_SPECIFIC_IE_:/* Get WMM IE. */
5139                         if (!memcmp(pIE->data, WMM_OUI, 4))
5140                                 pmlmeinfo->WMM_enable = 1;
5141                         break;
5142                 case _HT_CAPABILITY_IE_:        /* Get HT Cap IE. */
5143                         pmlmeinfo->HT_caps_enable = 1;
5144                         break;
5145                 case _HT_EXTRA_INFO_IE_:        /* Get HT Info IE. */
5146                         pmlmeinfo->HT_info_enable = 1;
5147
5148                         /* spec case only for cisco's ap because cisco's ap issue assoc rsp using mcs rate @40MHz or @20MHz */
5149                         {
5150                                 struct HT_info_element *pht_info = (struct HT_info_element *)(pIE->data);
5151
5152                                 if ((pregpriv->cbw40_enable) &&  (pht_info->infos[0] & BIT(2))) {
5153                                         /* switch to the 40M Hz mode according to the AP */
5154                                         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_40;
5155                                         switch (pht_info->infos[0] & 0x3) {
5156                                         case 1:
5157                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
5158                                                 break;
5159                                         case 3:
5160                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
5161                                                 break;
5162                                         default:
5163                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5164                                                 break;
5165                                 }
5166
5167                                         DBG_88E("set ch/bw before connected\n");
5168                                 }
5169                         }
5170                         break;
5171                 default:
5172                         break;
5173                 }
5174
5175                 i += (pIE->Length + 2);
5176         }
5177         /* disable dynamic functions, such as high power, DIG */
5178
5179         /* config the initial gain under linking, need to write the BB registers */
5180
5181         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, pmlmeinfo->network.MacAddress);
5182         join_type = 0;
5183         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
5184
5185         /* cancel link timer */
5186         del_timer_sync(&pmlmeext->link_timer);
5187
5188         start_clnt_join(padapter);
5189
5190         return H2C_SUCCESS;
5191 }
5192
5193 u8 disconnect_hdl(struct adapter *padapter, unsigned char *pbuf)
5194 {
5195         struct disconnect_parm *param = (struct disconnect_parm *)pbuf;
5196         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5197         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5198         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
5199         u8 val8;
5200
5201         if (is_client_associated_to_ap(padapter))
5202                 issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, param->deauth_timeout_ms/100, 100);
5203
5204         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
5205         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
5206
5207         /* restore to initial setting. */
5208         update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
5209
5210         if (((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) || ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)) {
5211                 /* Stop BCN */
5212                 val8 = 0;
5213                 rtw_hal_set_hwreg(padapter, HW_VAR_BCN_FUNC, (u8 *)(&val8));
5214         }
5215
5216
5217         /* set MSR to no link state -> infra. mode */
5218         Set_MSR(padapter, _HW_STATE_STATION_);
5219
5220         pmlmeinfo->state = WIFI_FW_NULL_STATE;
5221
5222         /* switch to the 20M Hz mode after disconnect */
5223         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
5224         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5225
5226         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
5227
5228         flush_all_cam_entry(padapter);
5229
5230         del_timer_sync(&pmlmeext->link_timer);
5231
5232         rtw_free_uc_swdec_pending_queue(padapter);
5233
5234         return  H2C_SUCCESS;
5235 }
5236
5237 static int rtw_scan_ch_decision(struct adapter *padapter, struct rtw_ieee80211_channel *out,
5238         u32 out_num, struct rtw_ieee80211_channel *in, u32 in_num)
5239 {
5240         int i, j;
5241         int set_idx;
5242         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5243
5244         /* clear out first */
5245         memset(out, 0, sizeof(struct rtw_ieee80211_channel)*out_num);
5246
5247         /* acquire channels from in */
5248         j = 0;
5249         for (i = 0; i < in_num; i++) {
5250                 set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, in[i].hw_value);
5251                 if (in[i].hw_value && !(in[i].flags & RTW_IEEE80211_CHAN_DISABLED) &&
5252                     set_idx >= 0) {
5253                         out[j] = in[i];
5254
5255                         if (pmlmeext->channel_set[set_idx].ScanType == SCAN_PASSIVE)
5256                                 out[j].flags &= RTW_IEEE80211_CHAN_PASSIVE_SCAN;
5257
5258                         j++;
5259                 }
5260                 if (j >= out_num)
5261                         break;
5262         }
5263
5264         /* if out is empty, use channel_set as default */
5265         if (j == 0) {
5266                 for (i = 0; i < pmlmeext->max_chan_nums; i++) {
5267                         out[i].hw_value = pmlmeext->channel_set[i].ChannelNum;
5268
5269                         if (pmlmeext->channel_set[i].ScanType == SCAN_PASSIVE)
5270                                 out[i].flags &= RTW_IEEE80211_CHAN_PASSIVE_SCAN;
5271
5272                         j++;
5273                 }
5274         }
5275
5276         return j;
5277 }
5278
5279 u8 sitesurvey_cmd_hdl(struct adapter *padapter, u8 *pbuf)
5280 {
5281         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5282         struct sitesurvey_parm  *pparm = (struct sitesurvey_parm *)pbuf;
5283         u8 bdelayscan = false;
5284         u8 val8;
5285         u32     initialgain;
5286         u32     i;
5287
5288         if (pmlmeext->sitesurvey_res.state == SCAN_DISABLE) {
5289                 /* for first time sitesurvey_cmd */
5290                 rtw_hal_set_hwreg(padapter, HW_VAR_CHECK_TXBUF, NULL);
5291
5292                 pmlmeext->sitesurvey_res.state = SCAN_START;
5293                 pmlmeext->sitesurvey_res.bss_cnt = 0;
5294                 pmlmeext->sitesurvey_res.channel_idx = 0;
5295
5296                 for (i = 0; i < RTW_SSID_SCAN_AMOUNT; i++) {
5297                         if (pparm->ssid[i].SsidLength) {
5298                                 memcpy(pmlmeext->sitesurvey_res.ssid[i].Ssid, pparm->ssid[i].Ssid, IW_ESSID_MAX_SIZE);
5299                                 pmlmeext->sitesurvey_res.ssid[i].SsidLength = pparm->ssid[i].SsidLength;
5300                         } else {
5301                                 pmlmeext->sitesurvey_res.ssid[i].SsidLength = 0;
5302                         }
5303                 }
5304
5305                 pmlmeext->sitesurvey_res.ch_num = rtw_scan_ch_decision(padapter
5306                         , pmlmeext->sitesurvey_res.ch, RTW_CHANNEL_SCAN_AMOUNT
5307                         , pparm->ch, pparm->ch_num
5308         );
5309
5310                 pmlmeext->sitesurvey_res.scan_mode = pparm->scan_mode;
5311
5312                 /* issue null data if associating to the AP */
5313                 if (is_client_associated_to_ap(padapter)) {
5314                         pmlmeext->sitesurvey_res.state = SCAN_TXNULL;
5315
5316                         issue_nulldata(padapter, NULL, 1, 3, 500);
5317
5318                         bdelayscan = true;
5319                 }
5320                 if (bdelayscan) {
5321                         /* delay 50ms to protect nulldata(1). */
5322                         set_survey_timer(pmlmeext, 50);
5323                         return H2C_SUCCESS;
5324                 }
5325         }
5326
5327         if ((pmlmeext->sitesurvey_res.state == SCAN_START) || (pmlmeext->sitesurvey_res.state == SCAN_TXNULL)) {
5328                 /* disable dynamic functions, such as high power, DIG */
5329                 Save_DM_Func_Flag(padapter);
5330                 Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false);
5331
5332                 /* config the initial gain under scanning, need to write the BB registers */
5333                 initialgain = 0x1E;
5334
5335                 rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
5336
5337                 /* set MSR to no link state */
5338                 Set_MSR(padapter, _HW_STATE_NOLINK_);
5339
5340                 val8 = 1; /* under site survey */
5341                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_SITESURVEY, (u8 *)(&val8));
5342
5343                 pmlmeext->sitesurvey_res.state = SCAN_PROCESS;
5344         }
5345
5346         site_survey(padapter);
5347
5348         return H2C_SUCCESS;
5349 }
5350
5351 u8 setauth_hdl(struct adapter *padapter, unsigned char *pbuf)
5352 {
5353         struct setauth_parm             *pparm = (struct setauth_parm *)pbuf;
5354         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5355         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5356
5357         if (pparm->mode < 4)
5358                 pmlmeinfo->auth_algo = pparm->mode;
5359         return  H2C_SUCCESS;
5360 }
5361
5362 u8 setkey_hdl(struct adapter *padapter, u8 *pbuf)
5363 {
5364         unsigned short                          ctrl;
5365         struct setkey_parm              *pparm = (struct setkey_parm *)pbuf;
5366         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5367         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5368         unsigned char                                   null_sta[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
5369
5370         /* main tx key for wep. */
5371         if (pparm->set_tx)
5372                 pmlmeinfo->key_index = pparm->keyid;
5373
5374         /* write cam */
5375         ctrl = BIT(15) | ((pparm->algorithm) << 2) | pparm->keyid;
5376
5377         DBG_88E_LEVEL(_drv_info_, "set group key to hw: alg:%d(WEP40-1 WEP104-5 TKIP-2 AES-4) "
5378                         "keyid:%d\n", pparm->algorithm, pparm->keyid);
5379         write_cam(padapter, pparm->keyid, ctrl, null_sta, pparm->key);
5380
5381         return H2C_SUCCESS;
5382 }
5383
5384 u8 set_stakey_hdl(struct adapter *padapter, u8 *pbuf)
5385 {
5386         u16 ctrl = 0;
5387         u8 cam_id;/* cam_entry */
5388         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5389         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5390         struct set_stakey_parm  *pparm = (struct set_stakey_parm *)pbuf;
5391
5392         /* cam_entry: */
5393         /* 0~3 for default key */
5394
5395         /* for concurrent mode (ap+sta): */
5396         /* default key is disable, using sw encrypt/decrypt */
5397         /* cam_entry = 4 for sta mode (macid = 0) */
5398         /* cam_entry(macid+3) = 5 ~ N for ap mode (aid = 1~N, macid = 2 ~N) */
5399
5400         /* for concurrent mode (sta+sta): */
5401         /* default key is disable, using sw encrypt/decrypt */
5402         /* cam_entry = 4 mapping to macid = 0 */
5403         /* cam_entry = 5 mapping to macid = 2 */
5404
5405         cam_id = 4;
5406
5407         DBG_88E_LEVEL(_drv_info_, "set pairwise key to hw: alg:%d(WEP40-1 WEP104-5 TKIP-2 AES-4) camid:%d\n",
5408                       pparm->algorithm, cam_id);
5409         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
5410                 struct sta_info *psta;
5411                 struct sta_priv *pstapriv = &padapter->stapriv;
5412
5413                 if (pparm->algorithm == _NO_PRIVACY_)   /*  clear cam entry */ {
5414                         clear_cam_entry(padapter, pparm->id);
5415                         return H2C_SUCCESS_RSP;
5416                 }
5417
5418                 psta = rtw_get_stainfo(pstapriv, pparm->addr);
5419                 if (psta) {
5420                         ctrl = BIT(15) | ((pparm->algorithm) << 2);
5421
5422                         DBG_88E("r871x_set_stakey_hdl(): enc_algorithm=%d\n", pparm->algorithm);
5423
5424                         if ((psta->mac_id < 1) || (psta->mac_id > (NUM_STA-4))) {
5425                                 DBG_88E("r871x_set_stakey_hdl():set_stakey failed, mac_id(aid)=%d\n", psta->mac_id);
5426                                 return H2C_REJECTED;
5427                         }
5428
5429                         cam_id = psta->mac_id + 3;/* 0~3 for default key, cmd_id = macid + 3, macid = aid+1; */
5430
5431                         DBG_88E("Write CAM, mac_addr =%pM, cam_entry=%d\n",
5432                                 pparm->addr, cam_id);
5433
5434                         write_cam(padapter, cam_id, ctrl, pparm->addr, pparm->key);
5435
5436                         return H2C_SUCCESS_RSP;
5437                 } else {
5438                         DBG_88E("r871x_set_stakey_hdl(): sta has been free\n");
5439                         return H2C_REJECTED;
5440                 }
5441         }
5442
5443         /* below for sta mode */
5444
5445         if (pparm->algorithm == _NO_PRIVACY_) { /*  clear cam entry */
5446                 clear_cam_entry(padapter, pparm->id);
5447                 return H2C_SUCCESS;
5448         }
5449         ctrl = BIT(15) | ((pparm->algorithm) << 2);
5450         write_cam(padapter, cam_id, ctrl, pparm->addr, pparm->key);
5451         pmlmeinfo->enc_algo = pparm->algorithm;
5452         return H2C_SUCCESS;
5453 }
5454
5455 u8 add_ba_hdl(struct adapter *padapter, unsigned char *pbuf)
5456 {
5457         struct addBaReq_parm    *pparm = (struct addBaReq_parm *)pbuf;
5458         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5459         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5460
5461         struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, pparm->addr);
5462
5463         if (!psta)
5464                 return  H2C_SUCCESS;
5465
5466         if (((pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) && (pmlmeinfo->HT_enable)) ||
5467             ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)) {
5468                 issue_action_BA(padapter, pparm->addr, RTW_WLAN_ACTION_ADDBA_REQ, (u16)pparm->tid);
5469                 mod_timer(&psta->addba_retry_timer,
5470                           jiffies + msecs_to_jiffies(ADDBA_TO));
5471         } else {
5472                 psta->htpriv.candidate_tid_bitmap &= ~BIT(pparm->tid);
5473         }
5474         return  H2C_SUCCESS;
5475 }
5476
5477 u8 set_tx_beacon_cmd(struct adapter *padapter)
5478 {
5479         struct cmd_obj  *ph2c;
5480         struct wlan_bssid_ex    *ptxBeacon_parm;
5481         struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
5482         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5483         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5484         u8 res = _SUCCESS;
5485         int len_diff = 0;
5486
5487
5488         ph2c = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
5489         if (ph2c == NULL) {
5490                 res = _FAIL;
5491                 goto exit;
5492         }
5493
5494         ptxBeacon_parm = kmemdup(&(pmlmeinfo->network),
5495                                 sizeof(struct wlan_bssid_ex), GFP_KERNEL);
5496         if (ptxBeacon_parm == NULL) {
5497                 kfree(ph2c);
5498                 res = _FAIL;
5499                 goto exit;
5500         }
5501
5502         len_diff = update_hidden_ssid(ptxBeacon_parm->IEs+_BEACON_IE_OFFSET_,
5503                                       ptxBeacon_parm->IELength-_BEACON_IE_OFFSET_,
5504                                       pmlmeinfo->hidden_ssid_mode);
5505         ptxBeacon_parm->IELength += len_diff;
5506
5507         init_h2fwcmd_w_parm_no_rsp(ph2c, ptxBeacon_parm, GEN_CMD_CODE(_TX_Beacon));
5508
5509         res = rtw_enqueue_cmd(pcmdpriv, ph2c);
5510
5511
5512 exit:
5513
5514
5515         return res;
5516 }
5517
5518 u8 mlme_evt_hdl(struct adapter *padapter, unsigned char *pbuf)
5519 {
5520         u8 evt_code;
5521         u16 evt_sz;
5522         uint    *peventbuf;
5523         void (*event_callback)(struct adapter *dev, u8 *pbuf);
5524
5525         peventbuf = (uint *)pbuf;
5526         evt_sz = (u16)(*peventbuf&0xffff);
5527         evt_code = (u8)((*peventbuf>>16)&0xff);
5528
5529         /*  checking if event code is valid */
5530         if (evt_code >= MAX_C2HEVT) {
5531                 RT_TRACE(_module_rtl871x_cmd_c_, _drv_err_, ("\nEvent Code(%d) mismatch!\n", evt_code));
5532                 goto _abort_event_;
5533         }
5534
5535         /*  checking if event size match the event parm size */
5536         if ((wlanevents[evt_code].parmsize != 0) &&
5537             (wlanevents[evt_code].parmsize != evt_sz)) {
5538                 RT_TRACE(_module_rtl871x_cmd_c_, _drv_err_,
5539                          ("\nEvent(%d) Parm Size mismatch (%d vs %d)!\n",
5540                          evt_code, wlanevents[evt_code].parmsize, evt_sz));
5541                 goto _abort_event_;
5542         }
5543
5544         peventbuf += 2;
5545
5546         if (peventbuf) {
5547                 event_callback = wlanevents[evt_code].event_callback;
5548                 event_callback(padapter, (u8 *)peventbuf);
5549
5550         }
5551
5552 _abort_event_:
5553         return H2C_SUCCESS;
5554 }
5555
5556 u8 tx_beacon_hdl(struct adapter *padapter, unsigned char *pbuf)
5557 {
5558         if (send_beacon(padapter) == _FAIL) {
5559                 DBG_88E("issue_beacon, fail!\n");
5560                 return H2C_PARAMETERS_ERROR;
5561         }
5562 #ifdef CONFIG_88EU_AP_MODE
5563         else { /* tx bc/mc frames after update TIM */
5564                 struct sta_info *psta_bmc;
5565                 struct list_head *xmitframe_plist, *xmitframe_phead;
5566                 struct xmit_frame *pxmitframe = NULL;
5567                 struct sta_priv  *pstapriv = &padapter->stapriv;
5568
5569                 /* for BC/MC Frames */
5570                 psta_bmc = rtw_get_bcmc_stainfo(padapter);
5571                 if (!psta_bmc)
5572                         return H2C_SUCCESS;
5573
5574                 if ((pstapriv->tim_bitmap&BIT(0)) && (psta_bmc->sleepq_len > 0)) {
5575                         msleep(10);/*  10ms, ATIM(HIQ) Windows */
5576                         spin_lock_bh(&psta_bmc->sleep_q.lock);
5577
5578                         xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
5579                         xmitframe_plist = xmitframe_phead->next;
5580
5581                         while (xmitframe_phead != xmitframe_plist) {
5582                                 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
5583
5584                                 xmitframe_plist = xmitframe_plist->next;
5585
5586                                 list_del_init(&pxmitframe->list);
5587
5588                                 psta_bmc->sleepq_len--;
5589                                 if (psta_bmc->sleepq_len > 0)
5590                                         pxmitframe->attrib.mdata = 1;
5591                                 else
5592                                         pxmitframe->attrib.mdata = 0;
5593
5594                                 pxmitframe->attrib.triggered = 1;
5595
5596                                 pxmitframe->attrib.qsel = 0x11;/* HIQ */
5597
5598                                 spin_unlock_bh(&psta_bmc->sleep_q.lock);
5599                                 if (rtw_hal_xmit(padapter, pxmitframe))
5600                                         rtw_os_xmit_complete(padapter, pxmitframe);
5601                                 spin_lock_bh(&psta_bmc->sleep_q.lock);
5602                         }
5603                         spin_unlock_bh(&psta_bmc->sleep_q.lock);
5604                 }
5605         }
5606 #endif
5607         return H2C_SUCCESS;
5608 }
5609
5610 u8 set_ch_hdl(struct adapter *padapter, u8 *pbuf)
5611 {
5612         struct set_ch_parm *set_ch_parm;
5613         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5614
5615         if (!pbuf)
5616                 return H2C_PARAMETERS_ERROR;
5617
5618         set_ch_parm = (struct set_ch_parm *)pbuf;
5619
5620         DBG_88E(FUNC_NDEV_FMT" ch:%u, bw:%u, ch_offset:%u\n",
5621                 FUNC_NDEV_ARG(padapter->pnetdev),
5622                 set_ch_parm->ch, set_ch_parm->bw, set_ch_parm->ch_offset);
5623
5624         pmlmeext->cur_channel = set_ch_parm->ch;
5625         pmlmeext->cur_ch_offset = set_ch_parm->ch_offset;
5626         pmlmeext->cur_bwmode = set_ch_parm->bw;
5627
5628         set_channel_bwmode(padapter, set_ch_parm->ch, set_ch_parm->ch_offset, set_ch_parm->bw);
5629
5630         return  H2C_SUCCESS;
5631 }
5632
5633 u8 set_chplan_hdl(struct adapter *padapter, unsigned char *pbuf)
5634 {
5635         struct SetChannelPlan_param *setChannelPlan_param;
5636         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5637
5638         if (!pbuf)
5639                 return H2C_PARAMETERS_ERROR;
5640
5641         setChannelPlan_param = (struct SetChannelPlan_param *)pbuf;
5642
5643         pmlmeext->max_chan_nums = init_channel_set(padapter, setChannelPlan_param->channel_plan, pmlmeext->channel_set);
5644         init_channel_list(padapter, pmlmeext->channel_set, pmlmeext->max_chan_nums, &pmlmeext->channel_list);
5645
5646         return  H2C_SUCCESS;
5647 }