40f7ef78a7338d9f03563ab8cac145fa78e88815
[linux-2.6-microblaze.git] / drivers / staging / r8188eu / core / rtw_mlme_ext.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
3
4 #define _RTW_MLME_EXT_C_
5
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/wifi.h"
9 #include "../include/rtw_mlme_ext.h"
10 #include "../include/wlan_bssdef.h"
11 #include "../include/mlme_osdep.h"
12 #include "../include/recv_osdep.h"
13
14 static struct mlme_handler mlme_sta_tbl[] = {
15         {WIFI_ASSOCREQ,         "OnAssocReq",   &OnAssocReq},
16         {WIFI_ASSOCRSP,         "OnAssocRsp",   &OnAssocRsp},
17         {WIFI_REASSOCREQ,       "OnReAssocReq", &OnAssocReq},
18         {WIFI_REASSOCRSP,       "OnReAssocRsp", &OnAssocRsp},
19         {WIFI_PROBEREQ,         "OnProbeReq",   &OnProbeReq},
20         {WIFI_PROBERSP,         "OnProbeRsp",           &OnProbeRsp},
21
22         /*----------------------------------------------------------
23                                         below 2 are reserved
24         -----------------------------------------------------------*/
25         {0,                                     "DoReserved",           &DoReserved},
26         {0,                                     "DoReserved",           &DoReserved},
27         {WIFI_BEACON,           "OnBeacon",             &OnBeacon},
28         {WIFI_ATIM,                     "OnATIM",               &OnAtim},
29         {WIFI_DISASSOC,         "OnDisassoc",           &OnDisassoc},
30         {WIFI_AUTH,                     "OnAuth",               &OnAuthClient},
31         {WIFI_DEAUTH,           "OnDeAuth",             &OnDeAuth},
32         {WIFI_ACTION,           "OnAction",             &OnAction},
33 };
34
35 static struct action_handler OnAction_tbl[] = {
36         {RTW_WLAN_CATEGORY_SPECTRUM_MGMT,        "ACTION_SPECTRUM_MGMT", on_action_spct},
37         {RTW_WLAN_CATEGORY_QOS, "ACTION_QOS", &OnAction_qos},
38         {RTW_WLAN_CATEGORY_DLS, "ACTION_DLS", &OnAction_dls},
39         {RTW_WLAN_CATEGORY_BACK, "ACTION_BACK", &OnAction_back},
40         {RTW_WLAN_CATEGORY_PUBLIC, "ACTION_PUBLIC", on_action_public},
41         {RTW_WLAN_CATEGORY_RADIO_MEASUREMENT, "ACTION_RADIO_MEASUREMENT", &DoReserved},
42         {RTW_WLAN_CATEGORY_FT, "ACTION_FT",     &DoReserved},
43         {RTW_WLAN_CATEGORY_HT,  "ACTION_HT",    &OnAction_ht},
44         {RTW_WLAN_CATEGORY_SA_QUERY, "ACTION_SA_QUERY", &DoReserved},
45         {RTW_WLAN_CATEGORY_WMM, "ACTION_WMM", &OnAction_wmm},
46         {RTW_WLAN_CATEGORY_P2P, "ACTION_P2P", &OnAction_p2p},
47 };
48
49 static u8 null_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
50
51 /**************************************************
52 OUI definitions for the vendor specific IE
53 ***************************************************/
54 unsigned char   RTW_WPA_OUI[] = {0x00, 0x50, 0xf2, 0x01};
55 unsigned char WMM_OUI[] = {0x00, 0x50, 0xf2, 0x02};
56 unsigned char   WPS_OUI[] = {0x00, 0x50, 0xf2, 0x04};
57 unsigned char   P2P_OUI[] = {0x50, 0x6F, 0x9A, 0x09};
58 unsigned char   WFD_OUI[] = {0x50, 0x6F, 0x9A, 0x0A};
59
60 unsigned char   WMM_INFO_OUI[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
61 unsigned char   WMM_PARA_OUI[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
62
63 unsigned char WPA_TKIP_CIPHER[4] = {0x00, 0x50, 0xf2, 0x02};
64 unsigned char RSN_TKIP_CIPHER[4] = {0x00, 0x0f, 0xac, 0x02};
65
66 extern unsigned char REALTEK_96B_IE[];
67
68 /********************************************************
69 MCS rate definitions
70 *********************************************************/
71 unsigned char   MCS_rate_2R[16] = {0xff, 0xff, 0x0, 0x0, 0x01, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
72 unsigned char   MCS_rate_1R[16] = {0xff, 0x00, 0x0, 0x0, 0x01, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
73
74 /********************************************************
75 ChannelPlan definitions
76 *********************************************************/
77 static struct rt_channel_plan_2g RTW_ChannelPlan2G[RT_CHANNEL_DOMAIN_2G_MAX] = {
78         {{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 */
79         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 13},              /*  0x01, RT_CHANNEL_DOMAIN_2G_ETSI1 */
80         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 11},                      /*  0x02, RT_CHANNEL_DOMAIN_2G_FCC1 */
81         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 14},  /*  0x03, RT_CHANNEL_DOMAIN_2G_MIKK1 */
82         {{10, 11, 12, 13}, 4},                                  /*  0x04, RT_CHANNEL_DOMAIN_2G_ETSI2 */
83         {{}, 0},                                                                        /*  0x05, RT_CHANNEL_DOMAIN_2G_NULL */
84 };
85
86 static struct rt_channel_plan_map       RTW_ChannelPlanMap[RT_CHANNEL_DOMAIN_MAX] = {
87         /*  0x00 ~ 0x1F , Old Define ===== */
88         {0x02}, /* 0x00, RT_CHANNEL_DOMAIN_FCC */
89         {0x02}, /* 0x01, RT_CHANNEL_DOMAIN_IC */
90         {0x01}, /* 0x02, RT_CHANNEL_DOMAIN_ETSI */
91         {0x01}, /* 0x03, RT_CHANNEL_DOMAIN_SPAIN */
92         {0x01}, /* 0x04, RT_CHANNEL_DOMAIN_FRANCE */
93         {0x03}, /* 0x05, RT_CHANNEL_DOMAIN_MKK */
94         {0x03}, /* 0x06, RT_CHANNEL_DOMAIN_MKK1 */
95         {0x01}, /* 0x07, RT_CHANNEL_DOMAIN_ISRAEL */
96         {0x03}, /* 0x08, RT_CHANNEL_DOMAIN_TELEC */
97         {0x03}, /* 0x09, RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN */
98         {0x00}, /* 0x0A, RT_CHANNEL_DOMAIN_WORLD_WIDE_13 */
99         {0x02}, /* 0x0B, RT_CHANNEL_DOMAIN_TAIWAN */
100         {0x01}, /* 0x0C, RT_CHANNEL_DOMAIN_CHINA */
101         {0x02}, /* 0x0D, RT_CHANNEL_DOMAIN_SINGAPORE_INDIA_MEXICO */
102         {0x02}, /* 0x0E, RT_CHANNEL_DOMAIN_KOREA */
103         {0x02}, /* 0x0F, RT_CHANNEL_DOMAIN_TURKEY */
104         {0x01}, /* 0x10, RT_CHANNEL_DOMAIN_JAPAN */
105         {0x02}, /* 0x11, RT_CHANNEL_DOMAIN_FCC_NO_DFS */
106         {0x01}, /* 0x12, RT_CHANNEL_DOMAIN_JAPAN_NO_DFS */
107         {0x00}, /* 0x13, RT_CHANNEL_DOMAIN_WORLD_WIDE_5G */
108         {0x02}, /* 0x14, RT_CHANNEL_DOMAIN_TAIWAN_NO_DFS */
109         {0x00}, /* 0x15, RT_CHANNEL_DOMAIN_ETSI_NO_DFS */
110         {0x00}, /* 0x16, RT_CHANNEL_DOMAIN_KOREA_NO_DFS */
111         {0x03}, /* 0x17, RT_CHANNEL_DOMAIN_JAPAN_NO_DFS */
112         {0x05}, /* 0x18, RT_CHANNEL_DOMAIN_PAKISTAN_NO_DFS */
113         {0x02}, /* 0x19, RT_CHANNEL_DOMAIN_TAIWAN2_NO_DFS */
114         {0x00}, /* 0x1A, */
115         {0x00}, /* 0x1B, */
116         {0x00}, /* 0x1C, */
117         {0x00}, /* 0x1D, */
118         {0x00}, /* 0x1E, */
119         {0x05}, /* 0x1F, RT_CHANNEL_DOMAIN_WORLD_WIDE_ONLY_5G */
120         /*  0x20 ~ 0x7F , New Define ===== */
121         {0x00}, /* 0x20, RT_CHANNEL_DOMAIN_WORLD_NULL */
122         {0x01}, /* 0x21, RT_CHANNEL_DOMAIN_ETSI1_NULL */
123         {0x02}, /* 0x22, RT_CHANNEL_DOMAIN_FCC1_NULL */
124         {0x03}, /* 0x23, RT_CHANNEL_DOMAIN_MKK1_NULL */
125         {0x04}, /* 0x24, RT_CHANNEL_DOMAIN_ETSI2_NULL */
126         {0x02}, /* 0x25, RT_CHANNEL_DOMAIN_FCC1_FCC1 */
127         {0x00}, /* 0x26, RT_CHANNEL_DOMAIN_WORLD_ETSI1 */
128         {0x03}, /* 0x27, RT_CHANNEL_DOMAIN_MKK1_MKK1 */
129         {0x00}, /* 0x28, RT_CHANNEL_DOMAIN_WORLD_KCC1 */
130         {0x00}, /* 0x29, RT_CHANNEL_DOMAIN_WORLD_FCC2 */
131         {0x00}, /* 0x2A, */
132         {0x00}, /* 0x2B, */
133         {0x00}, /* 0x2C, */
134         {0x00}, /* 0x2D, */
135         {0x00}, /* 0x2E, */
136         {0x00}, /* 0x2F, */
137         {0x00}, /* 0x30, RT_CHANNEL_DOMAIN_WORLD_FCC3 */
138         {0x00}, /* 0x31, RT_CHANNEL_DOMAIN_WORLD_FCC4 */
139         {0x00}, /* 0x32, RT_CHANNEL_DOMAIN_WORLD_FCC5 */
140         {0x00}, /* 0x33, RT_CHANNEL_DOMAIN_WORLD_FCC6 */
141         {0x02}, /* 0x34, RT_CHANNEL_DOMAIN_FCC1_FCC7 */
142         {0x00}, /* 0x35, RT_CHANNEL_DOMAIN_WORLD_ETSI2 */
143         {0x00}, /* 0x36, RT_CHANNEL_DOMAIN_WORLD_ETSI3 */
144         {0x03}, /* 0x37, RT_CHANNEL_DOMAIN_MKK1_MKK2 */
145         {0x03}, /* 0x38, RT_CHANNEL_DOMAIN_MKK1_MKK3 */
146         {0x02}, /* 0x39, RT_CHANNEL_DOMAIN_FCC1_NCC1 */
147         {0x00}, /* 0x3A, */
148         {0x00}, /* 0x3B, */
149         {0x00}, /* 0x3C, */
150         {0x00}, /* 0x3D, */
151         {0x00}, /* 0x3E, */
152         {0x00}, /* 0x3F, */
153         {0x02}, /* 0x40, RT_CHANNEL_DOMAIN_FCC1_NCC2 */
154         {0x03}, /* 0x41, RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN_2G */
155 };
156
157 static struct rt_channel_plan_map RTW_CHANNEL_PLAN_MAP_REALTEK_DEFINE = {0x03}; /* use the conbination for max channel numbers */
158
159 /*
160  * Search the @param channel_num in given @param channel_set
161  * @ch_set: the given channel set
162  * @ch: the given channel number
163  *
164  * return the index of channel_num in channel_set, -1 if not found
165  */
166 int rtw_ch_set_search_ch(struct rt_channel_info *ch_set, const u32 ch)
167 {
168         int i;
169         for (i = 0; ch_set[i].ChannelNum != 0; i++) {
170                 if (ch == ch_set[i].ChannelNum)
171                         break;
172         }
173
174         if (i >= ch_set[i].ChannelNum)
175                 return -1;
176         return i;
177 }
178
179 /****************************************************************************
180
181 Following are the initialization functions for WiFi MLME
182
183 *****************************************************************************/
184
185 int init_hw_mlme_ext(struct adapter *padapter)
186 {
187         struct  mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
188
189         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
190         return _SUCCESS;
191 }
192
193 static void init_mlme_ext_priv_value(struct adapter *padapter)
194 {
195         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
196         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
197         unsigned char   mixed_datarate[NumRates] = {
198                 _1M_RATE_, _2M_RATE_, _5M_RATE_, _11M_RATE_, _6M_RATE_,
199                 _9M_RATE_, _12M_RATE_, _18M_RATE_, _24M_RATE_, _36M_RATE_,
200                  _48M_RATE_, _54M_RATE_, 0xff
201         };
202         unsigned char   mixed_basicrate[NumRates] = {
203                 _1M_RATE_, _2M_RATE_, _5M_RATE_, _11M_RATE_, _6M_RATE_,
204                 _12M_RATE_, _24M_RATE_, 0xff,
205         };
206
207         atomic_set(&pmlmeext->event_seq, 0);
208         pmlmeext->mgnt_seq = 0;/* reset to zero when disconnect at client mode */
209
210         pmlmeext->cur_channel = padapter->registrypriv.channel;
211         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
212         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
213         pmlmeext->retry = 0;
214
215         pmlmeext->cur_wireless_mode = padapter->registrypriv.wireless_mode;
216
217         memcpy(pmlmeext->datarate, mixed_datarate, NumRates);
218         memcpy(pmlmeext->basicrate, mixed_basicrate, NumRates);
219
220         pmlmeext->tx_rate = IEEE80211_CCK_RATE_1MB;
221
222         pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
223         pmlmeext->sitesurvey_res.channel_idx = 0;
224         pmlmeext->sitesurvey_res.bss_cnt = 0;
225         pmlmeext->scan_abort = false;
226
227         pmlmeinfo->state = WIFI_FW_NULL_STATE;
228         pmlmeinfo->reauth_count = 0;
229         pmlmeinfo->reassoc_count = 0;
230         pmlmeinfo->link_count = 0;
231         pmlmeinfo->auth_seq = 0;
232         pmlmeinfo->auth_algo = dot11AuthAlgrthm_Open;
233         pmlmeinfo->key_index = 0;
234         pmlmeinfo->iv = 0;
235
236         pmlmeinfo->enc_algo = _NO_PRIVACY_;
237         pmlmeinfo->authModeToggle = 0;
238
239         memset(pmlmeinfo->chg_txt, 0, 128);
240
241         pmlmeinfo->slotTime = SHORT_SLOT_TIME;
242         pmlmeinfo->preamble_mode = PREAMBLE_AUTO;
243
244         pmlmeinfo->dialogToken = 0;
245
246         pmlmeext->action_public_rxseq = 0xffff;
247         pmlmeext->action_public_dialog_token = 0xff;
248 }
249
250 static int has_channel(struct rt_channel_info *channel_set,
251                                            u8 chanset_size,
252                                            u8 chan) {
253         int i;
254
255         for (i = 0; i < chanset_size; i++) {
256                 if (channel_set[i].ChannelNum == chan)
257                         return 1;
258         }
259         return 0;
260 }
261
262 static void init_channel_list(struct adapter *padapter, struct rt_channel_info *channel_set,
263                                                           u8 chanset_size,
264                                                           struct p2p_channels *channel_list) {
265         struct p2p_oper_class_map op_class[] = {
266                 { IEEE80211G,  81,   1,  13,  1, BW20 },
267                 { IEEE80211G,  82,  14,  14,  1, BW20 },
268                 { -1, 0, 0, 0, 0, BW20 }
269         };
270
271         int cla, op;
272
273         cla = 0;
274
275         for (op = 0; op_class[op].op_class; op++) {
276                 u8 ch;
277                 struct p2p_oper_class_map *o = &op_class[op];
278                 struct p2p_reg_class *reg = NULL;
279
280                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
281                         if (!has_channel(channel_set, chanset_size, ch)) {
282                                 continue;
283                         }
284
285                         if ((0 == padapter->registrypriv.ht_enable) && (8 == o->inc))
286                                 continue;
287
288                         if ((0 == (padapter->registrypriv.cbw40_enable & BIT(1))) &&
289                             ((BW40MINUS == o->bw) || (BW40PLUS == o->bw)))
290                                 continue;
291
292                         if (!reg) {
293                                 reg = &channel_list->reg_class[cla];
294                                 cla++;
295                                 reg->reg_class = o->op_class;
296                                 reg->channels = 0;
297                         }
298                         reg->channel[reg->channels] = ch;
299                         reg->channels++;
300                 }
301         }
302         channel_list->reg_classes = cla;
303 }
304
305 static u8 init_channel_set(struct adapter *padapter, u8 ChannelPlan, struct rt_channel_info *channel_set)
306 {
307         u8 index, chanset_size = 0;
308         u8 b2_4GBand = false;
309         u8 Index2G = 0;
310
311         memset(channel_set, 0, sizeof(struct rt_channel_info) * MAX_CHANNEL_NUM);
312
313         if (ChannelPlan >= RT_CHANNEL_DOMAIN_MAX && ChannelPlan != RT_CHANNEL_DOMAIN_REALTEK_DEFINE) {
314                 DBG_88E("ChannelPlan ID %x error !!!!!\n", ChannelPlan);
315                 return chanset_size;
316         }
317
318         if (padapter->registrypriv.wireless_mode & WIRELESS_11G) {
319                 b2_4GBand = true;
320                 if (RT_CHANNEL_DOMAIN_REALTEK_DEFINE == ChannelPlan)
321                         Index2G = RTW_CHANNEL_PLAN_MAP_REALTEK_DEFINE.Index2G;
322                 else
323                         Index2G = RTW_ChannelPlanMap[ChannelPlan].Index2G;
324         }
325
326         if (b2_4GBand) {
327                 for (index = 0; index < RTW_ChannelPlan2G[Index2G].Len; index++) {
328                         channel_set[chanset_size].ChannelNum = RTW_ChannelPlan2G[Index2G].Channel[index];
329
330                         if ((RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN == ChannelPlan) ||/* Channel 1~11 is active, and 12~14 is passive */
331                             (RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN_2G == ChannelPlan)) {
332                                 if (channel_set[chanset_size].ChannelNum >= 1 && channel_set[chanset_size].ChannelNum <= 11)
333                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
334                                 else if ((channel_set[chanset_size].ChannelNum  >= 12 && channel_set[chanset_size].ChannelNum  <= 14))
335                                         channel_set[chanset_size].ScanType  = SCAN_PASSIVE;
336                         } else if (RT_CHANNEL_DOMAIN_WORLD_WIDE_13 == ChannelPlan ||
337                                    RT_CHANNEL_DOMAIN_2G_WORLD == Index2G) {/*  channel 12~13, passive scan */
338                                 if (channel_set[chanset_size].ChannelNum <= 11)
339                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
340                                 else
341                                         channel_set[chanset_size].ScanType = SCAN_PASSIVE;
342                         } else {
343                                 channel_set[chanset_size].ScanType = SCAN_ACTIVE;
344                         }
345
346                         chanset_size++;
347                 }
348         }
349         return chanset_size;
350 }
351
352 int     init_mlme_ext_priv(struct adapter *padapter)
353 {
354         int     res = _SUCCESS;
355         struct registry_priv *pregistrypriv = &padapter->registrypriv;
356         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
357         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
358         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
359
360         pmlmeext->padapter = padapter;
361
362         init_mlme_ext_priv_value(padapter);
363         pmlmeinfo->bAcceptAddbaReq = pregistrypriv->bAcceptAddbaReq;
364
365         init_mlme_ext_timer(padapter);
366
367 #ifdef CONFIG_88EU_AP_MODE
368         init_mlme_ap_info(padapter);
369 #endif
370
371         pmlmeext->max_chan_nums = init_channel_set(padapter, pmlmepriv->ChannelPlan, pmlmeext->channel_set);
372         init_channel_list(padapter, pmlmeext->channel_set, pmlmeext->max_chan_nums, &pmlmeext->channel_list);
373
374         pmlmeext->chan_scan_time = SURVEY_TO;
375         pmlmeext->mlmeext_init = true;
376
377         pmlmeext->active_keep_alive_check = true;
378
379         return res;
380 }
381
382 void free_mlme_ext_priv(struct mlme_ext_priv *pmlmeext)
383 {
384         struct adapter *padapter = pmlmeext->padapter;
385
386         if (!padapter)
387                 return;
388
389         if (padapter->bDriverStopped) {
390                 _cancel_timer_ex(&pmlmeext->survey_timer);
391                 _cancel_timer_ex(&pmlmeext->link_timer);
392                 /* _cancel_timer_ex(&pmlmeext->ADDBA_timer); */
393         }
394 }
395
396 static void _mgt_dispatcher(struct adapter *padapter, struct mlme_handler *ptable, struct recv_frame *precv_frame)
397 {
398         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
399         u8 *pframe = precv_frame->rx_data;
400
401           if (ptable->func) {
402          /* receive the frames that ra(a1) is my address or ra(a1) is bc address. */
403                 if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN) &&
404                     memcmp(GetAddr1Ptr(pframe), bc_addr, ETH_ALEN))
405                         return;
406                 ptable->func(padapter, precv_frame);
407         }
408 }
409
410 void mgt_dispatcher(struct adapter *padapter, struct recv_frame *precv_frame)
411 {
412         int index;
413         struct mlme_handler *ptable;
414 #ifdef CONFIG_88EU_AP_MODE
415         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
416 #endif /* CONFIG_88EU_AP_MODE */
417         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
418         u8 *pframe = precv_frame->rx_data;
419         struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(pframe));
420
421         if (GetFrameType(pframe) != WIFI_MGT_TYPE)
422                 return;
423
424         /* receive the frames that ra(a1) is my address or ra(a1) is bc address. */
425         if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN) &&
426             memcmp(GetAddr1Ptr(pframe), bc_addr, ETH_ALEN))
427                 return;
428
429         ptable = mlme_sta_tbl;
430
431         index = GetFrameSubType(pframe) >> 4;
432
433         if (index > 13)
434                 return;
435         ptable += index;
436
437         if (psta) {
438                 if (GetRetry(pframe)) {
439                         if (precv_frame->attrib.seq_num == psta->RxMgmtFrameSeqNum) {
440                                 /* drop the duplicate management frame */
441                                 DBG_88E("Drop duplicate management frame with seq_num=%d.\n", precv_frame->attrib.seq_num);
442                                 return;
443                         }
444                 }
445                 psta->RxMgmtFrameSeqNum = precv_frame->attrib.seq_num;
446         }
447
448 #ifdef CONFIG_88EU_AP_MODE
449         switch (GetFrameSubType(pframe)) {
450         case WIFI_AUTH:
451                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
452                         ptable->func = &OnAuth;
453                 else
454                         ptable->func = &OnAuthClient;
455                 fallthrough;
456         case WIFI_ASSOCREQ:
457         case WIFI_REASSOCREQ:
458                 _mgt_dispatcher(padapter, ptable, precv_frame);
459                 break;
460         case WIFI_PROBEREQ:
461                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
462                         _mgt_dispatcher(padapter, ptable, precv_frame);
463                 else
464                         _mgt_dispatcher(padapter, ptable, precv_frame);
465                 break;
466         case WIFI_BEACON:
467                 _mgt_dispatcher(padapter, ptable, precv_frame);
468                 break;
469         case WIFI_ACTION:
470                 _mgt_dispatcher(padapter, ptable, precv_frame);
471                 break;
472         default:
473                 _mgt_dispatcher(padapter, ptable, precv_frame);
474                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
475                         rtw_hostapd_mlme_rx(padapter, precv_frame);
476                 break;
477         }
478 #else
479         _mgt_dispatcher(padapter, ptable, precv_frame);
480 #endif
481 }
482
483 #ifdef CONFIG_88EU_P2P
484 static u32 p2p_listen_state_process(struct adapter *padapter, unsigned char *da)
485 {
486         bool response = true;
487
488         /*      do nothing if the device name is empty */
489         if (!padapter->wdinfo.device_name_len)
490                 response = false;
491
492         if (response)
493                 issue_probersp_p2p(padapter, da);
494
495         return _SUCCESS;
496 }
497 #endif /* CONFIG_88EU_P2P */
498
499 /****************************************************************************
500
501 Following are the callback functions for each subtype of the management frames
502
503 *****************************************************************************/
504
505 unsigned int OnProbeReq(struct adapter *padapter, struct recv_frame *precv_frame)
506 {
507         unsigned int    ielen;
508         unsigned char   *p;
509         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
510         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
511         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
512         struct wlan_bssid_ex *cur = &(pmlmeinfo->network);
513         u8 *pframe = precv_frame->rx_data;
514         uint len = precv_frame->len;
515         u8 is_valid_p2p_probereq = false;
516
517 #ifdef CONFIG_88EU_P2P
518         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
519         u8 wifi_test_chk_rate = 1;
520
521         if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE) &&
522             !rtw_p2p_chk_state(pwdinfo, P2P_STATE_IDLE) &&
523             !rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT) &&
524             !rtw_p2p_chk_state(pwdinfo, P2P_STATE_FIND_PHASE_SEARCH) &&
525             !rtw_p2p_chk_state(pwdinfo, P2P_STATE_SCAN)) {
526                 /*      mcs_rate = 0 -> CCK 1M rate */
527                 /*      mcs_rate = 1 -> CCK 2M rate */
528                 /*      mcs_rate = 2 -> CCK 5.5M rate */
529                 /*      mcs_rate = 3 -> CCK 11M rate */
530                 /*      In the P2P mode, the driver should not support the CCK rate */
531
532                 /*      Commented by Kurt 2012/10/16 */
533                 /*      IOT issue: Google Nexus7 use 1M rate to send p2p_probe_req after GO nego completed and Nexus7 is client */
534                 if (wifi_test_chk_rate == 1) {
535                         is_valid_p2p_probereq = process_probe_req_p2p_ie(pwdinfo, pframe, len);
536                         if (is_valid_p2p_probereq) {
537                                 if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_DEVICE)) {
538                                         /*  FIXME */
539                                         report_survey_event(padapter, precv_frame);
540                                         p2p_listen_state_process(padapter,  get_sa(pframe));
541
542                                         return _SUCCESS;
543                                 }
544
545                                 if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO))
546                                         goto _continue;
547                         }
548                 }
549         }
550
551 _continue:
552 #endif /* CONFIG_88EU_P2P */
553
554         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
555                 return _SUCCESS;
556
557         if (!check_fwstate(pmlmepriv, _FW_LINKED) &&
558             !check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_AP_STATE))
559                 return _SUCCESS;
560
561         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _PROBEREQ_IE_OFFSET_, _SSID_IE_, (int *)&ielen,
562                         len - WLAN_HDR_A3_LEN - _PROBEREQ_IE_OFFSET_);
563
564         /* check (wildcard) SSID */
565         if (p) {
566                 if (is_valid_p2p_probereq)
567                         goto _issue_probersp;
568
569                 if ((ielen != 0 && memcmp((void *)(p+2), (void *)cur->Ssid.Ssid, cur->Ssid.SsidLength)) ||
570                     (ielen == 0 && pmlmeinfo->hidden_ssid_mode))
571                         return _SUCCESS;
572
573 _issue_probersp:
574
575                 if (check_fwstate(pmlmepriv, _FW_LINKED) &&
576                     (pmlmepriv->cur_network.join_res ||
577                     check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)))
578                         issue_probersp(padapter, get_sa(pframe), is_valid_p2p_probereq);
579         }
580         return _SUCCESS;
581 }
582
583 unsigned int OnProbeRsp(struct adapter *padapter, struct recv_frame *precv_frame)
584 {
585         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
586 #ifdef CONFIG_88EU_P2P
587         struct wifidirect_info  *pwdinfo = &padapter->wdinfo;
588         u8 *pframe = precv_frame->rx_data;
589 #endif
590
591 #ifdef CONFIG_88EU_P2P
592         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_TX_PROVISION_DIS_REQ)) {
593                 if (pwdinfo->tx_prov_disc_info.benable) {
594                         if (!memcmp(pwdinfo->tx_prov_disc_info.peerIFAddr, GetAddr2Ptr(pframe), ETH_ALEN)) {
595                                 if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT)) {
596                                         pwdinfo->tx_prov_disc_info.benable = false;
597                                         issue_p2p_provision_request(padapter,
598                                                                     pwdinfo->tx_prov_disc_info.ssid.Ssid,
599                                                                     pwdinfo->tx_prov_disc_info.ssid.SsidLength,
600                                                                     pwdinfo->tx_prov_disc_info.peerDevAddr);
601                                 } else if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_DEVICE) || rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) {
602                                         pwdinfo->tx_prov_disc_info.benable = false;
603                                         issue_p2p_provision_request(padapter, NULL, 0,
604                                                                     pwdinfo->tx_prov_disc_info.peerDevAddr);
605                                 }
606                         }
607                 }
608                 return _SUCCESS;
609         } else if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_GONEGO_ING)) {
610                 if (pwdinfo->nego_req_info.benable) {
611                         DBG_88E("[%s] P2P State is GONEGO ING!\n", __func__);
612                         if (!memcmp(pwdinfo->nego_req_info.peerDevAddr, GetAddr2Ptr(pframe), ETH_ALEN)) {
613                                 pwdinfo->nego_req_info.benable = false;
614                                 issue_p2p_GO_request(padapter, pwdinfo->nego_req_info.peerDevAddr);
615                         }
616                 }
617         } else if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_TX_INVITE_REQ)) {
618                 if (pwdinfo->invitereq_info.benable) {
619                         DBG_88E("[%s] P2P_STATE_TX_INVITE_REQ!\n", __func__);
620                         if (!memcmp(pwdinfo->invitereq_info.peer_macaddr, GetAddr2Ptr(pframe), ETH_ALEN)) {
621                                 pwdinfo->invitereq_info.benable = false;
622                                 issue_p2p_invitation_request(padapter, pwdinfo->invitereq_info.peer_macaddr);
623                         }
624                 }
625         }
626 #endif
627
628         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
629                 report_survey_event(padapter, precv_frame);
630                 return _SUCCESS;
631         }
632
633         return _SUCCESS;
634 }
635
636 unsigned int OnBeacon(struct adapter *padapter, struct recv_frame *precv_frame)
637 {
638         int cam_idx;
639         struct sta_info *psta;
640         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
641         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
642         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
643         struct sta_priv *pstapriv = &padapter->stapriv;
644         u8 *pframe = precv_frame->rx_data;
645         uint len = precv_frame->len;
646         struct wlan_bssid_ex *pbss;
647         int ret = _SUCCESS;
648
649         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
650                 report_survey_event(padapter, precv_frame);
651                 return _SUCCESS;
652         }
653
654         if (!memcmp(GetAddr3Ptr(pframe), get_my_bssid(&pmlmeinfo->network), ETH_ALEN)) {
655                 if (pmlmeinfo->state & WIFI_FW_AUTH_NULL) {
656                         /* we should update current network before auth, or some IE is wrong */
657                         pbss = kmalloc(sizeof(struct wlan_bssid_ex), GFP_ATOMIC);
658                         if (pbss) {
659                                 if (collect_bss_info(padapter, precv_frame, pbss) == _SUCCESS) {
660                                         update_network(&(pmlmepriv->cur_network.network), pbss, padapter, true);
661                                         rtw_get_bcn_info(&(pmlmepriv->cur_network));
662                                 }
663                                 kfree(pbss);
664                         }
665
666                         /* check the vendor of the assoc AP */
667                         pmlmeinfo->assoc_AP_vendor = check_assoc_AP(pframe+sizeof(struct rtw_ieee80211_hdr_3addr), len-sizeof(struct rtw_ieee80211_hdr_3addr));
668
669                         /* update TSF Value */
670                         update_TSF(pmlmeext, pframe, len);
671
672                         /* start auth */
673                         start_clnt_auth(padapter);
674
675                         return _SUCCESS;
676                 }
677
678                 if (((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) && (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)) {
679                         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
680                         if (psta) {
681                                 ret = rtw_check_bcn_info(padapter, pframe, len);
682                                 if (!ret) {
683                                                 DBG_88E_LEVEL(_drv_info_, "ap has changed, disconnect now\n ");
684                                                 receive_disconnect(padapter, pmlmeinfo->network.MacAddress, 0);
685                                                 return _SUCCESS;
686                                 }
687                                 /* update WMM, ERP in the beacon */
688                                 /* todo: the timer is used instead of the number of the beacon received */
689                                 if ((sta_rx_pkts(psta) & 0xf) == 0)
690                                         update_beacon_info(padapter, pframe, len, psta);
691                                 process_p2p_ps_ie(padapter, (pframe + WLAN_HDR_A3_LEN), (len - WLAN_HDR_A3_LEN));
692                         }
693                 } else if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
694                         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
695                         if (psta) {
696                                 /* update WMM, ERP in the beacon */
697                                 /* todo: the timer is used instead of the number of the beacon received */
698                                 if ((sta_rx_pkts(psta) & 0xf) == 0)
699                                         update_beacon_info(padapter, pframe, len, psta);
700                         } else {
701                                 /* allocate a new CAM entry for IBSS station */
702                                 cam_idx = allocate_fw_sta_entry(padapter);
703                                 if (cam_idx == NUM_STA)
704                                         goto _END_ONBEACON_;
705
706                                 /* get supported rate */
707                                 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) {
708                                         pmlmeinfo->FW_sta_info[cam_idx].status = 0;
709                                         goto _END_ONBEACON_;
710                                 }
711
712                                 /* update TSF Value */
713                                 update_TSF(pmlmeext, pframe, len);
714
715                                 /* report sta add event */
716                                 report_add_sta_event(padapter, GetAddr2Ptr(pframe), cam_idx);
717                         }
718                 }
719         }
720
721 _END_ONBEACON_:
722
723         return _SUCCESS;
724 }
725
726 unsigned int OnAuth(struct adapter *padapter, struct recv_frame *precv_frame)
727 {
728 #ifdef CONFIG_88EU_AP_MODE
729         unsigned int    auth_mode, ie_len;
730         u16 seq;
731         unsigned char   *sa, *p;
732         u16 algorithm;
733         int     status;
734         static struct sta_info stat;
735         struct  sta_info        *pstat = NULL;
736         struct  sta_priv *pstapriv = &padapter->stapriv;
737         struct security_priv *psecuritypriv = &padapter->securitypriv;
738         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
739         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
740         u8 *pframe = precv_frame->rx_data;
741         uint len = precv_frame->len;
742
743         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
744                 return _FAIL;
745
746         DBG_88E("+OnAuth\n");
747
748         sa = GetAddr2Ptr(pframe);
749
750         auth_mode = psecuritypriv->dot11AuthAlgrthm;
751         seq = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + 2));
752         algorithm = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN));
753
754         DBG_88E("auth alg=%x, seq=%X\n", algorithm, seq);
755
756         if (auth_mode == 2 && psecuritypriv->dot11PrivacyAlgrthm != _WEP40_ &&
757             psecuritypriv->dot11PrivacyAlgrthm != _WEP104_)
758                 auth_mode = 0;
759
760         if ((algorithm > 0 && auth_mode == 0) ||        /*  rx a shared-key auth but shared not enabled */
761             (algorithm == 0 && auth_mode == 1)) {       /*  rx a open-system auth but shared-key is enabled */
762                 DBG_88E("auth rejected due to bad alg [alg=%d, auth_mib=%d] %02X%02X%02X%02X%02X%02X\n",
763                         algorithm, auth_mode, sa[0], sa[1], sa[2], sa[3], sa[4], sa[5]);
764
765                 status = _STATS_NO_SUPP_ALG_;
766
767                 goto auth_fail;
768         }
769
770         if (!rtw_access_ctrl(padapter, sa)) {
771                 status = _STATS_UNABLE_HANDLE_STA_;
772                 goto auth_fail;
773         }
774
775         pstat = rtw_get_stainfo(pstapriv, sa);
776         if (!pstat) {
777                 /*  allocate a new one */
778                 DBG_88E("going to alloc stainfo for sa=%pM\n", sa);
779                 pstat = rtw_alloc_stainfo(pstapriv, sa);
780                 if (!pstat) {
781                         DBG_88E(" Exceed the upper limit of supported clients...\n");
782                         status = _STATS_UNABLE_HANDLE_STA_;
783                         goto auth_fail;
784                 }
785
786                 pstat->state = WIFI_FW_AUTH_NULL;
787                 pstat->auth_seq = 0;
788         } else {
789                 spin_lock_bh(&pstapriv->asoc_list_lock);
790                 if (!list_empty(&pstat->asoc_list)) {
791                         list_del_init(&pstat->asoc_list);
792                         pstapriv->asoc_list_cnt--;
793                 }
794                 spin_unlock_bh(&pstapriv->asoc_list_lock);
795
796                 if (seq == 1) {
797                         /* TODO: STA re_auth and auth timeout */
798                 }
799         }
800
801         spin_lock_bh(&pstapriv->auth_list_lock);
802         if (list_empty(&pstat->auth_list)) {
803                 list_add_tail(&pstat->auth_list, &pstapriv->auth_list);
804                 pstapriv->auth_list_cnt++;
805         }
806         spin_unlock_bh(&pstapriv->auth_list_lock);
807
808         if (pstat->auth_seq == 0)
809                 pstat->expire_to = pstapriv->auth_to;
810
811         if ((pstat->auth_seq + 1) != seq) {
812                 DBG_88E("(1)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
813                         seq, pstat->auth_seq+1);
814                 status = _STATS_OUT_OF_AUTH_SEQ_;
815                 goto auth_fail;
816         }
817
818         if (algorithm == 0 && (auth_mode == 0 || auth_mode == 2)) {
819                 if (seq == 1) {
820                         pstat->state &= ~WIFI_FW_AUTH_NULL;
821                         pstat->state |= WIFI_FW_AUTH_SUCCESS;
822                         pstat->expire_to = pstapriv->assoc_to;
823                         pstat->authalg = algorithm;
824                 } else {
825                         DBG_88E("(2)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
826                                 seq, pstat->auth_seq+1);
827                         status = _STATS_OUT_OF_AUTH_SEQ_;
828                         goto auth_fail;
829                 }
830         } else { /*  shared system or auto authentication */
831                 if (seq == 1) {
832                         /* prepare for the challenging txt... */
833
834                         pstat->state &= ~WIFI_FW_AUTH_NULL;
835                         pstat->state |= WIFI_FW_AUTH_STATE;
836                         pstat->authalg = algorithm;
837                         pstat->auth_seq = 2;
838                 } else if (seq == 3) {
839                         /* checking for challenging txt... */
840                         DBG_88E("checking for challenging txt...\n");
841
842                         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + 4 + _AUTH_IE_OFFSET_, _CHLGETXT_IE_, (int *)&ie_len,
843                                         len - WLAN_HDR_A3_LEN - _AUTH_IE_OFFSET_ - 4);
844
845                         if (!p || ie_len <= 0) {
846                                 DBG_88E("auth rejected because challenge failure!(1)\n");
847                                 status = _STATS_CHALLENGE_FAIL_;
848                                 goto auth_fail;
849                         }
850
851                         if (!memcmp((void *)(p + 2), pstat->chg_txt, 128)) {
852                                 pstat->state &= (~WIFI_FW_AUTH_STATE);
853                                 pstat->state |= WIFI_FW_AUTH_SUCCESS;
854                                 /*  challenging txt is correct... */
855                                 pstat->expire_to =  pstapriv->assoc_to;
856                         } else {
857                                 DBG_88E("auth rejected because challenge failure!\n");
858                                 status = _STATS_CHALLENGE_FAIL_;
859                                 goto auth_fail;
860                         }
861                 } else {
862                         DBG_88E("(3)auth rejected because out of seq [rx_seq=%d, exp_seq=%d]!\n",
863                                 seq, pstat->auth_seq+1);
864                         status = _STATS_OUT_OF_AUTH_SEQ_;
865                         goto auth_fail;
866                 }
867         }
868
869         /*  Now, we are going to issue_auth... */
870         pstat->auth_seq = seq + 1;
871
872 #ifdef CONFIG_88EU_AP_MODE
873         issue_auth(padapter, pstat, (unsigned short)(_STATS_SUCCESSFUL_));
874 #endif
875
876         if (pstat->state & WIFI_FW_AUTH_SUCCESS)
877                 pstat->auth_seq = 0;
878
879         return _SUCCESS;
880
881 auth_fail:
882
883         if (pstat)
884                 rtw_free_stainfo(padapter, pstat);
885
886         pstat = &stat;
887         memset((char *)pstat, '\0', sizeof(stat));
888         pstat->auth_seq = 2;
889         memcpy(pstat->hwaddr, sa, 6);
890
891 #ifdef CONFIG_88EU_AP_MODE
892         issue_auth(padapter, pstat, (unsigned short)status);
893 #endif
894
895 #endif
896         return _FAIL;
897 }
898
899 unsigned int OnAuthClient(struct adapter *padapter, struct recv_frame *precv_frame)
900 {
901         unsigned int    seq, len, status, offset;
902         unsigned char   *p;
903         unsigned int    go2asoc = 0;
904         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
905         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
906         u8 *pframe = precv_frame->rx_data;
907         uint pkt_len = precv_frame->len;
908
909         DBG_88E("%s\n", __func__);
910
911         /* check A1 matches or not */
912         if (memcmp(myid(&(padapter->eeprompriv)), get_da(pframe), ETH_ALEN))
913                 return _SUCCESS;
914
915         if (!(pmlmeinfo->state & WIFI_FW_AUTH_STATE))
916                 return _SUCCESS;
917
918         offset = (GetPrivacy(pframe)) ? 4 : 0;
919
920         seq     = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + offset + 2));
921         status  = le16_to_cpu(*(__le16 *)((size_t)pframe + WLAN_HDR_A3_LEN + offset + 4));
922
923         if (status != 0) {
924                 DBG_88E("clnt auth fail, status: %d\n", status);
925                 if (status == 13) { /*  pmlmeinfo->auth_algo == dot11AuthAlgrthm_Auto) */
926                         if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared)
927                                 pmlmeinfo->auth_algo = dot11AuthAlgrthm_Open;
928                         else
929                                 pmlmeinfo->auth_algo = dot11AuthAlgrthm_Shared;
930                 }
931
932                 set_link_timer(pmlmeext, 1);
933                 goto authclnt_fail;
934         }
935
936         if (seq == 2) {
937                 if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared) {
938                          /*  legendary shared system */
939                         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _AUTH_IE_OFFSET_, _CHLGETXT_IE_, (int *)&len,
940                                 pkt_len - WLAN_HDR_A3_LEN - _AUTH_IE_OFFSET_);
941
942                         if (!p)
943                                 goto authclnt_fail;
944
945                         memcpy((void *)(pmlmeinfo->chg_txt), (void *)(p + 2), len);
946                         pmlmeinfo->auth_seq = 3;
947                         issue_auth(padapter, NULL, 0);
948                         set_link_timer(pmlmeext, REAUTH_TO);
949
950                         return _SUCCESS;
951                 } else {
952                         /*  open system */
953                         go2asoc = 1;
954                 }
955         } else if (seq == 4) {
956                 if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared)
957                         go2asoc = 1;
958                 else
959                         goto authclnt_fail;
960         } else {
961                 /*  this is also illegal */
962                 goto authclnt_fail;
963         }
964
965         if (go2asoc) {
966                 DBG_88E_LEVEL(_drv_info_, "auth success, start assoc\n");
967                 start_clnt_assoc(padapter);
968                 return _SUCCESS;
969         }
970 authclnt_fail:
971         return _FAIL;
972 }
973
974 unsigned int OnAssocReq(struct adapter *padapter, struct recv_frame *precv_frame)
975 {
976 #ifdef CONFIG_88EU_AP_MODE
977         u16 capab_info;
978         struct rtw_ieee802_11_elems elems;
979         struct sta_info *pstat;
980         unsigned char           reassoc, *p, *pos, *wpa_ie;
981         unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
982         int             i, ie_len, wpa_ie_len, left;
983         unsigned char           supportRate[16];
984         int                                     supportRateNum;
985         unsigned short          status = _STATS_SUCCESSFUL_;
986         unsigned short          frame_type, ie_offset = 0;
987         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
988         struct security_priv *psecuritypriv = &padapter->securitypriv;
989         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
990         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
991         struct wlan_bssid_ex *cur = &(pmlmeinfo->network);
992         struct sta_priv *pstapriv = &padapter->stapriv;
993         u8 *pframe = precv_frame->rx_data;
994         uint pkt_len = precv_frame->len;
995 #ifdef CONFIG_88EU_P2P
996         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
997         u8 p2p_status_code = P2P_STATUS_SUCCESS;
998         u8 *p2pie;
999         u32 p2pielen = 0;
1000 #endif /* CONFIG_88EU_P2P */
1001
1002         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
1003                 return _FAIL;
1004
1005         frame_type = GetFrameSubType(pframe);
1006         if (frame_type == WIFI_ASSOCREQ) {
1007                 reassoc = 0;
1008                 ie_offset = _ASOCREQ_IE_OFFSET_;
1009         } else { /*  WIFI_REASSOCREQ */
1010                 reassoc = 1;
1011                 ie_offset = _REASOCREQ_IE_OFFSET_;
1012         }
1013
1014         if (pkt_len < IEEE80211_3ADDR_LEN + ie_offset) {
1015                 DBG_88E("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
1016                        "\n", reassoc, (unsigned long)pkt_len);
1017                 return _FAIL;
1018         }
1019
1020         pstat = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1021         if (pstat == (struct sta_info *)NULL) {
1022                 status = _RSON_CLS2_;
1023                 goto asoc_class2_error;
1024         }
1025
1026         capab_info = get_unaligned_le16(pframe + WLAN_HDR_A3_LEN);
1027
1028         left = pkt_len - (IEEE80211_3ADDR_LEN + ie_offset);
1029         pos = pframe + (IEEE80211_3ADDR_LEN + ie_offset);
1030
1031         DBG_88E("%s\n", __func__);
1032
1033         /*  check if this stat has been successfully authenticated/assocated */
1034         if (!((pstat->state) & WIFI_FW_AUTH_SUCCESS)) {
1035                 if (!((pstat->state) & WIFI_FW_ASSOC_SUCCESS)) {
1036                         status = _RSON_CLS2_;
1037                         goto asoc_class2_error;
1038                 } else {
1039                         pstat->state &= (~WIFI_FW_ASSOC_SUCCESS);
1040                         pstat->state |= WIFI_FW_ASSOC_STATE;
1041                 }
1042         } else {
1043                 pstat->state &= (~WIFI_FW_AUTH_SUCCESS);
1044                 pstat->state |= WIFI_FW_ASSOC_STATE;
1045         }
1046         pstat->capability = capab_info;
1047         /* now parse all ieee802_11 ie to point to elems */
1048         if (rtw_ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
1049             !elems.ssid) {
1050                 DBG_88E("STA %pM sent invalid association request\n",
1051                         pstat->hwaddr);
1052                 status = _STATS_FAILURE_;
1053                 goto OnAssocReqFail;
1054         }
1055
1056         /*  now we should check all the fields... */
1057         /*  checking SSID */
1058         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _SSID_IE_, &ie_len,
1059                 pkt_len - WLAN_HDR_A3_LEN - ie_offset);
1060         if (!p)
1061                 status = _STATS_FAILURE_;
1062
1063         if (ie_len == 0) { /*  broadcast ssid, however it is not allowed in assocreq */
1064                 status = _STATS_FAILURE_;
1065         } else {
1066                 /*  check if ssid match */
1067                 if (memcmp((void *)(p+2), cur->Ssid.Ssid, cur->Ssid.SsidLength))
1068                         status = _STATS_FAILURE_;
1069
1070                 if (ie_len != cur->Ssid.SsidLength)
1071                         status = _STATS_FAILURE_;
1072         }
1073
1074         if (_STATS_SUCCESSFUL_ != status)
1075                 goto OnAssocReqFail;
1076
1077         /*  check if the supported rate is ok */
1078         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _SUPPORTEDRATES_IE_, &ie_len, pkt_len - WLAN_HDR_A3_LEN - ie_offset);
1079         if (!p) {
1080                 DBG_88E("Rx a sta assoc-req which supported rate is empty!\n");
1081                 /*  use our own rate set as statoin used */
1082                 /* memcpy(supportRate, AP_BSSRATE, AP_BSSRATE_LEN); */
1083                 /* supportRateNum = AP_BSSRATE_LEN; */
1084
1085                 status = _STATS_FAILURE_;
1086                 goto OnAssocReqFail;
1087         } else {
1088                 memcpy(supportRate, p+2, ie_len);
1089                 supportRateNum = ie_len;
1090
1091                 p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _EXT_SUPPORTEDRATES_IE_, &ie_len,
1092                                 pkt_len - WLAN_HDR_A3_LEN - ie_offset);
1093                 if (p) {
1094                         if (supportRateNum <= sizeof(supportRate)) {
1095                                 memcpy(supportRate+supportRateNum, p+2, ie_len);
1096                                 supportRateNum += ie_len;
1097                         }
1098                 }
1099         }
1100
1101         /* todo: mask supportRate between AP & STA -> move to update raid */
1102         /* get_matched_rate(pmlmeext, supportRate, &supportRateNum, 0); */
1103
1104         /* update station supportRate */
1105         pstat->bssratelen = supportRateNum;
1106         memcpy(pstat->bssrateset, supportRate, supportRateNum);
1107         UpdateBrateTblForSoftAP(pstat->bssrateset, pstat->bssratelen);
1108
1109         /* check RSN/WPA/WPS */
1110         pstat->dot8021xalg = 0;
1111         pstat->wpa_psk = 0;
1112         pstat->wpa_group_cipher = 0;
1113         pstat->wpa2_group_cipher = 0;
1114         pstat->wpa_pairwise_cipher = 0;
1115         pstat->wpa2_pairwise_cipher = 0;
1116         memset(pstat->wpa_ie, 0, sizeof(pstat->wpa_ie));
1117         if ((psecuritypriv->wpa_psk & BIT(1)) && elems.rsn_ie) {
1118                 int group_cipher = 0, pairwise_cipher = 0;
1119
1120                 wpa_ie = elems.rsn_ie;
1121                 wpa_ie_len = elems.rsn_ie_len;
1122
1123                 if (rtw_parse_wpa2_ie(wpa_ie-2, wpa_ie_len+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1124                         pstat->dot8021xalg = 1;/* psk,  todo:802.1x */
1125                         pstat->wpa_psk |= BIT(1);
1126
1127                         pstat->wpa2_group_cipher = group_cipher&psecuritypriv->wpa2_group_cipher;
1128                         pstat->wpa2_pairwise_cipher = pairwise_cipher&psecuritypriv->wpa2_pairwise_cipher;
1129
1130                         if (!pstat->wpa2_group_cipher)
1131                                 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1132
1133                         if (!pstat->wpa2_pairwise_cipher)
1134                                 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1135                 } else {
1136                         status = WLAN_STATUS_INVALID_IE;
1137                 }
1138         } else if ((psecuritypriv->wpa_psk & BIT(0)) && elems.wpa_ie) {
1139                 int group_cipher = 0, pairwise_cipher = 0;
1140
1141                 wpa_ie = elems.wpa_ie;
1142                 wpa_ie_len = elems.wpa_ie_len;
1143
1144                 if (rtw_parse_wpa_ie(wpa_ie-2, wpa_ie_len+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1145                         pstat->dot8021xalg = 1;/* psk,  todo:802.1x */
1146                         pstat->wpa_psk |= BIT(0);
1147
1148                         pstat->wpa_group_cipher = group_cipher&psecuritypriv->wpa_group_cipher;
1149                         pstat->wpa_pairwise_cipher = pairwise_cipher&psecuritypriv->wpa_pairwise_cipher;
1150
1151                         if (!pstat->wpa_group_cipher)
1152                                 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1153
1154                         if (!pstat->wpa_pairwise_cipher)
1155                                 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1156                 } else {
1157                         status = WLAN_STATUS_INVALID_IE;
1158                 }
1159         } else {
1160                 wpa_ie = NULL;
1161                 wpa_ie_len = 0;
1162         }
1163
1164         if (_STATS_SUCCESSFUL_ != status)
1165                 goto OnAssocReqFail;
1166
1167         pstat->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
1168         if (!wpa_ie) {
1169                 if (elems.wps_ie) {
1170                         DBG_88E("STA included WPS IE in "
1171                                    "(Re)Association Request - assume WPS is "
1172                                    "used\n");
1173                         pstat->flags |= WLAN_STA_WPS;
1174                         /* wpabuf_free(sta->wps_ie); */
1175                         /* sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4, */
1176                         /*                              elems.wps_ie_len - 4); */
1177                 } else {
1178                         DBG_88E("STA did not include WPA/RSN IE "
1179                                    "in (Re)Association Request - possible WPS "
1180                                    "use\n");
1181                         pstat->flags |= WLAN_STA_MAYBE_WPS;
1182                 }
1183
1184                 /*  AP support WPA/RSN, and sta is going to do WPS, but AP is not ready */
1185                 /*  that the selected registrar of AP is _FLASE */
1186                 if ((psecuritypriv->wpa_psk > 0) && (pstat->flags & (WLAN_STA_WPS|WLAN_STA_MAYBE_WPS))) {
1187                         if (pmlmepriv->wps_beacon_ie) {
1188                                 u8 selected_registrar = 0;
1189
1190                                 rtw_get_wps_attr_content(pmlmepriv->wps_beacon_ie, pmlmepriv->wps_beacon_ie_len, WPS_ATTR_SELECTED_REGISTRAR, &selected_registrar, NULL);
1191
1192                                 if (!selected_registrar) {
1193                                         DBG_88E("selected_registrar is false , or AP is not ready to do WPS\n");
1194
1195                                         status = _STATS_UNABLE_HANDLE_STA_;
1196
1197                                         goto OnAssocReqFail;
1198                                 }
1199                         }
1200                 }
1201         } else {
1202                 int copy_len;
1203
1204                 if (psecuritypriv->wpa_psk == 0) {
1205                         DBG_88E("STA %pM: WPA/RSN IE in association "
1206                         "request, but AP don't support WPA/RSN\n", pstat->hwaddr);
1207
1208                         status = WLAN_STATUS_INVALID_IE;
1209
1210                         goto OnAssocReqFail;
1211                 }
1212
1213                 if (elems.wps_ie) {
1214                         DBG_88E("STA included WPS IE in "
1215                                    "(Re)Association Request - WPS is "
1216                                    "used\n");
1217                         pstat->flags |= WLAN_STA_WPS;
1218                         copy_len = 0;
1219                 } else {
1220                         copy_len = ((wpa_ie_len+2) > sizeof(pstat->wpa_ie)) ? (sizeof(pstat->wpa_ie)) : (wpa_ie_len+2);
1221                 }
1222                 if (copy_len > 0)
1223                         memcpy(pstat->wpa_ie, wpa_ie-2, copy_len);
1224         }
1225         /*  check if there is WMM IE & support WWM-PS */
1226         pstat->flags &= ~WLAN_STA_WME;
1227         pstat->qos_option = 0;
1228         pstat->qos_info = 0;
1229         pstat->has_legacy_ac = true;
1230         pstat->uapsd_vo = 0;
1231         pstat->uapsd_vi = 0;
1232         pstat->uapsd_be = 0;
1233         pstat->uapsd_bk = 0;
1234         if (pmlmepriv->qospriv.qos_option) {
1235                 p = pframe + WLAN_HDR_A3_LEN + ie_offset; ie_len = 0;
1236                 for (;;) {
1237                         p = rtw_get_ie(p, _VENDOR_SPECIFIC_IE_, &ie_len, pkt_len - WLAN_HDR_A3_LEN - ie_offset);
1238                         if (p) {
1239                                 if (!memcmp(p+2, WMM_IE, 6)) {
1240                                         pstat->flags |= WLAN_STA_WME;
1241
1242                                         pstat->qos_option = 1;
1243                                         pstat->qos_info = *(p+8);
1244
1245                                         pstat->max_sp_len = (pstat->qos_info>>5)&0x3;
1246
1247                                         if ((pstat->qos_info&0xf) != 0xf)
1248                                                 pstat->has_legacy_ac = true;
1249                                         else
1250                                                 pstat->has_legacy_ac = false;
1251
1252                                         if (pstat->qos_info&0xf) {
1253                                                 if (pstat->qos_info&BIT(0))
1254                                                         pstat->uapsd_vo = BIT(0)|BIT(1);
1255                                                 else
1256                                                         pstat->uapsd_vo = 0;
1257
1258                                                 if (pstat->qos_info&BIT(1))
1259                                                         pstat->uapsd_vi = BIT(0)|BIT(1);
1260                                                 else
1261                                                         pstat->uapsd_vi = 0;
1262
1263                                                 if (pstat->qos_info&BIT(2))
1264                                                         pstat->uapsd_bk = BIT(0)|BIT(1);
1265                                                 else
1266                                                         pstat->uapsd_bk = 0;
1267
1268                                                 if (pstat->qos_info&BIT(3))
1269                                                         pstat->uapsd_be = BIT(0)|BIT(1);
1270                                                 else
1271                                                         pstat->uapsd_be = 0;
1272                                         }
1273                                         break;
1274                                 }
1275                         } else {
1276                                 break;
1277                         }
1278                         p = p + ie_len + 2;
1279                 }
1280         }
1281
1282         /* save HT capabilities in the sta object */
1283         memset(&pstat->htpriv.ht_cap, 0, sizeof(struct ieee80211_ht_cap));
1284         if (elems.ht_capabilities && elems.ht_capabilities_len >= sizeof(struct ieee80211_ht_cap)) {
1285                 pstat->flags |= WLAN_STA_HT;
1286
1287                 pstat->flags |= WLAN_STA_WME;
1288
1289                 memcpy(&pstat->htpriv.ht_cap, elems.ht_capabilities, sizeof(struct ieee80211_ht_cap));
1290         } else {
1291                 pstat->flags &= ~WLAN_STA_HT;
1292         }
1293         if ((!pmlmepriv->htpriv.ht_option) && (pstat->flags&WLAN_STA_HT)) {
1294                 status = _STATS_FAILURE_;
1295                 goto OnAssocReqFail;
1296         }
1297
1298         if ((pstat->flags & WLAN_STA_HT) &&
1299             ((pstat->wpa2_pairwise_cipher&WPA_CIPHER_TKIP) ||
1300             (pstat->wpa_pairwise_cipher&WPA_CIPHER_TKIP))) {
1301                 DBG_88E("HT: %pM tried to "
1302                         "use TKIP with HT association\n", pstat->hwaddr);
1303
1304                 /* status = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY; */
1305                 /* goto OnAssocReqFail; */
1306         }
1307
1308         pstat->flags |= WLAN_STA_NONERP;
1309         for (i = 0; i < pstat->bssratelen; i++) {
1310                 if ((pstat->bssrateset[i] & 0x7f) > 22) {
1311                         pstat->flags &= ~WLAN_STA_NONERP;
1312                         break;
1313                 }
1314         }
1315
1316         if (pstat->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1317                 pstat->flags |= WLAN_STA_SHORT_PREAMBLE;
1318         else
1319                 pstat->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1320
1321         if (status != _STATS_SUCCESSFUL_)
1322                 goto OnAssocReqFail;
1323
1324 #ifdef CONFIG_88EU_P2P
1325         pstat->is_p2p_device = false;
1326         if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) {
1327                 p2pie = rtw_get_p2p_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, pkt_len - WLAN_HDR_A3_LEN - ie_offset, NULL, &p2pielen);
1328                 if (p2pie) {
1329                         pstat->is_p2p_device = true;
1330                         p2p_status_code = (u8)process_assoc_req_p2p_ie(pwdinfo, pframe, pkt_len, pstat);
1331                         if (p2p_status_code > 0) {
1332                                 pstat->p2p_status_code = p2p_status_code;
1333                                 status = _STATS_CAP_FAIL_;
1334                                 goto OnAssocReqFail;
1335                         }
1336                 }
1337         }
1338         pstat->p2p_status_code = p2p_status_code;
1339 #endif /* CONFIG_88EU_P2P */
1340
1341         /* TODO: identify_proprietary_vendor_ie(); */
1342         /*  Realtek proprietary IE */
1343         /*  identify if this is Broadcom sta */
1344         /*  identify if this is ralink sta */
1345         /*  Customer proprietary IE */
1346
1347         /* get a unique AID */
1348         if (pstat->aid > 0) {
1349                 DBG_88E("  old AID %d\n", pstat->aid);
1350         } else {
1351                 for (pstat->aid = 1; pstat->aid <= NUM_STA; pstat->aid++)
1352                         if (!pstapriv->sta_aid[pstat->aid - 1])
1353                                 break;
1354
1355                 /* if (pstat->aid > NUM_STA) { */
1356                 if (pstat->aid > pstapriv->max_num_sta) {
1357                         pstat->aid = 0;
1358
1359                         DBG_88E("  no room for more AIDs\n");
1360
1361                         status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1362
1363                         goto OnAssocReqFail;
1364                 } else {
1365                         pstapriv->sta_aid[pstat->aid - 1] = pstat;
1366                         DBG_88E("allocate new AID=(%d)\n", pstat->aid);
1367                 }
1368         }
1369
1370         pstat->state &= (~WIFI_FW_ASSOC_STATE);
1371         pstat->state |= WIFI_FW_ASSOC_SUCCESS;
1372
1373         spin_lock_bh(&pstapriv->auth_list_lock);
1374         if (!list_empty(&pstat->auth_list)) {
1375                 list_del_init(&pstat->auth_list);
1376                 pstapriv->auth_list_cnt--;
1377         }
1378         spin_unlock_bh(&pstapriv->auth_list_lock);
1379
1380         spin_lock_bh(&pstapriv->asoc_list_lock);
1381         if (list_empty(&pstat->asoc_list)) {
1382                 pstat->expire_to = pstapriv->expire_to;
1383                 list_add_tail(&pstat->asoc_list, &pstapriv->asoc_list);
1384                 pstapriv->asoc_list_cnt++;
1385         }
1386         spin_unlock_bh(&pstapriv->asoc_list_lock);
1387
1388         /*  now the station is qualified to join our BSS... */
1389         if (pstat && (pstat->state & WIFI_FW_ASSOC_SUCCESS) && (_STATS_SUCCESSFUL_ == status)) {
1390 #ifdef CONFIG_88EU_AP_MODE
1391                 /* 1 bss_cap_update & sta_info_update */
1392                 bss_cap_update_on_sta_join(padapter, pstat);
1393                 sta_info_update(padapter, pstat);
1394
1395                 /* issue assoc rsp before notify station join event. */
1396                 if (frame_type == WIFI_ASSOCREQ)
1397                         issue_asocrsp(padapter, status, pstat, WIFI_ASSOCRSP);
1398                 else
1399                         issue_asocrsp(padapter, status, pstat, WIFI_REASSOCRSP);
1400
1401                 /* 2 - report to upper layer */
1402                 DBG_88E("indicate_sta_join_event to upper layer - hostapd\n");
1403                 rtw_indicate_sta_assoc_event(padapter, pstat);
1404
1405                 /* 3-(1) report sta add event */
1406                 report_add_sta_event(padapter, pstat->hwaddr, pstat->aid);
1407 #endif
1408         }
1409
1410         return _SUCCESS;
1411
1412 asoc_class2_error:
1413
1414 #ifdef CONFIG_88EU_AP_MODE
1415         issue_deauth(padapter, (void *)GetAddr2Ptr(pframe), status);
1416 #endif
1417
1418         return _FAIL;
1419
1420 OnAssocReqFail:
1421
1422 #ifdef CONFIG_88EU_AP_MODE
1423         pstat->aid = 0;
1424         if (frame_type == WIFI_ASSOCREQ)
1425                 issue_asocrsp(padapter, status, pstat, WIFI_ASSOCRSP);
1426         else
1427                 issue_asocrsp(padapter, status, pstat, WIFI_REASSOCRSP);
1428 #endif
1429
1430 #endif /* CONFIG_88EU_AP_MODE */
1431
1432         return _FAIL;
1433 }
1434
1435 unsigned int OnAssocRsp(struct adapter *padapter, struct recv_frame *precv_frame)
1436 {
1437         uint i;
1438         int res;
1439         unsigned short  status;
1440         struct ndis_802_11_var_ie *pIE;
1441         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1442         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1443         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1444         /* struct wlan_bssid_ex *cur_network = &(pmlmeinfo->network); */
1445         u8 *pframe = precv_frame->rx_data;
1446         uint pkt_len = precv_frame->len;
1447
1448         DBG_88E("%s\n", __func__);
1449
1450         /* check A1 matches or not */
1451         if (memcmp(myid(&(padapter->eeprompriv)), get_da(pframe), ETH_ALEN))
1452                 return _SUCCESS;
1453
1454         if (!(pmlmeinfo->state & (WIFI_FW_AUTH_SUCCESS | WIFI_FW_ASSOC_STATE)))
1455                 return _SUCCESS;
1456
1457         if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)
1458                 return _SUCCESS;
1459
1460         _cancel_timer_ex(&pmlmeext->link_timer);
1461
1462         /* status */
1463         status = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 2));
1464         if (status > 0) {
1465                 DBG_88E("assoc reject, status code: %d\n", status);
1466                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
1467                 res = -4;
1468                 goto report_assoc_result;
1469         }
1470
1471         /* get capabilities */
1472         pmlmeinfo->capability = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
1473
1474         /* set slot time */
1475         pmlmeinfo->slotTime = (pmlmeinfo->capability & BIT(10)) ? 9 : 20;
1476
1477         /* AID */
1478         pmlmeinfo->aid = (int)(le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 4))&0x3fff);
1479         res = pmlmeinfo->aid;
1480
1481         /* following are moved to join event callback function */
1482         /* to handle HT, WMM, rate adaptive, update MAC reg */
1483         /* for not to handle the synchronous IO in the tasklet */
1484         for (i = (6 + WLAN_HDR_A3_LEN); i < pkt_len;) {
1485                 pIE = (struct ndis_802_11_var_ie *)(pframe + i);
1486
1487                 switch (pIE->ElementID) {
1488                 case _VENDOR_SPECIFIC_IE_:
1489                         if (!memcmp(pIE->data, WMM_PARA_OUI, 6))        /* WMM */
1490                                 WMM_param_handler(padapter, pIE);
1491                         break;
1492                 case _HT_CAPABILITY_IE_:        /* HT caps */
1493                         HT_caps_handler(padapter, pIE);
1494                         break;
1495                 case _HT_EXTRA_INFO_IE_:        /* HT info */
1496                         HT_info_handler(padapter, pIE);
1497                         break;
1498                 case _ERPINFO_IE_:
1499                         ERP_IE_handler(padapter, pIE);
1500                 default:
1501                         break;
1502                 }
1503
1504                 i += (pIE->Length + 2);
1505         }
1506
1507         pmlmeinfo->state &= (~WIFI_FW_ASSOC_STATE);
1508         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
1509
1510         /* Update Basic Rate Table for spec, 2010-12-28 , by thomas */
1511         UpdateBrateTbl(padapter, pmlmeinfo->network.SupportedRates);
1512
1513 report_assoc_result:
1514         if (res > 0)
1515                 rtw_buf_update(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len, pframe, pkt_len);
1516         else
1517                 kfree(pmlmepriv->assoc_rsp);
1518
1519         report_join_res(padapter, res);
1520
1521         return _SUCCESS;
1522 }
1523
1524 unsigned int OnDeAuth(struct adapter *padapter, struct recv_frame *precv_frame)
1525 {
1526         unsigned short  reason;
1527         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1528         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1529         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1530         u8 *pframe = precv_frame->rx_data;
1531 #ifdef CONFIG_88EU_P2P
1532         struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
1533 #endif /* CONFIG_88EU_P2P */
1534
1535         /* check A3 */
1536         if (!(!memcmp(GetAddr3Ptr(pframe), get_my_bssid(&pmlmeinfo->network), ETH_ALEN)))
1537                 return _SUCCESS;
1538
1539 #ifdef CONFIG_88EU_P2P
1540         if (pwdinfo->rx_invitereq_info.scan_op_ch_only) {
1541                 _cancel_timer_ex(&pwdinfo->reset_ch_sitesurvey);
1542                 _set_timer(&pwdinfo->reset_ch_sitesurvey, 10);
1543         }
1544 #endif /* CONFIG_88EU_P2P */
1545
1546         reason = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
1547
1548         DBG_88E("%s Reason code(%d)\n", __func__, reason);
1549
1550 #ifdef CONFIG_88EU_AP_MODE
1551         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1552                 struct sta_info *psta;
1553                 struct sta_priv *pstapriv = &padapter->stapriv;
1554
1555                 DBG_88E_LEVEL(_drv_always_, "ap recv deauth reason code(%d) sta:%pM\n",
1556                               reason, GetAddr2Ptr(pframe));
1557
1558                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1559                 if (psta) {
1560                         u8 updated = 0;
1561
1562                         spin_lock_bh(&pstapriv->asoc_list_lock);
1563                         if (!list_empty(&psta->asoc_list)) {
1564                                 list_del_init(&psta->asoc_list);
1565                                 pstapriv->asoc_list_cnt--;
1566                                 updated = ap_free_sta(padapter, psta, false, reason);
1567                         }
1568                         spin_unlock_bh(&pstapriv->asoc_list_lock);
1569
1570                         associated_clients_update(padapter, updated);
1571                 }
1572
1573                 return _SUCCESS;
1574         } else
1575 #endif
1576         {
1577                 int     ignore_received_deauth = 0;
1578
1579                 /* Before sending the auth frame to start the STA/GC mode connection with AP/GO,
1580                  *      we will send the deauth first.
1581                  *      However, the Win8.1 with BRCM Wi-Fi will send the deauth with reason code 6 to us after receieving our deauth.
1582                  *      Added the following code to avoid this case.
1583                  */
1584                 if ((pmlmeinfo->state & WIFI_FW_AUTH_STATE) ||
1585                     (pmlmeinfo->state & WIFI_FW_ASSOC_STATE)) {
1586                         if (reason == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA) {
1587                                 ignore_received_deauth = 1;
1588                         } else if (WLAN_REASON_PREV_AUTH_NOT_VALID == reason) {
1589                                 // TODO: 802.11r
1590                                 ignore_received_deauth = 1;
1591                         }
1592                 }
1593
1594                 DBG_88E_LEVEL(_drv_always_, "sta recv deauth reason code(%d) sta:%pM, ignore = %d\n",
1595                               reason, GetAddr3Ptr(pframe), ignore_received_deauth);
1596
1597                 if (!ignore_received_deauth)
1598                         receive_disconnect(padapter, GetAddr3Ptr(pframe), reason);
1599         }
1600         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
1601         return _SUCCESS;
1602 }
1603
1604 unsigned int OnDisassoc(struct adapter *padapter, struct recv_frame *precv_frame)
1605 {
1606         u16 reason;
1607         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1608         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1609         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1610         u8 *pframe = precv_frame->rx_data;
1611 #ifdef CONFIG_88EU_P2P
1612         struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
1613 #endif /* CONFIG_88EU_P2P */
1614
1615         /* check A3 */
1616         if (!(!memcmp(GetAddr3Ptr(pframe), get_my_bssid(&pmlmeinfo->network), ETH_ALEN)))
1617                 return _SUCCESS;
1618
1619 #ifdef CONFIG_88EU_P2P
1620         if (pwdinfo->rx_invitereq_info.scan_op_ch_only) {
1621                 _cancel_timer_ex(&pwdinfo->reset_ch_sitesurvey);
1622                 _set_timer(&pwdinfo->reset_ch_sitesurvey, 10);
1623         }
1624 #endif /* CONFIG_88EU_P2P */
1625
1626         reason = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
1627
1628         DBG_88E("%s Reason code(%d)\n", __func__, reason);
1629
1630 #ifdef CONFIG_88EU_AP_MODE
1631         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1632                 struct sta_info *psta;
1633                 struct sta_priv *pstapriv = &padapter->stapriv;
1634
1635                 DBG_88E_LEVEL(_drv_always_, "ap recv disassoc reason code(%d) sta:%pM\n",
1636                               reason, GetAddr2Ptr(pframe));
1637
1638                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1639                 if (psta) {
1640                         u8 updated = 0;
1641
1642                         spin_lock_bh(&pstapriv->asoc_list_lock);
1643                         if (!list_empty(&psta->asoc_list)) {
1644                                 list_del_init(&psta->asoc_list);
1645                                 pstapriv->asoc_list_cnt--;
1646                                 updated = ap_free_sta(padapter, psta, false, reason);
1647                         }
1648                         spin_unlock_bh(&pstapriv->asoc_list_lock);
1649
1650                         associated_clients_update(padapter, updated);
1651                 }
1652
1653                 return _SUCCESS;
1654         } else
1655 #endif
1656         {
1657                 DBG_88E_LEVEL(_drv_always_, "ap recv disassoc reason code(%d) sta:%pM\n",
1658                               reason, GetAddr3Ptr(pframe));
1659
1660                 receive_disconnect(padapter, GetAddr3Ptr(pframe), reason);
1661         }
1662         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
1663         return _SUCCESS;
1664 }
1665
1666 unsigned int OnAtim(struct adapter *padapter, struct recv_frame *precv_frame)
1667 {
1668         DBG_88E("%s\n", __func__);
1669         return _SUCCESS;
1670 }
1671
1672 unsigned int on_action_spct(struct adapter *padapter, struct recv_frame *precv_frame)
1673 {
1674         unsigned int ret = _FAIL;
1675         struct sta_info *psta = NULL;
1676         struct sta_priv *pstapriv = &padapter->stapriv;
1677         u8 *pframe = precv_frame->rx_data;
1678         u8 *frame_body = (u8 *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
1679         u8 category;
1680         u8 action;
1681
1682         DBG_88E(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(padapter->pnetdev));
1683
1684         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1685
1686         if (!psta)
1687                 goto exit;
1688
1689         category = frame_body[0];
1690         if (category != RTW_WLAN_CATEGORY_SPECTRUM_MGMT)
1691                 goto exit;
1692
1693         action = frame_body[1];
1694         switch (action) {
1695         case RTW_WLAN_ACTION_SPCT_MSR_REQ:
1696         case RTW_WLAN_ACTION_SPCT_MSR_RPRT:
1697         case RTW_WLAN_ACTION_SPCT_TPC_REQ:
1698         case RTW_WLAN_ACTION_SPCT_TPC_RPRT:
1699                 break;
1700         case RTW_WLAN_ACTION_SPCT_CHL_SWITCH:
1701                 break;
1702         default:
1703                 break;
1704         }
1705
1706 exit:
1707         return ret;
1708 }
1709
1710 unsigned int OnAction_qos(struct adapter *padapter, struct recv_frame *precv_frame)
1711 {
1712         return _SUCCESS;
1713 }
1714
1715 unsigned int OnAction_dls(struct adapter *padapter, struct recv_frame *precv_frame)
1716 {
1717         return _SUCCESS;
1718 }
1719
1720 unsigned int OnAction_back(struct adapter *padapter, struct recv_frame *precv_frame)
1721 {
1722         u8 *addr;
1723         struct sta_info *psta = NULL;
1724         struct recv_reorder_ctrl *preorder_ctrl;
1725         unsigned char           *frame_body;
1726         unsigned char           category, action;
1727         unsigned short  tid, status, reason_code = 0;
1728         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
1729         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
1730         u8 *pframe = precv_frame->rx_data;
1731         struct sta_priv *pstapriv = &padapter->stapriv;
1732         /* check RA matches or not */
1733         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe), ETH_ALEN))/* for if1, sta/ap mode */
1734                 return _SUCCESS;
1735
1736         DBG_88E("%s\n", __func__);
1737
1738         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
1739                 if (!(pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS))
1740                         return _SUCCESS;
1741
1742         addr = GetAddr2Ptr(pframe);
1743         psta = rtw_get_stainfo(pstapriv, addr);
1744
1745         if (!psta)
1746                 return _SUCCESS;
1747
1748         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
1749
1750         category = frame_body[0];
1751         if (category == RTW_WLAN_CATEGORY_BACK) { /*  representing Block Ack */
1752                 if (!pmlmeinfo->HT_enable)
1753                         return _SUCCESS;
1754                 action = frame_body[1];
1755                 DBG_88E("%s, action=%d\n", __func__, action);
1756                 switch (action) {
1757                 case RTW_WLAN_ACTION_ADDBA_REQ: /* ADDBA request */
1758                         memcpy(&(pmlmeinfo->ADDBA_req), &(frame_body[2]), sizeof(struct ADDBA_request));
1759                         process_addba_req(padapter, (u8 *)&(pmlmeinfo->ADDBA_req), addr);
1760
1761                         if (pmlmeinfo->bAcceptAddbaReq)
1762                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_ADDBA_RESP, 0);
1763                         else
1764                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_ADDBA_RESP, 37);/* reject ADDBA Req */
1765                         break;
1766                 case RTW_WLAN_ACTION_ADDBA_RESP: /* ADDBA response */
1767                         status = get_unaligned_le16(&frame_body[3]);
1768                         tid = ((frame_body[5] >> 2) & 0x7);
1769                         if (status == 0) {      /* successful */
1770                                 DBG_88E("agg_enable for TID=%d\n", tid);
1771                                 psta->htpriv.agg_enable_bitmap |= 1 << tid;
1772                                 psta->htpriv.candidate_tid_bitmap &= ~BIT(tid);
1773                         } else {
1774                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
1775                         }
1776                         break;
1777                 case RTW_WLAN_ACTION_DELBA: /* DELBA */
1778                         if ((frame_body[3] & BIT(3)) == 0) {
1779                                 psta->htpriv.agg_enable_bitmap &= ~(1 << ((frame_body[3] >> 4) & 0xf));
1780                                 psta->htpriv.candidate_tid_bitmap &= ~(1 << ((frame_body[3] >> 4) & 0xf));
1781                                 reason_code = get_unaligned_le16(&frame_body[4]);
1782                         } else if ((frame_body[3] & BIT(3)) == BIT(3)) {
1783                                 tid = (frame_body[3] >> 4) & 0x0F;
1784                                 preorder_ctrl =  &psta->recvreorder_ctrl[tid];
1785                                 preorder_ctrl->enable = false;
1786                                 preorder_ctrl->indicate_seq = 0xffff;
1787                         }
1788                         DBG_88E("%s(): DELBA: %x(%x)\n", __func__, pmlmeinfo->agg_enable_bitmap, reason_code);
1789                         /* todo: how to notify the host while receiving DELETE BA */
1790                         break;
1791                 default:
1792                         break;
1793                 }
1794         }
1795         return _SUCCESS;
1796 }
1797
1798 #ifdef CONFIG_88EU_P2P
1799
1800 static int get_reg_classes_full_count(struct p2p_channels *channel_list)
1801 {
1802         int cnt = 0;
1803         int i;
1804
1805         for (i = 0; i < channel_list->reg_classes; i++) {
1806                 cnt += channel_list->reg_class[i].channels;
1807         }
1808
1809         return cnt;
1810 }
1811
1812 void issue_p2p_GO_request(struct adapter *padapter, u8 *raddr)
1813 {
1814         unsigned char category = RTW_WLAN_CATEGORY_PUBLIC;
1815         u8 action = P2P_PUB_ACTION_ACTION;
1816         __be32 p2poui = cpu_to_be32(P2POUI);
1817         u8 oui_subtype = P2P_GO_NEGO_REQ;
1818         u8 wpsie[255] = { 0x00 }, p2pie[255] = { 0x00 };
1819         u8 wpsielen = 0, p2pielen = 0;
1820         u16 len_channellist_attr = 0;
1821         struct xmit_frame *pmgntframe;
1822         struct pkt_attrib *pattrib;
1823         unsigned char *pframe;
1824         struct rtw_ieee80211_hdr        *pwlanhdr;
1825         __le16 *fctrl;
1826         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
1827         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
1828         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
1829
1830         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
1831         if (!pmgntframe)
1832                 return;
1833
1834         DBG_88E("[%s] In\n", __func__);
1835         /* update attribute */
1836         pattrib = &pmgntframe->attrib;
1837         update_mgntframe_attrib(padapter, pattrib);
1838
1839         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
1840
1841         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
1842         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
1843
1844         fctrl = &(pwlanhdr->frame_ctl);
1845         *(fctrl) = 0;
1846
1847         memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
1848         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
1849         memcpy(pwlanhdr->addr3, myid(&(padapter->eeprompriv)), ETH_ALEN);
1850
1851         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
1852         pmlmeext->mgnt_seq++;
1853         SetFrameSubType(pframe, WIFI_ACTION);
1854
1855         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
1856         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
1857
1858         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
1859         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
1860         pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *)&(p2poui), &(pattrib->pktlen));
1861         pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pattrib->pktlen));
1862         pwdinfo->negotiation_dialog_token = 1;  /*      Initialize the dialog value */
1863         pframe = rtw_set_fixed_ie(pframe, 1, &pwdinfo->negotiation_dialog_token, &(pattrib->pktlen));
1864
1865         /*      WPS Section */
1866         wpsielen = 0;
1867         /*      WPS OUI */
1868         *(__be32 *)(wpsie) = cpu_to_be32(WPSOUI);
1869         wpsielen += 4;
1870
1871         /*      WPS version */
1872         /*      Type: */
1873         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1);
1874         wpsielen += 2;
1875
1876         /*      Length: */
1877         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001);
1878         wpsielen += 2;
1879
1880         /*      Value: */
1881         wpsie[wpsielen++] = WPS_VERSION_1;      /*      Version 1.0 */
1882
1883         /*      Device Password ID */
1884         /*      Type: */
1885         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_DEVICE_PWID);
1886         wpsielen += 2;
1887
1888         /*      Length: */
1889         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002);
1890         wpsielen += 2;
1891
1892         /*      Value: */
1893
1894         if (pwdinfo->ui_got_wps_info == P2P_GOT_WPSINFO_PEER_DISPLAY_PIN)
1895                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_USER_SPEC);
1896         else if (pwdinfo->ui_got_wps_info == P2P_GOT_WPSINFO_SELF_DISPLAY_PIN)
1897                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_REGISTRAR_SPEC);
1898         else if (pwdinfo->ui_got_wps_info == P2P_GOT_WPSINFO_PBC)
1899                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_PBC);
1900
1901         wpsielen += 2;
1902
1903         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *)wpsie, &pattrib->pktlen);
1904
1905         /*      P2P IE Section. */
1906
1907         /*      P2P OUI */
1908         p2pielen = 0;
1909         p2pie[p2pielen++] = 0x50;
1910         p2pie[p2pielen++] = 0x6F;
1911         p2pie[p2pielen++] = 0x9A;
1912         p2pie[p2pielen++] = 0x09;       /*      WFA P2P v1.0 */
1913
1914         /*      Commented by Albert 20110306 */
1915         /*      According to the P2P Specification, the group negoitation request frame should contain 9 P2P attributes */
1916         /*      1. P2P Capability */
1917         /*      2. Group Owner Intent */
1918         /*      3. Configuration Timeout */
1919         /*      4. Listen Channel */
1920         /*      5. Extended Listen Timing */
1921         /*      6. Intended P2P Interface Address */
1922         /*      7. Channel List */
1923         /*      8. P2P Device Info */
1924         /*      9. Operating Channel */
1925
1926         /*      P2P Capability */
1927         /*      Type: */
1928         p2pie[p2pielen++] = P2P_ATTR_CAPABILITY;
1929
1930         /*      Length: */
1931         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
1932         p2pielen += 2;
1933
1934         /*      Value: */
1935         /*      Device Capability Bitmap, 1 byte */
1936         p2pie[p2pielen++] = DMP_P2P_DEVCAP_SUPPORT;
1937
1938         /*      Group Capability Bitmap, 1 byte */
1939         if (pwdinfo->persistent_supported)
1940                 p2pie[p2pielen++] = P2P_GRPCAP_CROSS_CONN | P2P_GRPCAP_PERSISTENT_GROUP;
1941         else
1942                 p2pie[p2pielen++] = P2P_GRPCAP_CROSS_CONN;
1943
1944         /*      Group Owner Intent */
1945         /*      Type: */
1946         p2pie[p2pielen++] = P2P_ATTR_GO_INTENT;
1947
1948         /*      Length: */
1949         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0001);
1950         p2pielen += 2;
1951
1952         /*      Value: */
1953         /*      Todo the tie breaker bit. */
1954         p2pie[p2pielen++] = ((pwdinfo->intent << 1) | BIT(0));
1955
1956         /*      Configuration Timeout */
1957         /*      Type: */
1958         p2pie[p2pielen++] = P2P_ATTR_CONF_TIMEOUT;
1959
1960         /*      Length: */
1961         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
1962         p2pielen += 2;
1963
1964         /*      Value: */
1965         p2pie[p2pielen++] = 200;        /*      2 seconds needed to be the P2P GO */
1966         p2pie[p2pielen++] = 200;        /*      2 seconds needed to be the P2P Client */
1967
1968         /*      Listen Channel */
1969         /*      Type: */
1970         p2pie[p2pielen++] = P2P_ATTR_LISTEN_CH;
1971
1972         /*      Length: */
1973         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005);
1974         p2pielen += 2;
1975
1976         /*      Value: */
1977         /*      Country String */
1978         p2pie[p2pielen++] = 'X';
1979         p2pie[p2pielen++] = 'X';
1980
1981         /*      The third byte should be set to 0x04. */
1982         /*      Described in the "Operating Channel Attribute" section. */
1983         p2pie[p2pielen++] = 0x04;
1984
1985         /*      Operating Class */
1986         p2pie[p2pielen++] = 0x51;       /*      Copy from SD7 */
1987
1988         /*      Channel Number */
1989         p2pie[p2pielen++] = pwdinfo->listen_channel;    /*      listening channel number */
1990
1991         /*      Extended Listen Timing ATTR */
1992         /*      Type: */
1993         p2pie[p2pielen++] = P2P_ATTR_EX_LISTEN_TIMING;
1994
1995         /*      Length: */
1996         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0004);
1997         p2pielen += 2;
1998
1999         /*      Value: */
2000         /*      Availability Period */
2001         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0xFFFF);
2002         p2pielen += 2;
2003
2004         /*      Availability Interval */
2005         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0xFFFF);
2006         p2pielen += 2;
2007
2008         /*      Intended P2P Interface Address */
2009         /*      Type: */
2010         p2pie[p2pielen++] = P2P_ATTR_INTENTED_IF_ADDR;
2011
2012         /*      Length: */
2013         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN);
2014         p2pielen += 2;
2015
2016         /*      Value: */
2017         memcpy(p2pie + p2pielen, myid(&padapter->eeprompriv), ETH_ALEN);
2018         p2pielen += ETH_ALEN;
2019
2020         /*      Channel List */
2021         /*      Type: */
2022         p2pie[p2pielen++] = P2P_ATTR_CH_LIST;
2023
2024         /*  Length: */
2025         /*  Country String(3) */
2026         /*  + (Operating Class (1) + Number of Channels(1)) * Operation Classes (?) */
2027         /*  + number of channels in all classes */
2028         len_channellist_attr = 3
2029            + (1 + 1) * (u16)(pmlmeext->channel_list.reg_classes)
2030            + get_reg_classes_full_count(&pmlmeext->channel_list);
2031
2032         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(len_channellist_attr);
2033         p2pielen += 2;
2034
2035         /*      Value: */
2036         /*      Country String */
2037         p2pie[p2pielen++] = 'X';
2038         p2pie[p2pielen++] = 'X';
2039
2040         /*      The third byte should be set to 0x04. */
2041         /*      Described in the "Operating Channel Attribute" section. */
2042         p2pie[p2pielen++] = 0x04;
2043
2044         /*      Channel Entry List */
2045
2046         {
2047                 int i, j;
2048                 for (j = 0; j < pmlmeext->channel_list.reg_classes; j++) {
2049                         /*      Operating Class */
2050                         p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].reg_class;
2051
2052                         /*      Number of Channels */
2053                         p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].channels;
2054
2055                         /*      Channel List */
2056                         for (i = 0; i < pmlmeext->channel_list.reg_class[j].channels; i++) {
2057                                 p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].channel[i];
2058                         }
2059                 }
2060         }
2061
2062         /*      Device Info */
2063         /*      Type: */
2064         p2pie[p2pielen++] = P2P_ATTR_DEVICE_INFO;
2065
2066         /*      Length: */
2067         /*      21 -> P2P Device Address (6bytes) + Config Methods (2bytes) + Primary Device Type (8bytes) */
2068         /*      + NumofSecondDevType (1byte) + WPS Device Name ID field (2bytes) + WPS Device Name Len field (2bytes) */
2069         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(21 + pwdinfo->device_name_len);
2070         p2pielen += 2;
2071
2072         /*      Value: */
2073         /*      P2P Device Address */
2074         memcpy(p2pie + p2pielen, myid(&padapter->eeprompriv), ETH_ALEN);
2075         p2pielen += ETH_ALEN;
2076
2077         /*      Config Method */
2078         /*      This field should be big endian. Noted by P2P specification. */
2079
2080         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(pwdinfo->supported_wps_cm);
2081
2082         p2pielen += 2;
2083
2084         /*      Primary Device Type */
2085         /*      Category ID */
2086         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA);
2087         p2pielen += 2;
2088
2089         /*      OUI */
2090         *(__be32 *)(p2pie + p2pielen) = cpu_to_be32(WPSOUI);
2091         p2pielen += 4;
2092
2093         /*      Sub Category ID */
2094         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER);
2095         p2pielen += 2;
2096
2097         /*      Number of Secondary Device Types */
2098         p2pie[p2pielen++] = 0x00;       /*      No Secondary Device Type List */
2099
2100         /*      Device Name */
2101         /*      Type: */
2102         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_ATTR_DEVICE_NAME);
2103         p2pielen += 2;
2104
2105         /*      Length: */
2106         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(pwdinfo->device_name_len);
2107         p2pielen += 2;
2108
2109         /*      Value: */
2110         memcpy(p2pie + p2pielen, pwdinfo->device_name, pwdinfo->device_name_len);
2111         p2pielen += pwdinfo->device_name_len;
2112
2113         /*      Operating Channel */
2114         /*      Type: */
2115         p2pie[p2pielen++] = P2P_ATTR_OPERATING_CH;
2116
2117         /*      Length: */
2118         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005);
2119         p2pielen += 2;
2120
2121         /*      Value: */
2122         /*      Country String */
2123         p2pie[p2pielen++] = 'X';
2124         p2pie[p2pielen++] = 'X';
2125
2126         /*      The third byte should be set to 0x04. */
2127         /*      Described in the "Operating Channel Attribute" section. */
2128         p2pie[p2pielen++] = 0x04;
2129
2130         /*      Operating Class */
2131         p2pie[p2pielen++] = 0x51;
2132
2133         /*      Channel Number */
2134         p2pie[p2pielen++] = pwdinfo->operating_channel; /*      operating channel number */
2135
2136         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *)p2pie, &pattrib->pktlen);
2137
2138         pattrib->last_txcmdsz = pattrib->pktlen;
2139
2140         dump_mgntframe(padapter, pmgntframe);
2141 }
2142
2143 static void issue_p2p_GO_response(struct adapter *padapter, u8 *raddr, u8 *frame_body, uint len, u8 result)
2144 {
2145         unsigned char category = RTW_WLAN_CATEGORY_PUBLIC;
2146         u8 action = P2P_PUB_ACTION_ACTION;
2147         __be32                  p2poui = cpu_to_be32(P2POUI);
2148         u8 oui_subtype = P2P_GO_NEGO_RESP;
2149         u8 wpsie[255] = { 0x00 }, p2pie[255] = { 0x00 };
2150         u8 p2pielen = 0;
2151         uint                    wpsielen = 0;
2152         u16 wps_devicepassword_id = 0x0000;
2153         __be16                  be_tmp;
2154         uint                    wps_devicepassword_id_len = 0;
2155         u16 len_channellist_attr = 0;
2156
2157         struct xmit_frame                       *pmgntframe;
2158         struct pkt_attrib                       *pattrib;
2159         unsigned char                                   *pframe;
2160         struct rtw_ieee80211_hdr        *pwlanhdr;
2161         __le16 *fctrl;
2162         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
2163         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2164         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
2165
2166         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2167         if (!pmgntframe)
2168                 return;
2169
2170         DBG_88E("[%s] In, result=%d\n", __func__,  result);
2171         /* update attribute */
2172         pattrib = &pmgntframe->attrib;
2173         update_mgntframe_attrib(padapter, pattrib);
2174
2175         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2176
2177         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2178         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
2179
2180         fctrl = &(pwlanhdr->frame_ctl);
2181         *(fctrl) = 0;
2182
2183         memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
2184         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
2185         memcpy(pwlanhdr->addr3, myid(&(padapter->eeprompriv)), ETH_ALEN);
2186
2187         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
2188         pmlmeext->mgnt_seq++;
2189         SetFrameSubType(pframe, WIFI_ACTION);
2190
2191         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
2192         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
2193
2194         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
2195         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
2196         pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *)&(p2poui), &(pattrib->pktlen));
2197         pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pattrib->pktlen));
2198         pwdinfo->negotiation_dialog_token = frame_body[7];      /*      The Dialog Token of provisioning discovery request frame. */
2199         pframe = rtw_set_fixed_ie(pframe, 1, &(pwdinfo->negotiation_dialog_token), &(pattrib->pktlen));
2200
2201         /*      Commented by Albert 20110328 */
2202         /*      Try to get the device password ID from the WPS IE of group negotiation request frame */
2203         /*      WiFi Direct test plan 5.1.15 */
2204         rtw_get_wps_ie(frame_body + _PUBLIC_ACTION_IE_OFFSET_, len - _PUBLIC_ACTION_IE_OFFSET_, wpsie, &wpsielen);
2205         rtw_get_wps_attr_content(wpsie, wpsielen, WPS_ATTR_DEVICE_PWID, (u8 *)&be_tmp, &wps_devicepassword_id_len);
2206         wps_devicepassword_id = be16_to_cpu(be_tmp);
2207
2208         memset(wpsie, 0x00, 255);
2209         wpsielen = 0;
2210
2211         /*      WPS Section */
2212         wpsielen = 0;
2213         /*      WPS OUI */
2214         *(__be32 *)(wpsie) = cpu_to_be32(WPSOUI);
2215         wpsielen += 4;
2216
2217         /*      WPS version */
2218         /*      Type: */
2219         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1);
2220         wpsielen += 2;
2221
2222         /*      Length: */
2223         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001);
2224         wpsielen += 2;
2225
2226         /*      Value: */
2227         wpsie[wpsielen++] = WPS_VERSION_1;      /*      Version 1.0 */
2228
2229         /*      Device Password ID */
2230         /*      Type: */
2231         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_DEVICE_PWID);
2232         wpsielen += 2;
2233
2234         /*      Length: */
2235         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002);
2236         wpsielen += 2;
2237
2238         /*      Value: */
2239         if (wps_devicepassword_id == WPS_DPID_USER_SPEC)
2240                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_REGISTRAR_SPEC);
2241         else if (wps_devicepassword_id == WPS_DPID_REGISTRAR_SPEC)
2242                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_USER_SPEC);
2243         else
2244                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_PBC);
2245         wpsielen += 2;
2246
2247         /*      Commented by Kurt 20120113 */
2248         /*      If some device wants to do p2p handshake without sending prov_disc_req */
2249         /*      We have to get peer_req_cm from here. */
2250         if (!memcmp(pwdinfo->rx_prov_disc_info.strconfig_method_desc_of_prov_disc_req, "000", 3)) {
2251                 if (wps_devicepassword_id == WPS_DPID_USER_SPEC)
2252                         memcpy(pwdinfo->rx_prov_disc_info.strconfig_method_desc_of_prov_disc_req, "dis", 3);
2253                 else if (wps_devicepassword_id == WPS_DPID_REGISTRAR_SPEC)
2254                         memcpy(pwdinfo->rx_prov_disc_info.strconfig_method_desc_of_prov_disc_req, "pad", 3);
2255                 else
2256                         memcpy(pwdinfo->rx_prov_disc_info.strconfig_method_desc_of_prov_disc_req, "pbc", 3);
2257         }
2258
2259         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *)wpsie, &pattrib->pktlen);
2260
2261         /*      P2P IE Section. */
2262
2263         /*      P2P OUI */
2264         p2pielen = 0;
2265         p2pie[p2pielen++] = 0x50;
2266         p2pie[p2pielen++] = 0x6F;
2267         p2pie[p2pielen++] = 0x9A;
2268         p2pie[p2pielen++] = 0x09;       /*      WFA P2P v1.0 */
2269
2270         /*      Commented by Albert 20100908 */
2271         /*      According to the P2P Specification, the group negoitation response frame should contain 9 P2P attributes */
2272         /*      1. Status */
2273         /*      2. P2P Capability */
2274         /*      3. Group Owner Intent */
2275         /*      4. Configuration Timeout */
2276         /*      5. Operating Channel */
2277         /*      6. Intended P2P Interface Address */
2278         /*      7. Channel List */
2279         /*      8. Device Info */
2280         /*      9. Group ID     (Only GO) */
2281
2282         /*      ToDo: */
2283
2284         /*      P2P Status */
2285         /*      Type: */
2286         p2pie[p2pielen++] = P2P_ATTR_STATUS;
2287
2288         /*      Length: */
2289         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0001);
2290         p2pielen += 2;
2291
2292         /*      Value: */
2293         p2pie[p2pielen++] = result;
2294
2295         /*      P2P Capability */
2296         /*      Type: */
2297         p2pie[p2pielen++] = P2P_ATTR_CAPABILITY;
2298
2299         /*      Length: */
2300         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
2301         p2pielen += 2;
2302
2303         /*      Value: */
2304         /*      Device Capability Bitmap, 1 byte */
2305
2306         if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT)) {
2307                 /*      Commented by Albert 2011/03/08 */
2308                 /*      According to the P2P specification */
2309                 /*      if the sending device will be client, the P2P Capability should be reserved of group negotiation response frame */
2310                 p2pie[p2pielen++] = 0;
2311         } else {
2312                 /*      Be group owner or meet the error case */
2313                 p2pie[p2pielen++] = DMP_P2P_DEVCAP_SUPPORT;
2314         }
2315
2316         /*      Group Capability Bitmap, 1 byte */
2317         if (pwdinfo->persistent_supported) {
2318                 p2pie[p2pielen++] = P2P_GRPCAP_CROSS_CONN | P2P_GRPCAP_PERSISTENT_GROUP;
2319         } else {
2320                 p2pie[p2pielen++] = P2P_GRPCAP_CROSS_CONN;
2321         }
2322
2323         /*      Group Owner Intent */
2324         /*      Type: */
2325         p2pie[p2pielen++] = P2P_ATTR_GO_INTENT;
2326
2327         /*      Length: */
2328         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0001);
2329         p2pielen += 2;
2330
2331         /*      Value: */
2332         if (pwdinfo->peer_intent & 0x01) {
2333                 /*      Peer's tie breaker bit is 1, our tie breaker bit should be 0 */
2334                 p2pie[p2pielen++] = (pwdinfo->intent << 1);
2335         } else {
2336                 /*      Peer's tie breaker bit is 0, our tie breaker bit should be 1 */
2337                 p2pie[p2pielen++] = ((pwdinfo->intent << 1) | BIT(0));
2338         }
2339
2340         /*      Configuration Timeout */
2341         /*      Type: */
2342         p2pie[p2pielen++] = P2P_ATTR_CONF_TIMEOUT;
2343
2344         /*      Length: */
2345         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
2346         p2pielen += 2;
2347
2348         /*      Value: */
2349         p2pie[p2pielen++] = 200;        /*      2 seconds needed to be the P2P GO */
2350         p2pie[p2pielen++] = 200;        /*      2 seconds needed to be the P2P Client */
2351
2352         /*      Operating Channel */
2353         /*      Type: */
2354         p2pie[p2pielen++] = P2P_ATTR_OPERATING_CH;
2355
2356         /*      Length: */
2357         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005);
2358         p2pielen += 2;
2359
2360         /*      Value: */
2361         /*      Country String */
2362         p2pie[p2pielen++] = 'X';
2363         p2pie[p2pielen++] = 'X';
2364
2365         /*      The third byte should be set to 0x04. */
2366         /*      Described in the "Operating Channel Attribute" section. */
2367         p2pie[p2pielen++] = 0x04;
2368
2369         /*      Operating Class */
2370         p2pie[p2pielen++] = 0x51;
2371
2372         /*      Channel Number */
2373         p2pie[p2pielen++] = pwdinfo->operating_channel; /*      operating channel number */
2374
2375         /*      Intended P2P Interface Address */
2376         /*      Type: */
2377         p2pie[p2pielen++] = P2P_ATTR_INTENTED_IF_ADDR;
2378
2379         /*      Length: */
2380         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN);
2381         p2pielen += 2;
2382
2383         /*      Value: */
2384         memcpy(p2pie + p2pielen, myid(&padapter->eeprompriv), ETH_ALEN);
2385         p2pielen += ETH_ALEN;
2386
2387         /*      Channel List */
2388         /*      Type: */
2389         p2pie[p2pielen++] = P2P_ATTR_CH_LIST;
2390
2391         /*  Country String(3) */
2392         /*  + (Operating Class (1) + Number of Channels(1)) * Operation Classes (?) */
2393         /*  + number of channels in all classes */
2394         len_channellist_attr = 3
2395            + (1 + 1) * (u16)pmlmeext->channel_list.reg_classes
2396            + get_reg_classes_full_count(&pmlmeext->channel_list);
2397
2398         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(len_channellist_attr);
2399
2400         p2pielen += 2;
2401
2402         /*      Value: */
2403         /*      Country String */
2404         p2pie[p2pielen++] = 'X';
2405         p2pie[p2pielen++] = 'X';
2406
2407         /*      The third byte should be set to 0x04. */
2408         /*      Described in the "Operating Channel Attribute" section. */
2409         p2pie[p2pielen++] = 0x04;
2410
2411         /*      Channel Entry List */
2412
2413         {
2414                 int i, j;
2415                 for (j = 0; j < pmlmeext->channel_list.reg_classes; j++) {
2416                         /*      Operating Class */
2417                         p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].reg_class;
2418
2419                         /*      Number of Channels */
2420                         p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].channels;
2421
2422                         /*      Channel List */
2423                         for (i = 0; i < pmlmeext->channel_list.reg_class[j].channels; i++) {
2424                                 p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].channel[i];
2425                         }
2426                 }
2427         }
2428
2429         /*      Device Info */
2430         /*      Type: */
2431         p2pie[p2pielen++] = P2P_ATTR_DEVICE_INFO;
2432
2433         /*      Length: */
2434         /*      21 -> P2P Device Address (6bytes) + Config Methods (2bytes) + Primary Device Type (8bytes) */
2435         /*      + NumofSecondDevType (1byte) + WPS Device Name ID field (2bytes) + WPS Device Name Len field (2bytes) */
2436         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(21 + pwdinfo->device_name_len);
2437         p2pielen += 2;
2438
2439         /*      Value: */
2440         /*      P2P Device Address */
2441         memcpy(p2pie + p2pielen, myid(&padapter->eeprompriv), ETH_ALEN);
2442         p2pielen += ETH_ALEN;
2443
2444         /*      Config Method */
2445         /*      This field should be big endian. Noted by P2P specification. */
2446
2447         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(pwdinfo->supported_wps_cm);
2448
2449         p2pielen += 2;
2450
2451         /*      Primary Device Type */
2452         /*      Category ID */
2453         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA);
2454         p2pielen += 2;
2455
2456         /*      OUI */
2457         *(__be32 *)(p2pie + p2pielen) = cpu_to_be32(WPSOUI);
2458         p2pielen += 4;
2459
2460         /*      Sub Category ID */
2461         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER);
2462         p2pielen += 2;
2463
2464         /*      Number of Secondary Device Types */
2465         p2pie[p2pielen++] = 0x00;       /*      No Secondary Device Type List */
2466
2467         /*      Device Name */
2468         /*      Type: */
2469         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_ATTR_DEVICE_NAME);
2470         p2pielen += 2;
2471
2472         /*      Length: */
2473         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(pwdinfo->device_name_len);
2474         p2pielen += 2;
2475
2476         /*      Value: */
2477         memcpy(p2pie + p2pielen, pwdinfo->device_name, pwdinfo->device_name_len);
2478         p2pielen += pwdinfo->device_name_len;
2479
2480         if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) {
2481                 /*      Group ID Attribute */
2482                 /*      Type: */
2483                 p2pie[p2pielen++] = P2P_ATTR_GROUP_ID;
2484
2485                 /*      Length: */
2486                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN + pwdinfo->nego_ssidlen);
2487                 p2pielen += 2;
2488
2489                 /*      Value: */
2490                 /*      p2P Device Address */
2491                 memcpy(p2pie + p2pielen, pwdinfo->device_addr, ETH_ALEN);
2492                 p2pielen += ETH_ALEN;
2493
2494                 /*      SSID */
2495                 memcpy(p2pie + p2pielen, pwdinfo->nego_ssid, pwdinfo->nego_ssidlen);
2496                 p2pielen += pwdinfo->nego_ssidlen;
2497         }
2498
2499         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *)p2pie, &pattrib->pktlen);
2500
2501         pattrib->last_txcmdsz = pattrib->pktlen;
2502
2503         dump_mgntframe(padapter, pmgntframe);
2504 }
2505
2506 static void issue_p2p_GO_confirm(struct adapter *padapter, u8 *raddr, u8 result)
2507 {
2508         unsigned char category = RTW_WLAN_CATEGORY_PUBLIC;
2509         u8 action = P2P_PUB_ACTION_ACTION;
2510         __be32                  p2poui = cpu_to_be32(P2POUI);
2511         u8 oui_subtype = P2P_GO_NEGO_CONF;
2512         u8 p2pie[255] = { 0x00 };
2513         u8 p2pielen = 0;
2514
2515         struct xmit_frame                       *pmgntframe;
2516         struct pkt_attrib                       *pattrib;
2517         unsigned char                                   *pframe;
2518         struct rtw_ieee80211_hdr        *pwlanhdr;
2519         __le16 *fctrl;
2520         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
2521         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2522         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
2523
2524         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2525         if (!pmgntframe)
2526                 return;
2527
2528         DBG_88E("[%s] In\n", __func__);
2529         /* update attribute */
2530         pattrib = &pmgntframe->attrib;
2531         update_mgntframe_attrib(padapter, pattrib);
2532
2533         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2534
2535         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2536         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
2537
2538         fctrl = &(pwlanhdr->frame_ctl);
2539         *(fctrl) = 0;
2540
2541         memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
2542         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
2543         memcpy(pwlanhdr->addr3, myid(&(padapter->eeprompriv)), ETH_ALEN);
2544
2545         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
2546         pmlmeext->mgnt_seq++;
2547         SetFrameSubType(pframe, WIFI_ACTION);
2548
2549         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
2550         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
2551
2552         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
2553         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
2554         pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *)&(p2poui), &(pattrib->pktlen));
2555         pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pattrib->pktlen));
2556         pframe = rtw_set_fixed_ie(pframe, 1, &(pwdinfo->negotiation_dialog_token), &(pattrib->pktlen));
2557
2558         /*      P2P IE Section. */
2559
2560         /*      P2P OUI */
2561         p2pielen = 0;
2562         p2pie[p2pielen++] = 0x50;
2563         p2pie[p2pielen++] = 0x6F;
2564         p2pie[p2pielen++] = 0x9A;
2565         p2pie[p2pielen++] = 0x09;       /*      WFA P2P v1.0 */
2566
2567         /*      Commented by Albert 20110306 */
2568         /*      According to the P2P Specification, the group negoitation request frame should contain 5 P2P attributes */
2569         /*      1. Status */
2570         /*      2. P2P Capability */
2571         /*      3. Operating Channel */
2572         /*      4. Channel List */
2573         /*      5. Group ID     (if this WiFi is GO) */
2574
2575         /*      P2P Status */
2576         /*      Type: */
2577         p2pie[p2pielen++] = P2P_ATTR_STATUS;
2578
2579         /*      Length: */
2580         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0001);
2581         p2pielen += 2;
2582
2583         /*      Value: */
2584         p2pie[p2pielen++] = result;
2585
2586         /*      P2P Capability */
2587         /*      Type: */
2588         p2pie[p2pielen++] = P2P_ATTR_CAPABILITY;
2589
2590         /*      Length: */
2591         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
2592         p2pielen += 2;
2593
2594         /*      Value: */
2595         /*      Device Capability Bitmap, 1 byte */
2596         p2pie[p2pielen++] = DMP_P2P_DEVCAP_SUPPORT;
2597
2598         /*      Group Capability Bitmap, 1 byte */
2599         if (pwdinfo->persistent_supported)
2600                 p2pie[p2pielen++] = P2P_GRPCAP_CROSS_CONN | P2P_GRPCAP_PERSISTENT_GROUP;
2601         else
2602                 p2pie[p2pielen++] = P2P_GRPCAP_CROSS_CONN;
2603
2604         /*      Operating Channel */
2605         /*      Type: */
2606         p2pie[p2pielen++] = P2P_ATTR_OPERATING_CH;
2607
2608         /*      Length: */
2609         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005);
2610         p2pielen += 2;
2611
2612         /*      Value: */
2613         /*      Country String */
2614         p2pie[p2pielen++] = 'X';
2615         p2pie[p2pielen++] = 'X';
2616
2617         /*      The third byte should be set to 0x04. */
2618         /*      Described in the "Operating Channel Attribute" section. */
2619         p2pie[p2pielen++] = 0x04;
2620
2621         if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT)) {
2622                 /*      Operating Class */
2623                 p2pie[p2pielen++] = 0x51;
2624                 p2pie[p2pielen++] = pwdinfo->peer_operating_ch;
2625         } else {
2626                 /*      Operating Class */
2627                 p2pie[p2pielen++] = 0x51;
2628
2629                 /*      Channel Number */
2630                 p2pie[p2pielen++] = pwdinfo->operating_channel;         /*      Use the listen channel as the operating channel */
2631         }
2632
2633         /*      Channel List */
2634         /*      Type: */
2635         p2pie[p2pielen++] = P2P_ATTR_CH_LIST;
2636
2637         /*      Length: */
2638         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(pwdinfo->channel_list_attr_len);
2639         p2pielen += 2;
2640
2641         /*      Value: */
2642         memcpy(p2pie + p2pielen, pwdinfo->channel_list_attr, pwdinfo->channel_list_attr_len);
2643         p2pielen += pwdinfo->channel_list_attr_len;
2644
2645         if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) {
2646                 /*      Group ID Attribute */
2647                 /*      Type: */
2648                 p2pie[p2pielen++] = P2P_ATTR_GROUP_ID;
2649
2650                 /*      Length: */
2651                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN + pwdinfo->nego_ssidlen);
2652                 p2pielen += 2;
2653
2654                 /*      Value: */
2655                 /*      p2P Device Address */
2656                 memcpy(p2pie + p2pielen, pwdinfo->device_addr, ETH_ALEN);
2657                 p2pielen += ETH_ALEN;
2658
2659                 /*      SSID */
2660                 memcpy(p2pie + p2pielen, pwdinfo->nego_ssid, pwdinfo->nego_ssidlen);
2661                 p2pielen += pwdinfo->nego_ssidlen;
2662         }
2663         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *)p2pie, &pattrib->pktlen);
2664         pattrib->last_txcmdsz = pattrib->pktlen;
2665         dump_mgntframe(padapter, pmgntframe);
2666 }
2667
2668 void issue_p2p_invitation_request(struct adapter *padapter, u8 *raddr)
2669 {
2670         unsigned char category = RTW_WLAN_CATEGORY_PUBLIC;
2671         u8 action = P2P_PUB_ACTION_ACTION;
2672         __be32                  p2poui = cpu_to_be32(P2POUI);
2673         u8 oui_subtype = P2P_INVIT_REQ;
2674         u8 p2pie[255] = { 0x00 };
2675         u8 p2pielen = 0;
2676         u8 dialogToken = 3;
2677         u16 len_channellist_attr = 0;
2678         struct xmit_frame                       *pmgntframe;
2679         struct pkt_attrib                       *pattrib;
2680         unsigned char                                   *pframe;
2681         struct rtw_ieee80211_hdr        *pwlanhdr;
2682         __le16 *fctrl;
2683         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
2684         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2685         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
2686
2687         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2688         if (!pmgntframe)
2689                 return;
2690
2691         /* update attribute */
2692         pattrib = &pmgntframe->attrib;
2693         update_mgntframe_attrib(padapter, pattrib);
2694
2695         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2696
2697         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2698         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
2699
2700         fctrl = &(pwlanhdr->frame_ctl);
2701         *(fctrl) = 0;
2702
2703         memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
2704         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
2705         memcpy(pwlanhdr->addr3, raddr,  ETH_ALEN);
2706
2707         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
2708         pmlmeext->mgnt_seq++;
2709         SetFrameSubType(pframe, WIFI_ACTION);
2710
2711         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
2712         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
2713
2714         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
2715         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
2716         pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *)&(p2poui), &(pattrib->pktlen));
2717         pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pattrib->pktlen));
2718         pframe = rtw_set_fixed_ie(pframe, 1, &(dialogToken), &(pattrib->pktlen));
2719
2720         /*      P2P IE Section. */
2721
2722         /*      P2P OUI */
2723         p2pielen = 0;
2724         p2pie[p2pielen++] = 0x50;
2725         p2pie[p2pielen++] = 0x6F;
2726         p2pie[p2pielen++] = 0x9A;
2727         p2pie[p2pielen++] = 0x09;       /*      WFA P2P v1.0 */
2728
2729         /*      Commented by Albert 20101011 */
2730         /*      According to the P2P Specification, the P2P Invitation request frame should contain 7 P2P attributes */
2731         /*      1. Configuration Timeout */
2732         /*      2. Invitation Flags */
2733         /*      3. Operating Channel    (Only GO) */
2734         /*      4. P2P Group BSSID      (Should be included if I am the GO) */
2735         /*      5. Channel List */
2736         /*      6. P2P Group ID */
2737         /*      7. P2P Device Info */
2738
2739         /*      Configuration Timeout */
2740         /*      Type: */
2741         p2pie[p2pielen++] = P2P_ATTR_CONF_TIMEOUT;
2742
2743         /*      Length: */
2744         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
2745         p2pielen += 2;
2746
2747         /*      Value: */
2748         p2pie[p2pielen++] = 200;        /*      2 seconds needed to be the P2P GO */
2749         p2pie[p2pielen++] = 200;        /*      2 seconds needed to be the P2P Client */
2750
2751         /*      Invitation Flags */
2752         /*      Type: */
2753         p2pie[p2pielen++] = P2P_ATTR_INVITATION_FLAGS;
2754
2755         /*      Length: */
2756         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0001);
2757         p2pielen += 2;
2758
2759         /*      Value: */
2760         p2pie[p2pielen++] = P2P_INVITATION_FLAGS_PERSISTENT;
2761
2762         /*      Operating Channel */
2763         /*      Type: */
2764         p2pie[p2pielen++] = P2P_ATTR_OPERATING_CH;
2765
2766         /*      Length: */
2767         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005);
2768         p2pielen += 2;
2769
2770         /*      Value: */
2771         /*      Country String */
2772         p2pie[p2pielen++] = 'X';
2773         p2pie[p2pielen++] = 'X';
2774
2775         /*      The third byte should be set to 0x04. */
2776         /*      Described in the "Operating Channel Attribute" section. */
2777         p2pie[p2pielen++] = 0x04;
2778
2779         /*      Operating Class */
2780         p2pie[p2pielen++] = 0x51;
2781
2782         /*      Channel Number */
2783         p2pie[p2pielen++] = pwdinfo->invitereq_info.operating_ch;       /*      operating channel number */
2784
2785         if (!memcmp(myid(&padapter->eeprompriv), pwdinfo->invitereq_info.go_bssid, ETH_ALEN)) {
2786                 /*      P2P Group BSSID */
2787                 /*      Type: */
2788                 p2pie[p2pielen++] = P2P_ATTR_GROUP_BSSID;
2789
2790                 /*      Length: */
2791                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN);
2792                 p2pielen += 2;
2793
2794                 /*      Value: */
2795                 /*      P2P Device Address for GO */
2796                 memcpy(p2pie + p2pielen, pwdinfo->invitereq_info.go_bssid, ETH_ALEN);
2797                 p2pielen += ETH_ALEN;
2798         }
2799
2800         /*      Channel List */
2801         /*      Type: */
2802         p2pie[p2pielen++] = P2P_ATTR_CH_LIST;
2803
2804         /*      Length: */
2805         /*  Country String(3) */
2806         /*  + (Operating Class (1) + Number of Channels(1)) * Operation Classes (?) */
2807         /*  + number of channels in all classes */
2808         len_channellist_attr = 3
2809            + (1 + 1) * (u16)pmlmeext->channel_list.reg_classes
2810            + get_reg_classes_full_count(&pmlmeext->channel_list);
2811
2812         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(len_channellist_attr);
2813
2814         p2pielen += 2;
2815
2816         /*      Value: */
2817         /*      Country String */
2818         p2pie[p2pielen++] = 'X';
2819         p2pie[p2pielen++] = 'X';
2820
2821         /*      The third byte should be set to 0x04. */
2822         /*      Described in the "Operating Channel Attribute" section. */
2823         p2pie[p2pielen++] = 0x04;
2824
2825         /*      Channel Entry List */
2826         {
2827                 int i, j;
2828                 for (j = 0; j < pmlmeext->channel_list.reg_classes; j++) {
2829                         /*      Operating Class */
2830                         p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].reg_class;
2831
2832                         /*      Number of Channels */
2833                         p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].channels;
2834
2835                         /*      Channel List */
2836                         for (i = 0; i < pmlmeext->channel_list.reg_class[j].channels; i++) {
2837                                 p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].channel[i];
2838                         }
2839                 }
2840         }
2841
2842         /*      P2P Group ID */
2843         /*      Type: */
2844         p2pie[p2pielen++] = P2P_ATTR_GROUP_ID;
2845
2846         /*      Length: */
2847         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(6 + pwdinfo->invitereq_info.ssidlen);
2848         p2pielen += 2;
2849
2850         /*      Value: */
2851         /*      P2P Device Address for GO */
2852         memcpy(p2pie + p2pielen, pwdinfo->invitereq_info.go_bssid, ETH_ALEN);
2853         p2pielen += ETH_ALEN;
2854
2855         /*      SSID */
2856         memcpy(p2pie + p2pielen, pwdinfo->invitereq_info.go_ssid, pwdinfo->invitereq_info.ssidlen);
2857         p2pielen += pwdinfo->invitereq_info.ssidlen;
2858
2859         /*      Device Info */
2860         /*      Type: */
2861         p2pie[p2pielen++] = P2P_ATTR_DEVICE_INFO;
2862
2863         /*      Length: */
2864         /*      21 -> P2P Device Address (6bytes) + Config Methods (2bytes) + Primary Device Type (8bytes) */
2865         /*      + NumofSecondDevType (1byte) + WPS Device Name ID field (2bytes) + WPS Device Name Len field (2bytes) */
2866         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(21 + pwdinfo->device_name_len);
2867         p2pielen += 2;
2868
2869         /*      Value: */
2870         /*      P2P Device Address */
2871         memcpy(p2pie + p2pielen, myid(&padapter->eeprompriv), ETH_ALEN);
2872         p2pielen += ETH_ALEN;
2873
2874         /*      Config Method */
2875         /*      This field should be big endian. Noted by P2P specification. */
2876         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_CONFIG_METHOD_DISPLAY);
2877         p2pielen += 2;
2878
2879         /*      Primary Device Type */
2880         /*      Category ID */
2881         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA);
2882         p2pielen += 2;
2883
2884         /*      OUI */
2885         *(__be32 *)(p2pie + p2pielen) = cpu_to_be32(WPSOUI);
2886         p2pielen  += 4;
2887
2888         /*      Sub Category ID */
2889         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER);
2890         p2pielen += 2;
2891
2892         /*      Number of Secondary Device Types */
2893         p2pie[p2pielen++] = 0x00;       /*      No Secondary Device Type List */
2894
2895         /*      Device Name */
2896         /*      Type: */
2897         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_ATTR_DEVICE_NAME);
2898         p2pielen += 2;
2899
2900         /*      Length: */
2901         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(pwdinfo->device_name_len);
2902         p2pielen += 2;
2903
2904         /*      Value: */
2905         memcpy(p2pie + p2pielen, pwdinfo->device_name, pwdinfo->device_name_len);
2906         p2pielen += pwdinfo->device_name_len;
2907
2908         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *)p2pie, &pattrib->pktlen);
2909
2910         pattrib->last_txcmdsz = pattrib->pktlen;
2911
2912         dump_mgntframe(padapter, pmgntframe);
2913 }
2914
2915 void issue_p2p_invitation_response(struct adapter *padapter, u8 *raddr, u8 dialogToken, u8 status_code)
2916 {
2917         unsigned char category = RTW_WLAN_CATEGORY_PUBLIC;
2918         u8 action = P2P_PUB_ACTION_ACTION;
2919         __be32                  p2poui = cpu_to_be32(P2POUI);
2920         u8 oui_subtype = P2P_INVIT_RESP;
2921         u8 p2pie[255] = { 0x00 };
2922         u8 p2pielen = 0;
2923         u16 len_channellist_attr = 0;
2924         struct xmit_frame                       *pmgntframe;
2925         struct pkt_attrib                       *pattrib;
2926         unsigned char                                   *pframe;
2927         struct rtw_ieee80211_hdr        *pwlanhdr;
2928         __le16 *fctrl;
2929         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
2930         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
2931         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
2932
2933         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2934         if (!pmgntframe)
2935                 return;
2936
2937         /* update attribute */
2938         pattrib = &pmgntframe->attrib;
2939         update_mgntframe_attrib(padapter, pattrib);
2940
2941         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2942
2943         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2944         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
2945
2946         fctrl = &(pwlanhdr->frame_ctl);
2947         *(fctrl) = 0;
2948
2949         memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
2950         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
2951         memcpy(pwlanhdr->addr3, raddr,  ETH_ALEN);
2952
2953         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
2954         pmlmeext->mgnt_seq++;
2955         SetFrameSubType(pframe, WIFI_ACTION);
2956
2957         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
2958         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
2959
2960         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
2961         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
2962         pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *)&(p2poui), &(pattrib->pktlen));
2963         pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pattrib->pktlen));
2964         pframe = rtw_set_fixed_ie(pframe, 1, &(dialogToken), &(pattrib->pktlen));
2965
2966         /*      P2P IE Section. */
2967
2968         /*      P2P OUI */
2969         p2pielen = 0;
2970         p2pie[p2pielen++] = 0x50;
2971         p2pie[p2pielen++] = 0x6F;
2972         p2pie[p2pielen++] = 0x9A;
2973         p2pie[p2pielen++] = 0x09;       /*      WFA P2P v1.0 */
2974
2975         /*      Commented by Albert 20101005 */
2976         /*      According to the P2P Specification, the P2P Invitation response frame should contain 5 P2P attributes */
2977         /*      1. Status */
2978         /*      2. Configuration Timeout */
2979         /*      3. Operating Channel    (Only GO) */
2980         /*      4. P2P Group BSSID      (Only GO) */
2981         /*      5. Channel List */
2982
2983         /*      P2P Status */
2984         /*      Type: */
2985         p2pie[p2pielen++] = P2P_ATTR_STATUS;
2986
2987         /*      Length: */
2988         *(__le16  *)(p2pie + p2pielen) = cpu_to_le16(0x0001);
2989         p2pielen += 2;
2990
2991         /*      Value: */
2992         /*      When status code is P2P_STATUS_FAIL_INFO_UNAVAILABLE. */
2993         /*      Sent the event receiving the P2P Invitation Req frame to DMP UI. */
2994         /*      DMP had to compare the MAC address to find out the profile. */
2995         /*      So, the WiFi driver will send the P2P_STATUS_FAIL_INFO_UNAVAILABLE to NB. */
2996         /*      If the UI found the corresponding profile, the WiFi driver sends the P2P Invitation Req */
2997         /*      to NB to rebuild the persistent group. */
2998         p2pie[p2pielen++] = status_code;
2999
3000         /*      Configuration Timeout */
3001         /*      Type: */
3002         p2pie[p2pielen++] = P2P_ATTR_CONF_TIMEOUT;
3003
3004         /*      Length: */
3005         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
3006         p2pielen += 2;
3007
3008         /*      Value: */
3009         p2pie[p2pielen++] = 200;        /*      2 seconds needed to be the P2P GO */
3010         p2pie[p2pielen++] = 200;        /*      2 seconds needed to be the P2P Client */
3011
3012         if (status_code == P2P_STATUS_SUCCESS) {
3013                 if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) {
3014                         /*      The P2P Invitation request frame asks this Wi-Fi device to be the P2P GO */
3015                         /*      In this case, the P2P Invitation response frame should carry the two more P2P attributes. */
3016                         /*      First one is operating channel attribute. */
3017                         /*      Second one is P2P Group BSSID attribute. */
3018
3019                         /*      Operating Channel */
3020                         /*      Type: */
3021                         p2pie[p2pielen++] = P2P_ATTR_OPERATING_CH;
3022
3023                         /*      Length: */
3024                         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005);
3025                         p2pielen += 2;
3026
3027                         /*      Value: */
3028                         /*      Country String */
3029                         p2pie[p2pielen++] = 'X';
3030                         p2pie[p2pielen++] = 'X';
3031
3032                         /*      The third byte should be set to 0x04. */
3033                         /*      Described in the "Operating Channel Attribute" section. */
3034                         p2pie[p2pielen++] = 0x04;
3035
3036                         /*      Operating Class */
3037                         p2pie[p2pielen++] = 0x51;       /*      Copy from SD7 */
3038
3039                         /*      Channel Number */
3040                         p2pie[p2pielen++] = pwdinfo->operating_channel; /*      operating channel number */
3041
3042                         /*      P2P Group BSSID */
3043                         /*      Type: */
3044                         p2pie[p2pielen++] = P2P_ATTR_GROUP_BSSID;
3045
3046                         /*      Length: */
3047                         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN);
3048                         p2pielen += 2;
3049
3050                         /*      Value: */
3051                         /*      P2P Device Address for GO */
3052                         memcpy(p2pie + p2pielen, myid(&padapter->eeprompriv), ETH_ALEN);
3053                         p2pielen += ETH_ALEN;
3054                 }
3055
3056                 /*      Channel List */
3057                 /*      Type: */
3058                 p2pie[p2pielen++] = P2P_ATTR_CH_LIST;
3059
3060                 /*      Length: */
3061                 /*  Country String(3) */
3062                 /*  + (Operating Class (1) + Number of Channels(1)) * Operation Classes (?) */
3063                 /*  + number of channels in all classes */
3064                 len_channellist_attr = 3
3065                         + (1 + 1) * (u16)pmlmeext->channel_list.reg_classes
3066                         + get_reg_classes_full_count(&pmlmeext->channel_list);
3067
3068                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(len_channellist_attr);
3069                 p2pielen += 2;
3070
3071                 /*      Value: */
3072                 /*      Country String */
3073                 p2pie[p2pielen++] = 'X';
3074                 p2pie[p2pielen++] = 'X';
3075
3076                 /*      The third byte should be set to 0x04. */
3077                 /*      Described in the "Operating Channel Attribute" section. */
3078                 p2pie[p2pielen++] = 0x04;
3079
3080                 /*      Channel Entry List */
3081                 {
3082                         int i, j;
3083                         for (j = 0; j < pmlmeext->channel_list.reg_classes; j++) {
3084                                 /*      Operating Class */
3085                                 p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].reg_class;
3086
3087                                 /*      Number of Channels */
3088                                 p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].channels;
3089
3090                                 /*      Channel List */
3091                                 for (i = 0; i < pmlmeext->channel_list.reg_class[j].channels; i++) {
3092                                         p2pie[p2pielen++] = pmlmeext->channel_list.reg_class[j].channel[i];
3093                                 }
3094                         }
3095                 }
3096         }
3097
3098         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *)p2pie, &pattrib->pktlen);
3099
3100         pattrib->last_txcmdsz = pattrib->pktlen;
3101
3102         dump_mgntframe(padapter, pmgntframe);
3103 }
3104
3105 void issue_p2p_provision_request(struct adapter *padapter, u8 *pssid, u8 ussidlen, u8 *pdev_raddr)
3106 {
3107         unsigned char category = RTW_WLAN_CATEGORY_PUBLIC;
3108         u8 action = P2P_PUB_ACTION_ACTION;
3109         u8 dialogToken = 1;
3110         u8 oui_subtype = P2P_PROVISION_DISC_REQ;
3111         u8 wpsie[100] = { 0x00 };
3112         u8 wpsielen = 0;
3113         __be32 p2poui = cpu_to_be32(P2POUI);
3114         u32                     p2pielen = 0;
3115         struct xmit_frame                       *pmgntframe;
3116         struct pkt_attrib                       *pattrib;
3117         unsigned char                                   *pframe;
3118         struct rtw_ieee80211_hdr        *pwlanhdr;
3119         __le16 *fctrl;
3120         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
3121         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
3122         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
3123
3124         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3125         if (!pmgntframe)
3126                 return;
3127
3128         DBG_88E("[%s] In\n", __func__);
3129         /* update attribute */
3130         pattrib = &pmgntframe->attrib;
3131         update_mgntframe_attrib(padapter, pattrib);
3132
3133         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3134
3135         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3136         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
3137
3138         fctrl = &(pwlanhdr->frame_ctl);
3139         *(fctrl) = 0;
3140
3141         memcpy(pwlanhdr->addr1, pdev_raddr, ETH_ALEN);
3142         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
3143         memcpy(pwlanhdr->addr3, pdev_raddr, ETH_ALEN);
3144
3145         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3146         pmlmeext->mgnt_seq++;
3147         SetFrameSubType(pframe, WIFI_ACTION);
3148
3149         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
3150         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
3151
3152         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
3153         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
3154         pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *)&(p2poui), &(pattrib->pktlen));
3155         pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pattrib->pktlen));
3156         pframe = rtw_set_fixed_ie(pframe, 1, &(dialogToken), &(pattrib->pktlen));
3157
3158         p2pielen = build_prov_disc_request_p2p_ie(pwdinfo, pframe, pssid, ussidlen, pdev_raddr);
3159
3160         pframe += p2pielen;
3161         pattrib->pktlen += p2pielen;
3162
3163         wpsielen = 0;
3164         /*      WPS OUI */
3165         *(__be32 *)(wpsie) = cpu_to_be32(WPSOUI);
3166         wpsielen += 4;
3167
3168         /*      WPS version */
3169         /*      Type: */
3170         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1);
3171         wpsielen += 2;
3172
3173         /*      Length: */
3174         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001);
3175         wpsielen += 2;
3176
3177         /*      Value: */
3178         wpsie[wpsielen++] = WPS_VERSION_1;      /*      Version 1.0 */
3179
3180         /*      Config Method */
3181         /*      Type: */
3182         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_CONF_METHOD);
3183         wpsielen += 2;
3184
3185         /*      Length: */
3186         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002);
3187         wpsielen += 2;
3188
3189         /*      Value: */
3190         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(pwdinfo->tx_prov_disc_info.wps_config_method_request);
3191         wpsielen += 2;
3192
3193         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *)wpsie, &pattrib->pktlen);
3194
3195         pattrib->last_txcmdsz = pattrib->pktlen;
3196
3197         dump_mgntframe(padapter, pmgntframe);
3198 }
3199
3200 static u8 is_matched_in_profilelist(u8 *peermacaddr, struct profile_info *profileinfo)
3201 {
3202         u8 i, match_result = 0;
3203
3204         DBG_88E("[%s] peermac=%.2X %.2X %.2X %.2X %.2X %.2X\n", __func__,
3205                 peermacaddr[0], peermacaddr[1], peermacaddr[2], peermacaddr[3], peermacaddr[4], peermacaddr[5]);
3206
3207         for (i = 0; i < P2P_MAX_PERSISTENT_GROUP_NUM; i++, profileinfo++) {
3208                DBG_88E("[%s] profileinfo_mac=%.2X %.2X %.2X %.2X %.2X %.2X\n", __func__,
3209                             profileinfo->peermac[0], profileinfo->peermac[1], profileinfo->peermac[2], profileinfo->peermac[3], profileinfo->peermac[4], profileinfo->peermac[5]);
3210                 if (!memcmp(peermacaddr, profileinfo->peermac, ETH_ALEN)) {
3211                         match_result = 1;
3212                         DBG_88E("[%s] Match!\n", __func__);
3213                         break;
3214                 }
3215         }
3216         return match_result;
3217 }
3218
3219 void issue_probersp_p2p(struct adapter *padapter, unsigned char *da)
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         unsigned char                                   *mac;
3227         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
3228         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
3229         u16 beacon_interval = 100;
3230         u16 capInfo = 0;
3231         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
3232         u8 wpsie[255] = { 0x00 };
3233         u32                                     wpsielen = 0, p2pielen = 0;
3234
3235         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3236         if (!pmgntframe)
3237                 return;
3238
3239         /* update attribute */
3240         pattrib = &pmgntframe->attrib;
3241         update_mgntframe_attrib(padapter, pattrib);
3242
3243         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3244
3245         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3246         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
3247
3248         mac = myid(&(padapter->eeprompriv));
3249
3250         fctrl = &(pwlanhdr->frame_ctl);
3251         *(fctrl) = 0;
3252         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
3253         memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
3254
3255         /*      Use the device address for BSSID field. */
3256         memcpy(pwlanhdr->addr3, mac, ETH_ALEN);
3257
3258         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3259         pmlmeext->mgnt_seq++;
3260         SetFrameSubType(fctrl, WIFI_PROBERSP);
3261
3262         pattrib->hdrlen = sizeof(struct rtw_ieee80211_hdr_3addr);
3263         pattrib->pktlen = pattrib->hdrlen;
3264         pframe += pattrib->hdrlen;
3265
3266         /* timestamp will be inserted by hardware */
3267         pframe += 8;
3268         pattrib->pktlen += 8;
3269
3270         /*  beacon interval: 2 bytes */
3271         memcpy(pframe, (unsigned char *)&beacon_interval, 2);
3272         pframe += 2;
3273         pattrib->pktlen += 2;
3274
3275         /*      capability info: 2 bytes */
3276         /*      ESS and IBSS bits must be 0 (defined in the 3.1.2.1.1 of WiFi Direct Spec) */
3277         capInfo |= cap_ShortPremble;
3278         capInfo |= cap_ShortSlot;
3279
3280         memcpy(pframe, (unsigned char *)&capInfo, 2);
3281         pframe += 2;
3282         pattrib->pktlen += 2;
3283
3284         /*  SSID */
3285         pframe = rtw_set_ie(pframe, _SSID_IE_, 7, pwdinfo->p2p_wildcard_ssid, &pattrib->pktlen);
3286
3287         /*  supported rates... */
3288         /*      Use the OFDM rate in the P2P probe response frame. (6(B), 9(B), 12, 18, 24, 36, 48, 54) */
3289         pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, pwdinfo->support_rate, &pattrib->pktlen);
3290
3291         /*  DS parameter set */
3292         pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&pwdinfo->listen_channel, &pattrib->pktlen);
3293
3294         /*      Todo: WPS IE */
3295         /*      Noted by Albert 20100907 */
3296         /*      According to the WPS specification, all the WPS attribute is presented by Big Endian. */
3297
3298         wpsielen = 0;
3299         /*      WPS OUI */
3300         *(__be32 *)(wpsie) = cpu_to_be32(WPSOUI);
3301         wpsielen += 4;
3302
3303         /*      WPS version */
3304         /*      Type: */
3305         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1);
3306         wpsielen += 2;
3307
3308         /*      Length: */
3309         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001);
3310         wpsielen += 2;
3311
3312         /*      Value: */
3313         wpsie[wpsielen++] = WPS_VERSION_1;      /*      Version 1.0 */
3314
3315         /*      WiFi Simple Config State */
3316         /*      Type: */
3317         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_SIMPLE_CONF_STATE);
3318         wpsielen += 2;
3319
3320         /*      Length: */
3321         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001);
3322         wpsielen += 2;
3323
3324         /*      Value: */
3325         wpsie[wpsielen++] = WPS_WSC_STATE_NOT_CONFIG;   /*      Not Configured. */
3326
3327         /*      Response Type */
3328         /*      Type: */
3329         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_RESP_TYPE);
3330         wpsielen += 2;
3331
3332         /*      Length: */
3333         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001);
3334         wpsielen += 2;
3335
3336         /*      Value: */
3337         wpsie[wpsielen++] = WPS_RESPONSE_TYPE_8021X;
3338
3339         /*      UUID-E */
3340         /*      Type: */
3341         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_UUID_E);
3342         wpsielen += 2;
3343
3344         /*      Length: */
3345         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0010);
3346         wpsielen += 2;
3347
3348         /*      Value: */
3349         memcpy(wpsie + wpsielen, myid(&padapter->eeprompriv), ETH_ALEN);
3350         wpsielen += 0x10;
3351
3352         /*      Manufacturer */
3353         /*      Type: */
3354         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_MANUFACTURER);
3355         wpsielen += 2;
3356
3357         /*      Length: */
3358         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0007);
3359         wpsielen += 2;
3360
3361         /*      Value: */
3362         memcpy(wpsie + wpsielen, "Realtek", 7);
3363         wpsielen += 7;
3364
3365         /*      Model Name */
3366         /*      Type: */
3367         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_MODEL_NAME);
3368         wpsielen += 2;
3369
3370         /*      Length: */
3371         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0006);
3372         wpsielen += 2;
3373
3374         /*      Value: */
3375         memcpy(wpsie + wpsielen, "8188EU", 6);
3376         wpsielen += 6;
3377
3378         /*      Model Number */
3379         /*      Type: */
3380         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_MODEL_NUMBER);
3381         wpsielen += 2;
3382
3383         /*      Length: */
3384         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001);
3385         wpsielen += 2;
3386
3387         /*      Value: */
3388         wpsie[wpsielen++] = 0x31;               /*      character 1 */
3389
3390         /*      Serial Number */
3391         /*      Type: */
3392         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_SERIAL_NUMBER);
3393         wpsielen += 2;
3394
3395         /*      Length: */
3396         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(ETH_ALEN);
3397         wpsielen += 2;
3398
3399         /*      Value: */
3400         memcpy(wpsie + wpsielen, "123456", ETH_ALEN);
3401         wpsielen += ETH_ALEN;
3402
3403         /*      Primary Device Type */
3404         /*      Type: */
3405         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_PRIMARY_DEV_TYPE);
3406         wpsielen += 2;
3407
3408         /*      Length: */
3409         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0008);
3410         wpsielen += 2;
3411
3412         /*      Value: */
3413         /*      Category ID */
3414         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA);
3415         wpsielen += 2;
3416
3417         /*      OUI */
3418         *(__be32 *)(wpsie + wpsielen) = cpu_to_be32(WPSOUI);
3419         wpsielen += 4;
3420
3421         /*      Sub Category ID */
3422         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER);
3423         wpsielen += 2;
3424
3425         /*      Device Name */
3426         /*      Type: */
3427         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_DEVICE_NAME);
3428         wpsielen += 2;
3429
3430         /*      Length: */
3431         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(pwdinfo->device_name_len);
3432         wpsielen += 2;
3433
3434         /*      Value: */
3435         if (pwdinfo->device_name_len) {
3436                 memcpy(wpsie + wpsielen, pwdinfo->device_name, pwdinfo->device_name_len);
3437                 wpsielen += pwdinfo->device_name_len;
3438         }
3439
3440         /*      Config Method */
3441         /*      Type: */
3442         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_CONF_METHOD);
3443         wpsielen += 2;
3444
3445         /*      Length: */
3446         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002);
3447         wpsielen += 2;
3448
3449         /*      Value: */
3450         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(pwdinfo->supported_wps_cm);
3451         wpsielen += 2;
3452
3453         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *)wpsie, &pattrib->pktlen);
3454
3455         p2pielen = build_probe_resp_p2p_ie(pwdinfo, pframe);
3456         pframe += p2pielen;
3457         pattrib->pktlen += p2pielen;
3458
3459         pattrib->last_txcmdsz = pattrib->pktlen;
3460
3461         dump_mgntframe(padapter, pmgntframe);
3462 }
3463
3464 static int _issue_probereq_p2p(struct adapter *padapter, u8 *da, int wait_ack)
3465 {
3466         int ret = _FAIL;
3467         struct xmit_frame               *pmgntframe;
3468         struct pkt_attrib               *pattrib;
3469         unsigned char                   *pframe;
3470         struct rtw_ieee80211_hdr        *pwlanhdr;
3471         __le16 *fctrl;
3472         unsigned char                   *mac;
3473         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
3474         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
3475         u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3476         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
3477         u8 wpsie[255] = { 0x00 }, p2pie[255] = { 0x00 };
3478         u16 wpsielen = 0, p2pielen = 0;
3479         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3480
3481         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3482         if (!pmgntframe)
3483                 goto exit;
3484
3485         /* update attribute */
3486         pattrib = &pmgntframe->attrib;
3487         update_mgntframe_attrib(padapter, pattrib);
3488
3489         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3490
3491         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3492         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
3493
3494         mac = myid(&(padapter->eeprompriv));
3495
3496         fctrl = &(pwlanhdr->frame_ctl);
3497         *(fctrl) = 0;
3498
3499         if (da) {
3500                 memcpy(pwlanhdr->addr1, da, ETH_ALEN);
3501                 memcpy(pwlanhdr->addr3, da, ETH_ALEN);
3502         } else {
3503                 if ((pwdinfo->p2p_info.scan_op_ch_only) || (pwdinfo->rx_invitereq_info.scan_op_ch_only)) {
3504                         /*      This two flags will be set when this is only the P2P client mode. */
3505                         memcpy(pwlanhdr->addr1, pwdinfo->p2p_peer_interface_addr, ETH_ALEN);
3506                         memcpy(pwlanhdr->addr3, pwdinfo->p2p_peer_interface_addr, ETH_ALEN);
3507                 } else {
3508                         /*      broadcast probe request frame */
3509                         memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN);
3510                         memcpy(pwlanhdr->addr3, bc_addr, ETH_ALEN);
3511                 }
3512         }
3513         memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
3514
3515         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3516         pmlmeext->mgnt_seq++;
3517         SetFrameSubType(pframe, WIFI_PROBEREQ);
3518
3519         pframe += sizeof (struct rtw_ieee80211_hdr_3addr);
3520         pattrib->pktlen = sizeof (struct rtw_ieee80211_hdr_3addr);
3521
3522         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_TX_PROVISION_DIS_REQ))
3523                 pframe = rtw_set_ie(pframe, _SSID_IE_, pwdinfo->tx_prov_disc_info.ssid.SsidLength, pwdinfo->tx_prov_disc_info.ssid.Ssid, &(pattrib->pktlen));
3524         else
3525                 pframe = rtw_set_ie(pframe, _SSID_IE_, P2P_WILDCARD_SSID_LEN, pwdinfo->p2p_wildcard_ssid, &(pattrib->pktlen));
3526
3527         /*      Use the OFDM rate in the P2P probe request frame. (6(B), 9(B), 12(B), 24(B), 36, 48, 54) */
3528         pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, pwdinfo->support_rate, &pattrib->pktlen);
3529
3530         /*      WPS IE */
3531         /*      Noted by Albert 20110221 */
3532         /*      According to the WPS specification, all the WPS attribute is presented by Big Endian. */
3533
3534         wpsielen = 0;
3535         /*      WPS OUI */
3536         *(__be32 *)(wpsie) = cpu_to_be32(WPSOUI);
3537         wpsielen += 4;
3538
3539         /*      WPS version */
3540         /*      Type: */
3541         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1);
3542         wpsielen += 2;
3543
3544         /*      Length: */
3545         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001);
3546         wpsielen += 2;
3547
3548         /*      Value: */
3549         wpsie[wpsielen++] = WPS_VERSION_1;      /*      Version 1.0 */
3550
3551         if (!pmlmepriv->wps_probe_req_ie) {
3552                 /*      UUID-E */
3553                 /*      Type: */
3554                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_UUID_E);
3555                 wpsielen += 2;
3556
3557                 /*      Length: */
3558                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0010);
3559                 wpsielen += 2;
3560
3561                 /*      Value: */
3562                 memcpy(wpsie + wpsielen, myid(&padapter->eeprompriv), ETH_ALEN);
3563                 wpsielen += 0x10;
3564
3565                 /*      Config Method */
3566                 /*      Type: */
3567                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_CONF_METHOD);
3568                 wpsielen += 2;
3569
3570                 /*      Length: */
3571                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002);
3572                 wpsielen += 2;
3573
3574                 /*      Value: */
3575                 *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(pwdinfo->supported_wps_cm);
3576                 wpsielen += 2;
3577         }
3578
3579         /*      Device Name */
3580         /*      Type: */
3581         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_DEVICE_NAME);
3582         wpsielen += 2;
3583
3584         /*      Length: */
3585         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(pwdinfo->device_name_len);
3586         wpsielen += 2;
3587
3588         /*      Value: */
3589         memcpy(wpsie + wpsielen, pwdinfo->device_name, pwdinfo->device_name_len);
3590         wpsielen += pwdinfo->device_name_len;
3591
3592         /*      Primary Device Type */
3593         /*      Type: */
3594         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_PRIMARY_DEV_TYPE);
3595         wpsielen += 2;
3596
3597         /*      Length: */
3598         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0008);
3599         wpsielen += 2;
3600
3601         /*      Value: */
3602         /*      Category ID */
3603         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_PDT_CID_RTK_WIDI);
3604         wpsielen += 2;
3605
3606         /*      OUI */
3607         *(__be32 *)(wpsie + wpsielen) = cpu_to_be32(WPSOUI);
3608         wpsielen += 4;
3609
3610         /*      Sub Category ID */
3611         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_PDT_SCID_RTK_DMP);
3612         wpsielen += 2;
3613
3614         /*      Device Password ID */
3615         /*      Type: */
3616         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_DEVICE_PWID);
3617         wpsielen += 2;
3618
3619         /*      Length: */
3620         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002);
3621         wpsielen += 2;
3622
3623         /*      Value: */
3624         *(__be16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_REGISTRAR_SPEC);   /*      Registrar-specified */
3625         wpsielen += 2;
3626
3627         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *)wpsie, &pattrib->pktlen);
3628
3629         /*      P2P OUI */
3630         p2pielen = 0;
3631         p2pie[p2pielen++] = 0x50;
3632         p2pie[p2pielen++] = 0x6F;
3633         p2pie[p2pielen++] = 0x9A;
3634         p2pie[p2pielen++] = 0x09;       /*      WFA P2P v1.0 */
3635
3636         /*      Commented by Albert 20110221 */
3637         /*      According to the P2P Specification, the probe request frame should contain 5 P2P attributes */
3638         /*      1. P2P Capability */
3639         /*      2. P2P Device ID if this probe request wants to find the specific P2P device */
3640         /*      3. Listen Channel */
3641         /*      4. Extended Listen Timing */
3642         /*      5. Operating Channel if this WiFi is working as the group owner now */
3643
3644         /*      P2P Capability */
3645         /*      Type: */
3646         p2pie[p2pielen++] = P2P_ATTR_CAPABILITY;
3647
3648         /*      Length: */
3649         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
3650         p2pielen += 2;
3651
3652         /*      Value: */
3653         /*      Device Capability Bitmap, 1 byte */
3654         p2pie[p2pielen++] = DMP_P2P_DEVCAP_SUPPORT;
3655
3656         /*      Group Capability Bitmap, 1 byte */
3657         if (pwdinfo->persistent_supported)
3658                 p2pie[p2pielen++] = P2P_GRPCAP_PERSISTENT_GROUP | DMP_P2P_GRPCAP_SUPPORT;
3659         else
3660                 p2pie[p2pielen++] = DMP_P2P_GRPCAP_SUPPORT;
3661
3662         /*      Listen Channel */
3663         /*      Type: */
3664         p2pie[p2pielen++] = P2P_ATTR_LISTEN_CH;
3665
3666         /*      Length: */
3667         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005);
3668         p2pielen += 2;
3669
3670         /*      Value: */
3671         /*      Country String */
3672         p2pie[p2pielen++] = 'X';
3673         p2pie[p2pielen++] = 'X';
3674
3675         /*      The third byte should be set to 0x04. */
3676         /*      Described in the "Operating Channel Attribute" section. */
3677         p2pie[p2pielen++] = 0x04;
3678
3679         /*      Operating Class */
3680         p2pie[p2pielen++] = 0x51;       /*      Copy from SD7 */
3681
3682         /*      Channel Number */
3683         p2pie[p2pielen++] = pwdinfo->listen_channel;    /*      listen channel */
3684
3685         /*      Extended Listen Timing */
3686         /*      Type: */
3687         p2pie[p2pielen++] = P2P_ATTR_EX_LISTEN_TIMING;
3688
3689         /*      Length: */
3690         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0004);
3691         p2pielen += 2;
3692
3693         /*      Value: */
3694         /*      Availability Period */
3695         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0xFFFF);
3696         p2pielen += 2;
3697
3698         /*      Availability Interval */
3699         *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0xFFFF);
3700         p2pielen += 2;
3701
3702         if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) {
3703                 /*      Operating Channel (if this WiFi is working as the group owner now) */
3704                 /*      Type: */
3705                 p2pie[p2pielen++] = P2P_ATTR_OPERATING_CH;
3706
3707                 /*      Length: */
3708                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005);
3709                 p2pielen += 2;
3710
3711                 /*      Value: */
3712                 /*      Country String */
3713                 p2pie[p2pielen++] = 'X';
3714                 p2pie[p2pielen++] = 'X';
3715
3716                 /*      The third byte should be set to 0x04. */
3717                 /*      Described in the "Operating Channel Attribute" section. */
3718                 p2pie[p2pielen++] = 0x04;
3719
3720                 /*      Operating Class */
3721                 p2pie[p2pielen++] = 0x51;       /*      Copy from SD7 */
3722
3723                 /*      Channel Number */
3724                 p2pie[p2pielen++] = pwdinfo->operating_channel; /*      operating channel number */
3725         }
3726
3727         pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *)p2pie, &pattrib->pktlen);
3728
3729         if (pmlmepriv->wps_probe_req_ie) {
3730                 /* WPS IE */
3731                 memcpy(pframe, pmlmepriv->wps_probe_req_ie, pmlmepriv->wps_probe_req_ie_len);
3732                 pattrib->pktlen += pmlmepriv->wps_probe_req_ie_len;
3733                 pframe += pmlmepriv->wps_probe_req_ie_len;
3734         }
3735
3736         pattrib->last_txcmdsz = pattrib->pktlen;
3737
3738         if (wait_ack) {
3739                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
3740         } else {
3741                 dump_mgntframe(padapter, pmgntframe);
3742                 ret = _SUCCESS;
3743         }
3744
3745 exit:
3746         return ret;
3747 }
3748
3749 inline void issue_probereq_p2p(struct adapter *adapter, u8 *da)
3750 {
3751         _issue_probereq_p2p(adapter, da, false);
3752 }
3753
3754 int issue_probereq_p2p_ex(struct adapter *adapter, u8 *da, int try_cnt, int wait_ms)
3755 {
3756         int ret;
3757         int i = 0;
3758         u32 start = jiffies;
3759
3760         do {
3761                 ret = _issue_probereq_p2p(adapter, da, wait_ms > 0 ? true : false);
3762
3763                 i++;
3764
3765                 if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
3766                         break;
3767
3768                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
3769                         msleep(wait_ms);
3770         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
3771
3772         if (ret != _FAIL) {
3773                 ret = _SUCCESS;
3774                 goto exit;
3775         }
3776
3777         if (try_cnt && wait_ms) {
3778                 if (da)
3779                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
3780                                 FUNC_ADPT_ARG(adapter), da, rtw_get_oper_ch(adapter),
3781                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
3782                 else
3783                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
3784                                 FUNC_ADPT_ARG(adapter), rtw_get_oper_ch(adapter),
3785                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
3786         }
3787 exit:
3788         return ret;
3789 }
3790
3791 #endif /* CONFIG_88EU_P2P */
3792
3793 static s32 rtw_action_public_decache(struct recv_frame *recv_frame, s32 token)
3794 {
3795         struct adapter *adapter = recv_frame->adapter;
3796         struct mlme_ext_priv *mlmeext = &(adapter->mlmeextpriv);
3797         u8 *frame = recv_frame->rx_data;
3798         u16 seq_ctrl = ((recv_frame->attrib.seq_num&0xffff) << 4) |
3799                 (recv_frame->attrib.frag_num & 0xf);
3800
3801         if (GetRetry(frame)) {
3802                 if (token >= 0) {
3803                         if ((seq_ctrl == mlmeext->action_public_rxseq) && (token == mlmeext->action_public_dialog_token)) {
3804                                 DBG_88E(FUNC_ADPT_FMT" seq_ctrl = 0x%x, rxseq = 0x%x, token:%d\n",
3805                                         FUNC_ADPT_ARG(adapter), seq_ctrl, mlmeext->action_public_rxseq, token);
3806                                 return _FAIL;
3807                         }
3808                 } else {
3809                         if (seq_ctrl == mlmeext->action_public_rxseq) {
3810                                 DBG_88E(FUNC_ADPT_FMT" seq_ctrl = 0x%x, rxseq = 0x%x\n",
3811                                         FUNC_ADPT_ARG(adapter), seq_ctrl, mlmeext->action_public_rxseq);
3812                                 return _FAIL;
3813                         }
3814                 }
3815         }
3816
3817         mlmeext->action_public_rxseq = seq_ctrl;
3818
3819         if (token >= 0)
3820                 mlmeext->action_public_dialog_token = token;
3821
3822         return _SUCCESS;
3823 }
3824
3825 static unsigned int on_action_public_p2p(struct recv_frame *precv_frame)
3826 {
3827         u8 *pframe = precv_frame->rx_data;
3828         u8 *frame_body;
3829         u8 dialogToken = 0;
3830 #ifdef CONFIG_88EU_P2P
3831         struct adapter *padapter = precv_frame->adapter;
3832         uint len = precv_frame->len;
3833         u8 *p2p_ie;
3834         u32     p2p_ielen;
3835         struct  wifidirect_info *pwdinfo = &(padapter->wdinfo);
3836         u8      result = P2P_STATUS_SUCCESS;
3837         u8      empty_addr[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3838 #endif /* CONFIG_88EU_P2P */
3839
3840         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
3841
3842         dialogToken = frame_body[7];
3843
3844         if (rtw_action_public_decache(precv_frame, dialogToken) == _FAIL)
3845                 return _FAIL;
3846
3847 #ifdef CONFIG_88EU_P2P
3848         _cancel_timer_ex(&pwdinfo->reset_ch_sitesurvey);
3849         /*      Do nothing if the driver doesn't enable the P2P function. */
3850         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE) || rtw_p2p_chk_state(pwdinfo, P2P_STATE_IDLE))
3851                 return _SUCCESS;
3852
3853         len -= sizeof(struct rtw_ieee80211_hdr_3addr);
3854
3855         switch (frame_body[6]) { /* OUI Subtype */
3856         case P2P_GO_NEGO_REQ:
3857                 DBG_88E("[%s] Got GO Nego Req Frame\n", __func__);
3858                 memset(&pwdinfo->groupid_info, 0x00, sizeof(struct group_id_info));
3859
3860                 if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_RX_PROVISION_DIS_REQ))
3861                         rtw_p2p_set_state(pwdinfo, rtw_p2p_pre_state(pwdinfo));
3862
3863                 if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_GONEGO_FAIL)) {
3864                         /*      Commented by Albert 20110526 */
3865                         /*      In this case, this means the previous nego fail doesn't be reset yet. */
3866                         _cancel_timer_ex(&pwdinfo->restore_p2p_state_timer);
3867                         /*      Restore the previous p2p state */
3868                         rtw_p2p_set_state(pwdinfo, rtw_p2p_pre_state(pwdinfo));
3869                         DBG_88E("[%s] Restore the previous p2p state to %d\n", __func__, rtw_p2p_state(pwdinfo));
3870                 }
3871
3872                 /*      Commented by Kurt 20110902 */
3873                 /* Add if statement to avoid receiving duplicate prov disc req. such that pre_p2p_state would be covered. */
3874                 if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_GONEGO_ING))
3875                         rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo));
3876
3877                 /*      Commented by Kurt 20120113 */
3878                 /*      Get peer_dev_addr here if peer doesn't issue prov_disc frame. */
3879                 if (!memcmp(pwdinfo->rx_prov_disc_info.peerDevAddr, empty_addr, ETH_ALEN))
3880                         memcpy(pwdinfo->rx_prov_disc_info.peerDevAddr, GetAddr2Ptr(pframe), ETH_ALEN);
3881
3882                 result = process_p2p_group_negotation_req(pwdinfo, frame_body, len);
3883                 issue_p2p_GO_response(padapter, GetAddr2Ptr(pframe), frame_body, len, result);
3884
3885                 /*      Commented by Albert 20110718 */
3886                 /*      No matter negotiating or negotiation failure, the driver should set up the restore P2P state timer. */
3887                 _set_timer(&pwdinfo->restore_p2p_state_timer, 5000);
3888                 break;
3889         case P2P_GO_NEGO_RESP:
3890                 DBG_88E("[%s] Got GO Nego Resp Frame\n", __func__);
3891
3892                 if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_GONEGO_ING)) {
3893                         /*      Commented by Albert 20110425 */
3894                         /*      The restore timer is enabled when issuing the nego request frame of rtw_p2p_connect function. */
3895                         _cancel_timer_ex(&pwdinfo->restore_p2p_state_timer);
3896                         pwdinfo->nego_req_info.benable = false;
3897                         result = process_p2p_group_negotation_resp(pwdinfo, frame_body, len);
3898                         issue_p2p_GO_confirm(pwdinfo->padapter, GetAddr2Ptr(pframe), result);
3899                         if (P2P_STATUS_SUCCESS == result) {
3900                                 if (rtw_p2p_role(pwdinfo) == P2P_ROLE_CLIENT) {
3901                                         pwdinfo->p2p_info.operation_ch[0] = pwdinfo->peer_operating_ch;
3902                                         pwdinfo->p2p_info.scan_op_ch_only = 1;
3903                                         _set_timer(&pwdinfo->reset_ch_sitesurvey2, P2P_RESET_SCAN_CH);
3904                                 }
3905                         }
3906                         /*      Reset the dialog token for group negotiation frames. */
3907                         pwdinfo->negotiation_dialog_token = 1;
3908                         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_GONEGO_FAIL))
3909                                 _set_timer(&pwdinfo->restore_p2p_state_timer, 5000);
3910                 } else {
3911                         DBG_88E("[%s] Skipped GO Nego Resp Frame (p2p_state != P2P_STATE_GONEGO_ING)\n", __func__);
3912                 }
3913                 break;
3914         case P2P_GO_NEGO_CONF:
3915                 DBG_88E("[%s] Got GO Nego Confirm Frame\n", __func__);
3916                 result = process_p2p_group_negotation_confirm(pwdinfo, frame_body, len);
3917                 if (P2P_STATUS_SUCCESS == result) {
3918                         if (rtw_p2p_role(pwdinfo) == P2P_ROLE_CLIENT) {
3919                                 pwdinfo->p2p_info.operation_ch[0] = pwdinfo->peer_operating_ch;
3920                                 pwdinfo->p2p_info.scan_op_ch_only = 1;
3921                                 _set_timer(&pwdinfo->reset_ch_sitesurvey2, P2P_RESET_SCAN_CH);
3922                         }
3923                 }
3924                 break;
3925         case P2P_INVIT_REQ:
3926                 /*      Added by Albert 2010/10/05 */
3927                 /*      Received the P2P Invite Request frame. */
3928
3929                 DBG_88E("[%s] Got invite request frame!\n", __func__);
3930                 p2p_ie = rtw_get_p2p_ie(frame_body + _PUBLIC_ACTION_IE_OFFSET_, len - _PUBLIC_ACTION_IE_OFFSET_, NULL, &p2p_ielen);
3931                 if (p2p_ie) {
3932                         /*      Parse the necessary information from the P2P Invitation Request frame. */
3933                         /*      For example: The MAC address of sending this P2P Invitation Request frame. */
3934                         u32     attr_contentlen = 0;
3935                         u8      status_code = P2P_STATUS_FAIL_INFO_UNAVAILABLE;
3936                         struct group_id_info group_id;
3937                         u8      invitation_flag = 0;
3938
3939                         rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_INVITATION_FLAGS, &invitation_flag, &attr_contentlen);
3940                         if (attr_contentlen) {
3941                                 rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_GROUP_BSSID, pwdinfo->p2p_peer_interface_addr, &attr_contentlen);
3942                                 /*      Commented by Albert 20120510 */
3943                                 /*      Copy to the pwdinfo->p2p_peer_interface_addr. */
3944                                 /*      So that the WFD UI (or Sigma) can get the peer interface address by using the following command. */
3945                                 /*      #> iwpriv wlan0 p2p_get peer_ifa */
3946                                 /*      After having the peer interface address, the sigma can find the correct conf file for wpa_supplicant. */
3947
3948                                 if (attr_contentlen) {
3949                                         DBG_88E("[%s] GO's BSSID = %.2X %.2X %.2X %.2X %.2X %.2X\n", __func__,
3950                                                 pwdinfo->p2p_peer_interface_addr[0], pwdinfo->p2p_peer_interface_addr[1],
3951                                                 pwdinfo->p2p_peer_interface_addr[2], pwdinfo->p2p_peer_interface_addr[3],
3952                                                 pwdinfo->p2p_peer_interface_addr[4], pwdinfo->p2p_peer_interface_addr[5]);
3953                                 }
3954
3955                                 if (invitation_flag & P2P_INVITATION_FLAGS_PERSISTENT) {
3956                                         /*      Re-invoke the persistent group. */
3957
3958                                         memset(&group_id, 0x00, sizeof(struct group_id_info));
3959                                         rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_GROUP_ID, (u8 *)&group_id, &attr_contentlen);
3960                                         if (attr_contentlen) {
3961                                                 if (!memcmp(group_id.go_device_addr, myid(&padapter->eeprompriv), ETH_ALEN)) {
3962                                                         /*      The p2p device sending this p2p invitation request wants this Wi-Fi device to be the persistent GO. */
3963                                                         rtw_p2p_set_state(pwdinfo, P2P_STATE_RECV_INVITE_REQ_GO);
3964                                                         rtw_p2p_set_role(pwdinfo, P2P_ROLE_GO);
3965                                                         status_code = P2P_STATUS_SUCCESS;
3966                                                 } else {
3967                                                         /*      The p2p device sending this p2p invitation request wants to be the persistent GO. */
3968                                                         if (is_matched_in_profilelist(pwdinfo->p2p_peer_interface_addr, &pwdinfo->profileinfo[0])) {
3969                                                                 u8 operatingch_info[5] = { 0x00 };
3970                                                                 if (rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_OPERATING_CH, operatingch_info, &attr_contentlen)) {
3971                                                                         if (rtw_ch_set_search_ch(padapter->mlmeextpriv.channel_set, (u32)operatingch_info[4])) {
3972                                                                                 /*      The operating channel is acceptable for this device. */
3973                                                                                 pwdinfo->rx_invitereq_info.operation_ch[0] = operatingch_info[4];
3974                                                                                 pwdinfo->rx_invitereq_info.scan_op_ch_only = 1;
3975                                                                                 _set_timer(&pwdinfo->reset_ch_sitesurvey, P2P_RESET_SCAN_CH);
3976                                                                                 rtw_p2p_set_state(pwdinfo, P2P_STATE_RECV_INVITE_REQ_MATCH);
3977                                                                                 rtw_p2p_set_role(pwdinfo, P2P_ROLE_CLIENT);
3978                                                                                 status_code = P2P_STATUS_SUCCESS;
3979                                                                         } else {
3980                                                                                 /*      The operating channel isn't supported by this device. */
3981                                                                                 rtw_p2p_set_state(pwdinfo, P2P_STATE_RECV_INVITE_REQ_DISMATCH);
3982                                                                                 rtw_p2p_set_role(pwdinfo, P2P_ROLE_DEVICE);
3983                                                                                 status_code = P2P_STATUS_FAIL_NO_COMMON_CH;
3984                                                                                 _set_timer(&pwdinfo->restore_p2p_state_timer, 3000);
3985                                                                         }
3986                                                                 } else {
3987                                                                         /*      Commented by Albert 20121130 */
3988                                                                         /*      Intel will use the different P2P IE to store the operating channel information */
3989                                                                         /*      Workaround for Intel WiDi 3.5 */
3990                                                                         rtw_p2p_set_state(pwdinfo, P2P_STATE_RECV_INVITE_REQ_MATCH);
3991                                                                         rtw_p2p_set_role(pwdinfo, P2P_ROLE_CLIENT);
3992                                                                         status_code = P2P_STATUS_SUCCESS;
3993                                                                 }
3994                                                         } else {
3995                                                                 rtw_p2p_set_state(pwdinfo, P2P_STATE_RECV_INVITE_REQ_DISMATCH);
3996                                                                 status_code = P2P_STATUS_FAIL_UNKNOWN_P2PGROUP;
3997                                                         }
3998                                                 }
3999                                         } else {
4000                                                 DBG_88E("[%s] P2P Group ID Attribute NOT FOUND!\n", __func__);
4001                                                 status_code = P2P_STATUS_FAIL_INFO_UNAVAILABLE;
4002                                         }
4003                                 } else {
4004                                         /*      Received the invitation to join a P2P group. */
4005
4006                                         memset(&group_id, 0x00, sizeof(struct group_id_info));
4007                                         rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_GROUP_ID, (u8 *)&group_id, &attr_contentlen);
4008                                         if (attr_contentlen) {
4009                                                 if (!memcmp(group_id.go_device_addr, myid(&padapter->eeprompriv), ETH_ALEN)) {
4010                                                         /*      In this case, the GO can't be myself. */
4011                                                         rtw_p2p_set_state(pwdinfo, P2P_STATE_RECV_INVITE_REQ_DISMATCH);
4012                                                         status_code = P2P_STATUS_FAIL_INFO_UNAVAILABLE;
4013                                                 } else {
4014                                                         /*      The p2p device sending this p2p invitation request wants to join an existing P2P group */
4015                                                         /*      Commented by Albert 2012/06/28 */
4016                                                         /*      In this case, this Wi-Fi device should use the iwpriv command to get the peer device address. */
4017                                                         /*      The peer device address should be the destination address for the provisioning discovery request. */
4018                                                         /*      Then, this Wi-Fi device should use the iwpriv command to get the peer interface address. */
4019                                                         /*      The peer interface address should be the address for WPS mac address */
4020                                                         memcpy(pwdinfo->p2p_peer_device_addr, group_id.go_device_addr, ETH_ALEN);
4021                                                         rtw_p2p_set_role(pwdinfo, P2P_ROLE_CLIENT);
4022                                                         rtw_p2p_set_state(pwdinfo, P2P_STATE_RECV_INVITE_REQ_JOIN);
4023                                                         status_code = P2P_STATUS_SUCCESS;
4024                                                 }
4025                                         } else {
4026                                                 DBG_88E("[%s] P2P Group ID Attribute NOT FOUND!\n", __func__);
4027                                                 status_code = P2P_STATUS_FAIL_INFO_UNAVAILABLE;
4028                                         }
4029                                 }
4030                         } else {
4031                                 DBG_88E("[%s] P2P Invitation Flags Attribute NOT FOUND!\n", __func__);
4032                                 status_code = P2P_STATUS_FAIL_INFO_UNAVAILABLE;
4033                         }
4034
4035                         DBG_88E("[%s] status_code = %d\n", __func__, status_code);
4036
4037                         pwdinfo->inviteresp_info.token = frame_body[7];
4038                         issue_p2p_invitation_response(padapter, GetAddr2Ptr(pframe), pwdinfo->inviteresp_info.token, status_code);
4039                 }
4040                 break;
4041         case P2P_INVIT_RESP: {
4042                 u8      attr_content = 0x00;
4043                 u32     attr_contentlen = 0;
4044
4045                 DBG_88E("[%s] Got invite response frame!\n", __func__);
4046                 _cancel_timer_ex(&pwdinfo->restore_p2p_state_timer);
4047                 p2p_ie = rtw_get_p2p_ie(frame_body + _PUBLIC_ACTION_IE_OFFSET_, len - _PUBLIC_ACTION_IE_OFFSET_, NULL, &p2p_ielen);
4048                 if (p2p_ie) {
4049                         rtw_get_p2p_attr_content(p2p_ie, p2p_ielen, P2P_ATTR_STATUS, &attr_content, &attr_contentlen);
4050
4051                         if (attr_contentlen == 1) {
4052                                 DBG_88E("[%s] Status = %d\n", __func__, attr_content);
4053                                 pwdinfo->invitereq_info.benable = false;
4054
4055                                 if (attr_content == P2P_STATUS_SUCCESS) {
4056                                         if (!memcmp(pwdinfo->invitereq_info.go_bssid, myid(&padapter->eeprompriv), ETH_ALEN)) {
4057                                                 rtw_p2p_set_role(pwdinfo, P2P_ROLE_GO);
4058                                         } else {
4059                                                 rtw_p2p_set_role(pwdinfo, P2P_ROLE_CLIENT);
4060                                         }
4061                                         rtw_p2p_set_state(pwdinfo, P2P_STATE_RX_INVITE_RESP_OK);
4062                                 } else {
4063                                         rtw_p2p_set_role(pwdinfo, P2P_ROLE_DEVICE);
4064                                         rtw_p2p_set_state(pwdinfo, P2P_STATE_RX_INVITE_RESP_FAIL);
4065                                 }
4066                         } else {
4067                                 rtw_p2p_set_role(pwdinfo, P2P_ROLE_DEVICE);
4068                                 rtw_p2p_set_state(pwdinfo, P2P_STATE_RX_INVITE_RESP_FAIL);
4069                         }
4070                 } else {
4071                         rtw_p2p_set_role(pwdinfo, P2P_ROLE_DEVICE);
4072                         rtw_p2p_set_state(pwdinfo, P2P_STATE_RX_INVITE_RESP_FAIL);
4073                 }
4074
4075                 if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_RX_INVITE_RESP_FAIL))
4076                         _set_timer(&pwdinfo->restore_p2p_state_timer, 5000);
4077                 break;
4078         }
4079         case P2P_DEVDISC_REQ:
4080                 process_p2p_devdisc_req(pwdinfo, pframe, len);
4081                 break;
4082         case P2P_DEVDISC_RESP:
4083                 process_p2p_devdisc_resp(pwdinfo, pframe, len);
4084                 break;
4085         case P2P_PROVISION_DISC_REQ:
4086                 DBG_88E("[%s] Got Provisioning Discovery Request Frame\n", __func__);
4087                 process_p2p_provdisc_req(pwdinfo, pframe, len);
4088                 memcpy(pwdinfo->rx_prov_disc_info.peerDevAddr, GetAddr2Ptr(pframe), ETH_ALEN);
4089
4090                 /* 20110902 Kurt */
4091                 /* Add the following statement to avoid receiving duplicate prov disc req. such that pre_p2p_state would be covered. */
4092                 if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_RX_PROVISION_DIS_REQ))
4093                         rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo));
4094
4095                 rtw_p2p_set_state(pwdinfo, P2P_STATE_RX_PROVISION_DIS_REQ);
4096                 _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_PROVISION_TIMEOUT);
4097                 break;
4098         case P2P_PROVISION_DISC_RESP:
4099                 /*      Commented by Albert 20110707 */
4100                 /*      Should we check the pwdinfo->tx_prov_disc_info.bsent flag here?? */
4101                 DBG_88E("[%s] Got Provisioning Discovery Response Frame\n", __func__);
4102                 /*      Commented by Albert 20110426 */
4103                 /*      The restore timer is enabled when issuing the provisioing request frame in rtw_p2p_prov_disc function. */
4104                 _cancel_timer_ex(&pwdinfo->restore_p2p_state_timer);
4105                 rtw_p2p_set_state(pwdinfo, P2P_STATE_RX_PROVISION_DIS_RSP);
4106                 process_p2p_provdisc_resp(pwdinfo, pframe);
4107                 _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_PROVISION_TIMEOUT);
4108                 break;
4109         }
4110 #endif /* CONFIG_88EU_P2P */
4111
4112         return _SUCCESS;
4113 }
4114
4115 static unsigned int on_action_public_vendor(struct recv_frame *precv_frame)
4116 {
4117         unsigned int ret = _FAIL;
4118         u8 *pframe = precv_frame->rx_data;
4119         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
4120
4121         if (!memcmp(frame_body + 2, P2P_OUI, 4)) {
4122                 ret = on_action_public_p2p(precv_frame);
4123         }
4124
4125         return ret;
4126 }
4127
4128 static unsigned int on_action_public_default(struct recv_frame *precv_frame, u8 action)
4129 {
4130         unsigned int ret = _FAIL;
4131         u8 *pframe = precv_frame->rx_data;
4132         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
4133         u8 token;
4134
4135         token = frame_body[2];
4136
4137         if (rtw_action_public_decache(precv_frame, token) == _FAIL)
4138                 goto exit;
4139
4140         ret = _SUCCESS;
4141
4142 exit:
4143         return ret;
4144 }
4145
4146 unsigned int on_action_public(struct adapter *padapter, struct recv_frame *precv_frame)
4147 {
4148         unsigned int ret = _FAIL;
4149         u8 *pframe = precv_frame->rx_data;
4150         u8 *frame_body = pframe + sizeof(struct rtw_ieee80211_hdr_3addr);
4151         u8 category, action;
4152
4153         /* check RA matches or not */
4154         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe), ETH_ALEN))
4155                 goto exit;
4156
4157         category = frame_body[0];
4158         if (category != RTW_WLAN_CATEGORY_PUBLIC)
4159                 goto exit;
4160
4161         action = frame_body[1];
4162         switch (action) {
4163         case ACT_PUBLIC_VENDOR:
4164                 ret = on_action_public_vendor(precv_frame);
4165                 break;
4166         default:
4167                 ret = on_action_public_default(precv_frame, action);
4168                 break;
4169         }
4170
4171 exit:
4172         return ret;
4173 }
4174
4175 unsigned int OnAction_ht(struct adapter *padapter, struct recv_frame *precv_frame)
4176 {
4177         return _SUCCESS;
4178 }
4179
4180 unsigned int OnAction_wmm(struct adapter *padapter, struct recv_frame *precv_frame)
4181 {
4182         return _SUCCESS;
4183 }
4184
4185 unsigned int OnAction_p2p(struct adapter *padapter, struct recv_frame *precv_frame)
4186 {
4187 #ifdef CONFIG_88EU_P2P
4188         u8 *frame_body;
4189         u8 category, OUI_Subtype;
4190         u8 *pframe = precv_frame->rx_data;
4191         uint len = precv_frame->len;
4192         struct  wifidirect_info *pwdinfo = &(padapter->wdinfo);
4193
4194         DBG_88E("%s\n", __func__);
4195
4196         /* check RA matches or not */
4197         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe), ETH_ALEN))/* for if1, sta/ap mode */
4198                 return _SUCCESS;
4199
4200         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
4201
4202         category = frame_body[0];
4203         if (category != RTW_WLAN_CATEGORY_P2P)
4204                 return _SUCCESS;
4205
4206         if (be32_to_cpu(*((__be32 *)(frame_body + 1))) != P2POUI)
4207                 return _SUCCESS;
4208
4209         len -= sizeof(struct rtw_ieee80211_hdr_3addr);
4210         OUI_Subtype = frame_body[5];
4211
4212         switch (OUI_Subtype) {
4213         case P2P_NOTICE_OF_ABSENCE:
4214                 break;
4215         case P2P_PRESENCE_REQUEST:
4216                 process_p2p_presence_req(pwdinfo, pframe, len);
4217                 break;
4218         case P2P_PRESENCE_RESPONSE:
4219                 break;
4220         case P2P_GO_DISC_REQUEST:
4221                 break;
4222         default:
4223                 break;
4224         }
4225 #endif /* CONFIG_88EU_P2P */
4226         return _SUCCESS;
4227 }
4228
4229 unsigned int OnAction(struct adapter *padapter, struct recv_frame *precv_frame)
4230 {
4231         int i;
4232         unsigned char   category;
4233         struct action_handler *ptable;
4234         unsigned char   *frame_body;
4235         u8 *pframe = precv_frame->rx_data;
4236
4237         frame_body = (unsigned char *)(pframe + sizeof(struct rtw_ieee80211_hdr_3addr));
4238
4239         category = frame_body[0];
4240
4241         for (i = 0; i < sizeof(OnAction_tbl)/sizeof(struct action_handler); i++) {
4242                 ptable = &OnAction_tbl[i];
4243                 if (category == ptable->num)
4244                         ptable->func(padapter, precv_frame);
4245         }
4246         return _SUCCESS;
4247 }
4248
4249 unsigned int DoReserved(struct adapter *padapter, struct recv_frame *precv_frame)
4250 {
4251         return _SUCCESS;
4252 }
4253
4254 struct xmit_frame *alloc_mgtxmitframe(struct xmit_priv *pxmitpriv)
4255 {
4256         struct xmit_frame                       *pmgntframe;
4257         struct xmit_buf                         *pxmitbuf;
4258
4259         pmgntframe = rtw_alloc_xmitframe(pxmitpriv);
4260         if (!pmgntframe) {
4261                 DBG_88E("%s, alloc xmitframe fail\n", __func__);
4262                 return NULL;
4263         }
4264
4265         pxmitbuf = rtw_alloc_xmitbuf_ext(pxmitpriv);
4266         if (!pxmitbuf) {
4267                 DBG_88E("%s, alloc xmitbuf fail\n", __func__);
4268                 rtw_free_xmitframe(pxmitpriv, pmgntframe);
4269                 return NULL;
4270         }
4271         pmgntframe->frame_tag = MGNT_FRAMETAG;
4272         pmgntframe->pxmitbuf = pxmitbuf;
4273         pmgntframe->buf_addr = pxmitbuf->pbuf;
4274         pxmitbuf->priv_data = pmgntframe;
4275         return pmgntframe;
4276 }
4277
4278 /****************************************************************************
4279
4280 Following are some TX fuctions for WiFi MLME
4281
4282 *****************************************************************************/
4283
4284 void update_mgnt_tx_rate(struct adapter *padapter, u8 rate)
4285 {
4286         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
4287
4288         pmlmeext->tx_rate = rate;
4289         DBG_88E("%s(): rate = %x\n", __func__, rate);
4290 }
4291
4292 void update_mgntframe_attrib(struct adapter *padapter, struct pkt_attrib *pattrib)
4293 {
4294         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
4295
4296         memset((u8 *)(pattrib), 0, sizeof(struct pkt_attrib));
4297
4298         pattrib->hdrlen = 24;
4299         pattrib->nr_frags = 1;
4300         pattrib->priority = 7;
4301         pattrib->mac_id = 0;
4302         pattrib->qsel = 0x12;
4303
4304         pattrib->pktlen = 0;
4305
4306         if (pmlmeext->cur_wireless_mode & WIRELESS_11B)
4307                 pattrib->raid = 6;/* b mode */
4308         else
4309                 pattrib->raid = 5;/* a/g mode */
4310
4311         pattrib->encrypt = _NO_PRIVACY_;
4312         pattrib->bswenc = false;
4313
4314         pattrib->qos_en = false;
4315         pattrib->ht_en = false;
4316         pattrib->bwmode = HT_CHANNEL_WIDTH_20;
4317         pattrib->ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
4318         pattrib->sgi = false;
4319
4320         pattrib->seqnum = pmlmeext->mgnt_seq;
4321
4322         pattrib->retry_ctrl = true;
4323 }
4324
4325 void dump_mgntframe(struct adapter *padapter, struct xmit_frame *pmgntframe)
4326 {
4327         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
4328                 return;
4329
4330         rtw_hal_mgnt_xmit(padapter, pmgntframe);
4331 }
4332
4333 s32 dump_mgntframe_and_wait(struct adapter *padapter, struct xmit_frame *pmgntframe, int timeout_ms)
4334 {
4335         s32 ret = _FAIL;
4336         struct xmit_buf *pxmitbuf = pmgntframe->pxmitbuf;
4337         struct submit_ctx sctx;
4338
4339         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
4340                 return ret;
4341
4342         rtw_sctx_init(&sctx, timeout_ms);
4343         pxmitbuf->sctx = &sctx;
4344
4345         ret = rtw_hal_mgnt_xmit(padapter, pmgntframe);
4346
4347         if (ret == _SUCCESS)
4348                 ret = rtw_sctx_wait(&sctx);
4349
4350          return ret;
4351 }
4352
4353 s32 dump_mgntframe_and_wait_ack(struct adapter *padapter, struct xmit_frame *pmgntframe)
4354 {
4355         s32 ret = _FAIL;
4356         u32 timeout_ms = 500;/*   500ms */
4357         struct xmit_priv        *pxmitpriv = &padapter->xmitpriv;
4358
4359         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
4360                 return -1;
4361
4362         _enter_critical_mutex(&pxmitpriv->ack_tx_mutex, NULL);
4363         pxmitpriv->ack_tx = true;
4364
4365         pmgntframe->ack_report = 1;
4366         if (rtw_hal_mgnt_xmit(padapter, pmgntframe) == _SUCCESS) {
4367                 ret = rtw_ack_tx_wait(pxmitpriv, timeout_ms);
4368         }
4369
4370         pxmitpriv->ack_tx = false;
4371         _exit_critical_mutex(&pxmitpriv->ack_tx_mutex, NULL);
4372
4373          return ret;
4374 }
4375
4376 static int update_hidden_ssid(u8 *ies, u32 ies_len, u8 hidden_ssid_mode)
4377 {
4378         u8 *ssid_ie;
4379         int ssid_len_ori;
4380         int len_diff = 0;
4381
4382         ssid_ie = rtw_get_ie(ies,  WLAN_EID_SSID, &ssid_len_ori, ies_len);
4383
4384         if (ssid_ie && ssid_len_ori > 0) {
4385                 switch (hidden_ssid_mode) {
4386                 case 1: {
4387                         u8 *next_ie = ssid_ie + 2 + ssid_len_ori;
4388                         u32 remain_len = 0;
4389
4390                         remain_len = ies_len - (next_ie - ies);
4391
4392                         ssid_ie[1] = 0;
4393                         memcpy(ssid_ie+2, next_ie, remain_len);
4394                         len_diff -= ssid_len_ori;
4395
4396                         break;
4397                 }
4398                 case 2:
4399                         memset(&ssid_ie[2], 0, ssid_len_ori);
4400                         break;
4401                 default:
4402                         break;
4403                 }
4404         }
4405
4406         return len_diff;
4407 }
4408
4409 void issue_beacon(struct adapter *padapter, int timeout_ms)
4410 {
4411         struct xmit_frame       *pmgntframe;
4412         struct pkt_attrib       *pattrib;
4413         unsigned char   *pframe;
4414         struct rtw_ieee80211_hdr *pwlanhdr;
4415         __le16 *fctrl;
4416         unsigned int    rate_len;
4417         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
4418         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
4419         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
4420         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4421         struct wlan_bssid_ex            *cur_network = &(pmlmeinfo->network);
4422         u8      bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
4423 #ifdef CONFIG_88EU_P2P
4424         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
4425 #endif /* CONFIG_88EU_P2P */
4426
4427         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
4428         if (!pmgntframe) {
4429                 DBG_88E("%s, alloc mgnt frame fail\n", __func__);
4430                 return;
4431         }
4432 #if defined (CONFIG_88EU_AP_MODE)
4433         spin_lock_bh(&pmlmepriv->bcn_update_lock);
4434 #endif /* if defined (CONFIG_88EU_AP_MODE) */
4435
4436         /* update attribute */
4437         pattrib = &pmgntframe->attrib;
4438         update_mgntframe_attrib(padapter, pattrib);
4439         pattrib->qsel = 0x10;
4440
4441         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
4442
4443         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
4444         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
4445
4446         fctrl = &(pwlanhdr->frame_ctl);
4447         *(fctrl) = 0;
4448
4449         memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN);
4450         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
4451         memcpy(pwlanhdr->addr3, get_my_bssid(cur_network), ETH_ALEN);
4452
4453         SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
4454         /* pmlmeext->mgnt_seq++; */
4455         SetFrameSubType(pframe, WIFI_BEACON);
4456
4457         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
4458         pattrib->pktlen = sizeof (struct rtw_ieee80211_hdr_3addr);
4459
4460         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
4461 #ifdef CONFIG_88EU_P2P
4462                 /*  for P2P : Primary Device Type & Device Name */
4463                 u32 wpsielen = 0, insert_len = 0;
4464                 u8 *wpsie = NULL;
4465                 wpsie = rtw_get_wps_ie(cur_network->IEs+_FIXED_IE_LENGTH_, cur_network->IELength-_FIXED_IE_LENGTH_, NULL, &wpsielen);
4466
4467                 if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO) && wpsie && wpsielen > 0) {
4468                         uint wps_offset, remainder_ielen;
4469                         u8 *premainder_ie, *pframe_wscie;
4470
4471                         wps_offset = (uint)(wpsie - cur_network->IEs);
4472                         premainder_ie = wpsie + wpsielen;
4473                         remainder_ielen = cur_network->IELength - wps_offset - wpsielen;
4474                         pframe_wscie = pframe + wps_offset;
4475                         memcpy(pframe, cur_network->IEs, wps_offset+wpsielen);
4476                         pframe += (wps_offset + wpsielen);
4477                         pattrib->pktlen += (wps_offset + wpsielen);
4478
4479                         /* now pframe is end of wsc ie, insert Primary Device Type & Device Name */
4480                         /*      Primary Device Type */
4481                         /*      Type: */
4482                         *(__be16 *)(pframe + insert_len) = cpu_to_be16(WPS_ATTR_PRIMARY_DEV_TYPE);
4483                         insert_len += 2;
4484
4485                         /*      Length: */
4486                         *(__be16 *)(pframe + insert_len) = cpu_to_be16(0x0008);
4487                         insert_len += 2;
4488
4489                         /*      Value: */
4490                         /*      Category ID */
4491                         *(__be16 *)(pframe + insert_len) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA);
4492                         insert_len += 2;
4493
4494                         /*      OUI */
4495                         *(__be32 *)(pframe + insert_len) = cpu_to_be32(WPSOUI);
4496                         insert_len += 4;
4497
4498                         /*      Sub Category ID */
4499                         *(__be16 *)(pframe + insert_len) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER);
4500                         insert_len += 2;
4501
4502                         /*      Device Name */
4503                         /*      Type: */
4504                         *(__be16 *)(pframe + insert_len) = cpu_to_be16(WPS_ATTR_DEVICE_NAME);
4505                         insert_len += 2;
4506
4507                         /*      Length: */
4508                         *(__be16 *)(pframe + insert_len) = cpu_to_be16(pwdinfo->device_name_len);
4509                         insert_len += 2;
4510
4511                         /*      Value: */
4512                         memcpy(pframe + insert_len, pwdinfo->device_name, pwdinfo->device_name_len);
4513                         insert_len += pwdinfo->device_name_len;
4514
4515                         /* update wsc ie length */
4516                         *(pframe_wscie+1) = (wpsielen-2) + insert_len;
4517
4518                         /* pframe move to end */
4519                         pframe += insert_len;
4520                         pattrib->pktlen += insert_len;
4521
4522                         /* copy remainder_ie to pframe */
4523                         memcpy(pframe, premainder_ie, remainder_ielen);
4524                         pframe += remainder_ielen;
4525                         pattrib->pktlen += remainder_ielen;
4526                 } else
4527 #endif /* CONFIG_88EU_P2P */
4528                 {
4529                         int len_diff;
4530                         memcpy(pframe, cur_network->IEs, cur_network->IELength);
4531                         len_diff = update_hidden_ssid(
4532                                 pframe+_BEACON_IE_OFFSET_
4533                                 , cur_network->IELength-_BEACON_IE_OFFSET_
4534                                 , pmlmeinfo->hidden_ssid_mode
4535                         );
4536                         pframe += (cur_network->IELength+len_diff);
4537                         pattrib->pktlen += (cur_network->IELength+len_diff);
4538                 }
4539
4540                 {
4541                         u8 *wps_ie;
4542                         uint wps_ielen;
4543                         u8 sr = 0;
4544                         wps_ie = rtw_get_wps_ie(pmgntframe->buf_addr+TXDESC_OFFSET+sizeof (struct rtw_ieee80211_hdr_3addr)+_BEACON_IE_OFFSET_,
4545                                 pattrib->pktlen-sizeof (struct rtw_ieee80211_hdr_3addr)-_BEACON_IE_OFFSET_, NULL, &wps_ielen);
4546                         if (wps_ie && wps_ielen > 0)
4547                                 rtw_get_wps_attr_content(wps_ie,  wps_ielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL);
4548                         if (sr != 0)
4549                                 set_fwstate(pmlmepriv, WIFI_UNDER_WPS);
4550                         else
4551                                 _clr_fwstate_(pmlmepriv, WIFI_UNDER_WPS);
4552                 }
4553
4554 #ifdef CONFIG_88EU_P2P
4555                 if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) {
4556                         u32 len;
4557                         len = build_beacon_p2p_ie(pwdinfo, pframe);
4558
4559                         pframe += len;
4560                         pattrib->pktlen += len;
4561                 }
4562 #endif /* CONFIG_88EU_P2P */
4563
4564                 goto _issue_bcn;
4565         }
4566
4567         /* below for ad-hoc mode */
4568
4569         /* timestamp will be inserted by hardware */
4570         pframe += 8;
4571         pattrib->pktlen += 8;
4572
4573         /*  beacon interval: 2 bytes */
4574
4575         memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2);
4576
4577         pframe += 2;
4578         pattrib->pktlen += 2;
4579
4580         /*  capability info: 2 bytes */
4581
4582         memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2);
4583
4584         pframe += 2;
4585         pattrib->pktlen += 2;
4586
4587         /*  SSID */
4588         pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pattrib->pktlen);
4589
4590         /*  supported rates... */
4591         rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
4592         pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, ((rate_len > 8) ? 8 : rate_len), cur_network->SupportedRates, &pattrib->pktlen);
4593
4594         /*  DS parameter set */
4595         pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pattrib->pktlen);
4596
4597         {
4598                 u8 erpinfo = 0;
4599                 u32 ATIMWindow;
4600                 /*  IBSS Parameter Set... */
4601                 ATIMWindow = 0;
4602                 pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pattrib->pktlen);
4603
4604                 /* ERP IE */
4605                 pframe = rtw_set_ie(pframe, _ERPINFO_IE_, 1, &erpinfo, &pattrib->pktlen);
4606         }
4607
4608         /*  EXTERNDED SUPPORTED RATE */
4609         if (rate_len > 8)
4610                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pattrib->pktlen);
4611         /* todo:HT for adhoc */
4612 _issue_bcn:
4613
4614 #if defined (CONFIG_88EU_AP_MODE)
4615         pmlmepriv->update_bcn = false;
4616
4617         spin_unlock_bh(&pmlmepriv->bcn_update_lock);
4618 #endif /* if defined (CONFIG_88EU_AP_MODE) */
4619
4620         if ((pattrib->pktlen + TXDESC_SIZE) > 512) {
4621                 DBG_88E("beacon frame too large\n");
4622                 return;
4623         }
4624
4625         pattrib->last_txcmdsz = pattrib->pktlen;
4626
4627         /* DBG_88E("issue bcn_sz=%d\n", pattrib->last_txcmdsz); */
4628         if (timeout_ms > 0)
4629                 dump_mgntframe_and_wait(padapter, pmgntframe, timeout_ms);
4630         else
4631                 dump_mgntframe(padapter, pmgntframe);
4632 }
4633
4634 void issue_probersp(struct adapter *padapter, unsigned char *da, u8 is_valid_p2p_probereq)
4635 {
4636         struct xmit_frame                       *pmgntframe;
4637         struct pkt_attrib                       *pattrib;
4638         unsigned char                                   *pframe;
4639         struct rtw_ieee80211_hdr        *pwlanhdr;
4640         __le16 *fctrl;
4641         unsigned char                                   *mac, *bssid;
4642         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
4643 #if defined (CONFIG_88EU_AP_MODE)
4644         u8 *pwps_ie;
4645         uint wps_ielen;
4646         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
4647 #endif /* if defined (CONFIG_88EU_AP_MODE) */
4648         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
4649         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4650         struct wlan_bssid_ex            *cur_network = &(pmlmeinfo->network);
4651         unsigned int    rate_len;
4652 #ifdef CONFIG_88EU_P2P
4653         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
4654 #endif /* CONFIG_88EU_P2P */
4655
4656         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
4657         if (!pmgntframe) {
4658                 DBG_88E("%s, alloc mgnt frame fail\n", __func__);
4659                 return;
4660         }
4661
4662         /* update attribute */
4663         pattrib = &pmgntframe->attrib;
4664         update_mgntframe_attrib(padapter, pattrib);
4665
4666         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
4667
4668         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
4669         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
4670
4671         mac = myid(&(padapter->eeprompriv));
4672         bssid = cur_network->MacAddress;
4673
4674         fctrl = &(pwlanhdr->frame_ctl);
4675         *(fctrl) = 0;
4676         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
4677         memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
4678         memcpy(pwlanhdr->addr3, bssid, ETH_ALEN);
4679
4680         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
4681         pmlmeext->mgnt_seq++;
4682         SetFrameSubType(fctrl, WIFI_PROBERSP);
4683
4684         pattrib->hdrlen = sizeof(struct rtw_ieee80211_hdr_3addr);
4685         pattrib->pktlen = pattrib->hdrlen;
4686         pframe += pattrib->hdrlen;
4687
4688         if (cur_network->IELength > MAX_IE_SZ)
4689                 return;
4690
4691 #if defined(CONFIG_88EU_AP_MODE)
4692         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
4693                 pwps_ie = rtw_get_wps_ie(cur_network->IEs+_FIXED_IE_LENGTH_, cur_network->IELength-_FIXED_IE_LENGTH_, NULL, &wps_ielen);
4694
4695                 /* inerset & update wps_probe_resp_ie */
4696                 if (pmlmepriv->wps_probe_resp_ie && pwps_ie && wps_ielen > 0) {
4697                         uint wps_offset, remainder_ielen;
4698                         u8 *premainder_ie;
4699
4700                         wps_offset = (uint)(pwps_ie - cur_network->IEs);
4701
4702                         premainder_ie = pwps_ie + wps_ielen;
4703
4704                         remainder_ielen = cur_network->IELength - wps_offset - wps_ielen;
4705
4706                         memcpy(pframe, cur_network->IEs, wps_offset);
4707                         pframe += wps_offset;
4708                         pattrib->pktlen += wps_offset;
4709
4710                         wps_ielen = (uint)pmlmepriv->wps_probe_resp_ie[1];/* to get ie data len */
4711                         if ((wps_offset+wps_ielen+2) <= MAX_IE_SZ) {
4712                                 memcpy(pframe, pmlmepriv->wps_probe_resp_ie, wps_ielen+2);
4713                                 pframe += wps_ielen+2;
4714                                 pattrib->pktlen += wps_ielen+2;
4715                         }
4716
4717                         if ((wps_offset+wps_ielen+2+remainder_ielen) <= MAX_IE_SZ) {
4718                                 memcpy(pframe, premainder_ie, remainder_ielen);
4719                                 pframe += remainder_ielen;
4720                                 pattrib->pktlen += remainder_ielen;
4721                         }
4722                 } else {
4723                         memcpy(pframe, cur_network->IEs, cur_network->IELength);
4724                         pframe += cur_network->IELength;
4725                         pattrib->pktlen += cur_network->IELength;
4726                 }
4727         } else
4728 #endif
4729         {
4730                 /* timestamp will be inserted by hardware */
4731                 pframe += 8;
4732                 pattrib->pktlen += 8;
4733
4734                 /*  beacon interval: 2 bytes */
4735
4736                 memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2);
4737
4738                 pframe += 2;
4739                 pattrib->pktlen += 2;
4740
4741                 /*  capability info: 2 bytes */
4742
4743                 memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2);
4744
4745                 pframe += 2;
4746                 pattrib->pktlen += 2;
4747
4748                 /* below for ad-hoc mode */
4749
4750                 /*  SSID */
4751                 pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pattrib->pktlen);
4752
4753                 /*  supported rates... */
4754                 rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
4755                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, ((rate_len > 8) ? 8 : rate_len), cur_network->SupportedRates, &pattrib->pktlen);
4756
4757                 /*  DS parameter set */
4758                 pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pattrib->pktlen);
4759
4760                 if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
4761                         u8 erpinfo = 0;
4762                         u32 ATIMWindow;
4763                         /*  IBSS Parameter Set... */
4764                         /* ATIMWindow = cur->Configuration.ATIMWindow; */
4765                         ATIMWindow = 0;
4766                         pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pattrib->pktlen);
4767
4768                         /* ERP IE */
4769                         pframe = rtw_set_ie(pframe, _ERPINFO_IE_, 1, &erpinfo, &pattrib->pktlen);
4770                 }
4771
4772                 /*  EXTERNDED SUPPORTED RATE */
4773                 if (rate_len > 8)
4774                         pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pattrib->pktlen);
4775                 /* todo:HT for adhoc */
4776         }
4777
4778 #ifdef CONFIG_88EU_P2P
4779         if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO) && is_valid_p2p_probereq) {
4780                 u32 len;
4781                 len = build_probe_resp_p2p_ie(pwdinfo, pframe);
4782
4783                 pframe += len;
4784                 pattrib->pktlen += len;
4785         }
4786 #endif /* CONFIG_88EU_P2P */
4787
4788         pattrib->last_txcmdsz = pattrib->pktlen;
4789
4790         dump_mgntframe(padapter, pmgntframe);
4791 }
4792
4793 static int _issue_probereq(struct adapter *padapter, struct ndis_802_11_ssid *pssid, u8 *da, int wait_ack)
4794 {
4795         int ret = _FAIL;
4796         struct xmit_frame               *pmgntframe;
4797         struct pkt_attrib               *pattrib;
4798         unsigned char                   *pframe;
4799         struct rtw_ieee80211_hdr        *pwlanhdr;
4800         __le16 *fctrl;
4801         unsigned char                   *mac;
4802         unsigned char                   bssrate[NumRates];
4803         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
4804         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
4805         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
4806         int     bssrate_len = 0;
4807         u8      bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
4808
4809         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
4810         if (!pmgntframe)
4811                 goto exit;
4812
4813         /* update attribute */
4814         pattrib = &pmgntframe->attrib;
4815         update_mgntframe_attrib(padapter, pattrib);
4816
4817         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
4818
4819         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
4820         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
4821
4822         mac = myid(&(padapter->eeprompriv));
4823
4824         fctrl = &(pwlanhdr->frame_ctl);
4825         *(fctrl) = 0;
4826
4827         if (da) {
4828                 /*      unicast probe request frame */
4829                 memcpy(pwlanhdr->addr1, da, ETH_ALEN);
4830                 memcpy(pwlanhdr->addr3, da, ETH_ALEN);
4831         } else {
4832                 /*      broadcast probe request frame */
4833                 memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN);
4834                 memcpy(pwlanhdr->addr3, bc_addr, ETH_ALEN);
4835         }
4836
4837         memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
4838
4839         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
4840         pmlmeext->mgnt_seq++;
4841         SetFrameSubType(pframe, WIFI_PROBEREQ);
4842
4843         pframe += sizeof (struct rtw_ieee80211_hdr_3addr);
4844         pattrib->pktlen = sizeof (struct rtw_ieee80211_hdr_3addr);
4845
4846         if (pssid)
4847                 pframe = rtw_set_ie(pframe, _SSID_IE_, pssid->SsidLength, pssid->Ssid, &(pattrib->pktlen));
4848         else
4849                 pframe = rtw_set_ie(pframe, _SSID_IE_, 0, NULL, &(pattrib->pktlen));
4850
4851         get_rate_set(padapter, bssrate, &bssrate_len);
4852
4853         if (bssrate_len > 8) {
4854                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, bssrate, &(pattrib->pktlen));
4855                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (bssrate_len - 8), (bssrate + 8), &(pattrib->pktlen));
4856         } else {
4857                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, bssrate_len, bssrate, &(pattrib->pktlen));
4858         }
4859
4860         /* add wps_ie for wps2.0 */
4861         if (pmlmepriv->wps_probe_req_ie_len > 0 && pmlmepriv->wps_probe_req_ie) {
4862                 memcpy(pframe, pmlmepriv->wps_probe_req_ie, pmlmepriv->wps_probe_req_ie_len);
4863                 pframe += pmlmepriv->wps_probe_req_ie_len;
4864                 pattrib->pktlen += pmlmepriv->wps_probe_req_ie_len;
4865         }
4866
4867         pattrib->last_txcmdsz = pattrib->pktlen;
4868
4869         if (wait_ack) {
4870                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
4871         } else {
4872                 dump_mgntframe(padapter, pmgntframe);
4873                 ret = _SUCCESS;
4874         }
4875
4876 exit:
4877         return ret;
4878 }
4879
4880 inline void issue_probereq(struct adapter *padapter, struct ndis_802_11_ssid *pssid, u8 *da)
4881 {
4882         _issue_probereq(padapter, pssid, da, false);
4883 }
4884
4885 int issue_probereq_ex(struct adapter *padapter, struct ndis_802_11_ssid *pssid, u8 *da,
4886         int try_cnt, int wait_ms)
4887 {
4888         int ret;
4889         int i = 0;
4890         u32 start = jiffies;
4891
4892         do {
4893                 ret = _issue_probereq(padapter, pssid, da, wait_ms > 0 ? true : false);
4894
4895                 i++;
4896
4897                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
4898                         break;
4899
4900                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
4901                         msleep(wait_ms);
4902
4903         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
4904
4905         if (ret != _FAIL) {
4906                 ret = _SUCCESS;
4907                 goto exit;
4908         }
4909
4910         if (try_cnt && wait_ms) {
4911                 if (da)
4912                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
4913                                 FUNC_ADPT_ARG(padapter), da, rtw_get_oper_ch(padapter),
4914                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
4915                 else
4916                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
4917                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
4918                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
4919         }
4920 exit:
4921         return ret;
4922 }
4923
4924 /*  if psta == NULL, indiate we are station(client) now... */
4925 void issue_auth(struct adapter *padapter, struct sta_info *psta, unsigned short status)
4926 {
4927         struct xmit_frame *pmgntframe;
4928         struct pkt_attrib *pattrib;
4929         unsigned char *pframe;
4930         struct rtw_ieee80211_hdr *pwlanhdr;
4931         __le16 *fctrl;
4932         unsigned int val32;
4933         u16 val16;
4934 #ifdef CONFIG_88EU_AP_MODE
4935         __le16 le_val16;
4936 #endif
4937         int use_shared_key = 0;
4938         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
4939         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
4940         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
4941
4942         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
4943         if (!pmgntframe)
4944                 return;
4945
4946         /* update attribute */
4947         pattrib = &pmgntframe->attrib;
4948         update_mgntframe_attrib(padapter, pattrib);
4949
4950         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
4951
4952         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
4953         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
4954
4955         fctrl = &(pwlanhdr->frame_ctl);
4956         *(fctrl) = 0;
4957
4958         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
4959         pmlmeext->mgnt_seq++;
4960         SetFrameSubType(pframe, WIFI_AUTH);
4961
4962         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
4963         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
4964
4965         if (psta) {/*  for AP mode */
4966 #ifdef CONFIG_88EU_AP_MODE
4967
4968                 memcpy(pwlanhdr->addr1, psta->hwaddr, ETH_ALEN);
4969                 memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
4970                 memcpy(pwlanhdr->addr3, myid(&(padapter->eeprompriv)), ETH_ALEN);
4971
4972                 /*  setting auth algo number */
4973                 val16 = (u16)psta->authalg;
4974
4975                 if (status != _STATS_SUCCESSFUL_)
4976                         val16 = 0;
4977
4978                 if (val16) {
4979                         le_val16 = cpu_to_le16(val16);
4980                         use_shared_key = 1;
4981                 } else {
4982                         le_val16 = 0;
4983                 }
4984
4985                 pframe = rtw_set_fixed_ie(pframe, _AUTH_ALGM_NUM_, (unsigned char *)&le_val16, &(pattrib->pktlen));
4986
4987                 /*  setting auth seq number */
4988                 val16 = (u16)psta->auth_seq;
4989                 le_val16 = cpu_to_le16(val16);
4990                 pframe = rtw_set_fixed_ie(pframe, _AUTH_SEQ_NUM_, (unsigned char *)&le_val16, &(pattrib->pktlen));
4991
4992                 /*  setting status code... */
4993                 val16 = status;
4994                 le_val16 = cpu_to_le16(val16);
4995                 pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_, (unsigned char *)&le_val16, &(pattrib->pktlen));
4996
4997                 /*  added challenging text... */
4998                 if ((psta->auth_seq == 2) && (psta->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1))
4999                         pframe = rtw_set_ie(pframe, _CHLGETXT_IE_, 128, psta->chg_txt, &(pattrib->pktlen));
5000 #endif
5001         } else {
5002                 __le32 le_tmp32;
5003                 __le16 le_tmp16;
5004                 memcpy(pwlanhdr->addr1, get_my_bssid(&pmlmeinfo->network), ETH_ALEN);
5005                 memcpy(pwlanhdr->addr2, myid(&padapter->eeprompriv), ETH_ALEN);
5006                 memcpy(pwlanhdr->addr3, get_my_bssid(&pmlmeinfo->network), ETH_ALEN);
5007
5008                 /*  setting auth algo number */
5009                 val16 = (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared) ? 1 : 0;/*  0:OPEN System, 1:Shared key */
5010                 if (val16)
5011                         use_shared_key = 1;
5012
5013                 /* setting IV for auth seq #3 */
5014                 if ((pmlmeinfo->auth_seq == 3) && (pmlmeinfo->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1)) {
5015                         val32 = ((pmlmeinfo->iv++) | (pmlmeinfo->key_index << 30));
5016                         le_tmp32 = cpu_to_le32(val32);
5017                         pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *)&le_tmp32, &(pattrib->pktlen));
5018
5019                         pattrib->iv_len = 4;
5020                 }
5021
5022                 le_tmp16 = cpu_to_le16(val16);
5023                 pframe = rtw_set_fixed_ie(pframe, _AUTH_ALGM_NUM_, (unsigned char *)&le_tmp16, &(pattrib->pktlen));
5024
5025                 /*  setting auth seq number */
5026                 val16 = pmlmeinfo->auth_seq;
5027                 le_tmp16 = cpu_to_le16(val16);
5028                 pframe = rtw_set_fixed_ie(pframe, _AUTH_SEQ_NUM_, (unsigned char *)&le_tmp16, &(pattrib->pktlen));
5029
5030                 /*  setting status code... */
5031                 le_tmp16 = cpu_to_le16(status);
5032                 pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_, (unsigned char *)&le_tmp16, &(pattrib->pktlen));
5033
5034                 /*  then checking to see if sending challenging text... */
5035                 if ((pmlmeinfo->auth_seq == 3) && (pmlmeinfo->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1)) {
5036                         pframe = rtw_set_ie(pframe, _CHLGETXT_IE_, 128, pmlmeinfo->chg_txt, &(pattrib->pktlen));
5037
5038                         SetPrivacy(fctrl);
5039
5040                         pattrib->hdrlen = sizeof(struct rtw_ieee80211_hdr_3addr);
5041
5042                         pattrib->encrypt = _WEP40_;
5043
5044                         pattrib->icv_len = 4;
5045
5046                         pattrib->pktlen += pattrib->icv_len;
5047                 }
5048         }
5049
5050         pattrib->last_txcmdsz = pattrib->pktlen;
5051
5052         rtw_wep_encrypt(padapter, (u8 *)pmgntframe);
5053         DBG_88E("%s\n", __func__);
5054         dump_mgntframe(padapter, pmgntframe);
5055 }
5056
5057 void issue_asocrsp(struct adapter *padapter, unsigned short status, struct sta_info *pstat, int pkt_type)
5058 {
5059 #ifdef CONFIG_88EU_AP_MODE
5060         struct xmit_frame       *pmgntframe;
5061         struct rtw_ieee80211_hdr        *pwlanhdr;
5062         struct pkt_attrib *pattrib;
5063         unsigned char   *pbuf, *pframe;
5064         unsigned short val;
5065         __le16 *fctrl;
5066         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
5067         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
5068         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
5069         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5070         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
5071         u8 *ie = pnetwork->IEs;
5072         __le16 lestatus, leval;
5073 #ifdef CONFIG_88EU_P2P
5074         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
5075 #endif /* CONFIG_88EU_P2P */
5076
5077         DBG_88E("%s\n", __func__);
5078
5079         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
5080         if (!pmgntframe)
5081                 return;
5082
5083         /* update attribute */
5084         pattrib = &pmgntframe->attrib;
5085         update_mgntframe_attrib(padapter, pattrib);
5086
5087         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
5088
5089         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
5090         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
5091
5092         fctrl = &(pwlanhdr->frame_ctl);
5093         *(fctrl) = 0;
5094
5095         memcpy((void *)GetAddr1Ptr(pwlanhdr), pstat->hwaddr, ETH_ALEN);
5096         memcpy((void *)GetAddr2Ptr(pwlanhdr), myid(&(padapter->eeprompriv)), ETH_ALEN);
5097         memcpy((void *)GetAddr3Ptr(pwlanhdr), get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
5098
5099         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
5100         pmlmeext->mgnt_seq++;
5101         if ((pkt_type == WIFI_ASSOCRSP) || (pkt_type == WIFI_REASSOCRSP))
5102                 SetFrameSubType(pwlanhdr, pkt_type);
5103         else
5104                 return;
5105
5106         pattrib->hdrlen = sizeof(struct rtw_ieee80211_hdr_3addr);
5107         pattrib->pktlen += pattrib->hdrlen;
5108         pframe += pattrib->hdrlen;
5109
5110         /* capability */
5111         val = *(unsigned short *)rtw_get_capability_from_ie(ie);
5112
5113         pframe = rtw_set_fixed_ie(pframe, _CAPABILITY_, (unsigned char *)&val, &(pattrib->pktlen));
5114
5115         lestatus = cpu_to_le16(status);
5116         pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_, (unsigned char *)&lestatus, &(pattrib->pktlen));
5117
5118         leval = cpu_to_le16(pstat->aid | BIT(14) | BIT(15));
5119         pframe = rtw_set_fixed_ie(pframe, _ASOC_ID_, (unsigned char *)&leval, &(pattrib->pktlen));
5120
5121         if (pstat->bssratelen <= 8) {
5122                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, pstat->bssratelen, pstat->bssrateset, &(pattrib->pktlen));
5123         } else {
5124                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, pstat->bssrateset, &(pattrib->pktlen));
5125                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (pstat->bssratelen-8), pstat->bssrateset+8, &(pattrib->pktlen));
5126         }
5127
5128         if ((pstat->flags & WLAN_STA_HT) && (pmlmepriv->htpriv.ht_option)) {
5129                 uint ie_len = 0;
5130
5131                 /* FILL HT CAP INFO IE */
5132                 pbuf = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_CAPABILITY_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
5133                 if (pbuf && ie_len > 0) {
5134                         memcpy(pframe, pbuf, ie_len+2);
5135                         pframe += (ie_len+2);
5136                         pattrib->pktlen += (ie_len+2);
5137                 }
5138
5139                 /* FILL HT ADD INFO IE */
5140                 pbuf = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_ADD_INFO_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
5141                 if (pbuf && ie_len > 0) {
5142                         memcpy(pframe, pbuf, ie_len+2);
5143                         pframe += (ie_len+2);
5144                         pattrib->pktlen += (ie_len+2);
5145                 }
5146         }
5147
5148         /* FILL WMM IE */
5149         if ((pstat->flags & WLAN_STA_WME) && (pmlmepriv->qospriv.qos_option)) {
5150                 uint ie_len = 0;
5151                 unsigned char WMM_PARA_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
5152
5153                 for (pbuf = ie + _BEACON_IE_OFFSET_;; pbuf += (ie_len + 2)) {
5154                         pbuf = rtw_get_ie(pbuf, _VENDOR_SPECIFIC_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_ - (ie_len + 2)));
5155                         if (pbuf && !memcmp(pbuf+2, WMM_PARA_IE, 6)) {
5156                                 memcpy(pframe, pbuf, ie_len+2);
5157                                 pframe += (ie_len+2);
5158                                 pattrib->pktlen += (ie_len+2);
5159                                 break;
5160                         }
5161
5162                         if (!pbuf || ie_len == 0)
5163                                 break;
5164                 }
5165         }
5166
5167         if (pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_REALTEK)
5168                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, 6, REALTEK_96B_IE, &(pattrib->pktlen));
5169
5170         /* add WPS IE ie for wps 2.0 */
5171         if (pmlmepriv->wps_assoc_resp_ie && pmlmepriv->wps_assoc_resp_ie_len > 0) {
5172                 memcpy(pframe, pmlmepriv->wps_assoc_resp_ie, pmlmepriv->wps_assoc_resp_ie_len);
5173
5174                 pframe += pmlmepriv->wps_assoc_resp_ie_len;
5175                 pattrib->pktlen += pmlmepriv->wps_assoc_resp_ie_len;
5176         }
5177
5178 #ifdef CONFIG_88EU_P2P
5179         if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO) && (pstat->is_p2p_device)) {
5180                 u32 len;
5181
5182                 len = build_assoc_resp_p2p_ie(pwdinfo, pframe, pstat->p2p_status_code);
5183
5184                 pframe += len;
5185                 pattrib->pktlen += len;
5186         }
5187 #endif /* CONFIG_88EU_P2P */
5188         pattrib->last_txcmdsz = pattrib->pktlen;
5189         dump_mgntframe(padapter, pmgntframe);
5190 #endif
5191 }
5192
5193 void issue_assocreq(struct adapter *padapter)
5194 {
5195         int ret = _FAIL;
5196         struct xmit_frame       *pmgntframe;
5197         struct pkt_attrib       *pattrib;
5198         unsigned char           *pframe, *p;
5199         struct rtw_ieee80211_hdr        *pwlanhdr;
5200         __le16 *fctrl;
5201         __le16          le_tmp;
5202         unsigned int    i, j, ie_len, index = 0;
5203         unsigned char   rf_type, bssrate[NumRates], sta_bssrate[NumRates];
5204         struct ndis_802_11_var_ie *pIE;
5205         struct registry_priv    *pregpriv = &padapter->registrypriv;
5206         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
5207         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
5208         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
5209         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5210         int     bssrate_len = 0, sta_bssrate_len = 0;
5211 #ifdef CONFIG_88EU_P2P
5212         struct wifidirect_info  *pwdinfo = &(padapter->wdinfo);
5213         u8 p2pie[255] = { 0x00 };
5214         u16 p2pielen = 0;
5215 #endif /* CONFIG_88EU_P2P */
5216
5217         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
5218         if (!pmgntframe)
5219                 goto exit;
5220
5221         /* update attribute */
5222         pattrib = &pmgntframe->attrib;
5223         update_mgntframe_attrib(padapter, pattrib);
5224
5225         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
5226         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
5227         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
5228
5229         fctrl = &(pwlanhdr->frame_ctl);
5230         *(fctrl) = 0;
5231         memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
5232         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
5233         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
5234
5235         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
5236         pmlmeext->mgnt_seq++;
5237         SetFrameSubType(pframe, WIFI_ASSOCREQ);
5238
5239         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
5240         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
5241
5242         /* caps */
5243
5244         memcpy(pframe, rtw_get_capability_from_ie(pmlmeinfo->network.IEs), 2);
5245
5246         pframe += 2;
5247         pattrib->pktlen += 2;
5248
5249         /* listen interval */
5250         /* todo: listen interval for power saving */
5251         le_tmp = cpu_to_le16(3);
5252         memcpy(pframe, (unsigned char *)&le_tmp, 2);
5253         pframe += 2;
5254         pattrib->pktlen += 2;
5255
5256         /* SSID */
5257         pframe = rtw_set_ie(pframe, _SSID_IE_,  pmlmeinfo->network.Ssid.SsidLength, pmlmeinfo->network.Ssid.Ssid, &(pattrib->pktlen));
5258
5259         /* supported rate & extended supported rate */
5260
5261         /*  Check if the AP's supported rates are also supported by STA. */
5262         get_rate_set(padapter, sta_bssrate, &sta_bssrate_len);
5263
5264         if (pmlmeext->cur_channel == 14)/*  for JAPAN, channel 14 can only uses B Mode(CCK) */
5265                 sta_bssrate_len = 4;
5266
5267         for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) {
5268                 if (pmlmeinfo->network.SupportedRates[i] == 0)
5269                         break;
5270                 DBG_88E("network.SupportedRates[%d]=%02X\n", i, pmlmeinfo->network.SupportedRates[i]);
5271         }
5272
5273         for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) {
5274                 if (pmlmeinfo->network.SupportedRates[i] == 0)
5275                         break;
5276
5277                 /*  Check if the AP's supported rates are also supported by STA. */
5278                 for (j = 0; j < sta_bssrate_len; j++) {
5279                          /*  Avoid the proprietary data rate (22Mbps) of Handlink WSG-4000 AP */
5280                         if ((pmlmeinfo->network.SupportedRates[i]|IEEE80211_BASIC_RATE_MASK)
5281                                         == (sta_bssrate[j]|IEEE80211_BASIC_RATE_MASK))
5282                                 break;
5283                 }
5284
5285                 if (j == sta_bssrate_len) {
5286                         /*  the rate is not supported by STA */
5287                         DBG_88E("%s(): the rate[%d]=%02X is not supported by STA!\n", __func__, i, pmlmeinfo->network.SupportedRates[i]);
5288                 } else {
5289                         /*  the rate is supported by STA */
5290                         bssrate[index++] = pmlmeinfo->network.SupportedRates[i];
5291                 }
5292         }
5293
5294         bssrate_len = index;
5295         DBG_88E("bssrate_len=%d\n", bssrate_len);
5296
5297         if (bssrate_len == 0) {
5298                 rtw_free_xmitbuf(pxmitpriv, pmgntframe->pxmitbuf);
5299                 rtw_free_xmitframe(pxmitpriv, pmgntframe);
5300                 goto exit; /* don't connect to AP if no joint supported rate */
5301         }
5302
5303         if (bssrate_len > 8) {
5304                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, bssrate, &(pattrib->pktlen));
5305                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (bssrate_len - 8), (bssrate + 8), &(pattrib->pktlen));
5306         } else {
5307                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, bssrate_len, bssrate, &(pattrib->pktlen));
5308         }
5309
5310         /* RSN */
5311         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)));
5312         if (p)
5313                 pframe = rtw_set_ie(pframe, _RSN_IE_2_, ie_len, (p + 2), &(pattrib->pktlen));
5314
5315         /* HT caps */
5316         if (padapter->mlmepriv.htpriv.ht_option) {
5317                 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)));
5318                 if (p && !is_ap_in_tkip(padapter)) {
5319                         memcpy(&(pmlmeinfo->HT_caps), (p + 2), sizeof(struct HT_caps_element));
5320
5321                         /* to disable 40M Hz support while gd_bw_40MHz_en = 0 */
5322                         if (pregpriv->cbw40_enable == 0)
5323                                 pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info &= cpu_to_le16(~(BIT(6) | BIT(1)));
5324                         else
5325                                 pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info |= cpu_to_le16(BIT(1));
5326
5327                         /* todo: disable SM power save mode */
5328                         pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info |= cpu_to_le16(0x000c);
5329
5330                         rtw_hal_get_hwreg(padapter, HW_VAR_RF_TYPE, (u8 *)(&rf_type));
5331                         switch (rf_type) {
5332                         case RF_1T1R:
5333                                 if (pregpriv->rx_stbc)
5334                                         pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info |= cpu_to_le16(0x0100);/* RX STBC One spatial stream */
5335                                 memcpy(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_rate_1R, 16);
5336                                 break;
5337                         case RF_2T2R:
5338                         case RF_1T2R:
5339                         default:
5340                                 if ((pregpriv->rx_stbc == 0x3) ||/* enable for 2.4/5 GHz */
5341                                     ((pmlmeext->cur_wireless_mode & WIRELESS_11_24N) && (pregpriv->rx_stbc == 0x1)) || /* enable for 2.4GHz */
5342                                     (pregpriv->wifi_spec == 1)) {
5343                                         DBG_88E("declare supporting RX STBC\n");
5344                                         pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info |= cpu_to_le16(0x0200);/* RX STBC two spatial stream */
5345                                 }
5346                                 memcpy(pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate, MCS_rate_2R, 16);
5347                                 break;
5348                         }
5349                         pframe = rtw_set_ie(pframe, _HT_CAPABILITY_IE_, ie_len, (u8 *)(&(pmlmeinfo->HT_caps)), &(pattrib->pktlen));
5350                 }
5351         }
5352
5353         /* vendor specific IE, such as WPA, WMM, WPS */
5354         for (i = sizeof(struct ndis_802_11_fixed_ie); i < pmlmeinfo->network.IELength;) {
5355                 pIE = (struct ndis_802_11_var_ie *)(pmlmeinfo->network.IEs + i);
5356
5357                 switch (pIE->ElementID) {
5358                 case _VENDOR_SPECIFIC_IE_:
5359                         if ((!memcmp(pIE->data, RTW_WPA_OUI, 4)) ||
5360                             (!memcmp(pIE->data, WMM_OUI, 4)) ||
5361                             (!memcmp(pIE->data, WPS_OUI, 4))) {
5362                                 if (!padapter->registrypriv.wifi_spec) {
5363                                         /* Commented by Kurt 20110629 */
5364                                         /* In some older APs, WPS handshake */
5365                                         /* would be fail if we append vender extensions informations to AP */
5366                                         if (!memcmp(pIE->data, WPS_OUI, 4))
5367                                                 pIE->Length = 14;
5368                                 }
5369                                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, pIE->Length, pIE->data, &(pattrib->pktlen));
5370                         }
5371                         break;
5372                 default:
5373                         break;
5374                 }
5375                 i += (pIE->Length + 2);
5376         }
5377
5378         if (pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_REALTEK)
5379                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, 6, REALTEK_96B_IE, &(pattrib->pktlen));
5380
5381 #ifdef CONFIG_88EU_P2P
5382
5383         if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE) && !rtw_p2p_chk_state(pwdinfo, P2P_STATE_IDLE)) {
5384                 /*      Should add the P2P IE in the association request frame. */
5385                 /*      P2P OUI */
5386
5387                 p2pielen = 0;
5388                 p2pie[p2pielen++] = 0x50;
5389                 p2pie[p2pielen++] = 0x6F;
5390                 p2pie[p2pielen++] = 0x9A;
5391                 p2pie[p2pielen++] = 0x09;       /*      WFA P2P v1.0 */
5392
5393                 /*      Commented by Albert 20101109 */
5394                 /*      According to the P2P Specification, the association request frame should contain 3 P2P attributes */
5395                 /*      1. P2P Capability */
5396                 /*      2. Extended Listen Timing */
5397                 /*      3. Device Info */
5398                 /*      Commented by Albert 20110516 */
5399                 /*      4. P2P Interface */
5400
5401                 /*      P2P Capability */
5402                 /*      Type: */
5403                 p2pie[p2pielen++] = P2P_ATTR_CAPABILITY;
5404
5405                 /*      Length: */
5406                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002);
5407                 p2pielen += 2;
5408
5409                 /*      Value: */
5410                 /*      Device Capability Bitmap, 1 byte */
5411                 p2pie[p2pielen++] = DMP_P2P_DEVCAP_SUPPORT;
5412
5413                 /*      Group Capability Bitmap, 1 byte */
5414                 if (pwdinfo->persistent_supported)
5415                         p2pie[p2pielen++] = P2P_GRPCAP_PERSISTENT_GROUP | DMP_P2P_GRPCAP_SUPPORT;
5416                 else
5417                         p2pie[p2pielen++] = DMP_P2P_GRPCAP_SUPPORT;
5418
5419                 /*      Extended Listen Timing */
5420                 /*      Type: */
5421                 p2pie[p2pielen++] = P2P_ATTR_EX_LISTEN_TIMING;
5422
5423                 /*      Length: */
5424                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x0004);
5425                 p2pielen += 2;
5426
5427                 /*      Value: */
5428                 /*      Availability Period */
5429                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0xFFFF);
5430                 p2pielen += 2;
5431
5432                 /*      Availability Interval */
5433                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0xFFFF);
5434                 p2pielen += 2;
5435
5436                 /*      Device Info */
5437                 /*      Type: */
5438                 p2pie[p2pielen++] = P2P_ATTR_DEVICE_INFO;
5439
5440                 /*      Length: */
5441                 /*      21 -> P2P Device Address (6bytes) + Config Methods (2bytes) + Primary Device Type (8bytes) */
5442                 /*      + NumofSecondDevType (1byte) + WPS Device Name ID field (2bytes) + WPS Device Name Len field (2bytes) */
5443                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(21 + pwdinfo->device_name_len);
5444                 p2pielen += 2;
5445
5446                 /*      Value: */
5447                 /*      P2P Device Address */
5448                 memcpy(p2pie + p2pielen, myid(&padapter->eeprompriv), ETH_ALEN);
5449                 p2pielen += ETH_ALEN;
5450
5451                 /*      Config Method */
5452                 /*      This field should be big endian. Noted by P2P specification. */
5453                 if ((pwdinfo->ui_got_wps_info == P2P_GOT_WPSINFO_PEER_DISPLAY_PIN) ||
5454                     (pwdinfo->ui_got_wps_info == P2P_GOT_WPSINFO_SELF_DISPLAY_PIN))
5455                         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_CONFIG_METHOD_DISPLAY);
5456                 else
5457                         *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_CONFIG_METHOD_PBC);
5458
5459                 p2pielen += 2;
5460
5461                 /*      Primary Device Type */
5462                 /*      Category ID */
5463                 *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA);
5464                 p2pielen += 2;
5465
5466                 /*      OUI */
5467                 *(__be32 *)(p2pie + p2pielen) = cpu_to_be32(WPSOUI);
5468                 p2pielen += 4;
5469
5470                 /*      Sub Category ID */
5471                 *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER);
5472                 p2pielen += 2;
5473
5474                 /*      Number of Secondary Device Types */
5475                 p2pie[p2pielen++] = 0x00;       /*      No Secondary Device Type List */
5476
5477                 /*      Device Name */
5478                 /*      Type: */
5479                 *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_ATTR_DEVICE_NAME);
5480                 p2pielen += 2;
5481
5482                 /*      Length: */
5483                 *(__be16 *)(p2pie + p2pielen) = cpu_to_be16(pwdinfo->device_name_len);
5484                 p2pielen += 2;
5485
5486                 /*      Value: */
5487                 memcpy(p2pie + p2pielen, pwdinfo->device_name, pwdinfo->device_name_len);
5488                 p2pielen += pwdinfo->device_name_len;
5489
5490                 /*      P2P Interface */
5491                 /*      Type: */
5492                 p2pie[p2pielen++] = P2P_ATTR_INTERFACE;
5493
5494                 /*      Length: */
5495                 *(__le16 *)(p2pie + p2pielen) = cpu_to_le16(0x000D);
5496                 p2pielen += 2;
5497
5498                 /*      Value: */
5499                 memcpy(p2pie + p2pielen, pwdinfo->device_addr, ETH_ALEN);       /*      P2P Device Address */
5500                 p2pielen += ETH_ALEN;
5501
5502                 p2pie[p2pielen++] = 1;  /*      P2P Interface Address Count */
5503
5504                 memcpy(p2pie + p2pielen, pwdinfo->device_addr, ETH_ALEN);       /*      P2P Interface Address List */
5505                 p2pielen += ETH_ALEN;
5506
5507                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *)p2pie, &pattrib->pktlen);
5508         }
5509
5510 #endif /* CONFIG_88EU_P2P */
5511
5512         pattrib->last_txcmdsz = pattrib->pktlen;
5513         dump_mgntframe(padapter, pmgntframe);
5514
5515         ret = _SUCCESS;
5516
5517 exit:
5518         if (ret == _SUCCESS)
5519                 rtw_buf_update(&pmlmepriv->assoc_req, &pmlmepriv->assoc_req_len, (u8 *)pwlanhdr, pattrib->pktlen);
5520         else
5521                 kfree(pmlmepriv->assoc_req);
5522 }
5523
5524 /* when wait_ack is ture, this function shoule be called at process context */
5525 static int _issue_nulldata(struct adapter *padapter, unsigned char *da, unsigned int power_mode, int wait_ack)
5526 {
5527         int ret = _FAIL;
5528         struct xmit_frame                       *pmgntframe;
5529         struct pkt_attrib                       *pattrib;
5530         unsigned char                                   *pframe;
5531         struct rtw_ieee80211_hdr        *pwlanhdr;
5532         __le16 *fctrl;
5533         struct xmit_priv        *pxmitpriv;
5534         struct mlme_ext_priv    *pmlmeext;
5535         struct mlme_ext_info    *pmlmeinfo;
5536
5537         if (!padapter)
5538                 goto exit;
5539
5540         pxmitpriv = &(padapter->xmitpriv);
5541         pmlmeext = &(padapter->mlmeextpriv);
5542         pmlmeinfo = &(pmlmeext->mlmext_info);
5543
5544         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
5545         if (!pmgntframe)
5546                 goto exit;
5547
5548         /* update attribute */
5549         pattrib = &pmgntframe->attrib;
5550         update_mgntframe_attrib(padapter, pattrib);
5551         pattrib->retry_ctrl = false;
5552
5553         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
5554
5555         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
5556         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
5557
5558         fctrl = &(pwlanhdr->frame_ctl);
5559         *(fctrl) = 0;
5560
5561         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
5562                 SetFrDs(fctrl);
5563         else if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
5564                 SetToDs(fctrl);
5565
5566         if (power_mode)
5567                 SetPwrMgt(fctrl);
5568
5569         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
5570         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
5571         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
5572
5573         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
5574         pmlmeext->mgnt_seq++;
5575         SetFrameSubType(pframe, WIFI_DATA_NULL);
5576
5577         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
5578         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
5579
5580         pattrib->last_txcmdsz = pattrib->pktlen;
5581
5582         if (wait_ack) {
5583                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
5584         } else {
5585                 dump_mgntframe(padapter, pmgntframe);
5586                 ret = _SUCCESS;
5587         }
5588
5589 exit:
5590         return ret;
5591 }
5592
5593 /* when wait_ms > 0 , this function shoule be called at process context */
5594 /* da == NULL for station mode */
5595 int issue_nulldata(struct adapter *padapter, unsigned char *da, unsigned int power_mode, int try_cnt, int wait_ms)
5596 {
5597         int ret;
5598         int i = 0;
5599         u32 start = jiffies;
5600         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
5601         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5602
5603         /* da == NULL, assum it's null data for sta to ap*/
5604         if (!da)
5605                 da = get_my_bssid(&(pmlmeinfo->network));
5606
5607         do {
5608                 ret = _issue_nulldata(padapter, da, power_mode, wait_ms > 0 ? true : false);
5609
5610                 i++;
5611
5612                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
5613                         break;
5614
5615                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
5616                         msleep(wait_ms);
5617         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
5618
5619         if (ret != _FAIL) {
5620                 ret = _SUCCESS;
5621                 goto exit;
5622         }
5623
5624         if (try_cnt && wait_ms) {
5625                 if (da)
5626                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
5627                                 FUNC_ADPT_ARG(padapter), da, rtw_get_oper_ch(padapter),
5628                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
5629                 else
5630                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
5631                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
5632                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
5633         }
5634 exit:
5635         return ret;
5636 }
5637
5638 /* when wait_ack is ture, this function shoule be called at process context */
5639 static int _issue_qos_nulldata(struct adapter *padapter, unsigned char *da, u16 tid, int wait_ack)
5640 {
5641         int ret = _FAIL;
5642         struct xmit_frame                       *pmgntframe;
5643         struct pkt_attrib                       *pattrib;
5644         unsigned char                                   *pframe;
5645         struct rtw_ieee80211_hdr        *pwlanhdr;
5646         __le16 *fctrl;
5647         unsigned short *qc;
5648         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
5649         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
5650         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5651
5652         DBG_88E("%s\n", __func__);
5653
5654         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
5655         if (!pmgntframe)
5656                 goto exit;
5657
5658         /* update attribute */
5659         pattrib = &pmgntframe->attrib;
5660         update_mgntframe_attrib(padapter, pattrib);
5661
5662         pattrib->hdrlen += 2;
5663         pattrib->qos_en = true;
5664         pattrib->eosp = 1;
5665         pattrib->ack_policy = 0;
5666         pattrib->mdata = 0;
5667
5668         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
5669
5670         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
5671         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
5672
5673         fctrl = &(pwlanhdr->frame_ctl);
5674         *(fctrl) = 0;
5675
5676         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
5677                 SetFrDs(fctrl);
5678         else if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
5679                 SetToDs(fctrl);
5680
5681         if (pattrib->mdata)
5682                 SetMData(fctrl);
5683
5684         qc = (unsigned short *)(pframe + pattrib->hdrlen - 2);
5685
5686         SetPriority(qc, tid);
5687
5688         SetEOSP(qc, pattrib->eosp);
5689
5690         SetAckpolicy(qc, pattrib->ack_policy);
5691
5692         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
5693         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
5694         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
5695
5696         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
5697         pmlmeext->mgnt_seq++;
5698         SetFrameSubType(pframe, WIFI_QOS_DATA_NULL);
5699
5700         pframe += sizeof(struct rtw_ieee80211_hdr_3addr_qos);
5701         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr_qos);
5702
5703         pattrib->last_txcmdsz = pattrib->pktlen;
5704
5705         if (wait_ack) {
5706                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
5707         } else {
5708                 dump_mgntframe(padapter, pmgntframe);
5709                 ret = _SUCCESS;
5710         }
5711
5712 exit:
5713         return ret;
5714 }
5715
5716 /* when wait_ms > 0 , this function shoule be called at process context */
5717 /* da == NULL for station mode */
5718 int issue_qos_nulldata(struct adapter *padapter, unsigned char *da, u16 tid, int try_cnt, int wait_ms)
5719 {
5720         int ret;
5721         int i = 0;
5722         u32 start = jiffies;
5723         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
5724         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5725
5726         /* da == NULL, assum it's null data for sta to ap*/
5727         if (!da)
5728                 da = get_my_bssid(&(pmlmeinfo->network));
5729
5730         do {
5731                 ret = _issue_qos_nulldata(padapter, da, tid, wait_ms > 0 ? true : false);
5732
5733                 i++;
5734
5735                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
5736                         break;
5737
5738                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
5739                         msleep(wait_ms);
5740         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
5741
5742         if (ret != _FAIL) {
5743                 ret = _SUCCESS;
5744                 goto exit;
5745         }
5746
5747         if (try_cnt && wait_ms) {
5748                 if (da)
5749                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
5750                                 FUNC_ADPT_ARG(padapter), da, rtw_get_oper_ch(padapter),
5751                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
5752                 else
5753                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
5754                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
5755                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
5756         }
5757 exit:
5758         return ret;
5759 }
5760
5761 static int _issue_deauth(struct adapter *padapter, unsigned char *da, unsigned short reason, u8 wait_ack)
5762 {
5763         struct xmit_frame                       *pmgntframe;
5764         struct pkt_attrib                       *pattrib;
5765         unsigned char                                   *pframe;
5766         struct rtw_ieee80211_hdr        *pwlanhdr;
5767         __le16 *fctrl;
5768         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
5769         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
5770         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5771         int ret = _FAIL;
5772         __le16 le_tmp;
5773 #ifdef CONFIG_88EU_P2P
5774         struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
5775 #endif /* CONFIG_88EU_P2P */
5776
5777 #ifdef CONFIG_88EU_P2P
5778         if (!(rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) && (pwdinfo->rx_invitereq_info.scan_op_ch_only)) {
5779                 _cancel_timer_ex(&pwdinfo->reset_ch_sitesurvey);
5780                 _set_timer(&pwdinfo->reset_ch_sitesurvey, 10);
5781         }
5782 #endif /* CONFIG_88EU_P2P */
5783
5784         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
5785         if (!pmgntframe)
5786                 goto exit;
5787
5788         /* update attribute */
5789         pattrib = &pmgntframe->attrib;
5790         update_mgntframe_attrib(padapter, pattrib);
5791         pattrib->retry_ctrl = false;
5792
5793         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
5794
5795         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
5796         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
5797
5798         fctrl = &(pwlanhdr->frame_ctl);
5799         *(fctrl) = 0;
5800
5801         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
5802         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
5803         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
5804
5805         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
5806         pmlmeext->mgnt_seq++;
5807         SetFrameSubType(pframe, WIFI_DEAUTH);
5808
5809         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
5810         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
5811
5812         le_tmp = cpu_to_le16(reason);
5813         pframe = rtw_set_fixed_ie(pframe, _RSON_CODE_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
5814
5815         pattrib->last_txcmdsz = pattrib->pktlen;
5816
5817         if (wait_ack) {
5818                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
5819         } else {
5820                 dump_mgntframe(padapter, pmgntframe);
5821                 ret = _SUCCESS;
5822         }
5823
5824 exit:
5825         return ret;
5826 }
5827
5828 int issue_deauth(struct adapter *padapter, unsigned char *da, unsigned short reason)
5829 {
5830         DBG_88E("%s to %pM\n", __func__, da);
5831         return _issue_deauth(padapter, da, reason, false);
5832 }
5833
5834 int issue_deauth_ex(struct adapter *padapter, u8 *da, unsigned short reason, int try_cnt,
5835         int wait_ms)
5836 {
5837         int ret;
5838         int i = 0;
5839         u32 start = jiffies;
5840
5841         do {
5842                 ret = _issue_deauth(padapter, da, reason, wait_ms > 0 ? true : false);
5843
5844                 i++;
5845
5846                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
5847                         break;
5848
5849                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
5850                         msleep(wait_ms);
5851         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
5852
5853         if (ret != _FAIL) {
5854                 ret = _SUCCESS;
5855                 goto exit;
5856         }
5857
5858         if (try_cnt && wait_ms) {
5859                 if (da)
5860                         DBG_88E(FUNC_ADPT_FMT" to %pM, ch:%u%s, %d/%d in %u ms\n",
5861                                 FUNC_ADPT_ARG(padapter), da, rtw_get_oper_ch(padapter),
5862                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
5863                 else
5864                         DBG_88E(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
5865                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
5866                                 ret == _SUCCESS ? ", acked" : "", i, try_cnt, rtw_get_passing_time_ms(start));
5867         }
5868 exit:
5869         return ret;
5870 }
5871
5872 void issue_action_spct_ch_switch (struct adapter *padapter, u8 *ra, u8 new_ch, u8 ch_offset)
5873 {
5874         struct xmit_frame                       *pmgntframe;
5875         struct pkt_attrib                       *pattrib;
5876         unsigned char                           *pframe;
5877         struct rtw_ieee80211_hdr        *pwlanhdr;
5878         __le16 *fctrl;
5879         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
5880         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
5881
5882         DBG_88E(FUNC_NDEV_FMT" ra =%pM, ch:%u, offset:%u\n",
5883                 FUNC_NDEV_ARG(padapter->pnetdev), ra, new_ch, ch_offset);
5884
5885         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
5886         if (!pmgntframe)
5887                 return;
5888
5889         /* update attribute */
5890         pattrib = &pmgntframe->attrib;
5891         update_mgntframe_attrib(padapter, pattrib);
5892
5893         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
5894
5895         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
5896         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
5897
5898         fctrl = &(pwlanhdr->frame_ctl);
5899         *(fctrl) = 0;
5900
5901         memcpy(pwlanhdr->addr1, ra, ETH_ALEN); /* RA */
5902         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN); /* TA */
5903         memcpy(pwlanhdr->addr3, ra, ETH_ALEN); /* DA = RA */
5904
5905         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
5906         pmlmeext->mgnt_seq++;
5907         SetFrameSubType(pframe, WIFI_ACTION);
5908
5909         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
5910         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
5911
5912         /* category, action */
5913         {
5914                 u8 category, action;
5915                 category = RTW_WLAN_CATEGORY_SPECTRUM_MGMT;
5916                 action = RTW_WLAN_ACTION_SPCT_CHL_SWITCH;
5917
5918                 pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
5919                 pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
5920         }
5921
5922         pframe = rtw_set_ie_ch_switch (pframe, &(pattrib->pktlen), 0, new_ch, 0);
5923         pframe = rtw_set_ie_secondary_ch_offset(pframe, &(pattrib->pktlen),
5924                 hal_ch_offset_to_secondary_ch_offset(ch_offset));
5925
5926         pattrib->last_txcmdsz = pattrib->pktlen;
5927
5928         dump_mgntframe(padapter, pmgntframe);
5929 }
5930
5931 void issue_action_BA(struct adapter *padapter, unsigned char *raddr, unsigned char action, unsigned short status)
5932 {
5933         u8 category = RTW_WLAN_CATEGORY_BACK;
5934         u16 start_seq;
5935         u16 BA_para_set;
5936         u16 reason_code;
5937         u16 BA_timeout_value;
5938         __le16  le_tmp;
5939         u16 BA_starting_seqctrl = 0;
5940         enum ht_cap_ampdu_factor max_rx_ampdu_factor;
5941         struct xmit_frame *pmgntframe;
5942         struct pkt_attrib *pattrib;
5943         u8 *pframe;
5944         struct rtw_ieee80211_hdr *pwlanhdr;
5945         __le16 *fctrl;
5946         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
5947         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
5948         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
5949         struct sta_info *psta;
5950         struct sta_priv *pstapriv = &padapter->stapriv;
5951         struct registry_priv *pregpriv = &padapter->registrypriv;
5952
5953         DBG_88E("%s, category=%d, action=%d, status=%d\n", __func__, category, action, status);
5954
5955         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
5956         if (!pmgntframe)
5957                 return;
5958
5959         /* update attribute */
5960         pattrib = &pmgntframe->attrib;
5961         update_mgntframe_attrib(padapter, pattrib);
5962
5963         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
5964
5965         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
5966         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
5967
5968         fctrl = &(pwlanhdr->frame_ctl);
5969         *(fctrl) = 0;
5970
5971         /* memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); */
5972         memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
5973         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
5974         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
5975
5976         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
5977         pmlmeext->mgnt_seq++;
5978         SetFrameSubType(pframe, WIFI_ACTION);
5979
5980         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
5981         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
5982
5983         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
5984         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
5985
5986         if (category == 3) {
5987                 switch (action) {
5988                 case 0: /* ADDBA req */
5989                         do {
5990                                 pmlmeinfo->dialogToken++;
5991                         } while (pmlmeinfo->dialogToken == 0);
5992                         pframe = rtw_set_fixed_ie(pframe, 1, &(pmlmeinfo->dialogToken), &(pattrib->pktlen));
5993
5994                         BA_para_set = (0x1002 | ((status & 0xf) << 2)); /* immediate ack & 64 buffer size */
5995                         le_tmp = cpu_to_le16(BA_para_set);
5996                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
5997
5998                         BA_timeout_value = 5000;/*  5ms */
5999                         le_tmp = cpu_to_le16(BA_timeout_value);
6000                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
6001
6002                         psta = rtw_get_stainfo(pstapriv, raddr);
6003                         if (psta) {
6004                                 start_seq = (psta->sta_xmitpriv.txseq_tid[status & 0x07]&0xfff) + 1;
6005
6006                                 DBG_88E("BA_starting_seqctrl=%d for TID=%d\n", start_seq, status & 0x07);
6007
6008                                 psta->BA_starting_seqctrl[status & 0x07] = start_seq;
6009
6010                                 BA_starting_seqctrl = start_seq << 4;
6011                         }
6012                         le_tmp = cpu_to_le16(BA_starting_seqctrl);
6013                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
6014                         break;
6015                 case 1: /* ADDBA rsp */
6016                         pframe = rtw_set_fixed_ie(pframe, 1, &(pmlmeinfo->ADDBA_req.dialog_token), &(pattrib->pktlen));
6017                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&status), &(pattrib->pktlen));
6018                         BA_para_set = le16_to_cpu(pmlmeinfo->ADDBA_req.BA_para_set) & 0x3f;
6019                         rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor);
6020                         switch (max_rx_ampdu_factor) {
6021                         case MAX_AMPDU_FACTOR_64K:
6022                                 BA_para_set |= 0x1000; /* 64 buffer size */
6023                                 break;
6024                         case MAX_AMPDU_FACTOR_32K:
6025                                 BA_para_set |= 0x0800; /* 32 buffer size */
6026                                 break;
6027                         case MAX_AMPDU_FACTOR_16K:
6028                                 BA_para_set |= 0x0400; /* 16 buffer size */
6029                                 break;
6030                         case MAX_AMPDU_FACTOR_8K:
6031                                 BA_para_set |= 0x0200; /* 8 buffer size */
6032                                 break;
6033                         default:
6034                                 BA_para_set |= 0x1000; /* 64 buffer size */
6035                                 break;
6036                         }
6037
6038                         if (pregpriv->ampdu_amsdu == 0)/* disabled */
6039                                 BA_para_set = BA_para_set & ~BIT(0);
6040                         else if (pregpriv->ampdu_amsdu == 1)/* enabled */
6041                                 BA_para_set = BA_para_set | BIT(0);
6042                         le_tmp = cpu_to_le16(BA_para_set);
6043
6044                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
6045                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(pmlmeinfo->ADDBA_req.BA_timeout_value)), &(pattrib->pktlen));
6046                         break;
6047                 case 2:/* DELBA */
6048                         BA_para_set = (status & 0x1F) << 3;
6049                         le_tmp = cpu_to_le16(BA_para_set);
6050                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
6051
6052                         reason_code = 37;/* Requested from peer STA as it does not want to use the mechanism */
6053                         le_tmp = cpu_to_le16(reason_code);
6054                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
6055                         break;
6056                 default:
6057                         break;
6058                 }
6059         }
6060
6061         pattrib->last_txcmdsz = pattrib->pktlen;
6062
6063         dump_mgntframe(padapter, pmgntframe);
6064 }
6065
6066 static void issue_action_BSSCoexistPacket(struct adapter *padapter)
6067 {
6068         struct list_head *plist, *phead;
6069         unsigned char category, action;
6070         struct xmit_frame                       *pmgntframe;
6071         struct pkt_attrib                       *pattrib;
6072         unsigned char                           *pframe;
6073         struct rtw_ieee80211_hdr        *pwlanhdr;
6074         __le16 *fctrl;
6075         struct  wlan_network    *pnetwork = NULL;
6076         struct xmit_priv                        *pxmitpriv = &(padapter->xmitpriv);
6077         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
6078         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
6079         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6080         struct __queue *queue   = &(pmlmepriv->scanned_queue);
6081         u8 InfoContent[16] = {0};
6082         u8 ICS[8][15];
6083         if ((pmlmepriv->num_FortyMHzIntolerant == 0) || (pmlmepriv->num_sta_no_ht == 0))
6084                 return;
6085
6086         if (pmlmeinfo->bwmode_updated)
6087                 return;
6088
6089         DBG_88E("%s\n", __func__);
6090
6091         category = RTW_WLAN_CATEGORY_PUBLIC;
6092         action = ACT_PUBLIC_BSSCOEXIST;
6093
6094         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
6095         if (!pmgntframe)
6096                 return;
6097
6098         /* update attribute */
6099         pattrib = &pmgntframe->attrib;
6100         update_mgntframe_attrib(padapter, pattrib);
6101
6102         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
6103
6104         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
6105         pwlanhdr = (struct rtw_ieee80211_hdr *)pframe;
6106
6107         fctrl = &(pwlanhdr->frame_ctl);
6108         *(fctrl) = 0;
6109
6110         memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
6111         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
6112         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
6113
6114         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
6115         pmlmeext->mgnt_seq++;
6116         SetFrameSubType(pframe, WIFI_ACTION);
6117
6118         pframe += sizeof(struct rtw_ieee80211_hdr_3addr);
6119         pattrib->pktlen = sizeof(struct rtw_ieee80211_hdr_3addr);
6120
6121         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
6122         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
6123
6124         /*  */
6125         if (pmlmepriv->num_FortyMHzIntolerant > 0) {
6126                 u8 iedata = 0;
6127
6128                 iedata |= BIT(2);/* 20 MHz BSS Width Request */
6129
6130                 pframe = rtw_set_ie(pframe, EID_BSSCoexistence,  1, &iedata, &(pattrib->pktlen));
6131         }
6132
6133         /*  */
6134         memset(ICS, 0, sizeof(ICS));
6135         if (pmlmepriv->num_sta_no_ht > 0) {
6136                 int i;
6137
6138                 spin_lock_bh(&pmlmepriv->scanned_queue.lock);
6139
6140                 phead = get_list_head(queue);
6141                 plist = phead->next;
6142
6143                 while (phead != plist) {
6144                         int len;
6145                         u8 *p;
6146                         struct wlan_bssid_ex *pbss_network;
6147
6148                         pnetwork = container_of(plist, struct wlan_network, list);
6149
6150                         plist = plist->next;
6151
6152                         pbss_network = (struct wlan_bssid_ex *)&pnetwork->network;
6153
6154                         p = rtw_get_ie(pbss_network->IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, pbss_network->IELength - _FIXED_IE_LENGTH_);
6155                         if (!p || len == 0) { /* non-HT */
6156                                 if ((pbss_network->Configuration.DSConfig <= 0) || (pbss_network->Configuration.DSConfig > 14))
6157                                         continue;
6158
6159                                 ICS[0][pbss_network->Configuration.DSConfig] = 1;
6160
6161                                 if (ICS[0][0] == 0)
6162                                         ICS[0][0] = 1;
6163                         }
6164                 }
6165                 spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
6166
6167                 for (i = 0; i < 8; i++) {
6168                         if (ICS[i][0] == 1) {
6169                                 int j, k = 0;
6170
6171                                 InfoContent[k] = i;
6172                                 /* SET_BSS_INTOLERANT_ELE_REG_CLASS(InfoContent, i); */
6173                                 k++;
6174
6175                                 for (j = 1; j <= 14; j++) {
6176                                         if (ICS[i][j] == 1) {
6177                                                 if (k < 16) {
6178                                                         InfoContent[k] = j; /* channel number */
6179                                                         /* SET_BSS_INTOLERANT_ELE_CHANNEL(InfoContent+k, j); */
6180                                                         k++;
6181                                                 }
6182                                         }
6183                                 }
6184
6185                                 pframe = rtw_set_ie(pframe, EID_BSSIntolerantChlReport, k, InfoContent, &(pattrib->pktlen));
6186                         }
6187                 }
6188         }
6189
6190         pattrib->last_txcmdsz = pattrib->pktlen;
6191
6192         dump_mgntframe(padapter, pmgntframe);
6193 }
6194
6195 unsigned int send_delba(struct adapter *padapter, u8 initiator, u8 *addr)
6196 {
6197         struct sta_priv *pstapriv = &padapter->stapriv;
6198         struct sta_info *psta = NULL;
6199         /* struct recv_reorder_ctrl *preorder_ctrl; */
6200         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
6201         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6202         u16 tid;
6203
6204         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
6205                 if (!(pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS))
6206                         return _SUCCESS;
6207
6208         psta = rtw_get_stainfo(pstapriv, addr);
6209         if (!psta)
6210                 return _SUCCESS;
6211
6212         if (initiator == 0) { /*  recipient */
6213                 for (tid = 0; tid < MAXTID; tid++) {
6214                         if (psta->recvreorder_ctrl[tid].enable) {
6215                                 DBG_88E("rx agg disable tid(%d)\n", tid);
6216                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_DELBA, (((tid << 1) | initiator)&0x1F));
6217                                 psta->recvreorder_ctrl[tid].enable = false;
6218                                 psta->recvreorder_ctrl[tid].indicate_seq = 0xffff;
6219                         }
6220                 }
6221         } else if (initiator == 1) { /*  originator */
6222                 for (tid = 0; tid < MAXTID; tid++) {
6223                         if (psta->htpriv.agg_enable_bitmap & BIT(tid)) {
6224                                 DBG_88E("tx agg disable tid(%d)\n", tid);
6225                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_DELBA, (((tid << 1) | initiator)&0x1F));
6226                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
6227                                 psta->htpriv.candidate_tid_bitmap &= ~BIT(tid);
6228                         }
6229                 }
6230         }
6231
6232         return _SUCCESS;
6233 }
6234
6235 unsigned int send_beacon(struct adapter *padapter)
6236 {
6237         u8 bxmitok = false;
6238         int     issue = 0;
6239         int poll = 0;
6240
6241         u32 start = jiffies;
6242
6243         rtw_hal_set_hwreg(padapter, HW_VAR_BCN_VALID, NULL);
6244         do {
6245                 issue_beacon(padapter, 100);
6246                 issue++;
6247                 do {
6248                         yield();
6249                         rtw_hal_get_hwreg(padapter, HW_VAR_BCN_VALID, (u8 *)(&bxmitok));
6250                         poll++;
6251                 } while ((poll%10) != 0 && !bxmitok && !padapter->bSurpriseRemoved && !padapter->bDriverStopped);
6252         } while (!bxmitok && issue < 100 && !padapter->bSurpriseRemoved && !padapter->bDriverStopped);
6253
6254         if (padapter->bSurpriseRemoved || padapter->bDriverStopped)
6255                 return _FAIL;
6256         if (!bxmitok) {
6257                 DBG_88E("%s fail! %u ms\n", __func__, rtw_get_passing_time_ms(start));
6258                 return _FAIL;
6259         } else {
6260                 u32 passing_time = rtw_get_passing_time_ms(start);
6261
6262                 if (passing_time > 100 || issue > 3)
6263                         DBG_88E("%s success, issue:%d, poll:%d, %u ms\n", __func__, issue, poll, rtw_get_passing_time_ms(start));
6264                 return _SUCCESS;
6265         }
6266 }
6267
6268 /****************************************************************************
6269
6270 Following are some utitity fuctions for WiFi MLME
6271
6272 *****************************************************************************/
6273
6274 void site_survey(struct adapter *padapter)
6275 {
6276         unsigned char           survey_channel = 0, val8;
6277         enum rt_scan_type ScanType = SCAN_PASSIVE;
6278         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
6279         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6280         u32 initialgain = 0;
6281
6282 #ifdef CONFIG_88EU_P2P
6283         struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
6284
6285         if ((pwdinfo->rx_invitereq_info.scan_op_ch_only) || (pwdinfo->p2p_info.scan_op_ch_only)) {
6286                 if (pwdinfo->rx_invitereq_info.scan_op_ch_only) {
6287                         survey_channel = pwdinfo->rx_invitereq_info.operation_ch[pmlmeext->sitesurvey_res.channel_idx];
6288                 } else {
6289                         survey_channel = pwdinfo->p2p_info.operation_ch[pmlmeext->sitesurvey_res.channel_idx];
6290                 }
6291                 ScanType = SCAN_ACTIVE;
6292         } else if (rtw_p2p_findphase_ex_is_social(pwdinfo)) {
6293                 /*      Commented by Albert 2011/06/03 */
6294                 /*      The driver is in the find phase, it should go through the social channel. */
6295                 int ch_set_idx;
6296                 survey_channel = pwdinfo->social_chan[pmlmeext->sitesurvey_res.channel_idx];
6297                 ch_set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, survey_channel);
6298                 if (ch_set_idx >= 0)
6299                         ScanType = pmlmeext->channel_set[ch_set_idx].ScanType;
6300                 else
6301                         ScanType = SCAN_ACTIVE;
6302         } else
6303 #endif /* CONFIG_88EU_P2P */
6304         {
6305                 struct rtw_ieee80211_channel *ch;
6306                 if (pmlmeext->sitesurvey_res.channel_idx < pmlmeext->sitesurvey_res.ch_num) {
6307                         ch = &pmlmeext->sitesurvey_res.ch[pmlmeext->sitesurvey_res.channel_idx];
6308                         survey_channel = ch->hw_value;
6309                         ScanType = (ch->flags & RTW_IEEE80211_CHAN_PASSIVE_SCAN) ? SCAN_PASSIVE : SCAN_ACTIVE;
6310                 }
6311         }
6312
6313         if (survey_channel != 0) {
6314                 /* PAUSE 4-AC Queue when site_survey */
6315                 /* rtw_hal_get_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
6316                 /* val8 |= 0x0f; */
6317                 /* rtw_hal_set_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
6318                 if (pmlmeext->sitesurvey_res.channel_idx == 0)
6319                         set_channel_bwmode(padapter, survey_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
6320                 else
6321                         SelectChannel(padapter, survey_channel);
6322
6323                 if (ScanType == SCAN_ACTIVE) { /* obey the channel plan setting... */
6324                         #ifdef CONFIG_88EU_P2P
6325                         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_SCAN) ||
6326                             rtw_p2p_chk_state(pwdinfo, P2P_STATE_FIND_PHASE_SEARCH)) {
6327                                 issue_probereq_p2p(padapter, NULL);
6328                                 issue_probereq_p2p(padapter, NULL);
6329                                 issue_probereq_p2p(padapter, NULL);
6330                         } else
6331                         #endif /* CONFIG_88EU_P2P */
6332                         {
6333                                 int i;
6334                                 for (i = 0; i < RTW_SSID_SCAN_AMOUNT; i++) {
6335                                         if (pmlmeext->sitesurvey_res.ssid[i].SsidLength) {
6336                                                 /* todo: to issue two probe req??? */
6337                                                 issue_probereq(padapter, &(pmlmeext->sitesurvey_res.ssid[i]), NULL);
6338                                                 /* msleep(SURVEY_TO>>1); */
6339                                                 issue_probereq(padapter, &(pmlmeext->sitesurvey_res.ssid[i]), NULL);
6340                                         }
6341                                 }
6342
6343                                 if (pmlmeext->sitesurvey_res.scan_mode == SCAN_ACTIVE) {
6344                                         /* todo: to issue two probe req??? */
6345                                         issue_probereq(padapter, NULL, NULL);
6346                                         /* msleep(SURVEY_TO>>1); */
6347                                         issue_probereq(padapter, NULL, NULL);
6348                                 }
6349                         }
6350                 }
6351
6352                 set_survey_timer(pmlmeext, pmlmeext->chan_scan_time);
6353         } else {
6354                 /*      channel number is 0 or this channel is not valid. */
6355
6356 #ifdef CONFIG_88EU_P2P
6357                 if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_SCAN) || rtw_p2p_chk_state(pwdinfo, P2P_STATE_FIND_PHASE_SEARCH)) {
6358                         if ((pwdinfo->rx_invitereq_info.scan_op_ch_only) || (pwdinfo->p2p_info.scan_op_ch_only)) {
6359                                 /*      Set the find_phase_state_exchange_cnt to P2P_FINDPHASE_EX_CNT. */
6360                                 /*      This will let the following flow to run the scanning end. */
6361                                 rtw_p2p_findphase_ex_set(pwdinfo, P2P_FINDPHASE_EX_MAX);
6362                         }
6363                 }
6364
6365                 if (rtw_p2p_findphase_ex_is_needed(pwdinfo)) {
6366                         /*      Set the P2P State to the listen state of find phase and set the current channel to the listen channel */
6367                         set_channel_bwmode(padapter, pwdinfo->listen_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
6368                         rtw_p2p_set_state(pwdinfo, P2P_STATE_FIND_PHASE_LISTEN);
6369                         pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
6370
6371                         initialgain = 0xff; /* restore RX GAIN */
6372                         rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
6373                         /* turn on dynamic functions */
6374                         Restore_DM_Func_Flag(padapter);
6375                         /* Switch_DM_Func(padapter, DYNAMIC_FUNC_DIG|DYNAMIC_FUNC_HP|DYNAMIC_FUNC_SS, true); */
6376
6377                         _set_timer(&pwdinfo->find_phase_timer, (u32)((u32)(pwdinfo->listen_dwell) * 100));
6378                 } else
6379 #endif /* CONFIG_88EU_P2P */
6380                 {
6381                         /*  20100721:Interrupt scan operation here. */
6382                         /*  For SW antenna diversity before link, it needs to switch to another antenna and scan again. */
6383                         /*  It compares the scan result and select beter one to do connection. */
6384                         if (rtw_hal_antdiv_before_linked(padapter)) {
6385                                 pmlmeext->sitesurvey_res.bss_cnt = 0;
6386                                 pmlmeext->sitesurvey_res.channel_idx = -1;
6387                                 pmlmeext->chan_scan_time = SURVEY_TO / 2;
6388                                 set_survey_timer(pmlmeext, pmlmeext->chan_scan_time);
6389                                 return;
6390                         }
6391 #ifdef CONFIG_88EU_P2P
6392                         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_SCAN) || rtw_p2p_chk_state(pwdinfo, P2P_STATE_FIND_PHASE_SEARCH))
6393                                 rtw_p2p_set_state(pwdinfo, rtw_p2p_pre_state(pwdinfo));
6394                         rtw_p2p_findphase_ex_set(pwdinfo, P2P_FINDPHASE_EX_NONE);
6395 #endif /* CONFIG_88EU_P2P */
6396
6397                         pmlmeext->sitesurvey_res.state = SCAN_COMPLETE;
6398
6399                         /* switch back to the original channel */
6400
6401 #ifdef CONFIG_88EU_P2P
6402                         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_LISTEN))
6403                                 set_channel_bwmode(padapter, pwdinfo->listen_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
6404                         else
6405                                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
6406 #endif /* CONFIG_88EU_P2P */
6407
6408                         /* flush 4-AC Queue after site_survey */
6409                         /* val8 = 0; */
6410                         /* rtw_hal_set_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
6411
6412                         /* config MSR */
6413                         Set_MSR(padapter, (pmlmeinfo->state & 0x3));
6414
6415                         initialgain = 0xff; /* restore RX GAIN */
6416                         rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
6417                         /* turn on dynamic functions */
6418                         Restore_DM_Func_Flag(padapter);
6419                         /* Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true); */
6420
6421                         if (is_client_associated_to_ap(padapter))
6422                                 issue_nulldata(padapter, NULL, 0, 3, 500);
6423
6424                         val8 = 0; /* survey done */
6425                         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_SITESURVEY, (u8 *)(&val8));
6426
6427                         report_surveydone_event(padapter);
6428
6429                         pmlmeext->chan_scan_time = SURVEY_TO;
6430                         pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
6431
6432                         issue_action_BSSCoexistPacket(padapter);
6433                         issue_action_BSSCoexistPacket(padapter);
6434                         issue_action_BSSCoexistPacket(padapter);
6435                 }
6436         }
6437 }
6438
6439 /* collect bss info from Beacon and Probe request/response frames. */
6440 u8 collect_bss_info(struct adapter *padapter, struct recv_frame *precv_frame, struct wlan_bssid_ex *bssid)
6441 {
6442         int     i;
6443         u32     len;
6444         u8 *p;
6445         u16 val16, subtype;
6446         u8 *pframe = precv_frame->rx_data;
6447         u32     packet_len = precv_frame->len;
6448         u8 ie_offset;
6449         struct registry_priv    *pregistrypriv = &padapter->registrypriv;
6450         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
6451         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6452         __le32 le32_tmp;
6453
6454         len = packet_len - sizeof(struct rtw_ieee80211_hdr_3addr);
6455
6456         if (len > MAX_IE_SZ)
6457                 return _FAIL;
6458
6459         memset(bssid, 0, sizeof(struct wlan_bssid_ex));
6460
6461         subtype = GetFrameSubType(pframe);
6462
6463         if (subtype == WIFI_BEACON) {
6464                 bssid->Reserved[0] = 1;
6465                 ie_offset = _BEACON_IE_OFFSET_;
6466         } else {
6467                 /*  FIXME : more type */
6468                 if (subtype == WIFI_PROBEREQ) {
6469                         ie_offset = _PROBEREQ_IE_OFFSET_;
6470                         bssid->Reserved[0] = 2;
6471                 } else if (subtype == WIFI_PROBERSP) {
6472                         ie_offset = _PROBERSP_IE_OFFSET_;
6473                         bssid->Reserved[0] = 3;
6474                 } else {
6475                         bssid->Reserved[0] = 0;
6476                         ie_offset = _FIXED_IE_LENGTH_;
6477                 }
6478         }
6479
6480         bssid->Length = sizeof(struct wlan_bssid_ex) - MAX_IE_SZ + len;
6481
6482         /* below is to copy the information element */
6483         bssid->IELength = len;
6484         memcpy(bssid->IEs, (pframe + sizeof(struct rtw_ieee80211_hdr_3addr)), bssid->IELength);
6485
6486         /* get the signal strength */
6487         bssid->Rssi = precv_frame->attrib.phy_info.recvpower; /*  in dBM.raw data */
6488         bssid->PhyInfo.SignalQuality = precv_frame->attrib.phy_info.SignalQuality;/* in percentage */
6489         bssid->PhyInfo.SignalStrength = precv_frame->attrib.phy_info.SignalStrength;/* in percentage */
6490         rtw_hal_get_def_var(padapter, HAL_DEF_CURRENT_ANTENNA,  &bssid->PhyInfo.Optimum_antenna);
6491
6492         /*  checking SSID */
6493         p = rtw_get_ie(bssid->IEs + ie_offset, _SSID_IE_, &len, bssid->IELength - ie_offset);
6494         if (!p) {
6495                 DBG_88E("marc: cannot find SSID for survey event\n");
6496                 return _FAIL;
6497         }
6498
6499         if (*(p + 1)) {
6500                 if (len > NDIS_802_11_LENGTH_SSID) {
6501                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
6502                         return _FAIL;
6503                 }
6504                 memcpy(bssid->Ssid.Ssid, (p + 2), *(p + 1));
6505                 bssid->Ssid.SsidLength = *(p + 1);
6506         } else {
6507                 bssid->Ssid.SsidLength = 0;
6508         }
6509
6510         memset(bssid->SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
6511
6512         /* checking rate info... */
6513         i = 0;
6514         p = rtw_get_ie(bssid->IEs + ie_offset, _SUPPORTEDRATES_IE_, &len, bssid->IELength - ie_offset);
6515         if (p) {
6516                 if (len > NDIS_802_11_LENGTH_RATES_EX) {
6517                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
6518                         return _FAIL;
6519                 }
6520                 memcpy(bssid->SupportedRates, (p + 2), len);
6521                 i = len;
6522         }
6523
6524         p = rtw_get_ie(bssid->IEs + ie_offset, _EXT_SUPPORTEDRATES_IE_, &len, bssid->IELength - ie_offset);
6525         if (p) {
6526                 if (len > (NDIS_802_11_LENGTH_RATES_EX-i)) {
6527                         DBG_88E("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
6528                         return _FAIL;
6529                 }
6530                 memcpy(bssid->SupportedRates + i, (p + 2), len);
6531         }
6532
6533         /* todo: */
6534         bssid->NetworkTypeInUse = Ndis802_11OFDM24;
6535
6536         if (bssid->IELength < 12)
6537                 return _FAIL;
6538
6539         /*  Checking for DSConfig */
6540         p = rtw_get_ie(bssid->IEs + ie_offset, _DSSET_IE_, &len, bssid->IELength - ie_offset);
6541
6542         bssid->Configuration.DSConfig = 0;
6543         bssid->Configuration.Length = 0;
6544
6545         if (p) {
6546                 bssid->Configuration.DSConfig = *(p + 2);
6547         } else {/*  In 5G, some ap do not have DSSET IE */
6548                 /*  checking HT info for channel */
6549                 p = rtw_get_ie(bssid->IEs + ie_offset, _HT_ADD_INFO_IE_, &len, bssid->IELength - ie_offset);
6550                 if (p) {
6551                         struct HT_info_element *HT_info = (struct HT_info_element *)(p + 2);
6552                         bssid->Configuration.DSConfig = HT_info->primary_channel;
6553                 } else { /*  use current channel */
6554                         bssid->Configuration.DSConfig = rtw_get_oper_ch(padapter);
6555                 }
6556         }
6557
6558         memcpy(&le32_tmp, rtw_get_beacon_interval_from_ie(bssid->IEs), 2);
6559         bssid->Configuration.BeaconPeriod = le32_to_cpu(le32_tmp);
6560
6561         val16 = rtw_get_capability((struct wlan_bssid_ex *)bssid);
6562
6563         if (val16 & BIT(0)) {
6564                 bssid->InfrastructureMode = Ndis802_11Infrastructure;
6565                 memcpy(bssid->MacAddress, GetAddr2Ptr(pframe), ETH_ALEN);
6566         } else {
6567                 bssid->InfrastructureMode = Ndis802_11IBSS;
6568                 memcpy(bssid->MacAddress, GetAddr3Ptr(pframe), ETH_ALEN);
6569         }
6570
6571         if (val16 & BIT(4))
6572                 bssid->Privacy = 1;
6573         else
6574                 bssid->Privacy = 0;
6575
6576         bssid->Configuration.ATIMWindow = 0;
6577
6578         /* 20/40 BSS Coexistence check */
6579         if ((pregistrypriv->wifi_spec == 1) && (!pmlmeinfo->bwmode_updated)) {
6580                 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
6581                 p = rtw_get_ie(bssid->IEs + ie_offset, _HT_CAPABILITY_IE_, &len, bssid->IELength - ie_offset);
6582                 if (p && len > 0) {
6583                         struct HT_caps_element  *pHT_caps;
6584                         pHT_caps = (struct HT_caps_element *)(p + 2);
6585
6586                         if (le16_to_cpu(pHT_caps->u.HT_cap_element.HT_caps_info)&BIT(14))
6587                                 pmlmepriv->num_FortyMHzIntolerant++;
6588                 } else {
6589                         pmlmepriv->num_sta_no_ht++;
6590                 }
6591         }
6592
6593         /*  mark bss info receiving from nearby channel as SignalQuality 101 */
6594         if (bssid->Configuration.DSConfig != rtw_get_oper_ch(padapter))
6595                 bssid->PhyInfo.SignalQuality = 101;
6596         return _SUCCESS;
6597 }
6598
6599 void start_create_ibss(struct adapter *padapter)
6600 {
6601         unsigned short  caps;
6602         u8 val8;
6603         u8 join_type;
6604         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
6605         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6606         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
6607         pmlmeext->cur_channel = (u8)pnetwork->Configuration.DSConfig;
6608         pmlmeinfo->bcn_interval = get_beacon_interval(pnetwork);
6609
6610         /* update wireless mode */
6611         update_wireless_mode(padapter);
6612
6613         /* udpate capability */
6614         caps = rtw_get_capability((struct wlan_bssid_ex *)pnetwork);
6615         update_capinfo(padapter, caps);
6616         if (caps&cap_IBSS) {/* adhoc master */
6617                 val8 = 0xcf;
6618                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
6619
6620                 /* switch channel */
6621                 /* SelectChannel(padapter, pmlmeext->cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE); */
6622                 set_channel_bwmode(padapter, pmlmeext->cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
6623
6624                 beacon_timing_control(padapter);
6625
6626                 /* set msr to WIFI_FW_ADHOC_STATE */
6627                 pmlmeinfo->state = WIFI_FW_ADHOC_STATE;
6628                 Set_MSR(padapter, (pmlmeinfo->state & 0x3));
6629
6630                 /* issue beacon */
6631                 if (send_beacon(padapter) == _FAIL) {
6632                         report_join_res(padapter, -1);
6633                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
6634                 } else {
6635                         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, padapter->registrypriv.dev_network.MacAddress);
6636                         join_type = 0;
6637                         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
6638
6639                         report_join_res(padapter, 1);
6640                         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
6641                         rtw_indicate_connect(padapter);
6642                 }
6643         } else {
6644                 DBG_88E("start_create_ibss, invalid cap:%x\n", caps);
6645                 return;
6646         }
6647         /* update bc/mc sta_info */
6648         update_bmc_sta(padapter);
6649 }
6650
6651 void start_clnt_join(struct adapter *padapter)
6652 {
6653         unsigned short  caps;
6654         u8 val8;
6655         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
6656         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6657         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
6658         int beacon_timeout;
6659
6660         pmlmeext->cur_channel = (u8)pnetwork->Configuration.DSConfig;
6661         pmlmeinfo->bcn_interval = get_beacon_interval(pnetwork);
6662
6663         /* update wireless mode */
6664         update_wireless_mode(padapter);
6665
6666         /* udpate capability */
6667         caps = rtw_get_capability((struct wlan_bssid_ex *)pnetwork);
6668         update_capinfo(padapter, caps);
6669         if (caps&cap_ESS) {
6670                 Set_MSR(padapter, WIFI_FW_STATION_STATE);
6671
6672                 val8 = (pmlmeinfo->auth_algo == dot11AuthAlgrthm_8021X) ? 0xcc : 0xcf;
6673
6674                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
6675
6676                 /* switch channel */
6677                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
6678
6679                 /* here wait for receiving the beacon to start auth */
6680                 /* and enable a timer */
6681                 beacon_timeout = decide_wait_for_beacon_timeout(pmlmeinfo->bcn_interval);
6682                 set_link_timer(pmlmeext, beacon_timeout);
6683                 _set_timer(&padapter->mlmepriv.assoc_timer,
6684                            (REAUTH_TO * REAUTH_LIMIT) + (REASSOC_TO*REASSOC_LIMIT) + beacon_timeout);
6685
6686                 pmlmeinfo->state = WIFI_FW_AUTH_NULL | WIFI_FW_STATION_STATE;
6687         } else if (caps&cap_IBSS) { /* adhoc client */
6688                 Set_MSR(padapter, WIFI_FW_ADHOC_STATE);
6689
6690                 val8 = 0xcf;
6691                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
6692
6693                 /* switch channel */
6694                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
6695
6696                 beacon_timing_control(padapter);
6697
6698                 pmlmeinfo->state = WIFI_FW_ADHOC_STATE;
6699
6700                 report_join_res(padapter, 1);
6701         } else {
6702                 return;
6703         }
6704 }
6705
6706 void start_clnt_auth(struct adapter *padapter)
6707 {
6708         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
6709         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6710
6711         _cancel_timer_ex(&pmlmeext->link_timer);
6712
6713         pmlmeinfo->state &= (~WIFI_FW_AUTH_NULL);
6714         pmlmeinfo->state |= WIFI_FW_AUTH_STATE;
6715
6716         pmlmeinfo->auth_seq = 1;
6717         pmlmeinfo->reauth_count = 0;
6718         pmlmeinfo->reassoc_count = 0;
6719         pmlmeinfo->link_count = 0;
6720         pmlmeext->retry = 0;
6721
6722         /*  Because of AP's not receiving deauth before */
6723         /*  AP may: 1)not response auth or 2)deauth us after link is complete */
6724         /*  issue deauth before issuing auth to deal with the situation */
6725         /*      Commented by Albert 2012/07/21 */
6726         /*      For the Win8 P2P connection, it will be hard to have a successful connection if this Wi-Fi doesn't connect to it. */
6727         issue_deauth(padapter, (&(pmlmeinfo->network))->MacAddress, WLAN_REASON_DEAUTH_LEAVING);
6728
6729         DBG_88E_LEVEL(_drv_info_, "start auth\n");
6730         issue_auth(padapter, NULL, 0);
6731
6732         set_link_timer(pmlmeext, REAUTH_TO);
6733 }
6734
6735 void start_clnt_assoc(struct adapter *padapter)
6736 {
6737         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
6738         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6739
6740         _cancel_timer_ex(&pmlmeext->link_timer);
6741
6742         pmlmeinfo->state &= (~(WIFI_FW_AUTH_NULL | WIFI_FW_AUTH_STATE));
6743         pmlmeinfo->state |= (WIFI_FW_AUTH_SUCCESS | WIFI_FW_ASSOC_STATE);
6744
6745         issue_assocreq(padapter);
6746
6747         set_link_timer(pmlmeext, REASSOC_TO);
6748 }
6749
6750 unsigned int receive_disconnect(struct adapter *padapter, unsigned char *MacAddr, unsigned short reason)
6751 {
6752         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
6753         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
6754
6755         /* check A3 */
6756         if (!(!memcmp(MacAddr, get_my_bssid(&pmlmeinfo->network), ETH_ALEN)))
6757                 return _SUCCESS;
6758
6759         DBG_88E("%s\n", __func__);
6760
6761         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
6762                 if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {
6763                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
6764                         report_del_sta_event(padapter, MacAddr, reason);
6765                 } else if (pmlmeinfo->state & WIFI_FW_LINKING_STATE) {
6766                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
6767                         report_join_res(padapter, -2);
6768                 }
6769         }
6770         return _SUCCESS;
6771 }
6772
6773 static void process_80211d(struct adapter *padapter, struct wlan_bssid_ex *bssid)
6774 {
6775         struct registry_priv *pregistrypriv;
6776         struct mlme_ext_priv *pmlmeext;
6777         struct rt_channel_info *chplan_new;
6778         u8 channel;
6779         u8 i;
6780
6781         pregistrypriv = &padapter->registrypriv;
6782         pmlmeext = &padapter->mlmeextpriv;
6783
6784         /*  Adjust channel plan by AP Country IE */
6785         if (pregistrypriv->enable80211d &&
6786             (!pmlmeext->update_channel_plan_by_ap_done)) {
6787                 u8 *ie, *p;
6788                 u32 len;
6789                 struct rt_channel_plan chplan_ap;
6790                 struct rt_channel_info chplan_sta[MAX_CHANNEL_NUM];
6791                 u8 country[4];
6792                 u8 fcn; /*  first channel number */
6793                 u8 noc; /*  number of channel */
6794                 u8 j, k;
6795
6796                 ie = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _COUNTRY_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
6797                 if (!ie)
6798                         return;
6799                 if (len < 6)
6800                         return;
6801                 ie += 2;
6802                 p = ie;
6803                 ie += len;
6804
6805                 memset(country, 0, 4);
6806                 memcpy(country, p, 3);
6807                 p += 3;
6808
6809                 i = 0;
6810                 while ((ie - p) >= 3) {
6811                         fcn = *(p++);
6812                         noc = *(p++);
6813                         p++;
6814
6815                         for (j = 0; j < noc; j++) {
6816                                 if (fcn <= 14)
6817                                         channel = fcn + j; /*  2.4 GHz */
6818                                 else
6819                                         channel = fcn + j*4; /*  5 GHz */
6820
6821                                 chplan_ap.Channel[i++] = channel;
6822                         }
6823                 }
6824                 chplan_ap.Len = i;
6825
6826                 memcpy(chplan_sta, pmlmeext->channel_set, sizeof(chplan_sta));
6827
6828                 memset(pmlmeext->channel_set, 0, sizeof(pmlmeext->channel_set));
6829                 chplan_new = pmlmeext->channel_set;
6830
6831                 i = 0;
6832                 j = 0;
6833                 k = 0;
6834                 if (pregistrypriv->wireless_mode & WIRELESS_11G) {
6835                         do {
6836                                 if ((i == MAX_CHANNEL_NUM) ||
6837                                     (chplan_sta[i].ChannelNum == 0) ||
6838                                     (chplan_sta[i].ChannelNum > 14))
6839                                         break;
6840
6841                                 if ((j == chplan_ap.Len) || (chplan_ap.Channel[j] > 14))
6842                                         break;
6843
6844                                 if (chplan_sta[i].ChannelNum == chplan_ap.Channel[j]) {
6845                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
6846                                         chplan_new[k].ScanType = SCAN_ACTIVE;
6847                                         i++;
6848                                         j++;
6849                                         k++;
6850                                 } else if (chplan_sta[i].ChannelNum < chplan_ap.Channel[j]) {
6851                                         chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
6852                                         chplan_new[k].ScanType = SCAN_PASSIVE;
6853                                         i++;
6854                                         k++;
6855                                 } else if (chplan_sta[i].ChannelNum > chplan_ap.Channel[j]) {
6856                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
6857                                         chplan_new[k].ScanType = SCAN_ACTIVE;
6858                                         j++;
6859                                         k++;
6860                                 }
6861                         } while (1);
6862
6863                         /*  change AP not support channel to Passive scan */
6864                         while ((i < MAX_CHANNEL_NUM) &&
6865                                (chplan_sta[i].ChannelNum != 0) &&
6866                                (chplan_sta[i].ChannelNum <= 14)) {
6867                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
6868                                 chplan_new[k].ScanType = SCAN_PASSIVE;
6869                                 i++;
6870                                 k++;
6871                         }
6872
6873                         /*  add channel AP supported */
6874                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] <= 14)) {
6875                                 chplan_new[k].ChannelNum = chplan_ap.Channel[j];
6876                                 chplan_new[k].ScanType = SCAN_ACTIVE;
6877                                 j++;
6878                                 k++;
6879                         }
6880                 } else {
6881                         /*  keep original STA 2.4G channel plan */
6882                         while ((i < MAX_CHANNEL_NUM) &&
6883                                (chplan_sta[i].ChannelNum != 0) &&
6884                                (chplan_sta[i].ChannelNum <= 14)) {
6885                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
6886                                 chplan_new[k].ScanType = chplan_sta[i].ScanType;
6887                                 i++;
6888                                 k++;
6889                         }
6890
6891                         /*  skip AP 2.4G channel plan */
6892                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] <= 14))
6893                                 j++;
6894                 }
6895
6896                 /*  keep original STA 5G channel plan */
6897                 while ((i < MAX_CHANNEL_NUM) && (chplan_sta[i].ChannelNum != 0)) {
6898                         chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
6899                         chplan_new[k].ScanType = chplan_sta[i].ScanType;
6900                         i++;
6901                         k++;
6902                 }
6903
6904                 pmlmeext->update_channel_plan_by_ap_done = 1;
6905         }
6906
6907         /*  If channel is used by AP, set channel scan type to active */
6908         channel = bssid->Configuration.DSConfig;
6909         chplan_new = pmlmeext->channel_set;
6910         i = 0;
6911         while ((i < MAX_CHANNEL_NUM) && (chplan_new[i].ChannelNum != 0)) {
6912                 if (chplan_new[i].ChannelNum == channel) {
6913                         if (chplan_new[i].ScanType == SCAN_PASSIVE)
6914                                 chplan_new[i].ScanType = SCAN_ACTIVE;
6915                         break;
6916                 }
6917                 i++;
6918         }
6919 }
6920
6921 /****************************************************************************
6922
6923 Following are the functions to report events
6924
6925 *****************************************************************************/
6926
6927 void report_survey_event(struct adapter *padapter, struct recv_frame *precv_frame)
6928 {
6929         struct cmd_obj *pcmd_obj;
6930         u8 *pevtcmd;
6931         u32 cmdsz;
6932         struct survey_event     *psurvey_evt;
6933         struct C2HEvent_Header *pc2h_evt_hdr;
6934         struct mlme_ext_priv *pmlmeext;
6935         struct cmd_priv *pcmdpriv;
6936         /* u8 *pframe = precv_frame->rx_data; */
6937         /* uint len = precv_frame->len; */
6938
6939         if (!padapter)
6940                 return;
6941
6942         pmlmeext = &padapter->mlmeextpriv;
6943         pcmdpriv = &padapter->cmdpriv;
6944
6945         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
6946         if (!pcmd_obj)
6947                 return;
6948
6949         cmdsz = (sizeof(struct survey_event) + sizeof(struct C2HEvent_Header));
6950         pevtcmd = kzalloc(cmdsz, GFP_ATOMIC);
6951         if (!pevtcmd) {
6952                 kfree(pcmd_obj);
6953                 return;
6954         }
6955
6956         INIT_LIST_HEAD(&pcmd_obj->list);
6957
6958         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
6959         pcmd_obj->cmdsz = cmdsz;
6960         pcmd_obj->parmbuf = pevtcmd;
6961
6962         pcmd_obj->rsp = NULL;
6963         pcmd_obj->rspsz  = 0;
6964
6965         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
6966         pc2h_evt_hdr->len = sizeof(struct survey_event);
6967         pc2h_evt_hdr->ID = GEN_EVT_CODE(_Survey);
6968         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
6969
6970         psurvey_evt = (struct survey_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
6971
6972         if (collect_bss_info(padapter, precv_frame, (struct wlan_bssid_ex *)&psurvey_evt->bss) == _FAIL) {
6973                 kfree(pcmd_obj);
6974                 kfree(pevtcmd);
6975                 return;
6976         }
6977
6978         process_80211d(padapter, &psurvey_evt->bss);
6979
6980         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
6981
6982         pmlmeext->sitesurvey_res.bss_cnt++;
6983 }
6984
6985 void report_surveydone_event(struct adapter *padapter)
6986 {
6987         struct cmd_obj *pcmd_obj;
6988         u8 *pevtcmd;
6989         u32 cmdsz;
6990         struct surveydone_event *psurveydone_evt;
6991         struct C2HEvent_Header  *pc2h_evt_hdr;
6992         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
6993         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
6994
6995         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
6996         if (!pcmd_obj)
6997                 return;
6998
6999         cmdsz = (sizeof(struct surveydone_event) + sizeof(struct C2HEvent_Header));
7000         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
7001         if (!pevtcmd) {
7002                 kfree(pcmd_obj);
7003                 return;
7004         }
7005
7006         INIT_LIST_HEAD(&pcmd_obj->list);
7007
7008         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
7009         pcmd_obj->cmdsz = cmdsz;
7010         pcmd_obj->parmbuf = pevtcmd;
7011
7012         pcmd_obj->rsp = NULL;
7013         pcmd_obj->rspsz  = 0;
7014
7015         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
7016         pc2h_evt_hdr->len = sizeof(struct surveydone_event);
7017         pc2h_evt_hdr->ID = GEN_EVT_CODE(_SurveyDone);
7018         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
7019
7020         psurveydone_evt = (struct surveydone_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
7021         psurveydone_evt->bss_cnt = pmlmeext->sitesurvey_res.bss_cnt;
7022
7023         DBG_88E("survey done event(%x)\n", psurveydone_evt->bss_cnt);
7024
7025         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
7026 }
7027
7028 void report_join_res(struct adapter *padapter, int res)
7029 {
7030         struct cmd_obj *pcmd_obj;
7031         u8 *pevtcmd;
7032         u32 cmdsz;
7033         struct joinbss_event            *pjoinbss_evt;
7034         struct C2HEvent_Header  *pc2h_evt_hdr;
7035         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
7036         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7037         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
7038
7039         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
7040         if (!pcmd_obj)
7041                 return;
7042
7043         cmdsz = (sizeof(struct joinbss_event) + sizeof(struct C2HEvent_Header));
7044         pevtcmd = kzalloc(cmdsz, GFP_ATOMIC);
7045         if (!pevtcmd) {
7046                 kfree(pcmd_obj);
7047                 return;
7048         }
7049
7050         INIT_LIST_HEAD(&pcmd_obj->list);
7051
7052         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
7053         pcmd_obj->cmdsz = cmdsz;
7054         pcmd_obj->parmbuf = pevtcmd;
7055
7056         pcmd_obj->rsp = NULL;
7057         pcmd_obj->rspsz  = 0;
7058
7059         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
7060         pc2h_evt_hdr->len = sizeof(struct joinbss_event);
7061         pc2h_evt_hdr->ID = GEN_EVT_CODE(_JoinBss);
7062         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
7063
7064         pjoinbss_evt = (struct joinbss_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
7065         memcpy((unsigned char *)(&(pjoinbss_evt->network.network)), &(pmlmeinfo->network), sizeof(struct wlan_bssid_ex));
7066         pjoinbss_evt->network.join_res  = res;
7067         pjoinbss_evt->network.aid = res;
7068
7069         DBG_88E("report_join_res(%d)\n", res);
7070
7071         rtw_joinbss_event_prehandle(padapter, (u8 *)&pjoinbss_evt->network);
7072
7073         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
7074 }
7075
7076 void report_del_sta_event(struct adapter *padapter, unsigned char *MacAddr, unsigned short reason)
7077 {
7078         struct cmd_obj *pcmd_obj;
7079         u8 *pevtcmd;
7080         u32 cmdsz;
7081         struct sta_info *psta;
7082         int     mac_id;
7083         struct stadel_event                     *pdel_sta_evt;
7084         struct C2HEvent_Header  *pc2h_evt_hdr;
7085         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
7086         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
7087
7088         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
7089         if (!pcmd_obj)
7090                 return;
7091
7092         cmdsz = (sizeof(struct stadel_event) + sizeof(struct C2HEvent_Header));
7093         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
7094         if (!pevtcmd) {
7095                 kfree(pcmd_obj);
7096                 return;
7097         }
7098
7099         INIT_LIST_HEAD(&pcmd_obj->list);
7100
7101         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
7102         pcmd_obj->cmdsz = cmdsz;
7103         pcmd_obj->parmbuf = pevtcmd;
7104
7105         pcmd_obj->rsp = NULL;
7106         pcmd_obj->rspsz  = 0;
7107
7108         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
7109         pc2h_evt_hdr->len = sizeof(struct stadel_event);
7110         pc2h_evt_hdr->ID = GEN_EVT_CODE(_DelSTA);
7111         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
7112
7113         pdel_sta_evt = (struct stadel_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
7114         memcpy((unsigned char *)(&(pdel_sta_evt->macaddr)), MacAddr, ETH_ALEN);
7115         memcpy((unsigned char *)(pdel_sta_evt->rsvd), (unsigned char *)(&reason), 2);
7116
7117         psta = rtw_get_stainfo(&padapter->stapriv, MacAddr);
7118         if (psta)
7119                 mac_id = (int)psta->mac_id;
7120         else
7121                 mac_id = (-1);
7122
7123         pdel_sta_evt->mac_id = mac_id;
7124
7125         DBG_88E("report_del_sta_event: delete STA, mac_id =%d\n", mac_id);
7126
7127         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
7128 }
7129
7130 void report_add_sta_event(struct adapter *padapter, unsigned char *MacAddr, int cam_idx)
7131 {
7132         struct cmd_obj *pcmd_obj;
7133         u8 *pevtcmd;
7134         u32 cmdsz;
7135         struct stassoc_event            *padd_sta_evt;
7136         struct C2HEvent_Header  *pc2h_evt_hdr;
7137         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
7138         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
7139
7140         pcmd_obj = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
7141         if (!pcmd_obj)
7142                 return;
7143
7144         cmdsz = (sizeof(struct stassoc_event) + sizeof(struct C2HEvent_Header));
7145         pevtcmd = kzalloc(cmdsz, GFP_KERNEL);
7146         if (!pevtcmd) {
7147                 kfree(pcmd_obj);
7148                 return;
7149         }
7150
7151         INIT_LIST_HEAD(&pcmd_obj->list);
7152
7153         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
7154         pcmd_obj->cmdsz = cmdsz;
7155         pcmd_obj->parmbuf = pevtcmd;
7156
7157         pcmd_obj->rsp = NULL;
7158         pcmd_obj->rspsz  = 0;
7159
7160         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
7161         pc2h_evt_hdr->len = sizeof(struct stassoc_event);
7162         pc2h_evt_hdr->ID = GEN_EVT_CODE(_AddSTA);
7163         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
7164
7165         padd_sta_evt = (struct stassoc_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
7166         memcpy((unsigned char *)(&(padd_sta_evt->macaddr)), MacAddr, ETH_ALEN);
7167         padd_sta_evt->cam_id = cam_idx;
7168
7169         DBG_88E("report_add_sta_event: add STA\n");
7170
7171         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
7172 }
7173
7174 /****************************************************************************
7175
7176 Following are the event callback functions
7177
7178 *****************************************************************************/
7179
7180 /* for sta/adhoc mode */
7181 void update_sta_info(struct adapter *padapter, struct sta_info *psta)
7182 {
7183         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
7184         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7185         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7186
7187         /* ERP */
7188         VCS_update(padapter, psta);
7189
7190         /* HT */
7191         if (pmlmepriv->htpriv.ht_option) {
7192                 psta->htpriv.ht_option = true;
7193
7194                 psta->htpriv.ampdu_enable = pmlmepriv->htpriv.ampdu_enable;
7195
7196                 if (support_short_GI(padapter, &(pmlmeinfo->HT_caps)))
7197                         psta->htpriv.sgi = true;
7198
7199                 psta->qos_option = true;
7200         } else {
7201                 psta->htpriv.ht_option = false;
7202
7203                 psta->htpriv.ampdu_enable = false;
7204
7205                 psta->htpriv.sgi = false;
7206                 psta->qos_option = false;
7207         }
7208         psta->htpriv.bwmode = pmlmeext->cur_bwmode;
7209         psta->htpriv.ch_offset = pmlmeext->cur_ch_offset;
7210
7211         psta->htpriv.agg_enable_bitmap = 0x0;/* reset */
7212         psta->htpriv.candidate_tid_bitmap = 0x0;/* reset */
7213
7214         /* QoS */
7215         if (pmlmepriv->qospriv.qos_option)
7216                 psta->qos_option = true;
7217
7218         psta->state = _FW_LINKED;
7219 }
7220
7221 void mlmeext_joinbss_event_callback(struct adapter *padapter, int join_res)
7222 {
7223         struct sta_info         *psta, *psta_bmc;
7224         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7225         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7226         struct wlan_bssid_ex *cur_network = &(pmlmeinfo->network);
7227         struct sta_priv         *pstapriv = &padapter->stapriv;
7228         u8 join_type;
7229         u16 media_status;
7230
7231         if (join_res < 0) {
7232                 join_type = 1;
7233                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
7234                 rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
7235
7236                 /* restore to initial setting. */
7237                 update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
7238
7239                 goto exit_mlmeext_joinbss_event_callback;
7240         }
7241
7242         if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
7243                 /* for bc/mc */
7244                 psta_bmc = rtw_get_bcmc_stainfo(padapter);
7245                 if (psta_bmc) {
7246                         pmlmeinfo->FW_sta_info[psta_bmc->mac_id].psta = psta_bmc;
7247                         update_bmc_sta_support_rate(padapter, psta_bmc->mac_id);
7248                         Update_RA_Entry(padapter, psta_bmc->mac_id);
7249                 }
7250         }
7251
7252         /* turn on dynamic functions */
7253         Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true);
7254
7255         /*  update IOT-releated issue */
7256         update_IOT_info(padapter);
7257
7258         rtw_hal_set_hwreg(padapter, HW_VAR_BASIC_RATE, cur_network->SupportedRates);
7259
7260         /* BCN interval */
7261         rtw_hal_set_hwreg(padapter, HW_VAR_BEACON_INTERVAL, (u8 *)(&pmlmeinfo->bcn_interval));
7262
7263         /* udpate capability */
7264         update_capinfo(padapter, pmlmeinfo->capability);
7265
7266         /* WMM, Update EDCA param */
7267         WMMOnAssocRsp(padapter);
7268
7269         /* HT */
7270         HTOnAssocRsp(padapter);
7271
7272         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
7273
7274         psta = rtw_get_stainfo(pstapriv, cur_network->MacAddress);
7275         if (psta) { /* only for infra. mode */
7276                 pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
7277
7278                 psta->wireless_mode = pmlmeext->cur_wireless_mode;
7279
7280                 /* set per sta rate after updating HT cap. */
7281                 set_sta_rate(padapter, psta);
7282                 rtw_hal_set_hwreg(padapter, HW_VAR_TX_RPT_MAX_MACID, (u8 *)&psta->mac_id);
7283                 media_status = (psta->mac_id<<8)|1; /*   MACID|OPMODE: 1 means connect */
7284                 rtw_hal_set_hwreg(padapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status);
7285         }
7286
7287         join_type = 2;
7288         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
7289
7290         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
7291                 /*  correcting TSF */
7292                 correct_TSF(padapter, pmlmeext);
7293         }
7294         rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_CONNECT, 0);
7295
7296 exit_mlmeext_joinbss_event_callback:
7297
7298         DBG_88E("=>%s\n", __func__);
7299 }
7300
7301 void mlmeext_sta_add_event_callback(struct adapter *padapter, struct sta_info *psta)
7302 {
7303         struct mlme_ext_priv    *pmlmeext = &(padapter->mlmeextpriv);
7304         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7305         u8 join_type;
7306
7307         DBG_88E("%s\n", __func__);
7308
7309         if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
7310                 if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {/* adhoc master or sta_count>1 */
7311                         /* nothing to do */
7312                 } else { /* adhoc client */
7313                         /*  correcting TSF */
7314                         correct_TSF(padapter, pmlmeext);
7315
7316                         /* start beacon */
7317                         if (send_beacon(padapter) == _FAIL) {
7318                                 pmlmeinfo->FW_sta_info[psta->mac_id].status = 0;
7319                                 pmlmeinfo->state ^= WIFI_FW_ADHOC_STATE;
7320                                 return;
7321                         }
7322                         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
7323                 }
7324
7325                 join_type = 2;
7326                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
7327         }
7328
7329         pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
7330
7331         /* rate radaptive */
7332         Update_RA_Entry(padapter, psta->mac_id);
7333
7334         /* update adhoc sta_info */
7335         update_sta_info(padapter, psta);
7336 }
7337
7338 void mlmeext_sta_del_event_callback(struct adapter *padapter)
7339 {
7340         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7341         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7342
7343         if (is_client_associated_to_ap(padapter) || is_IBSS_empty(padapter)) {
7344                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
7345                 rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
7346
7347                 /* restore to initial setting. */
7348                 update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
7349
7350                 /* switch to the 20M Hz mode after disconnect */
7351                 pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
7352                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
7353
7354                 /* SelectChannel(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset); */
7355                 set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
7356
7357                 flush_all_cam_entry(padapter);
7358
7359                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
7360
7361                 /* set MSR to no link state -> infra. mode */
7362                 Set_MSR(padapter, _HW_STATE_STATION_);
7363
7364                 _cancel_timer_ex(&pmlmeext->link_timer);
7365         }
7366 }
7367
7368 /****************************************************************************
7369
7370 Following are the functions for the timer handlers
7371
7372 *****************************************************************************/
7373 void _linked_rx_signal_strehgth_display(struct adapter *padapter);
7374 void _linked_rx_signal_strehgth_display(struct adapter *padapter)
7375 {
7376         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7377       struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7378         u8 mac_id;
7379         int UndecoratedSmoothedPWDB;
7380         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
7381                 mac_id = 0;
7382         else if ((pmlmeinfo->state&0x03) == _HW_STATE_AP_)
7383                 mac_id = 2;
7384
7385         rtw_hal_get_def_var(padapter, HW_DEF_RA_INFO_DUMP, &mac_id);
7386
7387         rtw_hal_get_def_var(padapter, HAL_DEF_UNDERCORATEDSMOOTHEDPWDB, &UndecoratedSmoothedPWDB);
7388         DBG_88E("UndecoratedSmoothedPWDB:%d\n", UndecoratedSmoothedPWDB);
7389 }
7390
7391 static u8 chk_ap_is_alive(struct adapter *padapter, struct sta_info *psta)
7392 {
7393         u8 ret = false;
7394
7395         if ((sta_rx_data_pkts(psta) == sta_last_rx_data_pkts(psta)) &&
7396             sta_rx_beacon_pkts(psta) == sta_last_rx_beacon_pkts(psta) &&
7397             sta_rx_probersp_pkts(psta) == sta_last_rx_probersp_pkts(psta))
7398                 ret = false;
7399         else
7400                 ret = true;
7401
7402         sta_update_last_rx_pkts(psta);
7403
7404         return ret;
7405 }
7406
7407 void linked_status_chk(struct adapter *padapter)
7408 {
7409         u32     i;
7410         struct sta_info         *psta;
7411         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
7412         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7413         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7414         struct sta_priv         *pstapriv = &padapter->stapriv;
7415
7416         if (padapter->bRxRSSIDisplay)
7417                 _linked_rx_signal_strehgth_display(padapter);
7418
7419         rtw_hal_sreset_linked_status_check(padapter);
7420
7421         if (is_client_associated_to_ap(padapter)) {
7422                 /* linked infrastructure client mode */
7423
7424                 int tx_chk = _SUCCESS, rx_chk = _SUCCESS;
7425                 int rx_chk_limit;
7426
7427                 rx_chk_limit = 4;
7428                 psta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.MacAddress);
7429                 if (psta) {
7430                         bool is_p2p_enable = false;
7431                         #ifdef CONFIG_88EU_P2P
7432                         is_p2p_enable = !rtw_p2p_chk_state(&padapter->wdinfo, P2P_STATE_NONE);
7433                         #endif
7434
7435                         if (!chk_ap_is_alive(padapter, psta))
7436                                 rx_chk = _FAIL;
7437
7438                         if (pxmitpriv->last_tx_pkts == pxmitpriv->tx_pkts)
7439                                 tx_chk = _FAIL;
7440
7441                         if (pmlmeext->active_keep_alive_check && (rx_chk == _FAIL || tx_chk == _FAIL)) {
7442                                 u8 backup_oper_channel = 0;
7443
7444                                 /* switch to correct channel of current network  before issue keep-alive frames */
7445                                 if (rtw_get_oper_ch(padapter) != pmlmeext->cur_channel) {
7446                                         backup_oper_channel = rtw_get_oper_ch(padapter);
7447                                         SelectChannel(padapter, pmlmeext->cur_channel);
7448                                 }
7449
7450                                 if (rx_chk != _SUCCESS)
7451                                         issue_probereq_ex(padapter, &pmlmeinfo->network.Ssid, psta->hwaddr, 3, 1);
7452
7453                                 if ((tx_chk != _SUCCESS && pmlmeinfo->link_count++ == 0xf) || rx_chk != _SUCCESS) {
7454                                         tx_chk = issue_nulldata(padapter, psta->hwaddr, 0, 3, 1);
7455                                         /* if tx acked and p2p disabled, set rx_chk _SUCCESS to reset retry count */
7456                                         if (tx_chk == _SUCCESS && !is_p2p_enable)
7457                                                 rx_chk = _SUCCESS;
7458                                 }
7459
7460                                 /* back to the original operation channel */
7461                                 if (backup_oper_channel > 0)
7462                                         SelectChannel(padapter, backup_oper_channel);
7463                         } else {
7464                                 if (rx_chk != _SUCCESS) {
7465                                         if (pmlmeext->retry == 0) {
7466                                                 issue_probereq(padapter, &pmlmeinfo->network.Ssid, pmlmeinfo->network.MacAddress);
7467                                                 issue_probereq(padapter, &pmlmeinfo->network.Ssid, pmlmeinfo->network.MacAddress);
7468                                                 issue_probereq(padapter, &pmlmeinfo->network.Ssid, pmlmeinfo->network.MacAddress);
7469                                         }
7470                                 }
7471
7472                                 if (tx_chk != _SUCCESS && pmlmeinfo->link_count++ == 0xf) {
7473                                         tx_chk = issue_nulldata(padapter, NULL, 0, 1, 0);
7474                                 }
7475                         }
7476
7477                         if (rx_chk == _FAIL) {
7478                                 pmlmeext->retry++;
7479                                 if (pmlmeext->retry > rx_chk_limit) {
7480                                         DBG_88E_LEVEL(_drv_always_, FUNC_ADPT_FMT" disconnect or roaming\n",
7481                                                       FUNC_ADPT_ARG(padapter));
7482                                         receive_disconnect(padapter, pmlmeinfo->network.MacAddress,
7483                                                            WLAN_REASON_EXPIRATION_CHK);
7484                                         return;
7485                                 }
7486                         } else {
7487                                 pmlmeext->retry = 0;
7488                         }
7489
7490                         if (tx_chk == _FAIL) {
7491                                 pmlmeinfo->link_count &= 0xf;
7492                         } else {
7493                                 pxmitpriv->last_tx_pkts = pxmitpriv->tx_pkts;
7494                                 pmlmeinfo->link_count = 0;
7495                         }
7496                 } /* end of if ((psta = rtw_get_stainfo(pstapriv, passoc_res->network.MacAddress)) != NULL) */
7497         } else if (is_client_associated_to_ibss(padapter)) {
7498                 /* linked IBSS mode */
7499                 /* for each assoc list entry to check the rx pkt counter */
7500                 for (i = IBSS_START_MAC_ID; i < NUM_STA; i++) {
7501                         if (pmlmeinfo->FW_sta_info[i].status == 1) {
7502                                 psta = pmlmeinfo->FW_sta_info[i].psta;
7503
7504                                 if (NULL == psta)
7505                                         continue;
7506                                 if (pmlmeinfo->FW_sta_info[i].rx_pkt == sta_rx_pkts(psta)) {
7507                                         if (pmlmeinfo->FW_sta_info[i].retry < 3) {
7508                                                 pmlmeinfo->FW_sta_info[i].retry++;
7509                                         } else {
7510                                                 pmlmeinfo->FW_sta_info[i].retry = 0;
7511                                                 pmlmeinfo->FW_sta_info[i].status = 0;
7512                                                 report_del_sta_event(padapter, psta->hwaddr
7513                                                         , 65535/*  indicate disconnect caused by no rx */
7514                                         );
7515                                         }
7516                                 } else {
7517                                         pmlmeinfo->FW_sta_info[i].retry = 0;
7518                                         pmlmeinfo->FW_sta_info[i].rx_pkt = (u32)sta_rx_pkts(psta);
7519                                 }
7520                         }
7521                 }
7522         }
7523 }
7524
7525 void survey_timer_hdl(struct adapter *padapter)
7526 {
7527         struct cmd_obj  *ph2c;
7528         struct sitesurvey_parm  *psurveyPara;
7529         struct cmd_priv                                 *pcmdpriv = &padapter->cmdpriv;
7530         struct mlme_ext_priv            *pmlmeext = &padapter->mlmeextpriv;
7531 #ifdef CONFIG_88EU_P2P
7532         struct wifidirect_info *pwdinfo = &(padapter->wdinfo);
7533 #endif
7534
7535         /* issue rtw_sitesurvey_cmd */
7536         if (pmlmeext->sitesurvey_res.state > SCAN_START) {
7537                 if (pmlmeext->sitesurvey_res.state ==  SCAN_PROCESS)
7538                         pmlmeext->sitesurvey_res.channel_idx++;
7539
7540                 if (pmlmeext->scan_abort) {
7541                         #ifdef CONFIG_88EU_P2P
7542                         if (!rtw_p2p_chk_state(&padapter->wdinfo, P2P_STATE_NONE)) {
7543                                 rtw_p2p_findphase_ex_set(pwdinfo, P2P_FINDPHASE_EX_MAX);
7544                                 pmlmeext->sitesurvey_res.channel_idx = 3;
7545                                 DBG_88E("%s idx:%d, cnt:%u\n", __func__
7546                                         , pmlmeext->sitesurvey_res.channel_idx
7547                                         , pwdinfo->find_phase_state_exchange_cnt
7548                         );
7549                         } else
7550                         #endif
7551                         {
7552                                 pmlmeext->sitesurvey_res.channel_idx = pmlmeext->sitesurvey_res.ch_num;
7553                                 DBG_88E("%s idx:%d\n", __func__
7554                                         , pmlmeext->sitesurvey_res.channel_idx
7555                         );
7556                         }
7557
7558                         pmlmeext->scan_abort = false;/* reset */
7559                 }
7560
7561                 ph2c = kzalloc(sizeof(struct cmd_obj), GFP_ATOMIC);
7562                 if (!ph2c)
7563                         goto exit_survey_timer_hdl;
7564
7565                 psurveyPara = kzalloc(sizeof(struct sitesurvey_parm), GFP_ATOMIC);
7566                 if (!psurveyPara) {
7567                         kfree(ph2c);
7568                         goto exit_survey_timer_hdl;
7569                 }
7570
7571                 init_h2fwcmd_w_parm_no_rsp(ph2c, psurveyPara, GEN_CMD_CODE(_SiteSurvey));
7572                 rtw_enqueue_cmd(pcmdpriv, ph2c);
7573         }
7574
7575 exit_survey_timer_hdl:
7576         return;
7577 }
7578
7579 void link_timer_hdl(struct adapter *padapter)
7580 {
7581         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7582         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7583
7584         if (pmlmeinfo->state & WIFI_FW_AUTH_NULL) {
7585                 DBG_88E("link_timer_hdl:no beacon while connecting\n");
7586                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
7587                 report_join_res(padapter, -3);
7588         } else if (pmlmeinfo->state & WIFI_FW_AUTH_STATE) {
7589                 /* re-auth timer */
7590                 if (++pmlmeinfo->reauth_count > REAUTH_LIMIT) {
7591                         pmlmeinfo->state = 0;
7592                         report_join_res(padapter, -1);
7593                         return;
7594                 }
7595
7596                 DBG_88E("link_timer_hdl: auth timeout and try again\n");
7597                 pmlmeinfo->auth_seq = 1;
7598                 issue_auth(padapter, NULL, 0);
7599                 set_link_timer(pmlmeext, REAUTH_TO);
7600         } else if (pmlmeinfo->state & WIFI_FW_ASSOC_STATE) {
7601                 /* re-assoc timer */
7602                 if (++pmlmeinfo->reassoc_count > REASSOC_LIMIT) {
7603                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
7604                         report_join_res(padapter, -2);
7605                         return;
7606                 }
7607
7608                 DBG_88E("link_timer_hdl: assoc timeout and try again\n");
7609                 issue_assocreq(padapter);
7610                 set_link_timer(pmlmeext, REASSOC_TO);
7611         }
7612 }
7613
7614 void addba_timer_hdl(struct sta_info *psta)
7615 {
7616         struct ht_priv  *phtpriv;
7617
7618         if (!psta)
7619                 return;
7620
7621         phtpriv = &psta->htpriv;
7622
7623         if ((phtpriv->ht_option) && (phtpriv->ampdu_enable)) {
7624                 if (phtpriv->candidate_tid_bitmap)
7625                         phtpriv->candidate_tid_bitmap = 0x0;
7626         }
7627 }
7628
7629 u8 NULL_hdl(struct adapter *padapter, u8 *pbuf)
7630 {
7631         return H2C_SUCCESS;
7632 }
7633
7634 u8 setopmode_hdl(struct adapter *padapter, u8 *pbuf)
7635 {
7636         u8 type;
7637         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7638         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7639         struct setopmode_parm *psetop = (struct setopmode_parm *)pbuf;
7640
7641         if (psetop->mode == Ndis802_11APMode) {
7642                 pmlmeinfo->state = WIFI_FW_AP_STATE;
7643                 type = _HW_STATE_AP_;
7644         } else if (psetop->mode == Ndis802_11Infrastructure) {
7645                 pmlmeinfo->state &= ~(BIT(0)|BIT(1));/*  clear state */
7646                 pmlmeinfo->state |= WIFI_FW_STATION_STATE;/* set to     STATION_STATE */
7647                 type = _HW_STATE_STATION_;
7648         } else if (psetop->mode == Ndis802_11IBSS) {
7649                 type = _HW_STATE_ADHOC_;
7650         } else {
7651                 type = _HW_STATE_NOLINK_;
7652         }
7653
7654         rtw_hal_set_hwreg(padapter, HW_VAR_SET_OPMODE, (u8 *)(&type));
7655         /* Set_NETYPE0_MSR(padapter, type); */
7656
7657         return H2C_SUCCESS;
7658 }
7659
7660 u8 createbss_hdl(struct adapter *padapter, u8 *pbuf)
7661 {
7662         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7663         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7664         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
7665         struct joinbss_parm *pparm = (struct joinbss_parm *)pbuf;
7666         /* u32  initialgain; */
7667
7668         if (pparm->network.InfrastructureMode == Ndis802_11APMode) {
7669 #ifdef CONFIG_88EU_AP_MODE
7670
7671                 if (pmlmeinfo->state == WIFI_FW_AP_STATE) {
7672                         /* todo: */
7673                         return H2C_SUCCESS;
7674                 }
7675 #endif
7676         }
7677
7678         /* below is for ad-hoc master */
7679         if (pparm->network.InfrastructureMode == Ndis802_11IBSS) {
7680                 rtw_joinbss_reset(padapter);
7681
7682                 pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
7683                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
7684                 pmlmeinfo->ERP_enable = 0;
7685                 pmlmeinfo->WMM_enable = 0;
7686                 pmlmeinfo->HT_enable = 0;
7687                 pmlmeinfo->HT_caps_enable = 0;
7688                 pmlmeinfo->HT_info_enable = 0;
7689                 pmlmeinfo->agg_enable_bitmap = 0;
7690                 pmlmeinfo->candidate_tid_bitmap = 0;
7691
7692                 /* disable dynamic functions, such as high power, DIG */
7693                 Save_DM_Func_Flag(padapter);
7694                 Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false);
7695
7696                 /* config the initial gain under linking, need to write the BB registers */
7697                 /* initialgain = 0x1E; */
7698                 /* rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain)); */
7699
7700                 /* cancel link timer */
7701                 _cancel_timer_ex(&pmlmeext->link_timer);
7702
7703                 /* clear CAM */
7704                 flush_all_cam_entry(padapter);
7705
7706                 memcpy(pnetwork, pbuf, FIELD_OFFSET(struct wlan_bssid_ex, IELength));
7707                 pnetwork->IELength = ((struct wlan_bssid_ex *)pbuf)->IELength;
7708
7709                 if (pnetwork->IELength > MAX_IE_SZ)/* Check pbuf->IELength */
7710                         return H2C_PARAMETERS_ERROR;
7711
7712                 memcpy(pnetwork->IEs, ((struct wlan_bssid_ex *)pbuf)->IEs, pnetwork->IELength);
7713
7714                 start_create_ibss(padapter);
7715         }
7716
7717         return H2C_SUCCESS;
7718 }
7719
7720 u8 join_cmd_hdl(struct adapter *padapter, u8 *pbuf)
7721 {
7722         u8 join_type;
7723         struct ndis_802_11_var_ie *pIE;
7724         struct registry_priv    *pregpriv = &padapter->registrypriv;
7725         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7726         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7727         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
7728         struct joinbss_parm     *pparm = (struct joinbss_parm *)pbuf;
7729         u32 i;
7730
7731         /* check already connecting to AP or not */
7732         if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {
7733                 if (pmlmeinfo->state & WIFI_FW_STATION_STATE)
7734                         issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, 5, 100);
7735
7736                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
7737
7738                 /* clear CAM */
7739                 flush_all_cam_entry(padapter);
7740
7741                 _cancel_timer_ex(&pmlmeext->link_timer);
7742
7743                 /* set MSR to nolink -> infra. mode */
7744                 Set_MSR(padapter, _HW_STATE_STATION_);
7745
7746                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
7747         }
7748
7749         rtw_antenna_select_cmd(padapter, pparm->network.PhyInfo.Optimum_antenna, false);
7750
7751         rtw_joinbss_reset(padapter);
7752
7753         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
7754         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
7755         pmlmeinfo->ERP_enable = 0;
7756         pmlmeinfo->WMM_enable = 0;
7757         pmlmeinfo->HT_enable = 0;
7758         pmlmeinfo->HT_caps_enable = 0;
7759         pmlmeinfo->HT_info_enable = 0;
7760         pmlmeinfo->agg_enable_bitmap = 0;
7761         pmlmeinfo->candidate_tid_bitmap = 0;
7762         pmlmeinfo->bwmode_updated = false;
7763
7764         memcpy(pnetwork, pbuf, FIELD_OFFSET(struct wlan_bssid_ex, IELength));
7765         pnetwork->IELength = ((struct wlan_bssid_ex *)pbuf)->IELength;
7766
7767         if (pnetwork->IELength > MAX_IE_SZ)/* Check pbuf->IELength */
7768                 return H2C_PARAMETERS_ERROR;
7769
7770         memcpy(pnetwork->IEs, ((struct wlan_bssid_ex *)pbuf)->IEs, pnetwork->IELength);
7771
7772         /* Check AP vendor to move rtw_joinbss_cmd() */
7773
7774         for (i = sizeof(struct ndis_802_11_fixed_ie); i < pnetwork->IELength;) {
7775                 pIE = (struct ndis_802_11_var_ie *)(pnetwork->IEs + i);
7776
7777                 switch (pIE->ElementID) {
7778                 case _VENDOR_SPECIFIC_IE_:/* Get WMM IE. */
7779                         if (!memcmp(pIE->data, WMM_OUI, 4))
7780                                 pmlmeinfo->WMM_enable = 1;
7781                         break;
7782                 case _HT_CAPABILITY_IE_:        /* Get HT Cap IE. */
7783                         pmlmeinfo->HT_caps_enable = 1;
7784                         break;
7785                 case _HT_EXTRA_INFO_IE_:        /* Get HT Info IE. */
7786                         pmlmeinfo->HT_info_enable = 1;
7787
7788                         /* spec case only for cisco's ap because cisco's ap issue assoc rsp using mcs rate @40MHz or @20MHz */
7789                         {
7790                                 struct HT_info_element *pht_info = (struct HT_info_element *)(pIE->data);
7791
7792                                 if ((pregpriv->cbw40_enable) &&  (pht_info->infos[0] & BIT(2))) {
7793                                         /* switch to the 40M Hz mode according to the AP */
7794                                         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_40;
7795                                         switch (pht_info->infos[0] & 0x3) {
7796                                         case 1:
7797                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
7798                                                 break;
7799                                         case 3:
7800                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
7801                                                 break;
7802                                         default:
7803                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
7804                                                 break;
7805                                 }
7806
7807                                         DBG_88E("set ch/bw before connected\n");
7808                                 }
7809                         }
7810                         break;
7811                 default:
7812                         break;
7813                 }
7814
7815                 i += (pIE->Length + 2);
7816         }
7817         /* disable dynamic functions, such as high power, DIG */
7818
7819         /* config the initial gain under linking, need to write the BB registers */
7820
7821         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, pmlmeinfo->network.MacAddress);
7822         join_type = 0;
7823         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
7824
7825         /* cancel link timer */
7826         _cancel_timer_ex(&pmlmeext->link_timer);
7827
7828         start_clnt_join(padapter);
7829
7830         return H2C_SUCCESS;
7831 }
7832
7833 u8 disconnect_hdl(struct adapter *padapter, unsigned char *pbuf)
7834 {
7835         struct disconnect_parm *param = (struct disconnect_parm *)pbuf;
7836         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7837         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
7838         struct wlan_bssid_ex *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
7839         u8 val8;
7840
7841         if (is_client_associated_to_ap(padapter))
7842                 issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, param->deauth_timeout_ms/100, 100);
7843
7844         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
7845         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
7846
7847         /* restore to initial setting. */
7848         update_tx_basic_rate(padapter, padapter->registrypriv.wireless_mode);
7849
7850         if (((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) || ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)) {
7851                 /* Stop BCN */
7852                 val8 = 0;
7853                 rtw_hal_set_hwreg(padapter, HW_VAR_BCN_FUNC, (u8 *)(&val8));
7854         }
7855
7856         /* set MSR to no link state -> infra. mode */
7857         Set_MSR(padapter, _HW_STATE_STATION_);
7858
7859         pmlmeinfo->state = WIFI_FW_NULL_STATE;
7860
7861         /* switch to the 20M Hz mode after disconnect */
7862         pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_20;
7863         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
7864
7865         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
7866
7867         flush_all_cam_entry(padapter);
7868
7869         _cancel_timer_ex(&pmlmeext->link_timer);
7870
7871         rtw_free_uc_swdec_pending_queue(padapter);
7872
7873         return  H2C_SUCCESS;
7874 }
7875
7876 static int rtw_scan_ch_decision(struct adapter *padapter, struct rtw_ieee80211_channel *out,
7877         u32 out_num, struct rtw_ieee80211_channel *in, u32 in_num)
7878 {
7879         int i, j;
7880         int set_idx;
7881         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7882
7883         /* clear out first */
7884         memset(out, 0, sizeof(struct rtw_ieee80211_channel)*out_num);
7885
7886         /* acquire channels from in */
7887         j = 0;
7888         for (i = 0; i < in_num; i++) {
7889                 set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, in[i].hw_value);
7890                 if (in[i].hw_value && !(in[i].flags & RTW_IEEE80211_CHAN_DISABLED) &&
7891                     set_idx >= 0) {
7892                         memcpy(&out[j], &in[i], sizeof(struct rtw_ieee80211_channel));
7893
7894                         if (pmlmeext->channel_set[set_idx].ScanType == SCAN_PASSIVE)
7895                                 out[j].flags &= RTW_IEEE80211_CHAN_PASSIVE_SCAN;
7896
7897                         j++;
7898                 }
7899                 if (j >= out_num)
7900                         break;
7901         }
7902
7903         /* if out is empty, use channel_set as default */
7904         if (j == 0) {
7905                 for (i = 0; i < pmlmeext->max_chan_nums; i++) {
7906                         out[i].hw_value = pmlmeext->channel_set[i].ChannelNum;
7907
7908                         if (pmlmeext->channel_set[i].ScanType == SCAN_PASSIVE)
7909                                 out[i].flags &= RTW_IEEE80211_CHAN_PASSIVE_SCAN;
7910
7911                         j++;
7912                 }
7913         }
7914
7915         return j;
7916 }
7917
7918 u8 sitesurvey_cmd_hdl(struct adapter *padapter, u8 *pbuf)
7919 {
7920         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
7921         struct sitesurvey_parm  *pparm = (struct sitesurvey_parm *)pbuf;
7922         u8 bdelayscan = false;
7923         u8 val8;
7924         u32     initialgain;
7925         u32     i;
7926
7927 #ifdef CONFIG_88EU_P2P
7928         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
7929 #endif
7930
7931         if (pmlmeext->sitesurvey_res.state == SCAN_DISABLE) {
7932                 /* for first time sitesurvey_cmd */
7933                 rtw_hal_set_hwreg(padapter, HW_VAR_CHECK_TXBUF, NULL);
7934
7935                 pmlmeext->sitesurvey_res.state = SCAN_START;
7936                 pmlmeext->sitesurvey_res.bss_cnt = 0;
7937                 pmlmeext->sitesurvey_res.channel_idx = 0;
7938
7939                 for (i = 0; i < RTW_SSID_SCAN_AMOUNT; i++) {
7940                         if (pparm->ssid[i].SsidLength) {
7941                                 memcpy(pmlmeext->sitesurvey_res.ssid[i].Ssid, pparm->ssid[i].Ssid, IW_ESSID_MAX_SIZE);
7942                                 pmlmeext->sitesurvey_res.ssid[i].SsidLength = pparm->ssid[i].SsidLength;
7943                         } else {
7944                                 pmlmeext->sitesurvey_res.ssid[i].SsidLength = 0;
7945                         }
7946                 }
7947
7948                 pmlmeext->sitesurvey_res.ch_num = rtw_scan_ch_decision(padapter
7949                         , pmlmeext->sitesurvey_res.ch, RTW_CHANNEL_SCAN_AMOUNT
7950                         , pparm->ch, pparm->ch_num
7951         );
7952
7953                 pmlmeext->sitesurvey_res.scan_mode = pparm->scan_mode;
7954
7955                 /* issue null data if associating to the AP */
7956                 if (is_client_associated_to_ap(padapter)) {
7957                         pmlmeext->sitesurvey_res.state = SCAN_TXNULL;
7958
7959                         issue_nulldata(padapter, NULL, 1, 3, 500);
7960
7961                         bdelayscan = true;
7962                 }
7963                 if (bdelayscan) {
7964                         /* delay 50ms to protect nulldata(1). */
7965                         set_survey_timer(pmlmeext, 50);
7966                         return H2C_SUCCESS;
7967                 }
7968         }
7969
7970         if ((pmlmeext->sitesurvey_res.state == SCAN_START) || (pmlmeext->sitesurvey_res.state == SCAN_TXNULL)) {
7971                 /* disable dynamic functions, such as high power, DIG */
7972                 Save_DM_Func_Flag(padapter);
7973                 Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false);
7974
7975                 /* config the initial gain under scanning, need to write the BB registers */
7976 #ifdef CONFIG_88EU_P2P
7977                 if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE))
7978                         initialgain = 0x1E;
7979                 else
7980                         initialgain = 0x28;
7981 #else   /*  CONFIG_88EU_P2P */
7982                 initialgain = 0x1E;
7983 #endif /*  CONFIG_88EU_P2P */
7984
7985                 rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
7986
7987                 /* set MSR to no link state */
7988                 Set_MSR(padapter, _HW_STATE_NOLINK_);
7989
7990                 val8 = 1; /* under site survey */
7991                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_SITESURVEY, (u8 *)(&val8));
7992
7993                 pmlmeext->sitesurvey_res.state = SCAN_PROCESS;
7994         }
7995
7996         site_survey(padapter);
7997
7998         return H2C_SUCCESS;
7999 }
8000
8001 u8 setauth_hdl(struct adapter *padapter, unsigned char *pbuf)
8002 {
8003         struct setauth_parm             *pparm = (struct setauth_parm *)pbuf;
8004         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
8005         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
8006
8007         if (pparm->mode < 4)
8008                 pmlmeinfo->auth_algo = pparm->mode;
8009         return  H2C_SUCCESS;
8010 }
8011
8012 u8 setkey_hdl(struct adapter *padapter, u8 *pbuf)
8013 {
8014         unsigned short                          ctrl;
8015         struct setkey_parm              *pparm = (struct setkey_parm *)pbuf;
8016         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
8017         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
8018         unsigned char                                   null_sta[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
8019
8020         /* main tx key for wep. */
8021         if (pparm->set_tx)
8022                 pmlmeinfo->key_index = pparm->keyid;
8023
8024         /* write cam */
8025         ctrl = BIT(15) | ((pparm->algorithm) << 2) | pparm->keyid;
8026
8027         DBG_88E_LEVEL(_drv_info_, "set group key to hw: alg:%d(WEP40-1 WEP104-5 TKIP-2 AES-4) "
8028                         "keyid:%d\n", pparm->algorithm, pparm->keyid);
8029         write_cam(padapter, pparm->keyid, ctrl, null_sta, pparm->key);
8030
8031         return H2C_SUCCESS;
8032 }
8033
8034 u8 set_stakey_hdl(struct adapter *padapter, u8 *pbuf)
8035 {
8036         u16 ctrl = 0;
8037         u8 cam_id;/* cam_entry */
8038         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
8039         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
8040         struct set_stakey_parm  *pparm = (struct set_stakey_parm *)pbuf;
8041
8042         /* cam_entry: */
8043         /* 0~3 for default key */
8044
8045         /* for concurrent mode (ap+sta): */
8046         /* default key is disable, using sw encrypt/decrypt */
8047         /* cam_entry = 4 for sta mode (macid = 0) */
8048         /* cam_entry(macid+3) = 5 ~ N for ap mode (aid = 1~N, macid = 2 ~N) */
8049
8050         /* for concurrent mode (sta+sta): */
8051         /* default key is disable, using sw encrypt/decrypt */
8052         /* cam_entry = 4 mapping to macid = 0 */
8053         /* cam_entry = 5 mapping to macid = 2 */
8054
8055         cam_id = 4;
8056
8057         DBG_88E_LEVEL(_drv_info_, "set pairwise key to hw: alg:%d(WEP40-1 WEP104-5 TKIP-2 AES-4) camid:%d\n",
8058                       pparm->algorithm, cam_id);
8059         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
8060                 struct sta_info *psta;
8061                 struct sta_priv *pstapriv = &padapter->stapriv;
8062
8063                 if (pparm->algorithm == _NO_PRIVACY_)   /*  clear cam entry */ {
8064                         clear_cam_entry(padapter, pparm->id);
8065                         return H2C_SUCCESS_RSP;
8066                 }
8067
8068                 psta = rtw_get_stainfo(pstapriv, pparm->addr);
8069                 if (psta) {
8070                         ctrl = (BIT(15) | ((pparm->algorithm) << 2));
8071
8072                         DBG_88E("r871x_set_stakey_hdl(): enc_algorithm=%d\n", pparm->algorithm);
8073
8074                         if ((psta->mac_id < 1) || (psta->mac_id > (NUM_STA-4))) {
8075                                 DBG_88E("r871x_set_stakey_hdl():set_stakey failed, mac_id(aid)=%d\n", psta->mac_id);
8076                                 return H2C_REJECTED;
8077                         }
8078
8079                         cam_id = (psta->mac_id + 3);/* 0~3 for default key, cmd_id = macid + 3, macid = aid+1; */
8080
8081                         DBG_88E("Write CAM, mac_addr =%x:%x:%x:%x:%x:%x, cam_entry=%d\n", pparm->addr[0],
8082                                 pparm->addr[1], pparm->addr[2], pparm->addr[3], pparm->addr[4],
8083                                 pparm->addr[5], cam_id);
8084
8085                         write_cam(padapter, cam_id, ctrl, pparm->addr, pparm->key);
8086
8087                         return H2C_SUCCESS_RSP;
8088                 } else {
8089                         DBG_88E("r871x_set_stakey_hdl(): sta has been free\n");
8090                         return H2C_REJECTED;
8091                 }
8092         }
8093
8094         /* below for sta mode */
8095
8096         if (pparm->algorithm == _NO_PRIVACY_) { /*  clear cam entry */
8097                 clear_cam_entry(padapter, pparm->id);
8098                 return H2C_SUCCESS;
8099         }
8100         ctrl = BIT(15) | ((pparm->algorithm) << 2);
8101         write_cam(padapter, cam_id, ctrl, pparm->addr, pparm->key);
8102         pmlmeinfo->enc_algo = pparm->algorithm;
8103         return H2C_SUCCESS;
8104 }
8105
8106 u8 add_ba_hdl(struct adapter *padapter, unsigned char *pbuf)
8107 {
8108         struct addBaReq_parm    *pparm = (struct addBaReq_parm *)pbuf;
8109         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
8110         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
8111
8112         struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, pparm->addr);
8113
8114         if (!psta)
8115                 return  H2C_SUCCESS;
8116
8117         if (((pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) && (pmlmeinfo->HT_enable)) ||
8118             ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)) {
8119                 issue_action_BA(padapter, pparm->addr, RTW_WLAN_ACTION_ADDBA_REQ, (u16)pparm->tid);
8120                 _set_timer(&psta->addba_retry_timer, ADDBA_TO);
8121         } else {
8122                 psta->htpriv.candidate_tid_bitmap &= ~BIT(pparm->tid);
8123         }
8124         return  H2C_SUCCESS;
8125 }
8126
8127 u8 set_tx_beacon_cmd(struct adapter *padapter)
8128 {
8129         struct cmd_obj  *ph2c;
8130         struct Tx_Beacon_param  *ptxBeacon_parm;
8131         struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
8132         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
8133         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
8134         u8 res = _SUCCESS;
8135         int len_diff = 0;
8136
8137         ph2c = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
8138         if (!ph2c) {
8139                 res = _FAIL;
8140                 goto exit;
8141         }
8142
8143         ptxBeacon_parm = kzalloc(sizeof(struct Tx_Beacon_param), GFP_KERNEL);
8144         if (!ptxBeacon_parm) {
8145                 kfree(ph2c);
8146                 res = _FAIL;
8147                 goto exit;
8148         }
8149
8150         memcpy(&(ptxBeacon_parm->network), &(pmlmeinfo->network), sizeof(struct wlan_bssid_ex));
8151
8152         len_diff = update_hidden_ssid(ptxBeacon_parm->network.IEs+_BEACON_IE_OFFSET_,
8153                                       ptxBeacon_parm->network.IELength-_BEACON_IE_OFFSET_,
8154                                       pmlmeinfo->hidden_ssid_mode);
8155         ptxBeacon_parm->network.IELength += len_diff;
8156
8157         init_h2fwcmd_w_parm_no_rsp(ph2c, ptxBeacon_parm, GEN_CMD_CODE(_TX_Beacon));
8158
8159         res = rtw_enqueue_cmd(pcmdpriv, ph2c);
8160
8161 exit:
8162
8163         return res;
8164 }
8165
8166 u8 mlme_evt_hdl(struct adapter *padapter, unsigned char *pbuf)
8167 {
8168         u8 evt_code;
8169         u16 evt_sz;
8170         uint    *peventbuf;
8171         void (*event_callback)(struct adapter *dev, u8 *pbuf);
8172         struct evt_priv *pevt_priv = &(padapter->evtpriv);
8173
8174         peventbuf = (uint *)pbuf;
8175         evt_sz = (u16)(*peventbuf&0xffff);
8176         evt_code = (u8)((*peventbuf>>16)&0xff);
8177
8178         /*  checking if event code is valid */
8179         if (evt_code >= MAX_C2HEVT)
8180                 goto _abort_event_;
8181
8182         /*  checking if event size match the event parm size */
8183         if ((wlanevents[evt_code].parmsize != 0) &&
8184             (wlanevents[evt_code].parmsize != evt_sz))
8185                 goto _abort_event_;
8186
8187         atomic_inc(&pevt_priv->event_seq);
8188
8189         peventbuf += 2;
8190
8191         if (peventbuf) {
8192                 event_callback = wlanevents[evt_code].event_callback;
8193                 event_callback(padapter, (u8 *)peventbuf);
8194
8195                 pevt_priv->evt_done_cnt++;
8196         }
8197
8198 _abort_event_:
8199         return H2C_SUCCESS;
8200 }
8201
8202 u8 h2c_msg_hdl(struct adapter *padapter, unsigned char *pbuf)
8203 {
8204         if (!pbuf)
8205                 return H2C_PARAMETERS_ERROR;
8206
8207         return H2C_SUCCESS;
8208 }
8209
8210 u8 tx_beacon_hdl(struct adapter *padapter, unsigned char *pbuf)
8211 {
8212         if (send_beacon(padapter) == _FAIL) {
8213                 DBG_88E("issue_beacon, fail!\n");
8214                 return H2C_PARAMETERS_ERROR;
8215         }
8216 #ifdef CONFIG_88EU_AP_MODE
8217         else { /* tx bc/mc frames after update TIM */
8218                 struct sta_info *psta_bmc;
8219                 struct list_head *xmitframe_plist, *xmitframe_phead;
8220                 struct xmit_frame *pxmitframe = NULL;
8221                 struct sta_priv  *pstapriv = &padapter->stapriv;
8222
8223                 /* for BC/MC Frames */
8224                 psta_bmc = rtw_get_bcmc_stainfo(padapter);
8225                 if (!psta_bmc)
8226                         return H2C_SUCCESS;
8227
8228                 if ((pstapriv->tim_bitmap&BIT(0)) && (psta_bmc->sleepq_len > 0)) {
8229                         msleep(10);/*  10ms, ATIM(HIQ) Windows */
8230                         spin_lock_bh(&psta_bmc->sleep_q.lock);
8231
8232                         xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
8233                         xmitframe_plist = xmitframe_phead->next;
8234
8235                         while (xmitframe_phead != xmitframe_plist) {
8236                                 pxmitframe = container_of(xmitframe_plist, struct xmit_frame, list);
8237
8238                                 xmitframe_plist = xmitframe_plist->next;
8239
8240                                 list_del_init(&pxmitframe->list);
8241
8242                                 psta_bmc->sleepq_len--;
8243                                 if (psta_bmc->sleepq_len > 0)
8244                                         pxmitframe->attrib.mdata = 1;
8245                                 else
8246                                         pxmitframe->attrib.mdata = 0;
8247
8248                                 pxmitframe->attrib.triggered = 1;
8249
8250                                 pxmitframe->attrib.qsel = 0x11;/* HIQ */
8251
8252                                 spin_unlock_bh(&psta_bmc->sleep_q.lock);
8253                                 if (rtw_hal_xmit(padapter, pxmitframe))
8254                                         rtw_os_xmit_complete(padapter, pxmitframe);
8255                                 spin_lock_bh(&psta_bmc->sleep_q.lock);
8256                         }
8257                         spin_unlock_bh(&psta_bmc->sleep_q.lock);
8258                 }
8259         }
8260 #endif
8261         return H2C_SUCCESS;
8262 }
8263
8264 u8 set_ch_hdl(struct adapter *padapter, u8 *pbuf)
8265 {
8266         struct set_ch_parm *set_ch_parm;
8267         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
8268
8269         if (!pbuf)
8270                 return H2C_PARAMETERS_ERROR;
8271
8272         set_ch_parm = (struct set_ch_parm *)pbuf;
8273
8274         DBG_88E(FUNC_NDEV_FMT" ch:%u, bw:%u, ch_offset:%u\n",
8275                 FUNC_NDEV_ARG(padapter->pnetdev),
8276                 set_ch_parm->ch, set_ch_parm->bw, set_ch_parm->ch_offset);
8277
8278         pmlmeext->cur_channel = set_ch_parm->ch;
8279         pmlmeext->cur_ch_offset = set_ch_parm->ch_offset;
8280         pmlmeext->cur_bwmode = set_ch_parm->bw;
8281
8282         set_channel_bwmode(padapter, set_ch_parm->ch, set_ch_parm->ch_offset, set_ch_parm->bw);
8283
8284         return  H2C_SUCCESS;
8285 }
8286
8287 u8 set_chplan_hdl(struct adapter *padapter, unsigned char *pbuf)
8288 {
8289         struct SetChannelPlan_param *setChannelPlan_param;
8290         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
8291
8292         if (!pbuf)
8293                 return H2C_PARAMETERS_ERROR;
8294
8295         setChannelPlan_param = (struct SetChannelPlan_param *)pbuf;
8296
8297         pmlmeext->max_chan_nums = init_channel_set(padapter, setChannelPlan_param->channel_plan, pmlmeext->channel_set);
8298         init_channel_list(padapter, pmlmeext->channel_set, pmlmeext->max_chan_nums, &pmlmeext->channel_list);
8299
8300         return  H2C_SUCCESS;
8301 }
8302
8303 u8 led_blink_hdl(struct adapter *padapter, unsigned char *pbuf)
8304 {
8305         if (!pbuf)
8306                 return H2C_PARAMETERS_ERROR;
8307         return  H2C_SUCCESS;
8308 }
8309
8310 u8 set_csa_hdl(struct adapter *padapter, unsigned char *pbuf)
8311 {
8312         return  H2C_REJECTED;
8313 }
8314
8315 /*  TDLS_WRCR           : write RCR DATA BIT */
8316 /*  TDLS_SD_PTI         : issue peer traffic indication */
8317 /*  TDLS_CS_OFF         : go back to the channel linked with AP, terminating channel switch procedure */
8318 /*  TDLS_INIT_CH_SEN    : init channel sensing, receive all data and mgnt frame */
8319 /*  TDLS_DONE_CH_SEN: channel sensing and report candidate channel */
8320 /*  TDLS_OFF_CH         : first time set channel to off channel */
8321 /*  TDLS_BASE_CH                : go back tp the channel linked with AP when set base channel as target channel */
8322 /*  TDLS_P_OFF_CH       : periodically go to off channel */
8323 /*  TDLS_P_BASE_CH      : periodically go back to base channel */
8324 /*  TDLS_RS_RCR         : restore RCR */
8325 /*  TDLS_CKALV_PH1      : check alive timer phase1 */
8326 /*  TDLS_CKALV_PH2      : check alive timer phase2 */
8327 /*  TDLS_FREE_STA       : free tdls sta */
8328 u8 tdls_hdl(struct adapter *padapter, unsigned char *pbuf)
8329 {
8330         return H2C_REJECTED;
8331 }