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