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