GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / staging / rtl8723bs / core / rtw_mlme_ext.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #define _RTW_MLME_EXT_C_
8
9 #include <drv_types.h>
10 #include <rtw_debug.h>
11 #include <rtw_wifi_regd.h>
12 #include <linux/kernel.h>
13
14
15 static struct mlme_handler mlme_sta_tbl[] = {
16         {WIFI_ASSOCREQ,         "OnAssocReq",   &OnAssocReq},
17         {WIFI_ASSOCRSP,         "OnAssocRsp",   &OnAssocRsp},
18         {WIFI_REASSOCREQ,       "OnReAssocReq", &OnAssocReq},
19         {WIFI_REASSOCRSP,       "OnReAssocRsp", &OnAssocRsp},
20         {WIFI_PROBEREQ,         "OnProbeReq",   &OnProbeReq},
21         {WIFI_PROBERSP,         "OnProbeRsp",           &OnProbeRsp},
22
23         /*----------------------------------------------------------
24                                         below 2 are reserved
25         -----------------------------------------------------------*/
26         {0,                                     "DoReserved",           &DoReserved},
27         {0,                                     "DoReserved",           &DoReserved},
28         {WIFI_BEACON,           "OnBeacon",             &OnBeacon},
29         {WIFI_ATIM,                     "OnATIM",               &OnAtim},
30         {WIFI_DISASSOC,         "OnDisassoc",           &OnDisassoc},
31         {WIFI_AUTH,                     "OnAuth",               &OnAuthClient},
32         {WIFI_DEAUTH,           "OnDeAuth",             &OnDeAuth},
33         {WIFI_ACTION,           "OnAction",             &OnAction},
34         {WIFI_ACTION_NOACK, "OnActionNoAck",    &OnAction},
35 };
36
37 static struct action_handler OnAction_tbl[] = {
38         {RTW_WLAN_CATEGORY_SPECTRUM_MGMT,        "ACTION_SPECTRUM_MGMT", on_action_spct},
39         {RTW_WLAN_CATEGORY_QOS, "ACTION_QOS", &DoReserved},
40         {RTW_WLAN_CATEGORY_DLS, "ACTION_DLS", &DoReserved},
41         {RTW_WLAN_CATEGORY_BACK, "ACTION_BACK", &OnAction_back},
42         {RTW_WLAN_CATEGORY_PUBLIC, "ACTION_PUBLIC", on_action_public},
43         {RTW_WLAN_CATEGORY_RADIO_MEASUREMENT, "ACTION_RADIO_MEASUREMENT", &DoReserved},
44         {RTW_WLAN_CATEGORY_FT, "ACTION_FT",     &DoReserved},
45         {RTW_WLAN_CATEGORY_HT,  "ACTION_HT",    &OnAction_ht},
46         {RTW_WLAN_CATEGORY_SA_QUERY, "ACTION_SA_QUERY", &OnAction_sa_query},
47         {RTW_WLAN_CATEGORY_UNPROTECTED_WNM, "ACTION_UNPROTECTED_WNM", &DoReserved},
48         {RTW_WLAN_CATEGORY_SELF_PROTECTED, "ACTION_SELF_PROTECTED", &DoReserved},
49         {RTW_WLAN_CATEGORY_WMM, "ACTION_WMM", &DoReserved},
50         {RTW_WLAN_CATEGORY_VHT, "ACTION_VHT", &DoReserved},
51         {RTW_WLAN_CATEGORY_P2P, "ACTION_P2P", &DoReserved},
52 };
53
54
55 static u8 null_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
56
57 /**************************************************
58 OUI definitions for the vendor specific IE
59 ***************************************************/
60 unsigned char RTW_WPA_OUI[] = {0x00, 0x50, 0xf2, 0x01};
61 unsigned char WMM_OUI[] = {0x00, 0x50, 0xf2, 0x02};
62 unsigned char WPS_OUI[] = {0x00, 0x50, 0xf2, 0x04};
63 unsigned char P2P_OUI[] = {0x50, 0x6F, 0x9A, 0x09};
64 unsigned char WFD_OUI[] = {0x50, 0x6F, 0x9A, 0x0A};
65
66 unsigned char WMM_INFO_OUI[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
67 unsigned char WMM_PARA_OUI[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
68
69 static unsigned char REALTEK_96B_IE[] = {0x00, 0xe0, 0x4c, 0x02, 0x01, 0x20};
70
71 /********************************************************
72 ChannelPlan definitions
73 *********************************************************/
74 static RT_CHANNEL_PLAN_2G       RTW_ChannelPlan2G[RT_CHANNEL_DOMAIN_2G_MAX] = {
75         {{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 */
76         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 13},              /*  0x01, RT_CHANNEL_DOMAIN_2G_ETSI1 */
77         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 11},                      /*  0x02, RT_CHANNEL_DOMAIN_2G_FCC1 */
78         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 14},  /*  0x03, RT_CHANNEL_DOMAIN_2G_MIKK1 */
79         {{10, 11, 12, 13}, 4},                                          /*  0x04, RT_CHANNEL_DOMAIN_2G_ETSI2 */
80         {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 14},  /*  0x05, RT_CHANNEL_DOMAIN_2G_GLOBAL , Passive scan CH 12, 13, 14 */
81         {{}, 0},                                                                /*  0x06, RT_CHANNEL_DOMAIN_2G_NULL */
82 };
83
84 static RT_CHANNEL_PLAN_5G       RTW_ChannelPlan5G[RT_CHANNEL_DOMAIN_5G_MAX] = {
85         {{}, 0},                                                                                                                                                                        /*  0x00, RT_CHANNEL_DOMAIN_5G_NULL */
86         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140}, 19},                                          /*  0x01, RT_CHANNEL_DOMAIN_5G_ETSI1 */
87         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 149, 153, 157, 161, 165}, 24}, /*  0x02, RT_CHANNEL_DOMAIN_5G_ETSI2 */
88         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 149, 153, 157, 161, 165}, 22},                   /*  0x03, RT_CHANNEL_DOMAIN_5G_ETSI3 */
89         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 149, 153, 157, 161, 165}, 24}, /*  0x04, RT_CHANNEL_DOMAIN_5G_FCC1 */
90         {{36, 40, 44, 48, 149, 153, 157, 161, 165}, 9},                                                                                                         /*  0x05, RT_CHANNEL_DOMAIN_5G_FCC2 */
91         {{36, 40, 44, 48, 52, 56, 60, 64, 149, 153, 157, 161, 165}, 13},                                                                                        /*  0x06, RT_CHANNEL_DOMAIN_5G_FCC3 */
92         {{36, 40, 44, 48, 52, 56, 60, 64, 149, 153, 157, 161}, 12},                                                                                             /*  0x07, RT_CHANNEL_DOMAIN_5G_FCC4 */
93         {{149, 153, 157, 161, 165}, 5},                                                                                                                                 /*  0x08, RT_CHANNEL_DOMAIN_5G_FCC5 */
94         {{36, 40, 44, 48, 52, 56, 60, 64}, 8},                                                                                                                          /*  0x09, RT_CHANNEL_DOMAIN_5G_FCC6 */
95         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 136, 140, 149, 153, 157, 161, 165}, 20},                                     /*  0x0A, RT_CHANNEL_DOMAIN_5G_FCC7_IC1 */
96         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 149, 153, 157, 161, 165}, 20},                                     /*  0x0B, RT_CHANNEL_DOMAIN_5G_KCC1 */
97         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140}, 19},                                          /*  0x0C, RT_CHANNEL_DOMAIN_5G_MKK1 */
98         {{36, 40, 44, 48, 52, 56, 60, 64}, 8},                                                                                                                          /*  0x0D, RT_CHANNEL_DOMAIN_5G_MKK2 */
99         {{100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140}, 11},                                                                                  /*  0x0E, RT_CHANNEL_DOMAIN_5G_MKK3 */
100         {{56, 60, 64, 100, 104, 108, 112, 116, 136, 140, 149, 153, 157, 161, 165}, 15},                                                         /*  0x0F, RT_CHANNEL_DOMAIN_5G_NCC1 */
101         {{56, 60, 64, 149, 153, 157, 161, 165}, 8},                                                                                                                     /*  0x10, RT_CHANNEL_DOMAIN_5G_NCC2 */
102         {{149, 153, 157, 161, 165}, 5},                                                                                                                                 /*  0x11, RT_CHANNEL_DOMAIN_5G_NCC3 */
103         {{36, 40, 44, 48}, 4},                                                                                                                                                  /*  0x12, RT_CHANNEL_DOMAIN_5G_ETSI4 */
104         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 136, 140, 149, 153, 157, 161, 165}, 20},                                     /*  0x13, RT_CHANNEL_DOMAIN_5G_ETSI5 */
105         {{149, 153, 157, 161}, 4},                                                                                                                                              /*  0x14, RT_CHANNEL_DOMAIN_5G_FCC8 */
106         {{36, 40, 44, 48, 52, 56, 60, 64}, 8},                                                                                                                          /*  0x15, RT_CHANNEL_DOMAIN_5G_ETSI6 */
107         {{36, 40, 44, 48, 52, 56, 60, 64, 149, 153, 157, 161, 165}, 13},                                                                                        /*  0x16, RT_CHANNEL_DOMAIN_5G_ETSI7 */
108         {{36, 40, 44, 48, 149, 153, 157, 161, 165}, 9},                                                                                                         /*  0x17, RT_CHANNEL_DOMAIN_5G_ETSI8 */
109         {{100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140}, 11},                                                                                  /*  0x18, RT_CHANNEL_DOMAIN_5G_ETSI9 */
110         {{149, 153, 157, 161, 165}, 5},                                                                                                                                 /*  0x19, RT_CHANNEL_DOMAIN_5G_ETSI10 */
111         {{36, 40, 44, 48, 52, 56, 60, 64, 132, 136, 140, 149, 153, 157, 161, 165}, 16},                                                                 /*  0x1A, RT_CHANNEL_DOMAIN_5G_ETSI11 */
112         {{52, 56, 60, 64, 100, 104, 108, 112, 116, 132, 136, 140, 149, 153, 157, 161, 165}, 17},                                                        /*  0x1B, RT_CHANNEL_DOMAIN_5G_NCC4 */
113         {{149, 153, 157, 161}, 4},                                                                                                                                              /*  0x1C, RT_CHANNEL_DOMAIN_5G_ETSI12 */
114         {{36, 40, 44, 48, 100, 104, 108, 112, 116, 132, 136, 140, 149, 153, 157, 161, 165}, 17},                                                        /*  0x1D, RT_CHANNEL_DOMAIN_5G_FCC9 */
115         {{36, 40, 44, 48, 100, 104, 108, 112, 116, 132, 136, 140}, 12},                                                                                 /*  0x1E, RT_CHANNEL_DOMAIN_5G_ETSI13 */
116         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 132, 136, 140, 149, 153, 157, 161}, 20},                                     /*  0x1F, RT_CHANNEL_DOMAIN_5G_FCC10 */
117
118         /*  Driver self defined for old channel plan Compatible , Remember to modify if have new channel plan definition ===== */
119         {{36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 132, 136, 140, 149, 153, 157, 161, 165}, 21},                                /*  0x20, RT_CHANNEL_DOMAIN_5G_FCC */
120         {{36, 40, 44, 48}, 4},                                                                                                                                                  /*  0x21, RT_CHANNEL_DOMAIN_5G_JAPAN_NO_DFS */
121         {{36, 40, 44, 48, 149, 153, 157, 161}, 8},                                                                                                                      /*  0x22, RT_CHANNEL_DOMAIN_5G_FCC4_NO_DFS */
122 };
123
124 static RT_CHANNEL_PLAN_MAP      RTW_ChannelPlanMap[RT_CHANNEL_DOMAIN_MAX] = {
125         /*  0x00 ~ 0x1F , Old Define ===== */
126         {0x02, 0x20},   /* 0x00, RT_CHANNEL_DOMAIN_FCC */
127         {0x02, 0x0A},   /* 0x01, RT_CHANNEL_DOMAIN_IC */
128         {0x01, 0x01},   /* 0x02, RT_CHANNEL_DOMAIN_ETSI */
129         {0x01, 0x00},   /* 0x03, RT_CHANNEL_DOMAIN_SPAIN */
130         {0x01, 0x00},   /* 0x04, RT_CHANNEL_DOMAIN_FRANCE */
131         {0x03, 0x00},   /* 0x05, RT_CHANNEL_DOMAIN_MKK */
132         {0x03, 0x00},   /* 0x06, RT_CHANNEL_DOMAIN_MKK1 */
133         {0x01, 0x09},   /* 0x07, RT_CHANNEL_DOMAIN_ISRAEL */
134         {0x03, 0x09},   /* 0x08, RT_CHANNEL_DOMAIN_TELEC */
135         {0x03, 0x00},   /* 0x09, RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN */
136         {0x00, 0x00},   /* 0x0A, RT_CHANNEL_DOMAIN_WORLD_WIDE_13 */
137         {0x02, 0x0F},   /* 0x0B, RT_CHANNEL_DOMAIN_TAIWAN */
138         {0x01, 0x08},   /* 0x0C, RT_CHANNEL_DOMAIN_CHINA */
139         {0x02, 0x06},   /* 0x0D, RT_CHANNEL_DOMAIN_SINGAPORE_INDIA_MEXICO */
140         {0x02, 0x0B},   /* 0x0E, RT_CHANNEL_DOMAIN_KOREA */
141         {0x02, 0x09},   /* 0x0F, RT_CHANNEL_DOMAIN_TURKEY */
142         {0x01, 0x01},   /* 0x10, RT_CHANNEL_DOMAIN_JAPAN */
143         {0x02, 0x05},   /* 0x11, RT_CHANNEL_DOMAIN_FCC_NO_DFS */
144         {0x01, 0x21},   /* 0x12, RT_CHANNEL_DOMAIN_JAPAN_NO_DFS */
145         {0x00, 0x04},   /* 0x13, RT_CHANNEL_DOMAIN_WORLD_WIDE_5G */
146         {0x02, 0x10},   /* 0x14, RT_CHANNEL_DOMAIN_TAIWAN_NO_DFS */
147         {0x00, 0x21},   /* 0x15, RT_CHANNEL_DOMAIN_ETSI_NO_DFS */
148         {0x00, 0x22},   /* 0x16, RT_CHANNEL_DOMAIN_KOREA_NO_DFS */
149         {0x03, 0x21},   /* 0x17, RT_CHANNEL_DOMAIN_JAPAN_NO_DFS */
150         {0x06, 0x08},   /* 0x18, RT_CHANNEL_DOMAIN_PAKISTAN_NO_DFS */
151         {0x02, 0x08},   /* 0x19, RT_CHANNEL_DOMAIN_TAIWAN2_NO_DFS */
152         {0x00, 0x00},   /* 0x1A, */
153         {0x00, 0x00},   /* 0x1B, */
154         {0x00, 0x00},   /* 0x1C, */
155         {0x00, 0x00},   /* 0x1D, */
156         {0x00, 0x00},   /* 0x1E, */
157         {0x06, 0x04},   /* 0x1F, RT_CHANNEL_DOMAIN_WORLD_WIDE_ONLY_5G */
158         /*  0x20 ~ 0x7F , New Define ===== */
159         {0x00, 0x00},   /* 0x20, RT_CHANNEL_DOMAIN_WORLD_NULL */
160         {0x01, 0x00},   /* 0x21, RT_CHANNEL_DOMAIN_ETSI1_NULL */
161         {0x02, 0x00},   /* 0x22, RT_CHANNEL_DOMAIN_FCC1_NULL */
162         {0x03, 0x00},   /* 0x23, RT_CHANNEL_DOMAIN_MKK1_NULL */
163         {0x04, 0x00},   /* 0x24, RT_CHANNEL_DOMAIN_ETSI2_NULL */
164         {0x02, 0x04},   /* 0x25, RT_CHANNEL_DOMAIN_FCC1_FCC1 */
165         {0x00, 0x01},   /* 0x26, RT_CHANNEL_DOMAIN_WORLD_ETSI1 */
166         {0x03, 0x0C},   /* 0x27, RT_CHANNEL_DOMAIN_MKK1_MKK1 */
167         {0x00, 0x0B},   /* 0x28, RT_CHANNEL_DOMAIN_WORLD_KCC1 */
168         {0x00, 0x05},   /* 0x29, RT_CHANNEL_DOMAIN_WORLD_FCC2 */
169         {0x00, 0x00},   /* 0x2A, */
170         {0x00, 0x00},   /* 0x2B, */
171         {0x00, 0x00},   /* 0x2C, */
172         {0x00, 0x00},   /* 0x2D, */
173         {0x00, 0x00},   /* 0x2E, */
174         {0x00, 0x00},   /* 0x2F, */
175         {0x00, 0x06},   /* 0x30, RT_CHANNEL_DOMAIN_WORLD_FCC3 */
176         {0x00, 0x07},   /* 0x31, RT_CHANNEL_DOMAIN_WORLD_FCC4 */
177         {0x00, 0x08},   /* 0x32, RT_CHANNEL_DOMAIN_WORLD_FCC5 */
178         {0x00, 0x09},   /* 0x33, RT_CHANNEL_DOMAIN_WORLD_FCC6 */
179         {0x02, 0x0A},   /* 0x34, RT_CHANNEL_DOMAIN_FCC1_FCC7 */
180         {0x00, 0x02},   /* 0x35, RT_CHANNEL_DOMAIN_WORLD_ETSI2 */
181         {0x00, 0x03},   /* 0x36, RT_CHANNEL_DOMAIN_WORLD_ETSI3 */
182         {0x03, 0x0D},   /* 0x37, RT_CHANNEL_DOMAIN_MKK1_MKK2 */
183         {0x03, 0x0E},   /* 0x38, RT_CHANNEL_DOMAIN_MKK1_MKK3 */
184         {0x02, 0x0F},   /* 0x39, RT_CHANNEL_DOMAIN_FCC1_NCC1 */
185         {0x00, 0x00},   /* 0x3A, */
186         {0x00, 0x00},   /* 0x3B, */
187         {0x00, 0x00},   /* 0x3C, */
188         {0x00, 0x00},   /* 0x3D, */
189         {0x00, 0x00},   /* 0x3E, */
190         {0x00, 0x00},   /* 0x3F, */
191         {0x02, 0x10},   /* 0x40, RT_CHANNEL_DOMAIN_FCC1_NCC2 */
192         {0x05, 0x00},   /* 0x41, RT_CHANNEL_DOMAIN_GLOBAL_NULL */
193         {0x01, 0x12},   /* 0x42, RT_CHANNEL_DOMAIN_ETSI1_ETSI4 */
194         {0x02, 0x05},   /* 0x43, RT_CHANNEL_DOMAIN_FCC1_FCC2 */
195         {0x02, 0x11},   /* 0x44, RT_CHANNEL_DOMAIN_FCC1_NCC3 */
196         {0x00, 0x13},   /* 0x45, RT_CHANNEL_DOMAIN_WORLD_ETSI5 */
197         {0x02, 0x14},   /* 0x46, RT_CHANNEL_DOMAIN_FCC1_FCC8 */
198         {0x00, 0x15},   /* 0x47, RT_CHANNEL_DOMAIN_WORLD_ETSI6 */
199         {0x00, 0x16},   /* 0x48, RT_CHANNEL_DOMAIN_WORLD_ETSI7 */
200         {0x00, 0x17},   /* 0x49, RT_CHANNEL_DOMAIN_WORLD_ETSI8 */
201         {0x00, 0x18},   /* 0x50, RT_CHANNEL_DOMAIN_WORLD_ETSI9 */
202         {0x00, 0x19},   /* 0x51, RT_CHANNEL_DOMAIN_WORLD_ETSI10 */
203         {0x00, 0x1A},   /* 0x52, RT_CHANNEL_DOMAIN_WORLD_ETSI11 */
204         {0x02, 0x1B},   /* 0x53, RT_CHANNEL_DOMAIN_FCC1_NCC4 */
205         {0x00, 0x1C},   /* 0x54, RT_CHANNEL_DOMAIN_WORLD_ETSI12 */
206         {0x02, 0x1D},   /* 0x55, RT_CHANNEL_DOMAIN_FCC1_FCC9 */
207         {0x00, 0x1E},   /* 0x56, RT_CHANNEL_DOMAIN_WORLD_ETSI13 */
208         {0x02, 0x1F},   /* 0x57, RT_CHANNEL_DOMAIN_FCC1_FCC10 */
209 };
210
211 static RT_CHANNEL_PLAN_MAP      RTW_CHANNEL_PLAN_MAP_REALTEK_DEFINE = {0x03, 0x02}; /* use the conbination for max channel numbers */
212
213 /*
214  * Search the @param ch in given @param ch_set
215  * @ch_set: the given channel set
216  * @ch: the given channel number
217  *
218  * return the index of channel_num in channel_set, -1 if not found
219  */
220 int rtw_ch_set_search_ch(RT_CHANNEL_INFO *ch_set, const u32 ch)
221 {
222         int i;
223         for (i = 0; ch_set[i].ChannelNum != 0; i++) {
224                 if (ch == ch_set[i].ChannelNum)
225                         break;
226         }
227
228         if (i >= ch_set[i].ChannelNum)
229                 return -1;
230         return i;
231 }
232
233 /*
234  * Check the @param ch is fit with setband setting of @param adapter
235  * @adapter: the given adapter
236  * @ch: the given channel number
237  *
238  * return true when check valid, false not valid
239  */
240 bool rtw_mlme_band_check(struct adapter *adapter, const u32 ch)
241 {
242         if (adapter->setband == GHZ24_50 /* 2.4G and 5G */
243                 || (adapter->setband == GHZ_24 && ch < 35) /* 2.4G only */
244                 || (adapter->setband == GHZ_50 && ch > 35) /* 5G only */
245         ) {
246                 return true;
247         }
248         return false;
249 }
250
251 /****************************************************************************
252
253 Following are the initialization functions for WiFi MLME
254
255 *****************************************************************************/
256
257 int init_hw_mlme_ext(struct adapter *padapter)
258 {
259         struct  mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
260
261         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
262         return _SUCCESS;
263 }
264
265 void init_mlme_default_rate_set(struct adapter *padapter)
266 {
267         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
268
269         unsigned char mixed_datarate[NumRates] = {_1M_RATE_, _2M_RATE_, _5M_RATE_, _11M_RATE_, _6M_RATE_, _9M_RATE_, _12M_RATE_, _18M_RATE_, _24M_RATE_, _36M_RATE_, _48M_RATE_, _54M_RATE_, 0xff};
270         unsigned char mixed_basicrate[NumRates] = {_1M_RATE_, _2M_RATE_, _5M_RATE_, _11M_RATE_, _6M_RATE_, _12M_RATE_, _24M_RATE_, 0xff,};
271         unsigned char supported_mcs_set[16] = {0xff, 0xff, 0x00, 0x00, 0x01, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
272
273         memcpy(pmlmeext->datarate, mixed_datarate, NumRates);
274         memcpy(pmlmeext->basicrate, mixed_basicrate, NumRates);
275
276         memcpy(pmlmeext->default_supported_mcs_set, supported_mcs_set, sizeof(pmlmeext->default_supported_mcs_set));
277 }
278
279 static void init_mlme_ext_priv_value(struct adapter *padapter)
280 {
281         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
282         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
283
284         atomic_set(&pmlmeext->event_seq, 0);
285         pmlmeext->mgnt_seq = 0;/* reset to zero when disconnect at client mode */
286         pmlmeext->sa_query_seq = 0;
287         pmlmeext->mgnt_80211w_IPN = 0;
288         pmlmeext->mgnt_80211w_IPN_rx = 0;
289         pmlmeext->cur_channel = padapter->registrypriv.channel;
290         pmlmeext->cur_bwmode = CHANNEL_WIDTH_20;
291         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
292
293         pmlmeext->retry = 0;
294
295         pmlmeext->cur_wireless_mode = padapter->registrypriv.wireless_mode;
296
297         init_mlme_default_rate_set(padapter);
298
299         if (pmlmeext->cur_channel > 14)
300                 pmlmeext->tx_rate = IEEE80211_OFDM_RATE_6MB;
301         else
302                 pmlmeext->tx_rate = IEEE80211_CCK_RATE_1MB;
303
304         pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
305         pmlmeext->sitesurvey_res.channel_idx = 0;
306         pmlmeext->sitesurvey_res.bss_cnt = 0;
307         pmlmeext->scan_abort = false;
308
309         pmlmeinfo->state = WIFI_FW_NULL_STATE;
310         pmlmeinfo->reauth_count = 0;
311         pmlmeinfo->reassoc_count = 0;
312         pmlmeinfo->link_count = 0;
313         pmlmeinfo->auth_seq = 0;
314         pmlmeinfo->auth_algo = dot11AuthAlgrthm_Open;
315         pmlmeinfo->key_index = 0;
316         pmlmeinfo->iv = 0;
317
318         pmlmeinfo->enc_algo = _NO_PRIVACY_;
319         pmlmeinfo->authModeToggle = 0;
320
321         memset(pmlmeinfo->chg_txt, 0, 128);
322
323         pmlmeinfo->slotTime = SHORT_SLOT_TIME;
324         pmlmeinfo->preamble_mode = PREAMBLE_AUTO;
325
326         pmlmeinfo->dialogToken = 0;
327
328         pmlmeext->action_public_rxseq = 0xffff;
329         pmlmeext->action_public_dialog_token = 0xff;
330 }
331
332 static int has_channel(RT_CHANNEL_INFO *channel_set,
333                                            u8 chanset_size,
334                                            u8 chan)
335 {
336         int i;
337
338         for (i = 0; i < chanset_size; i++) {
339                 if (channel_set[i].ChannelNum == chan) {
340                         return 1;
341                 }
342         }
343
344         return 0;
345 }
346
347 static void init_channel_list(struct adapter *padapter, RT_CHANNEL_INFO *channel_set,
348                                                           u8 chanset_size,
349                                                           struct p2p_channels *channel_list)
350 {
351
352         struct p2p_oper_class_map op_class[] = {
353                 { IEEE80211G,  81,   1,  13,  1, BW20 },
354                 { IEEE80211G,  82,  14,  14,  1, BW20 },
355                 { IEEE80211A, 115,  36,  48,  4, BW20 },
356                 { IEEE80211A, 116,  36,  44,  8, BW40PLUS },
357                 { IEEE80211A, 117,  40,  48,  8, BW40MINUS },
358                 { IEEE80211A, 124, 149, 161,  4, BW20 },
359                 { IEEE80211A, 125, 149, 169,  4, BW20 },
360                 { IEEE80211A, 126, 149, 157,  8, BW40PLUS },
361                 { IEEE80211A, 127, 153, 161,  8, BW40MINUS },
362                 { -1, 0, 0, 0, 0, BW20 }
363         };
364
365         int cla, op;
366
367         cla = 0;
368
369         for (op = 0; op_class[op].op_class; op++) {
370                 u8 ch;
371                 struct p2p_oper_class_map *o = &op_class[op];
372                 struct p2p_reg_class *reg = NULL;
373
374                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
375                         if (!has_channel(channel_set, chanset_size, ch)) {
376                                 continue;
377                         }
378
379                         if ((0 == padapter->registrypriv.ht_enable) && (8 == o->inc))
380                                 continue;
381
382                         if ((0 < (padapter->registrypriv.bw_mode & 0xf0)) &&
383                                 ((BW40MINUS == o->bw) || (BW40PLUS == o->bw)))
384                                 continue;
385
386                         if (reg == NULL) {
387                                 reg = &channel_list->reg_class[cla];
388                                 cla++;
389                                 reg->reg_class = o->op_class;
390                                 reg->channels = 0;
391                         }
392                         reg->channel[reg->channels] = ch;
393                         reg->channels++;
394                 }
395         }
396         channel_list->reg_classes = cla;
397
398 }
399
400 static u8 init_channel_set(struct adapter *padapter, u8 ChannelPlan, RT_CHANNEL_INFO *channel_set)
401 {
402         u8 index, chanset_size = 0;
403         u8 b5GBand = false, b2_4GBand = false;
404         u8 Index2G = 0, Index5G = 0;
405
406         memset(channel_set, 0, sizeof(RT_CHANNEL_INFO)*MAX_CHANNEL_NUM);
407
408         if (ChannelPlan >= RT_CHANNEL_DOMAIN_MAX && ChannelPlan != RT_CHANNEL_DOMAIN_REALTEK_DEFINE) {
409                 DBG_871X("ChannelPlan ID %x error !!!!!\n", ChannelPlan);
410                 return chanset_size;
411         }
412
413         if (IsSupported24G(padapter->registrypriv.wireless_mode)) {
414                 b2_4GBand = true;
415                 if (RT_CHANNEL_DOMAIN_REALTEK_DEFINE == ChannelPlan)
416                         Index2G = RTW_CHANNEL_PLAN_MAP_REALTEK_DEFINE.Index2G;
417                 else
418                         Index2G = RTW_ChannelPlanMap[ChannelPlan].Index2G;
419         }
420
421         if (b2_4GBand) {
422                 for (index = 0; index < RTW_ChannelPlan2G[Index2G].Len; index++) {
423                         channel_set[chanset_size].ChannelNum = RTW_ChannelPlan2G[Index2G].Channel[index];
424
425                         if ((RT_CHANNEL_DOMAIN_GLOBAL_DOAMIN == ChannelPlan) ||/* Channel 1~11 is active, and 12~14 is passive */
426                                 (RT_CHANNEL_DOMAIN_GLOBAL_NULL == ChannelPlan)) {
427                                 if (channel_set[chanset_size].ChannelNum >= 1 && channel_set[chanset_size].ChannelNum <= 11)
428                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
429                                 else if ((channel_set[chanset_size].ChannelNum  >= 12 && channel_set[chanset_size].ChannelNum  <= 14))
430                                         channel_set[chanset_size].ScanType  = SCAN_PASSIVE;
431                         } else if (RT_CHANNEL_DOMAIN_WORLD_WIDE_13 == ChannelPlan ||
432                                 RT_CHANNEL_DOMAIN_WORLD_WIDE_5G == ChannelPlan ||
433                                 RT_CHANNEL_DOMAIN_2G_WORLD == Index2G) { /*  channel 12~13, passive scan */
434                                 if (channel_set[chanset_size].ChannelNum <= 11)
435                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
436                                 else
437                                         channel_set[chanset_size].ScanType = SCAN_PASSIVE;
438                         } else
439                                 channel_set[chanset_size].ScanType = SCAN_ACTIVE;
440
441                         chanset_size++;
442                 }
443         }
444
445         if (b5GBand) {
446                 for (index = 0; index < RTW_ChannelPlan5G[Index5G].Len; index++) {
447                         if (RTW_ChannelPlan5G[Index5G].Channel[index] <= 48
448                                 || RTW_ChannelPlan5G[Index5G].Channel[index] >= 149) {
449                                 channel_set[chanset_size].ChannelNum = RTW_ChannelPlan5G[Index5G].Channel[index];
450                                 if (RT_CHANNEL_DOMAIN_WORLD_WIDE_5G == ChannelPlan)/* passive scan for all 5G channels */
451                                         channel_set[chanset_size].ScanType = SCAN_PASSIVE;
452                                 else
453                                         channel_set[chanset_size].ScanType = SCAN_ACTIVE;
454                                 DBG_871X("%s(): channel_set[%d].ChannelNum = %d\n", __func__, chanset_size, channel_set[chanset_size].ChannelNum);
455                                 chanset_size++;
456                         }
457                 }
458         }
459
460         DBG_871X("%s ChannelPlan ID %x Chan num:%d \n", __func__, ChannelPlan, chanset_size);
461         return chanset_size;
462 }
463
464 int     init_mlme_ext_priv(struct adapter *padapter)
465 {
466         int     res = _SUCCESS;
467         struct registry_priv *pregistrypriv = &padapter->registrypriv;
468         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
469         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
470         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
471
472         pmlmeext->padapter = padapter;
473
474         /* fill_fwpriv(padapter, &(pmlmeext->fwpriv)); */
475
476         init_mlme_ext_priv_value(padapter);
477         pmlmeinfo->accept_addba_req = pregistrypriv->accept_addba_req;
478
479         init_mlme_ext_timer(padapter);
480
481         init_mlme_ap_info(padapter);
482
483         pmlmeext->max_chan_nums = init_channel_set(padapter, pmlmepriv->ChannelPlan, pmlmeext->channel_set);
484         init_channel_list(padapter, pmlmeext->channel_set, pmlmeext->max_chan_nums, &pmlmeext->channel_list);
485         pmlmeext->last_scan_time = 0;
486         pmlmeext->chan_scan_time = SURVEY_TO;
487         pmlmeext->mlmeext_init = true;
488         pmlmeext->active_keep_alive_check = true;
489
490 #ifdef DBG_FIXED_CHAN
491         pmlmeext->fixed_chan = 0xFF;
492 #endif
493
494         return res;
495
496 }
497
498 void free_mlme_ext_priv(struct mlme_ext_priv *pmlmeext)
499 {
500         struct adapter *padapter = pmlmeext->padapter;
501
502         if (!padapter)
503                 return;
504
505         if (padapter->bDriverStopped) {
506                 del_timer_sync(&pmlmeext->survey_timer);
507                 del_timer_sync(&pmlmeext->link_timer);
508                 /* del_timer_sync(&pmlmeext->ADDBA_timer); */
509         }
510 }
511
512 static void _mgt_dispatcher(struct adapter *padapter, struct mlme_handler *ptable, union recv_frame *precv_frame)
513 {
514         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
515         u8 *pframe = precv_frame->u.hdr.rx_data;
516
517         if (ptable->func) {
518                 /* receive the frames that ra(a1) is my address or ra(a1) is bc address. */
519                 if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN) &&
520                     memcmp(GetAddr1Ptr(pframe), bc_addr, ETH_ALEN))
521                         return;
522
523                 ptable->func(padapter, precv_frame);
524         }
525 }
526
527 void mgt_dispatcher(struct adapter *padapter, union recv_frame *precv_frame)
528 {
529         int index;
530         struct mlme_handler *ptable;
531         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
532         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
533         u8 *pframe = precv_frame->u.hdr.rx_data;
534         struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, GetAddr2Ptr(pframe));
535         struct dvobj_priv *psdpriv = padapter->dvobj;
536         struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
537
538         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
539                  ("+mgt_dispatcher: type(0x%x) subtype(0x%x)\n",
540                   GetFrameType(pframe), GetFrameSubType(pframe)));
541
542         if (GetFrameType(pframe) != WIFI_MGT_TYPE) {
543                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("mgt_dispatcher: type(0x%x) error!\n", GetFrameType(pframe)));
544                 return;
545         }
546
547         /* receive the frames that ra(a1) is my address or ra(a1) is bc address. */
548         if (memcmp(GetAddr1Ptr(pframe), myid(&padapter->eeprompriv), ETH_ALEN) &&
549                 memcmp(GetAddr1Ptr(pframe), bc_addr, ETH_ALEN)) {
550                 return;
551         }
552
553         ptable = mlme_sta_tbl;
554
555         index = GetFrameSubType(pframe) >> 4;
556
557         if (index >= ARRAY_SIZE(mlme_sta_tbl)) {
558                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("Currently we do not support reserved sub-fr-type =%d\n", index));
559                 return;
560         }
561         ptable += index;
562
563         if (psta != NULL) {
564                 if (GetRetry(pframe)) {
565                         if (precv_frame->u.hdr.attrib.seq_num == psta->RxMgmtFrameSeqNum) {
566                                 /* drop the duplicate management frame */
567                                 pdbgpriv->dbg_rx_dup_mgt_frame_drop_count++;
568                                 DBG_871X("Drop duplicate management frame with seq_num = %d.\n", precv_frame->u.hdr.attrib.seq_num);
569                                 return;
570                         }
571                 }
572                 psta->RxMgmtFrameSeqNum = precv_frame->u.hdr.attrib.seq_num;
573         }
574
575         switch (GetFrameSubType(pframe)) {
576         case WIFI_AUTH:
577                 if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
578                         ptable->func = &OnAuth;
579                 else
580                         ptable->func = &OnAuthClient;
581                 /* fall through */
582         case WIFI_ASSOCREQ:
583         case WIFI_REASSOCREQ:
584                 _mgt_dispatcher(padapter, ptable, precv_frame);
585                 break;
586         case WIFI_PROBEREQ:
587                 _mgt_dispatcher(padapter, ptable, precv_frame);
588                 break;
589         case WIFI_BEACON:
590                 _mgt_dispatcher(padapter, ptable, precv_frame);
591                 break;
592         case WIFI_ACTION:
593                 /* if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) */
594                 _mgt_dispatcher(padapter, ptable, precv_frame);
595                 break;
596         default:
597                 _mgt_dispatcher(padapter, ptable, precv_frame);
598                 break;
599         }
600 }
601
602 /****************************************************************************
603
604 Following are the callback functions for each subtype of the management frames
605
606 *****************************************************************************/
607
608 unsigned int OnProbeReq(struct adapter *padapter, union recv_frame *precv_frame)
609 {
610         unsigned int    ielen;
611         unsigned char *p;
612         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
613         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
614         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
615         struct wlan_bssid_ex    *cur = &(pmlmeinfo->network);
616         u8 *pframe = precv_frame->u.hdr.rx_data;
617         uint len = precv_frame->u.hdr.len;
618         u8 is_valid_p2p_probereq = false;
619
620         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
621                 return _SUCCESS;
622
623         if (check_fwstate(pmlmepriv, _FW_LINKED) == false &&
624                 check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE|WIFI_AP_STATE) == false) {
625                 return _SUCCESS;
626         }
627
628
629         /* DBG_871X("+OnProbeReq\n"); */
630
631 #ifdef CONFIG_AUTO_AP_MODE
632         if (check_fwstate(pmlmepriv, _FW_LINKED) &&
633                         pmlmepriv->cur_network.join_res) {
634                 struct sta_info *psta;
635                 u8 *mac_addr, *peer_addr;
636                 struct sta_priv *pstapriv = &padapter->stapriv;
637                 u8 RC_OUI[4] = {0x00, 0xE0, 0x4C, 0x0A};
638                 /* EID[1] + EID_LEN[1] + RC_OUI[4] + MAC[6] + PairingID[2] + ChannelNum[2] */
639
640                 p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _PROBEREQ_IE_OFFSET_, _VENDOR_SPECIFIC_IE_, (int *)&ielen,
641                         len - WLAN_HDR_A3_LEN - _PROBEREQ_IE_OFFSET_);
642
643                 if (!p || ielen != 14)
644                         goto _non_rc_device;
645
646                 if (memcmp(p+2, RC_OUI, sizeof(RC_OUI)))
647                         goto _non_rc_device;
648
649                 if (memcmp(p+6, get_sa(pframe), ETH_ALEN)) {
650                         DBG_871X("%s, do rc pairing ("MAC_FMT"), but mac addr mismatch!("MAC_FMT")\n", __func__,
651                                 MAC_ARG(get_sa(pframe)), MAC_ARG(p+6));
652
653                         goto _non_rc_device;
654                 }
655
656                 DBG_871X("%s, got the pairing device("MAC_FMT")\n", __func__,  MAC_ARG(get_sa(pframe)));
657
658                 /* new a station */
659                 psta = rtw_get_stainfo(pstapriv, get_sa(pframe));
660                 if (psta == NULL) {
661                         /*  allocate a new one */
662                         DBG_871X("going to alloc stainfo for rc ="MAC_FMT"\n",  MAC_ARG(get_sa(pframe)));
663                         psta = rtw_alloc_stainfo(pstapriv, get_sa(pframe));
664                         if (psta == NULL) {
665                                 /* TODO: */
666                                 DBG_871X(" Exceed the upper limit of supported clients...\n");
667                                 return _SUCCESS;
668                         }
669
670                         spin_lock_bh(&pstapriv->asoc_list_lock);
671                         if (list_empty(&psta->asoc_list)) {
672                                 psta->expire_to = pstapriv->expire_to;
673                                 list_add_tail(&psta->asoc_list, &pstapriv->asoc_list);
674                                 pstapriv->asoc_list_cnt++;
675                         }
676                         spin_unlock_bh(&pstapriv->asoc_list_lock);
677
678                         /* generate pairing ID */
679                         mac_addr = myid(&(padapter->eeprompriv));
680                         peer_addr = psta->hwaddr;
681                         psta->pid = (u16)(((mac_addr[4]<<8) + mac_addr[5]) + ((peer_addr[4]<<8) + peer_addr[5]));
682
683                         /* update peer stainfo */
684                         psta->isrc = true;
685                         /* psta->aid = 0; */
686                         /* psta->mac_id = 2; */
687
688                         /* get a unique AID */
689                         if (psta->aid > 0) {
690                                 DBG_871X("old AID %d\n", psta->aid);
691                         } else {
692                                 for (psta->aid = 1; psta->aid <= NUM_STA; psta->aid++)
693                                         if (pstapriv->sta_aid[psta->aid - 1] == NULL)
694                                                 break;
695
696                                 if (psta->aid > pstapriv->max_num_sta) {
697                                         psta->aid = 0;
698                                         DBG_871X("no room for more AIDs\n");
699                                         return _SUCCESS;
700                                 } else {
701                                         pstapriv->sta_aid[psta->aid - 1] = psta;
702                                         DBG_871X("allocate new AID = (%d)\n", psta->aid);
703                                 }
704                         }
705
706                         psta->qos_option = 1;
707                         psta->bw_mode = CHANNEL_WIDTH_20;
708                         psta->ieee8021x_blocked = false;
709                         psta->htpriv.ht_option = true;
710                         psta->htpriv.ampdu_enable = false;
711                         psta->htpriv.sgi_20m = false;
712                         psta->htpriv.sgi_40m = false;
713                         psta->htpriv.ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
714                         psta->htpriv.agg_enable_bitmap = 0x0;/* reset */
715                         psta->htpriv.candidate_tid_bitmap = 0x0;/* reset */
716
717                         rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, true);
718
719                         memset((void *)&psta->sta_stats, 0, sizeof(struct stainfo_stats));
720
721                         spin_lock_bh(&psta->lock);
722                         psta->state |= _FW_LINKED;
723                         spin_unlock_bh(&psta->lock);
724
725                         report_add_sta_event(padapter, psta->hwaddr, psta->aid);
726
727                 }
728
729                 issue_probersp(padapter, get_sa(pframe), false);
730
731                 return _SUCCESS;
732
733         }
734
735 _non_rc_device:
736
737         return _SUCCESS;
738
739 #endif /* CONFIG_AUTO_AP_MODE */
740
741         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _PROBEREQ_IE_OFFSET_, _SSID_IE_, (int *)&ielen,
742                         len - WLAN_HDR_A3_LEN - _PROBEREQ_IE_OFFSET_);
743
744
745         /* check (wildcard) SSID */
746         if (p != NULL) {
747                 if (is_valid_p2p_probereq)
748                         goto _issue_probersp;
749
750                 if ((ielen != 0 && false == !memcmp((void *)(p+2), (void *)cur->Ssid.Ssid, cur->Ssid.SsidLength))
751                         || (ielen == 0 && pmlmeinfo->hidden_ssid_mode)
752                 )
753                         return _SUCCESS;
754
755 _issue_probersp:
756                 if ((check_fwstate(pmlmepriv, _FW_LINKED)  &&
757                         pmlmepriv->cur_network.join_res) || check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
758                         /* DBG_871X("+issue_probersp during ap mode\n"); */
759                         issue_probersp(padapter, get_sa(pframe), is_valid_p2p_probereq);
760                 }
761
762         }
763
764         return _SUCCESS;
765
766 }
767
768 unsigned int OnProbeRsp(struct adapter *padapter, union recv_frame *precv_frame)
769 {
770         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
771
772         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
773                 report_survey_event(padapter, precv_frame);
774                 return _SUCCESS;
775         }
776
777         return _SUCCESS;
778
779 }
780
781 unsigned int OnBeacon(struct adapter *padapter, union recv_frame *precv_frame)
782 {
783         int cam_idx;
784         struct sta_info *psta;
785         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
786         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
787         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
788         struct sta_priv *pstapriv = &padapter->stapriv;
789         u8 *pframe = precv_frame->u.hdr.rx_data;
790         uint len = precv_frame->u.hdr.len;
791         struct wlan_bssid_ex *pbss;
792         int ret = _SUCCESS;
793         u8 *p = NULL;
794         u32 ielen = 0;
795
796         p = rtw_get_ie(pframe + sizeof(struct ieee80211_hdr_3addr) + _BEACON_IE_OFFSET_, _EXT_SUPPORTEDRATES_IE_, &ielen, precv_frame->u.hdr.len - sizeof(struct ieee80211_hdr_3addr) - _BEACON_IE_OFFSET_);
797         if ((p != NULL) && (ielen > 0)) {
798                 if ((*(p + 1 + ielen) == 0x2D) && (*(p + 2 + ielen) != 0x2D)) {
799                         /* Invalid value 0x2D is detected in Extended Supported Rates (ESR) IE. Try to fix the IE length to avoid failed Beacon parsing. */
800                         DBG_871X("[WIFIDBG] Error in ESR IE is detected in Beacon of BSSID:"MAC_FMT". Fix the length of ESR IE to avoid failed Beacon parsing.\n", MAC_ARG(GetAddr3Ptr(pframe)));
801                         *(p + 1) = ielen - 1;
802                 }
803         }
804
805         if (pmlmeext->sitesurvey_res.state == SCAN_PROCESS) {
806                 report_survey_event(padapter, precv_frame);
807                 return _SUCCESS;
808         }
809
810         if (!memcmp(GetAddr3Ptr(pframe), get_my_bssid(&pmlmeinfo->network), ETH_ALEN)) {
811                 if (pmlmeinfo->state & WIFI_FW_AUTH_NULL) {
812                         /* we should update current network before auth, or some IE is wrong */
813                         pbss = rtw_malloc(sizeof(struct wlan_bssid_ex));
814                         if (pbss) {
815                                 if (collect_bss_info(padapter, precv_frame, pbss) == _SUCCESS) {
816                                         update_network(&(pmlmepriv->cur_network.network), pbss, padapter, true);
817                                         rtw_get_bcn_info(&(pmlmepriv->cur_network));
818                                 }
819                                 kfree((u8 *)pbss);
820                         }
821
822                         /* check the vendor of the assoc AP */
823                         pmlmeinfo->assoc_AP_vendor = check_assoc_AP(pframe+sizeof(struct ieee80211_hdr_3addr), len-sizeof(struct ieee80211_hdr_3addr));
824
825                         /* update TSF Value */
826                         update_TSF(pmlmeext, pframe, len);
827
828                         /* reset for adaptive_early_32k */
829                         pmlmeext->adaptive_tsf_done = false;
830                         pmlmeext->DrvBcnEarly = 0xff;
831                         pmlmeext->DrvBcnTimeOut = 0xff;
832                         pmlmeext->bcn_cnt = 0;
833                         memset(pmlmeext->bcn_delay_cnt, 0, sizeof(pmlmeext->bcn_delay_cnt));
834                         memset(pmlmeext->bcn_delay_ratio, 0, sizeof(pmlmeext->bcn_delay_ratio));
835
836                         /* start auth */
837                         start_clnt_auth(padapter);
838
839                         return _SUCCESS;
840                 }
841
842                 if (((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) && (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)) {
843                         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
844                         if (psta != NULL) {
845                                 ret = rtw_check_bcn_info(padapter, pframe, len);
846                                 if (!ret) {
847                                                 DBG_871X_LEVEL(_drv_always_, "ap has changed, disconnect now\n ");
848                                                 receive_disconnect(padapter, pmlmeinfo->network.MacAddress, 0);
849                                                 return _SUCCESS;
850                                 }
851                                 /* update WMM, ERP in the beacon */
852                                 /* todo: the timer is used instead of the number of the beacon received */
853                                 if ((sta_rx_pkts(psta) & 0xf) == 0)
854                                         /* DBG_871X("update_bcn_info\n"); */
855                                         update_beacon_info(padapter, pframe, len, psta);
856
857                                 adaptive_early_32k(pmlmeext, pframe, len);
858                         }
859                 } else if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
860                         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
861                         if (psta != NULL) {
862                                 /* update WMM, ERP in the beacon */
863                                 /* todo: the timer is used instead of the number of the beacon received */
864                                 if ((sta_rx_pkts(psta) & 0xf) == 0) {
865                                         /* DBG_871X("update_bcn_info\n"); */
866                                         update_beacon_info(padapter, pframe, len, psta);
867                                 }
868                         } else{
869                                 /* allocate a new CAM entry for IBSS station */
870                                 cam_idx = allocate_fw_sta_entry(padapter);
871                                 if (cam_idx == NUM_STA)
872                                         goto _END_ONBEACON_;
873
874                                 /* get supported rate */
875                                 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) {
876                                         pmlmeinfo->FW_sta_info[cam_idx].status = 0;
877                                         goto _END_ONBEACON_;
878                                 }
879
880                                 /* update TSF Value */
881                                 update_TSF(pmlmeext, pframe, len);
882
883                                 /* report sta add event */
884                                 report_add_sta_event(padapter, GetAddr2Ptr(pframe), cam_idx);
885                         }
886                 }
887         }
888
889 _END_ONBEACON_:
890
891         return _SUCCESS;
892
893 }
894
895 unsigned int OnAuth(struct adapter *padapter, union recv_frame *precv_frame)
896 {
897         unsigned int    auth_mode, seq, ie_len;
898         unsigned char *sa, *p;
899         u16 algorithm;
900         int     status;
901         static struct sta_info stat;
902         struct  sta_info *pstat = NULL;
903         struct  sta_priv *pstapriv = &padapter->stapriv;
904         struct security_priv *psecuritypriv = &padapter->securitypriv;
905         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
906         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
907         u8 *pframe = precv_frame->u.hdr.rx_data;
908         uint len = precv_frame->u.hdr.len;
909         u8 offset = 0;
910
911         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
912                 return _FAIL;
913
914         DBG_871X("+OnAuth\n");
915
916         sa = GetAddr2Ptr(pframe);
917
918         auth_mode = psecuritypriv->dot11AuthAlgrthm;
919
920         if (GetPrivacy(pframe)) {
921                 u8 *iv;
922                 struct rx_pkt_attrib     *prxattrib = &(precv_frame->u.hdr.attrib);
923
924                 prxattrib->hdrlen = WLAN_HDR_A3_LEN;
925                 prxattrib->encrypt = _WEP40_;
926
927                 iv = pframe+prxattrib->hdrlen;
928                 prxattrib->key_index = ((iv[3]>>6)&0x3);
929
930                 prxattrib->iv_len = 4;
931                 prxattrib->icv_len = 4;
932
933                 rtw_wep_decrypt(padapter, (u8 *)precv_frame);
934
935                 offset = 4;
936         }
937
938         algorithm = le16_to_cpu(*(__le16 *)((SIZE_PTR)pframe + WLAN_HDR_A3_LEN + offset));
939         seq     = le16_to_cpu(*(__le16 *)((SIZE_PTR)pframe + WLAN_HDR_A3_LEN + offset + 2));
940
941         DBG_871X("auth alg =%x, seq =%X\n", algorithm, seq);
942
943         if (auth_mode == 2 &&
944                         psecuritypriv->dot11PrivacyAlgrthm != _WEP40_ &&
945                         psecuritypriv->dot11PrivacyAlgrthm != _WEP104_)
946                 auth_mode = 0;
947
948         if ((algorithm > 0 && auth_mode == 0) ||        /*  rx a shared-key auth but shared not enabled */
949                 (algorithm == 0 && auth_mode == 1)) {   /*  rx a open-system auth but shared-key is enabled */
950                 DBG_871X("auth rejected due to bad alg [alg =%d, auth_mib =%d] %02X%02X%02X%02X%02X%02X\n",
951                         algorithm, auth_mode, sa[0], sa[1], sa[2], sa[3], sa[4], sa[5]);
952
953                 status = _STATS_NO_SUPP_ALG_;
954
955                 goto auth_fail;
956         }
957
958         if (rtw_access_ctrl(padapter, sa) == false) {
959                 status = _STATS_UNABLE_HANDLE_STA_;
960                 goto auth_fail;
961         }
962
963         pstat = rtw_get_stainfo(pstapriv, sa);
964         if (pstat == NULL) {
965
966                 /*  allocate a new one */
967                 DBG_871X("going to alloc stainfo for sa ="MAC_FMT"\n",  MAC_ARG(sa));
968                 pstat = rtw_alloc_stainfo(pstapriv, sa);
969                 if (pstat == NULL) {
970                         DBG_871X(" Exceed the upper limit of supported clients...\n");
971                         status = _STATS_UNABLE_HANDLE_STA_;
972                         goto auth_fail;
973                 }
974
975                 pstat->state = WIFI_FW_AUTH_NULL;
976                 pstat->auth_seq = 0;
977
978                 /* pstat->flags = 0; */
979                 /* pstat->capability = 0; */
980         } else{
981
982                 spin_lock_bh(&pstapriv->asoc_list_lock);
983                 if (list_empty(&pstat->asoc_list) == false) {
984                         list_del_init(&pstat->asoc_list);
985                         pstapriv->asoc_list_cnt--;
986                         if (pstat->expire_to > 0) {
987                                 /* TODO: STA re_auth within expire_to */
988                         }
989                 }
990                 spin_unlock_bh(&pstapriv->asoc_list_lock);
991
992                 if (seq == 1) {
993                         /* TODO: STA re_auth and auth timeout */
994                 }
995         }
996
997         spin_lock_bh(&pstapriv->auth_list_lock);
998         if (list_empty(&pstat->auth_list)) {
999
1000                 list_add_tail(&pstat->auth_list, &pstapriv->auth_list);
1001                 pstapriv->auth_list_cnt++;
1002         }
1003         spin_unlock_bh(&pstapriv->auth_list_lock);
1004
1005         if (pstat->auth_seq == 0)
1006                 pstat->expire_to = pstapriv->auth_to;
1007
1008
1009         if ((pstat->auth_seq + 1) != seq) {
1010                 DBG_871X("(1)auth rejected because out of seq [rx_seq =%d, exp_seq =%d]!\n",
1011                         seq, pstat->auth_seq+1);
1012                 status = _STATS_OUT_OF_AUTH_SEQ_;
1013                 goto auth_fail;
1014         }
1015
1016         if (algorithm == 0 && (auth_mode == 0 || auth_mode == 2 || auth_mode == 3)) {
1017                 if (seq == 1) {
1018                         pstat->state &= ~WIFI_FW_AUTH_NULL;
1019                         pstat->state |= WIFI_FW_AUTH_SUCCESS;
1020                         pstat->expire_to = pstapriv->assoc_to;
1021                         pstat->authalg = algorithm;
1022                 } else{
1023                         DBG_871X("(2)auth rejected because out of seq [rx_seq =%d, exp_seq =%d]!\n",
1024                                 seq, pstat->auth_seq+1);
1025                         status = _STATS_OUT_OF_AUTH_SEQ_;
1026                         goto auth_fail;
1027                 }
1028         } else{ /*  shared system or auto authentication */
1029                 if (seq == 1) {
1030                         /* prepare for the challenging txt... */
1031                         memset((void *)pstat->chg_txt, 78, 128);
1032
1033                         pstat->state &= ~WIFI_FW_AUTH_NULL;
1034                         pstat->state |= WIFI_FW_AUTH_STATE;
1035                         pstat->authalg = algorithm;
1036                         pstat->auth_seq = 2;
1037                 } else if (seq == 3) {
1038                         /* checking for challenging txt... */
1039                         DBG_871X("checking for challenging txt...\n");
1040
1041                         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + 4 + _AUTH_IE_OFFSET_, _CHLGETXT_IE_, (int *)&ie_len,
1042                                         len - WLAN_HDR_A3_LEN - _AUTH_IE_OFFSET_ - 4);
1043
1044                         if ((p == NULL) || (ie_len <= 0)) {
1045                                 DBG_871X("auth rejected because challenge failure!(1)\n");
1046                                 status = _STATS_CHALLENGE_FAIL_;
1047                                 goto auth_fail;
1048                         }
1049
1050                         if (!memcmp((void *)(p + 2), pstat->chg_txt, 128)) {
1051                                 pstat->state &= (~WIFI_FW_AUTH_STATE);
1052                                 pstat->state |= WIFI_FW_AUTH_SUCCESS;
1053                                 /*  challenging txt is correct... */
1054                                 pstat->expire_to =  pstapriv->assoc_to;
1055                         } else{
1056                                 DBG_871X("auth rejected because challenge failure!\n");
1057                                 status = _STATS_CHALLENGE_FAIL_;
1058                                 goto auth_fail;
1059                         }
1060                 } else{
1061                         DBG_871X("(3)auth rejected because out of seq [rx_seq =%d, exp_seq =%d]!\n",
1062                                 seq, pstat->auth_seq+1);
1063                         status = _STATS_OUT_OF_AUTH_SEQ_;
1064                         goto auth_fail;
1065                 }
1066         }
1067
1068
1069         /*  Now, we are going to issue_auth... */
1070         pstat->auth_seq = seq + 1;
1071
1072         issue_auth(padapter, pstat, (unsigned short)(_STATS_SUCCESSFUL_));
1073
1074         if (pstat->state & WIFI_FW_AUTH_SUCCESS)
1075                 pstat->auth_seq = 0;
1076
1077
1078         return _SUCCESS;
1079
1080 auth_fail:
1081
1082         if (pstat)
1083                 rtw_free_stainfo(padapter, pstat);
1084
1085         pstat = &stat;
1086         memset((char *)pstat, '\0', sizeof(stat));
1087         pstat->auth_seq = 2;
1088         memcpy(pstat->hwaddr, sa, 6);
1089
1090         issue_auth(padapter, pstat, (unsigned short)status);
1091
1092         return _FAIL;
1093
1094 }
1095
1096 unsigned int OnAuthClient(struct adapter *padapter, union recv_frame *precv_frame)
1097 {
1098         unsigned int    seq, len, status, algthm, offset;
1099         unsigned char *p;
1100         unsigned int    go2asoc = 0;
1101         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1102         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1103         u8 *pframe = precv_frame->u.hdr.rx_data;
1104         uint pkt_len = precv_frame->u.hdr.len;
1105
1106         DBG_871X("%s\n", __func__);
1107
1108         /* check A1 matches or not */
1109         if (memcmp(myid(&(padapter->eeprompriv)), get_da(pframe), ETH_ALEN))
1110                 return _SUCCESS;
1111
1112         if (!(pmlmeinfo->state & WIFI_FW_AUTH_STATE))
1113                 return _SUCCESS;
1114
1115         offset = (GetPrivacy(pframe)) ? 4 : 0;
1116
1117         algthm  = le16_to_cpu(*(__le16 *)((SIZE_PTR)pframe + WLAN_HDR_A3_LEN + offset));
1118         seq     = le16_to_cpu(*(__le16 *)((SIZE_PTR)pframe + WLAN_HDR_A3_LEN + offset + 2));
1119         status  = le16_to_cpu(*(__le16 *)((SIZE_PTR)pframe + WLAN_HDR_A3_LEN + offset + 4));
1120
1121         if (status != 0) {
1122                 DBG_871X("clnt auth fail, status: %d\n", status);
1123                 if (status == 13) { /*  pmlmeinfo->auth_algo == dot11AuthAlgrthm_Auto) */
1124                         if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared)
1125                                 pmlmeinfo->auth_algo = dot11AuthAlgrthm_Open;
1126                         else
1127                                 pmlmeinfo->auth_algo = dot11AuthAlgrthm_Shared;
1128                         /* pmlmeinfo->reauth_count = 0; */
1129                 }
1130
1131                 set_link_timer(pmlmeext, 1);
1132                 goto authclnt_fail;
1133         }
1134
1135         if (seq == 2) {
1136                 if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared) {
1137                          /*  legendary shared system */
1138                         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + _AUTH_IE_OFFSET_, _CHLGETXT_IE_, (int *)&len,
1139                                 pkt_len - WLAN_HDR_A3_LEN - _AUTH_IE_OFFSET_);
1140
1141                         if (p == NULL) {
1142                                 /* DBG_871X("marc: no challenge text?\n"); */
1143                                 goto authclnt_fail;
1144                         }
1145
1146                         memcpy((void *)(pmlmeinfo->chg_txt), (void *)(p + 2), len);
1147                         pmlmeinfo->auth_seq = 3;
1148                         issue_auth(padapter, NULL, 0);
1149                         set_link_timer(pmlmeext, REAUTH_TO);
1150
1151                         return _SUCCESS;
1152                 } else{
1153                         /*  open system */
1154                         go2asoc = 1;
1155                 }
1156         } else if (seq == 4) {
1157                 if (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared) {
1158                         go2asoc = 1;
1159                 } else{
1160                         goto authclnt_fail;
1161                 }
1162         } else{
1163                 /*  this is also illegal */
1164                 /* DBG_871X("marc: clnt auth failed due to illegal seq =%x\n", seq); */
1165                 goto authclnt_fail;
1166         }
1167
1168         if (go2asoc) {
1169                 DBG_871X_LEVEL(_drv_always_, "auth success, start assoc\n");
1170                 start_clnt_assoc(padapter);
1171                 return _SUCCESS;
1172         }
1173
1174 authclnt_fail:
1175
1176         /* pmlmeinfo->state &= ~(WIFI_FW_AUTH_STATE); */
1177
1178         return _FAIL;
1179
1180 }
1181
1182 unsigned int OnAssocReq(struct adapter *padapter, union recv_frame *precv_frame)
1183 {
1184         u16 capab_info, listen_interval;
1185         struct rtw_ieee802_11_elems elems;
1186         struct sta_info *pstat;
1187         unsigned char   reassoc, *p, *pos, *wpa_ie;
1188         unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01};
1189         int             i, ie_len, wpa_ie_len, left;
1190         unsigned char   supportRate[16];
1191         int                                     supportRateNum;
1192         unsigned short          status = _STATS_SUCCESSFUL_;
1193         unsigned short          frame_type, ie_offset = 0;
1194         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1195         struct security_priv *psecuritypriv = &padapter->securitypriv;
1196         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1197         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1198         struct wlan_bssid_ex    *cur = &(pmlmeinfo->network);
1199         struct sta_priv *pstapriv = &padapter->stapriv;
1200         u8 *pframe = precv_frame->u.hdr.rx_data;
1201         uint pkt_len = precv_frame->u.hdr.len;
1202
1203         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
1204                 return _FAIL;
1205
1206         frame_type = GetFrameSubType(pframe);
1207         if (frame_type == WIFI_ASSOCREQ) {
1208                 reassoc = 0;
1209                 ie_offset = _ASOCREQ_IE_OFFSET_;
1210         } else{ /*  WIFI_REASSOCREQ */
1211                 reassoc = 1;
1212                 ie_offset = _REASOCREQ_IE_OFFSET_;
1213         }
1214
1215
1216         if (pkt_len < sizeof(struct ieee80211_hdr_3addr) + ie_offset) {
1217                 DBG_871X("handle_assoc(reassoc =%d) - too short payload (len =%lu)"
1218                        "\n", reassoc, (unsigned long)pkt_len);
1219                 return _FAIL;
1220         }
1221
1222         pstat = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1223         if (pstat == NULL) {
1224                 status = _RSON_CLS2_;
1225                 goto asoc_class2_error;
1226         }
1227
1228         capab_info = RTW_GET_LE16(pframe + WLAN_HDR_A3_LEN);
1229         /* capab_info = le16_to_cpu(*(unsigned short *)(pframe + WLAN_HDR_A3_LEN)); */
1230         /* listen_interval = le16_to_cpu(*(unsigned short *)(pframe + WLAN_HDR_A3_LEN+2)); */
1231         listen_interval = RTW_GET_LE16(pframe + WLAN_HDR_A3_LEN+2);
1232
1233         left = pkt_len - (sizeof(struct ieee80211_hdr_3addr) + ie_offset);
1234         pos = pframe + (sizeof(struct ieee80211_hdr_3addr) + ie_offset);
1235
1236
1237         DBG_871X("%s\n", __func__);
1238
1239         /*  check if this stat has been successfully authenticated/assocated */
1240         if (!((pstat->state) & WIFI_FW_AUTH_SUCCESS)) {
1241                 if (!((pstat->state) & WIFI_FW_ASSOC_SUCCESS)) {
1242                         status = _RSON_CLS2_;
1243                         goto asoc_class2_error;
1244                 } else{
1245                         pstat->state &= (~WIFI_FW_ASSOC_SUCCESS);
1246                         pstat->state |= WIFI_FW_ASSOC_STATE;
1247                 }
1248         } else{
1249                 pstat->state &= (~WIFI_FW_AUTH_SUCCESS);
1250                 pstat->state |= WIFI_FW_ASSOC_STATE;
1251         }
1252
1253
1254         pstat->capability = capab_info;
1255
1256         /* now parse all ieee802_11 ie to point to elems */
1257         if (rtw_ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
1258             !elems.ssid) {
1259                 DBG_871X("STA " MAC_FMT " sent invalid association request\n",
1260                        MAC_ARG(pstat->hwaddr));
1261                 status = _STATS_FAILURE_;
1262                 goto OnAssocReqFail;
1263         }
1264
1265
1266         /*  now we should check all the fields... */
1267         /*  checking SSID */
1268         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _SSID_IE_, &ie_len,
1269                 pkt_len - WLAN_HDR_A3_LEN - ie_offset);
1270         if (p == NULL) {
1271                 status = _STATS_FAILURE_;
1272         }
1273
1274         if (ie_len == 0) /*  broadcast ssid, however it is not allowed in assocreq */
1275                 status = _STATS_FAILURE_;
1276         else {
1277                 /*  check if ssid match */
1278                 if (memcmp((void *)(p+2), cur->Ssid.Ssid, cur->Ssid.SsidLength))
1279                         status = _STATS_FAILURE_;
1280
1281                 if (ie_len != cur->Ssid.SsidLength)
1282                         status = _STATS_FAILURE_;
1283         }
1284
1285         if (_STATS_SUCCESSFUL_ != status)
1286                 goto OnAssocReqFail;
1287
1288         /*  check if the supported rate is ok */
1289         p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _SUPPORTEDRATES_IE_, &ie_len, pkt_len - WLAN_HDR_A3_LEN - ie_offset);
1290         if (p == NULL) {
1291                 DBG_871X("Rx a sta assoc-req which supported rate is empty!\n");
1292                 /*  use our own rate set as statoin used */
1293                 /* memcpy(supportRate, AP_BSSRATE, AP_BSSRATE_LEN); */
1294                 /* supportRateNum = AP_BSSRATE_LEN; */
1295
1296                 status = _STATS_FAILURE_;
1297                 goto OnAssocReqFail;
1298         } else {
1299                 memcpy(supportRate, p+2, ie_len);
1300                 supportRateNum = ie_len;
1301
1302                 p = rtw_get_ie(pframe + WLAN_HDR_A3_LEN + ie_offset, _EXT_SUPPORTEDRATES_IE_, &ie_len,
1303                                 pkt_len - WLAN_HDR_A3_LEN - ie_offset);
1304                 if (p !=  NULL) {
1305
1306                         if (supportRateNum <= sizeof(supportRate)) {
1307                                 memcpy(supportRate+supportRateNum, p+2, ie_len);
1308                                 supportRateNum += ie_len;
1309                         }
1310                 }
1311         }
1312
1313         /* todo: mask supportRate between AP & STA -> move to update raid */
1314         /* get_matched_rate(pmlmeext, supportRate, &supportRateNum, 0); */
1315
1316         /* update station supportRate */
1317         pstat->bssratelen = supportRateNum;
1318         memcpy(pstat->bssrateset, supportRate, supportRateNum);
1319         UpdateBrateTblForSoftAP(pstat->bssrateset, pstat->bssratelen);
1320
1321         /* check RSN/WPA/WPS */
1322         pstat->dot8021xalg = 0;
1323         pstat->wpa_psk = 0;
1324         pstat->wpa_group_cipher = 0;
1325         pstat->wpa2_group_cipher = 0;
1326         pstat->wpa_pairwise_cipher = 0;
1327         pstat->wpa2_pairwise_cipher = 0;
1328         memset(pstat->wpa_ie, 0, sizeof(pstat->wpa_ie));
1329         if ((psecuritypriv->wpa_psk & BIT(1)) && elems.rsn_ie) {
1330
1331                 int group_cipher = 0, pairwise_cipher = 0;
1332
1333                 wpa_ie = elems.rsn_ie;
1334                 wpa_ie_len = elems.rsn_ie_len;
1335
1336                 if (rtw_parse_wpa2_ie(wpa_ie-2, wpa_ie_len+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1337                         pstat->dot8021xalg = 1;/* psk,  todo:802.1x */
1338                         pstat->wpa_psk |= BIT(1);
1339
1340                         pstat->wpa2_group_cipher = group_cipher&psecuritypriv->wpa2_group_cipher;
1341                         pstat->wpa2_pairwise_cipher = pairwise_cipher&psecuritypriv->wpa2_pairwise_cipher;
1342
1343                         if (!pstat->wpa2_group_cipher)
1344                                 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1345
1346                         if (!pstat->wpa2_pairwise_cipher)
1347                                 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1348                 } else{
1349                         status = WLAN_STATUS_INVALID_IE;
1350                 }
1351
1352         } else if ((psecuritypriv->wpa_psk & BIT(0)) && elems.wpa_ie) {
1353
1354                 int group_cipher = 0, pairwise_cipher = 0;
1355
1356                 wpa_ie = elems.wpa_ie;
1357                 wpa_ie_len = elems.wpa_ie_len;
1358
1359                 if (rtw_parse_wpa_ie(wpa_ie-2, wpa_ie_len+2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1360                         pstat->dot8021xalg = 1;/* psk,  todo:802.1x */
1361                         pstat->wpa_psk |= BIT(0);
1362
1363                         pstat->wpa_group_cipher = group_cipher&psecuritypriv->wpa_group_cipher;
1364                         pstat->wpa_pairwise_cipher = pairwise_cipher&psecuritypriv->wpa_pairwise_cipher;
1365
1366                         if (!pstat->wpa_group_cipher)
1367                                 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1368
1369                         if (!pstat->wpa_pairwise_cipher)
1370                                 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1371
1372                 } else{
1373                         status = WLAN_STATUS_INVALID_IE;
1374                 }
1375
1376         } else {
1377                 wpa_ie = NULL;
1378                 wpa_ie_len = 0;
1379         }
1380
1381         if (_STATS_SUCCESSFUL_ != status)
1382                 goto OnAssocReqFail;
1383
1384         pstat->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
1385         if (wpa_ie == NULL) {
1386                 if (elems.wps_ie) {
1387                         DBG_871X("STA included WPS IE in "
1388                                    "(Re)Association Request - assume WPS is "
1389                                    "used\n");
1390                         pstat->flags |= WLAN_STA_WPS;
1391                         /* wpabuf_free(sta->wps_ie); */
1392                         /* sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4, */
1393                         /*                              elems.wps_ie_len - 4); */
1394                 } else {
1395                         DBG_871X("STA did not include WPA/RSN IE "
1396                                    "in (Re)Association Request - possible WPS "
1397                                    "use\n");
1398                         pstat->flags |= WLAN_STA_MAYBE_WPS;
1399                 }
1400
1401
1402                 /*  AP support WPA/RSN, and sta is going to do WPS, but AP is not ready */
1403                 /*  that the selected registrar of AP is _FLASE */
1404                 if ((psecuritypriv->wpa_psk > 0)
1405                         && (pstat->flags & (WLAN_STA_WPS|WLAN_STA_MAYBE_WPS))) {
1406                         if (pmlmepriv->wps_beacon_ie) {
1407                                 u8 selected_registrar = 0;
1408
1409                                 rtw_get_wps_attr_content(pmlmepriv->wps_beacon_ie, pmlmepriv->wps_beacon_ie_len, WPS_ATTR_SELECTED_REGISTRAR, &selected_registrar, NULL);
1410
1411                                 if (!selected_registrar) {
1412                                         DBG_871X("selected_registrar is false , or AP is not ready to do WPS\n");
1413
1414                                         status = _STATS_UNABLE_HANDLE_STA_;
1415
1416                                         goto OnAssocReqFail;
1417                                 }
1418                         }
1419                 }
1420
1421         } else{
1422                 int copy_len;
1423
1424                 if (psecuritypriv->wpa_psk == 0) {
1425                         DBG_871X("STA " MAC_FMT ": WPA/RSN IE in association "
1426                         "request, but AP don't support WPA/RSN\n", MAC_ARG(pstat->hwaddr));
1427
1428                         status = WLAN_STATUS_INVALID_IE;
1429
1430                         goto OnAssocReqFail;
1431
1432                 }
1433
1434                 if (elems.wps_ie) {
1435                         DBG_871X("STA included WPS IE in "
1436                                    "(Re)Association Request - WPS is "
1437                                    "used\n");
1438                         pstat->flags |= WLAN_STA_WPS;
1439                         copy_len = 0;
1440                 } else{
1441                         copy_len = ((wpa_ie_len+2) > sizeof(pstat->wpa_ie)) ? (sizeof(pstat->wpa_ie)):(wpa_ie_len+2);
1442                 }
1443
1444
1445                 if (copy_len > 0)
1446                         memcpy(pstat->wpa_ie, wpa_ie-2, copy_len);
1447
1448         }
1449
1450
1451         /*  check if there is WMM IE & support WWM-PS */
1452         pstat->flags &= ~WLAN_STA_WME;
1453         pstat->qos_option = 0;
1454         pstat->qos_info = 0;
1455         pstat->has_legacy_ac = true;
1456         pstat->uapsd_vo = 0;
1457         pstat->uapsd_vi = 0;
1458         pstat->uapsd_be = 0;
1459         pstat->uapsd_bk = 0;
1460         if (pmlmepriv->qospriv.qos_option) {
1461                 p = pframe + WLAN_HDR_A3_LEN + ie_offset; ie_len = 0;
1462                 for (;;) {
1463                         p = rtw_get_ie(p, _VENDOR_SPECIFIC_IE_, &ie_len, pkt_len - WLAN_HDR_A3_LEN - ie_offset);
1464                         if (p != NULL) {
1465                                 if (!memcmp(p+2, WMM_IE, 6)) {
1466
1467                                         pstat->flags |= WLAN_STA_WME;
1468
1469                                         pstat->qos_option = 1;
1470                                         pstat->qos_info = *(p+8);
1471
1472                                         pstat->max_sp_len = (pstat->qos_info>>5)&0x3;
1473
1474                                         if ((pstat->qos_info&0xf) != 0xf)
1475                                                 pstat->has_legacy_ac = true;
1476                                         else
1477                                                 pstat->has_legacy_ac = false;
1478
1479                                         if (pstat->qos_info&0xf) {
1480                                                 if (pstat->qos_info&BIT(0))
1481                                                         pstat->uapsd_vo = BIT(0)|BIT(1);
1482                                                 else
1483                                                         pstat->uapsd_vo = 0;
1484
1485                                                 if (pstat->qos_info&BIT(1))
1486                                                         pstat->uapsd_vi = BIT(0)|BIT(1);
1487                                                 else
1488                                                         pstat->uapsd_vi = 0;
1489
1490                                                 if (pstat->qos_info&BIT(2))
1491                                                         pstat->uapsd_bk = BIT(0)|BIT(1);
1492                                                 else
1493                                                         pstat->uapsd_bk = 0;
1494
1495                                                 if (pstat->qos_info&BIT(3))
1496                                                         pstat->uapsd_be = BIT(0)|BIT(1);
1497                                                 else
1498                                                         pstat->uapsd_be = 0;
1499
1500                                         }
1501
1502                                         break;
1503                                 }
1504                         } else {
1505                                 break;
1506                         }
1507                         p = p + ie_len + 2;
1508                 }
1509         }
1510
1511         /* save HT capabilities in the sta object */
1512         memset(&pstat->htpriv.ht_cap, 0, sizeof(struct rtw_ieee80211_ht_cap));
1513         if (elems.ht_capabilities && elems.ht_capabilities_len >= sizeof(struct rtw_ieee80211_ht_cap)) {
1514                 pstat->flags |= WLAN_STA_HT;
1515
1516                 pstat->flags |= WLAN_STA_WME;
1517
1518                 memcpy(&pstat->htpriv.ht_cap, elems.ht_capabilities, sizeof(struct rtw_ieee80211_ht_cap));
1519
1520         } else
1521                 pstat->flags &= ~WLAN_STA_HT;
1522
1523
1524         if ((pmlmepriv->htpriv.ht_option == false) && (pstat->flags&WLAN_STA_HT)) {
1525                 status = _STATS_FAILURE_;
1526                 goto OnAssocReqFail;
1527         }
1528
1529
1530         if ((pstat->flags & WLAN_STA_HT) &&
1531                     ((pstat->wpa2_pairwise_cipher&WPA_CIPHER_TKIP) ||
1532                       (pstat->wpa_pairwise_cipher&WPA_CIPHER_TKIP))) {
1533                 DBG_871X("HT: " MAC_FMT " tried to "
1534                                    "use TKIP with HT association\n", MAC_ARG(pstat->hwaddr));
1535
1536                 /* status = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY; */
1537                 /* goto OnAssocReqFail; */
1538         }
1539         pstat->flags |= WLAN_STA_NONERP;
1540         for (i = 0; i < pstat->bssratelen; i++) {
1541                 if ((pstat->bssrateset[i] & 0x7f) > 22) {
1542                         pstat->flags &= ~WLAN_STA_NONERP;
1543                         break;
1544                 }
1545         }
1546
1547         if (pstat->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1548                 pstat->flags |= WLAN_STA_SHORT_PREAMBLE;
1549         else
1550                 pstat->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1551
1552
1553
1554         if (status != _STATS_SUCCESSFUL_)
1555                 goto OnAssocReqFail;
1556
1557         /* TODO: identify_proprietary_vendor_ie(); */
1558         /*  Realtek proprietary IE */
1559         /*  identify if this is Broadcom sta */
1560         /*  identify if this is ralink sta */
1561         /*  Customer proprietary IE */
1562
1563
1564
1565         /* get a unique AID */
1566         if (pstat->aid > 0) {
1567                 DBG_871X("  old AID %d\n", pstat->aid);
1568         } else {
1569                 for (pstat->aid = 1; pstat->aid <= NUM_STA; pstat->aid++)
1570                         if (pstapriv->sta_aid[pstat->aid - 1] == NULL)
1571                                 break;
1572
1573                 /* if (pstat->aid > NUM_STA) { */
1574                 if (pstat->aid > pstapriv->max_num_sta) {
1575
1576                         pstat->aid = 0;
1577
1578                         DBG_871X("  no room for more AIDs\n");
1579
1580                         status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1581
1582                         goto OnAssocReqFail;
1583
1584
1585                 } else {
1586                         pstapriv->sta_aid[pstat->aid - 1] = pstat;
1587                         DBG_871X("allocate new AID = (%d)\n", pstat->aid);
1588                 }
1589         }
1590
1591
1592         pstat->state &= (~WIFI_FW_ASSOC_STATE);
1593         pstat->state |= WIFI_FW_ASSOC_SUCCESS;
1594
1595         spin_lock_bh(&pstapriv->auth_list_lock);
1596         if (!list_empty(&pstat->auth_list)) {
1597                 list_del_init(&pstat->auth_list);
1598                 pstapriv->auth_list_cnt--;
1599         }
1600         spin_unlock_bh(&pstapriv->auth_list_lock);
1601
1602         spin_lock_bh(&pstapriv->asoc_list_lock);
1603         if (list_empty(&pstat->asoc_list)) {
1604                 pstat->expire_to = pstapriv->expire_to;
1605                 list_add_tail(&pstat->asoc_list, &pstapriv->asoc_list);
1606                 pstapriv->asoc_list_cnt++;
1607         }
1608         spin_unlock_bh(&pstapriv->asoc_list_lock);
1609
1610         /*  now the station is qualified to join our BSS... */
1611         if (pstat && (pstat->state & WIFI_FW_ASSOC_SUCCESS) && (_STATS_SUCCESSFUL_ == status)) {
1612                 /* 1 bss_cap_update & sta_info_update */
1613                 bss_cap_update_on_sta_join(padapter, pstat);
1614                 sta_info_update(padapter, pstat);
1615
1616                 /* 2 issue assoc rsp before notify station join event. */
1617                 if (frame_type == WIFI_ASSOCREQ)
1618                         issue_asocrsp(padapter, status, pstat, WIFI_ASSOCRSP);
1619                 else
1620                         issue_asocrsp(padapter, status, pstat, WIFI_REASSOCRSP);
1621
1622                 spin_lock_bh(&pstat->lock);
1623                 if (pstat->passoc_req) {
1624                         kfree(pstat->passoc_req);
1625                         pstat->passoc_req = NULL;
1626                         pstat->assoc_req_len = 0;
1627                 }
1628
1629                 pstat->passoc_req =  rtw_zmalloc(pkt_len);
1630                 if (pstat->passoc_req) {
1631                         memcpy(pstat->passoc_req, pframe, pkt_len);
1632                         pstat->assoc_req_len = pkt_len;
1633                 }
1634                 spin_unlock_bh(&pstat->lock);
1635
1636                 /* 3-(1) report sta add event */
1637                 report_add_sta_event(padapter, pstat->hwaddr, pstat->aid);
1638         }
1639
1640         return _SUCCESS;
1641
1642 asoc_class2_error:
1643
1644         issue_deauth(padapter, (void *)GetAddr2Ptr(pframe), status);
1645
1646         return _FAIL;
1647
1648 OnAssocReqFail:
1649
1650         pstat->aid = 0;
1651         if (frame_type == WIFI_ASSOCREQ)
1652                 issue_asocrsp(padapter, status, pstat, WIFI_ASSOCRSP);
1653         else
1654                 issue_asocrsp(padapter, status, pstat, WIFI_REASSOCRSP);
1655
1656         return _FAIL;
1657 }
1658
1659 unsigned int OnAssocRsp(struct adapter *padapter, union recv_frame *precv_frame)
1660 {
1661         uint i;
1662         int res;
1663         unsigned short  status;
1664         struct ndis_80211_var_ie *pIE;
1665         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1666         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1667         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1668         /* struct wlan_bssid_ex                 *cur_network = &(pmlmeinfo->network); */
1669         u8 *pframe = precv_frame->u.hdr.rx_data;
1670         uint pkt_len = precv_frame->u.hdr.len;
1671
1672         DBG_871X("%s\n", __func__);
1673
1674         /* check A1 matches or not */
1675         if (memcmp(myid(&(padapter->eeprompriv)), get_da(pframe), ETH_ALEN))
1676                 return _SUCCESS;
1677
1678         if (!(pmlmeinfo->state & (WIFI_FW_AUTH_SUCCESS | WIFI_FW_ASSOC_STATE)))
1679                 return _SUCCESS;
1680
1681         if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS)
1682                 return _SUCCESS;
1683
1684         del_timer_sync(&pmlmeext->link_timer);
1685
1686         /* status */
1687         status = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 2));
1688         if (status > 0) {
1689                 DBG_871X("assoc reject, status code: %d\n", status);
1690                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
1691                 res = -4;
1692                 goto report_assoc_result;
1693         }
1694
1695         /* get capabilities */
1696         pmlmeinfo->capability = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
1697
1698         /* set slot time */
1699         pmlmeinfo->slotTime = (pmlmeinfo->capability & BIT(10)) ? 9 : 20;
1700
1701         /* AID */
1702         res = pmlmeinfo->aid = (int)(le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN + 4))&0x3fff);
1703
1704         /* following are moved to join event callback function */
1705         /* to handle HT, WMM, rate adaptive, update MAC reg */
1706         /* for not to handle the synchronous IO in the tasklet */
1707         for (i = (6 + WLAN_HDR_A3_LEN); i < pkt_len;) {
1708                 pIE = (struct ndis_80211_var_ie *)(pframe + i);
1709
1710                 switch (pIE->ElementID) {
1711                 case _VENDOR_SPECIFIC_IE_:
1712                         if (!memcmp(pIE->data, WMM_PARA_OUI, 6))        /* WMM */
1713                                 WMM_param_handler(padapter, pIE);
1714                         break;
1715
1716                 case _HT_CAPABILITY_IE_:        /* HT caps */
1717                         HT_caps_handler(padapter, pIE);
1718                         break;
1719
1720                 case _HT_EXTRA_INFO_IE_:        /* HT info */
1721                         HT_info_handler(padapter, pIE);
1722                         break;
1723
1724                 case _ERPINFO_IE_:
1725                         ERP_IE_handler(padapter, pIE);
1726
1727                 default:
1728                         break;
1729                 }
1730
1731                 i += (pIE->Length + 2);
1732         }
1733
1734         pmlmeinfo->state &= (~WIFI_FW_ASSOC_STATE);
1735         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
1736
1737         /* Update Basic Rate Table for spec, 2010-12-28 , by thomas */
1738         UpdateBrateTbl(padapter, pmlmeinfo->network.SupportedRates);
1739
1740 report_assoc_result:
1741         if (res > 0) {
1742                 rtw_buf_update(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len, pframe, pkt_len);
1743         } else {
1744                 rtw_buf_free(&pmlmepriv->assoc_rsp, &pmlmepriv->assoc_rsp_len);
1745         }
1746
1747         report_join_res(padapter, res);
1748
1749         return _SUCCESS;
1750 }
1751
1752 unsigned int OnDeAuth(struct adapter *padapter, union recv_frame *precv_frame)
1753 {
1754         unsigned short  reason;
1755         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1756         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1757         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1758         u8 *pframe = precv_frame->u.hdr.rx_data;
1759
1760         /* check A3 */
1761         if (memcmp(GetAddr3Ptr(pframe), get_my_bssid(&pmlmeinfo->network), ETH_ALEN))
1762                 return _SUCCESS;
1763
1764         reason = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
1765
1766         DBG_871X("%s Reason code(%d)\n", __func__, reason);
1767
1768         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1769                 struct sta_info *psta;
1770                 struct sta_priv *pstapriv = &padapter->stapriv;
1771
1772                 /* spin_lock_bh(&(pstapriv->sta_hash_lock)); */
1773                 /* rtw_free_stainfo(padapter, psta); */
1774                 /* spin_unlock_bh(&(pstapriv->sta_hash_lock)); */
1775
1776                 DBG_871X_LEVEL(_drv_always_, "ap recv deauth reason code(%d) sta:%pM\n",
1777                                 reason, GetAddr2Ptr(pframe));
1778
1779                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1780                 if (psta) {
1781                         u8 updated = false;
1782
1783                         spin_lock_bh(&pstapriv->asoc_list_lock);
1784                         if (list_empty(&psta->asoc_list) == false) {
1785                                 list_del_init(&psta->asoc_list);
1786                                 pstapriv->asoc_list_cnt--;
1787                                 updated = ap_free_sta(padapter, psta, false, reason);
1788
1789                         }
1790                         spin_unlock_bh(&pstapriv->asoc_list_lock);
1791
1792                         associated_clients_update(padapter, updated);
1793                 }
1794
1795
1796                 return _SUCCESS;
1797         } else{
1798                 int     ignore_received_deauth = 0;
1799
1800                 /*      Commented by Albert 20130604 */
1801                 /*      Before sending the auth frame to start the STA/GC mode connection with AP/GO, */
1802                 /*      we will send the deauth first. */
1803                 /*      However, the Win8.1 with BRCM Wi-Fi will send the deauth with reason code 6 to us after receieving our deauth. */
1804                 /*      Added the following code to avoid this case. */
1805                 if ((pmlmeinfo->state & WIFI_FW_AUTH_STATE) ||
1806                         (pmlmeinfo->state & WIFI_FW_ASSOC_STATE)) {
1807                         if (reason == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA) {
1808                                 ignore_received_deauth = 1;
1809                         } else if (WLAN_REASON_PREV_AUTH_NOT_VALID == reason) {
1810                                 /*  TODO: 802.11r */
1811                                 ignore_received_deauth = 1;
1812                         }
1813                 }
1814
1815                 DBG_871X_LEVEL(_drv_always_, "sta recv deauth reason code(%d) sta:%pM, ignore = %d\n",
1816                                 reason, GetAddr3Ptr(pframe), ignore_received_deauth);
1817
1818                 if (0 == ignore_received_deauth) {
1819                         receive_disconnect(padapter, GetAddr3Ptr(pframe), reason);
1820                 }
1821         }
1822         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
1823         return _SUCCESS;
1824
1825 }
1826
1827 unsigned int OnDisassoc(struct adapter *padapter, union recv_frame *precv_frame)
1828 {
1829         unsigned short  reason;
1830         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1831         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1832         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1833         u8 *pframe = precv_frame->u.hdr.rx_data;
1834
1835         /* check A3 */
1836         if (memcmp(GetAddr3Ptr(pframe), get_my_bssid(&pmlmeinfo->network), ETH_ALEN))
1837                 return _SUCCESS;
1838
1839         reason = le16_to_cpu(*(__le16 *)(pframe + WLAN_HDR_A3_LEN));
1840
1841         DBG_871X("%s Reason code(%d)\n", __func__, reason);
1842
1843         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1844                 struct sta_info *psta;
1845                 struct sta_priv *pstapriv = &padapter->stapriv;
1846
1847                 /* spin_lock_bh(&(pstapriv->sta_hash_lock)); */
1848                 /* rtw_free_stainfo(padapter, psta); */
1849                 /* spin_unlock_bh(&(pstapriv->sta_hash_lock)); */
1850
1851                 DBG_871X_LEVEL(_drv_always_, "ap recv disassoc reason code(%d) sta:%pM\n",
1852                                 reason, GetAddr2Ptr(pframe));
1853
1854                 psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1855                 if (psta) {
1856                         u8 updated = false;
1857
1858                         spin_lock_bh(&pstapriv->asoc_list_lock);
1859                         if (list_empty(&psta->asoc_list) == false) {
1860                                 list_del_init(&psta->asoc_list);
1861                                 pstapriv->asoc_list_cnt--;
1862                                 updated = ap_free_sta(padapter, psta, false, reason);
1863
1864                         }
1865                         spin_unlock_bh(&pstapriv->asoc_list_lock);
1866
1867                         associated_clients_update(padapter, updated);
1868                 }
1869
1870                 return _SUCCESS;
1871         } else{
1872                 DBG_871X_LEVEL(_drv_always_, "sta recv disassoc reason code(%d) sta:%pM\n",
1873                                 reason, GetAddr3Ptr(pframe));
1874
1875                 receive_disconnect(padapter, GetAddr3Ptr(pframe), reason);
1876         }
1877         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
1878         return _SUCCESS;
1879
1880 }
1881
1882 unsigned int OnAtim(struct adapter *padapter, union recv_frame *precv_frame)
1883 {
1884         DBG_871X("%s\n", __func__);
1885         return _SUCCESS;
1886 }
1887
1888 unsigned int on_action_spct(struct adapter *padapter, union recv_frame *precv_frame)
1889 {
1890         unsigned int ret = _FAIL;
1891         struct sta_info *psta = NULL;
1892         struct sta_priv *pstapriv = &padapter->stapriv;
1893         u8 *pframe = precv_frame->u.hdr.rx_data;
1894         u8 *frame_body = (u8 *)(pframe + sizeof(struct ieee80211_hdr_3addr));
1895         u8 category;
1896         u8 action;
1897
1898         DBG_871X(FUNC_NDEV_FMT"\n", FUNC_NDEV_ARG(padapter->pnetdev));
1899
1900         psta = rtw_get_stainfo(pstapriv, GetAddr2Ptr(pframe));
1901
1902         if (!psta)
1903                 goto exit;
1904
1905         category = frame_body[0];
1906         if (category != RTW_WLAN_CATEGORY_SPECTRUM_MGMT)
1907                 goto exit;
1908
1909         action = frame_body[1];
1910         switch (action) {
1911         case RTW_WLAN_ACTION_SPCT_MSR_REQ:
1912         case RTW_WLAN_ACTION_SPCT_MSR_RPRT:
1913         case RTW_WLAN_ACTION_SPCT_TPC_REQ:
1914         case RTW_WLAN_ACTION_SPCT_TPC_RPRT:
1915         case RTW_WLAN_ACTION_SPCT_CHL_SWITCH:
1916                 break;
1917         default:
1918                 break;
1919         }
1920
1921 exit:
1922         return ret;
1923 }
1924
1925 unsigned int OnAction_back(struct adapter *padapter, union recv_frame *precv_frame)
1926 {
1927         u8 *addr;
1928         struct sta_info *psta = NULL;
1929         struct recv_reorder_ctrl *preorder_ctrl;
1930         unsigned char   *frame_body;
1931         unsigned char   category, action;
1932         unsigned short  tid, status, reason_code = 0;
1933         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1934         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
1935         u8 *pframe = precv_frame->u.hdr.rx_data;
1936         struct sta_priv *pstapriv = &padapter->stapriv;
1937
1938         DBG_871X("%s\n", __func__);
1939
1940         /* check RA matches or not */
1941         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe), ETH_ALEN))/* for if1, sta/ap mode */
1942                 return _SUCCESS;
1943
1944         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
1945                 if (!(pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS))
1946                         return _SUCCESS;
1947
1948         addr = GetAddr2Ptr(pframe);
1949         psta = rtw_get_stainfo(pstapriv, addr);
1950
1951         if (psta == NULL)
1952                 return _SUCCESS;
1953
1954         frame_body = (unsigned char *)(pframe + sizeof(struct ieee80211_hdr_3addr));
1955
1956         category = frame_body[0];
1957         if (category == RTW_WLAN_CATEGORY_BACK) {/*  representing Block Ack */
1958                 if (!pmlmeinfo->HT_enable) {
1959                         return _SUCCESS;
1960                 }
1961
1962                 action = frame_body[1];
1963                 DBG_871X("%s, action =%d\n", __func__, action);
1964                 switch (action) {
1965                 case RTW_WLAN_ACTION_ADDBA_REQ: /* ADDBA request */
1966
1967                         memcpy(&(pmlmeinfo->ADDBA_req), &(frame_body[2]), sizeof(struct ADDBA_request));
1968                         /* process_addba_req(padapter, (u8 *)&(pmlmeinfo->ADDBA_req), GetAddr3Ptr(pframe)); */
1969                         process_addba_req(padapter, (u8 *)&(pmlmeinfo->ADDBA_req), addr);
1970
1971                         if (pmlmeinfo->accept_addba_req) {
1972                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_ADDBA_RESP, 0);
1973                         } else{
1974                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_ADDBA_RESP, 37);/* reject ADDBA Req */
1975                         }
1976
1977                         break;
1978
1979                 case RTW_WLAN_ACTION_ADDBA_RESP: /* ADDBA response */
1980                         status = RTW_GET_LE16(&frame_body[3]);
1981                         tid = ((frame_body[5] >> 2) & 0x7);
1982
1983                         if (status == 0) {
1984                                 /* successful */
1985                                 DBG_871X("agg_enable for TID =%d\n", tid);
1986                                 psta->htpriv.agg_enable_bitmap |= 1 << tid;
1987                                 psta->htpriv.candidate_tid_bitmap &= ~BIT(tid);
1988                         } else{
1989                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
1990                         }
1991
1992                         if (psta->state & WIFI_STA_ALIVE_CHK_STATE) {
1993                                 DBG_871X("%s alive check - rx ADDBA response\n", __func__);
1994                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
1995                                 psta->expire_to = pstapriv->expire_to;
1996                                 psta->state ^= WIFI_STA_ALIVE_CHK_STATE;
1997                         }
1998
1999                         /* DBG_871X("marc: ADDBA RSP: %x\n", pmlmeinfo->agg_enable_bitmap); */
2000                         break;
2001
2002                 case RTW_WLAN_ACTION_DELBA: /* DELBA */
2003                         if ((frame_body[3] & BIT(3)) == 0) {
2004                                 psta->htpriv.agg_enable_bitmap &= ~(1 << ((frame_body[3] >> 4) & 0xf));
2005                                 psta->htpriv.candidate_tid_bitmap &= ~(1 << ((frame_body[3] >> 4) & 0xf));
2006
2007                                 /* reason_code = frame_body[4] | (frame_body[5] << 8); */
2008                                 reason_code = RTW_GET_LE16(&frame_body[4]);
2009                         } else if ((frame_body[3] & BIT(3)) == BIT(3)) {
2010                                 tid = (frame_body[3] >> 4) & 0x0F;
2011
2012                                 preorder_ctrl =  &psta->recvreorder_ctrl[tid];
2013                                 preorder_ctrl->enable = false;
2014                                 preorder_ctrl->indicate_seq = 0xffff;
2015                                 #ifdef DBG_RX_SEQ
2016                                 DBG_871X("DBG_RX_SEQ %s:%d indicate_seq:%u\n", __func__, __LINE__,
2017                                         preorder_ctrl->indicate_seq);
2018                                 #endif
2019                         }
2020
2021                         DBG_871X("%s(): DELBA: %x(%x)\n", __func__, pmlmeinfo->agg_enable_bitmap, reason_code);
2022                         /* todo: how to notify the host while receiving DELETE BA */
2023                         break;
2024
2025                 default:
2026                         break;
2027                 }
2028         }
2029         return _SUCCESS;
2030 }
2031
2032 static s32 rtw_action_public_decache(union recv_frame *recv_frame, s32 token)
2033 {
2034         struct adapter *adapter = recv_frame->u.hdr.adapter;
2035         struct mlme_ext_priv *mlmeext = &(adapter->mlmeextpriv);
2036         u8 *frame = recv_frame->u.hdr.rx_data;
2037         u16 seq_ctrl = ((recv_frame->u.hdr.attrib.seq_num&0xffff) << 4) |
2038                 (recv_frame->u.hdr.attrib.frag_num & 0xf);
2039
2040         if (GetRetry(frame)) {
2041                 if (token >= 0) {
2042                         if ((seq_ctrl == mlmeext->action_public_rxseq)
2043                                 && (token == mlmeext->action_public_dialog_token)) {
2044                                 DBG_871X(FUNC_ADPT_FMT" seq_ctrl = 0x%x, rxseq = 0x%x, token:%d\n",
2045                                         FUNC_ADPT_ARG(adapter), seq_ctrl, mlmeext->action_public_rxseq, token);
2046                                 return _FAIL;
2047                         }
2048                 } else {
2049                         if (seq_ctrl == mlmeext->action_public_rxseq) {
2050                                 DBG_871X(FUNC_ADPT_FMT" seq_ctrl = 0x%x, rxseq = 0x%x\n",
2051                                         FUNC_ADPT_ARG(adapter), seq_ctrl, mlmeext->action_public_rxseq);
2052                                 return _FAIL;
2053                         }
2054                 }
2055         }
2056
2057         mlmeext->action_public_rxseq = seq_ctrl;
2058
2059         if (token >= 0)
2060                 mlmeext->action_public_dialog_token = token;
2061
2062         return _SUCCESS;
2063 }
2064
2065 static unsigned int on_action_public_p2p(union recv_frame *precv_frame)
2066 {
2067         u8 *pframe = precv_frame->u.hdr.rx_data;
2068         u8 *frame_body;
2069         u8 dialogToken = 0;
2070
2071         frame_body = (unsigned char *)(pframe + sizeof(struct ieee80211_hdr_3addr));
2072
2073         dialogToken = frame_body[7];
2074
2075         if (rtw_action_public_decache(precv_frame, dialogToken) == _FAIL)
2076                 return _FAIL;
2077
2078         return _SUCCESS;
2079 }
2080
2081 static unsigned int on_action_public_vendor(union recv_frame *precv_frame)
2082 {
2083         unsigned int ret = _FAIL;
2084         u8 *pframe = precv_frame->u.hdr.rx_data;
2085         u8 *frame_body = pframe + sizeof(struct ieee80211_hdr_3addr);
2086
2087         if (!memcmp(frame_body + 2, P2P_OUI, 4)) {
2088                 ret = on_action_public_p2p(precv_frame);
2089         }
2090
2091         return ret;
2092 }
2093
2094 static unsigned int on_action_public_default(union recv_frame *precv_frame, u8 action)
2095 {
2096         unsigned int ret = _FAIL;
2097         u8 *pframe = precv_frame->u.hdr.rx_data;
2098         uint frame_len = precv_frame->u.hdr.len;
2099         u8 *frame_body = pframe + sizeof(struct ieee80211_hdr_3addr);
2100         u8 token;
2101         struct adapter *adapter = precv_frame->u.hdr.adapter;
2102         int cnt = 0;
2103         char msg[64];
2104
2105         token = frame_body[2];
2106
2107         if (rtw_action_public_decache(precv_frame, token) == _FAIL)
2108                 goto exit;
2109
2110         cnt += sprintf((msg+cnt), "%s(token:%u)", action_public_str(action), token);
2111         rtw_cfg80211_rx_action(adapter, pframe, frame_len, msg);
2112
2113         ret = _SUCCESS;
2114
2115 exit:
2116         return ret;
2117 }
2118
2119 unsigned int on_action_public(struct adapter *padapter, union recv_frame *precv_frame)
2120 {
2121         unsigned int ret = _FAIL;
2122         u8 *pframe = precv_frame->u.hdr.rx_data;
2123         u8 *frame_body = pframe + sizeof(struct ieee80211_hdr_3addr);
2124         u8 category, action;
2125
2126         /* check RA matches or not */
2127         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe), ETH_ALEN))
2128                 goto exit;
2129
2130         category = frame_body[0];
2131         if (category != RTW_WLAN_CATEGORY_PUBLIC)
2132                 goto exit;
2133
2134         action = frame_body[1];
2135         switch (action) {
2136         case ACT_PUBLIC_VENDOR:
2137                 ret = on_action_public_vendor(precv_frame);
2138                 break;
2139         default:
2140                 ret = on_action_public_default(precv_frame, action);
2141                 break;
2142         }
2143
2144 exit:
2145         return ret;
2146 }
2147
2148 unsigned int OnAction_ht(struct adapter *padapter, union recv_frame *precv_frame)
2149 {
2150         u8 *pframe = precv_frame->u.hdr.rx_data;
2151         u8 *frame_body = pframe + sizeof(struct ieee80211_hdr_3addr);
2152         u8 category, action;
2153
2154         /* check RA matches or not */
2155         if (memcmp(myid(&(padapter->eeprompriv)), GetAddr1Ptr(pframe), ETH_ALEN))
2156                 goto exit;
2157
2158         category = frame_body[0];
2159         if (category != RTW_WLAN_CATEGORY_HT)
2160                 goto exit;
2161
2162         action = frame_body[1];
2163         switch (action) {
2164         case RTW_WLAN_ACTION_HT_COMPRESS_BEAMFORMING:
2165                 break;
2166         default:
2167                 break;
2168         }
2169
2170 exit:
2171
2172         return _SUCCESS;
2173 }
2174
2175 unsigned int OnAction_sa_query(struct adapter *padapter, union recv_frame *precv_frame)
2176 {
2177         u8 *pframe = precv_frame->u.hdr.rx_data;
2178         struct rx_pkt_attrib *pattrib = &precv_frame->u.hdr.attrib;
2179         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2180         unsigned short tid;
2181         /* Baron */
2182
2183         DBG_871X("OnAction_sa_query\n");
2184
2185         switch (pframe[WLAN_HDR_A3_LEN+1]) {
2186         case 0: /* SA Query req */
2187                 memcpy(&tid, &pframe[WLAN_HDR_A3_LEN+2], sizeof(unsigned short));
2188                 DBG_871X("OnAction_sa_query request, action =%d, tid =%04x\n", pframe[WLAN_HDR_A3_LEN+1], tid);
2189                 issue_action_SA_Query(padapter, GetAddr2Ptr(pframe), 1, tid);
2190                 break;
2191
2192         case 1: /* SA Query rsp */
2193                 del_timer_sync(&pmlmeext->sa_query_timer);
2194                 DBG_871X("OnAction_sa_query response, action =%d, tid =%04x, cancel timer\n", pframe[WLAN_HDR_A3_LEN+1], pframe[WLAN_HDR_A3_LEN+2]);
2195                 break;
2196         default:
2197                 break;
2198         }
2199         if (0) {
2200                 int pp;
2201                 printk("pattrib->pktlen = %d =>", pattrib->pkt_len);
2202                 for (pp = 0; pp < pattrib->pkt_len; pp++)
2203                         printk(" %02x ", pframe[pp]);
2204                 printk("\n");
2205         }
2206
2207         return _SUCCESS;
2208 }
2209
2210 unsigned int OnAction(struct adapter *padapter, union recv_frame *precv_frame)
2211 {
2212         int i;
2213         unsigned char category;
2214         struct action_handler *ptable;
2215         unsigned char *frame_body;
2216         u8 *pframe = precv_frame->u.hdr.rx_data;
2217
2218         frame_body = (unsigned char *)(pframe + sizeof(struct ieee80211_hdr_3addr));
2219
2220         category = frame_body[0];
2221
2222         for (i = 0; i < ARRAY_SIZE(OnAction_tbl); i++) {
2223                 ptable = &OnAction_tbl[i];
2224
2225                 if (category == ptable->num)
2226                         ptable->func(padapter, precv_frame);
2227
2228         }
2229
2230         return _SUCCESS;
2231
2232 }
2233
2234 unsigned int DoReserved(struct adapter *padapter, union recv_frame *precv_frame)
2235 {
2236
2237         /* DBG_871X("rcvd mgt frame(%x, %x)\n", (GetFrameSubType(pframe) >> 4), *(unsigned int *)GetAddr1Ptr(pframe)); */
2238         return _SUCCESS;
2239 }
2240
2241 static struct xmit_frame *_alloc_mgtxmitframe(struct xmit_priv *pxmitpriv, bool once)
2242 {
2243         struct xmit_frame *pmgntframe;
2244         struct xmit_buf *pxmitbuf;
2245
2246         if (once)
2247                 pmgntframe = rtw_alloc_xmitframe_once(pxmitpriv);
2248         else
2249                 pmgntframe = rtw_alloc_xmitframe_ext(pxmitpriv);
2250
2251         if (pmgntframe == NULL) {
2252                 DBG_871X(FUNC_ADPT_FMT" alloc xmitframe fail, once:%d\n", FUNC_ADPT_ARG(pxmitpriv->adapter), once);
2253                 goto exit;
2254         }
2255
2256         pxmitbuf = rtw_alloc_xmitbuf_ext(pxmitpriv);
2257         if (pxmitbuf == NULL) {
2258                 DBG_871X(FUNC_ADPT_FMT" alloc xmitbuf fail\n", FUNC_ADPT_ARG(pxmitpriv->adapter));
2259                 rtw_free_xmitframe(pxmitpriv, pmgntframe);
2260                 pmgntframe = NULL;
2261                 goto exit;
2262         }
2263
2264         pmgntframe->frame_tag = MGNT_FRAMETAG;
2265         pmgntframe->pxmitbuf = pxmitbuf;
2266         pmgntframe->buf_addr = pxmitbuf->pbuf;
2267         pxmitbuf->priv_data = pmgntframe;
2268
2269 exit:
2270         return pmgntframe;
2271
2272 }
2273
2274 inline struct xmit_frame *alloc_mgtxmitframe(struct xmit_priv *pxmitpriv)
2275 {
2276         return _alloc_mgtxmitframe(pxmitpriv, false);
2277 }
2278
2279 /****************************************************************************
2280
2281 Following are some TX fuctions for WiFi MLME
2282
2283 *****************************************************************************/
2284
2285 void update_mgnt_tx_rate(struct adapter *padapter, u8 rate)
2286 {
2287         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2288
2289         pmlmeext->tx_rate = rate;
2290         /* DBG_871X("%s(): rate = %x\n", __func__, rate); */
2291 }
2292
2293 void update_mgntframe_attrib(struct adapter *padapter, struct pkt_attrib *pattrib)
2294 {
2295         u8 wireless_mode;
2296         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2297
2298         /* memset((u8 *)(pattrib), 0, sizeof(struct pkt_attrib)); */
2299
2300         pattrib->hdrlen = 24;
2301         pattrib->nr_frags = 1;
2302         pattrib->priority = 7;
2303         pattrib->mac_id = 0;
2304         pattrib->qsel = 0x12;
2305
2306         pattrib->pktlen = 0;
2307
2308         if (pmlmeext->tx_rate == IEEE80211_CCK_RATE_1MB)
2309                 wireless_mode = WIRELESS_11B;
2310         else
2311                 wireless_mode = WIRELESS_11G;
2312         pattrib->raid =  rtw_get_mgntframe_raid(padapter, wireless_mode);
2313         pattrib->rate = pmlmeext->tx_rate;
2314
2315         pattrib->encrypt = _NO_PRIVACY_;
2316         pattrib->bswenc = false;
2317
2318         pattrib->qos_en = false;
2319         pattrib->ht_en = false;
2320         pattrib->bwmode = CHANNEL_WIDTH_20;
2321         pattrib->ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
2322         pattrib->sgi = false;
2323
2324         pattrib->seqnum = pmlmeext->mgnt_seq;
2325
2326         pattrib->retry_ctrl = true;
2327
2328         pattrib->mbssid = 0;
2329
2330 }
2331
2332 void update_mgntframe_attrib_addr(struct adapter *padapter, struct xmit_frame *pmgntframe)
2333 {
2334         u8 *pframe;
2335         struct pkt_attrib       *pattrib = &pmgntframe->attrib;
2336
2337         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2338
2339         memcpy(pattrib->ra, GetAddr1Ptr(pframe), ETH_ALEN);
2340         memcpy(pattrib->ta, GetAddr2Ptr(pframe), ETH_ALEN);
2341 }
2342
2343 void dump_mgntframe(struct adapter *padapter, struct xmit_frame *pmgntframe)
2344 {
2345         if (padapter->bSurpriseRemoved ||
2346                 padapter->bDriverStopped) {
2347                 rtw_free_xmitbuf(&padapter->xmitpriv, pmgntframe->pxmitbuf);
2348                 rtw_free_xmitframe(&padapter->xmitpriv, pmgntframe);
2349                 return;
2350         }
2351
2352         rtw_hal_mgnt_xmit(padapter, pmgntframe);
2353 }
2354
2355 s32 dump_mgntframe_and_wait(struct adapter *padapter, struct xmit_frame *pmgntframe, int timeout_ms)
2356 {
2357         s32 ret = _FAIL;
2358         _irqL irqL;
2359         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2360         struct xmit_buf *pxmitbuf = pmgntframe->pxmitbuf;
2361         struct submit_ctx sctx;
2362
2363         if (padapter->bSurpriseRemoved ||
2364                 padapter->bDriverStopped) {
2365                 rtw_free_xmitbuf(&padapter->xmitpriv, pmgntframe->pxmitbuf);
2366                 rtw_free_xmitframe(&padapter->xmitpriv, pmgntframe);
2367                 return ret;
2368         }
2369
2370         rtw_sctx_init(&sctx, timeout_ms);
2371         pxmitbuf->sctx = &sctx;
2372
2373         ret = rtw_hal_mgnt_xmit(padapter, pmgntframe);
2374
2375         if (ret == _SUCCESS)
2376                 ret = rtw_sctx_wait(&sctx, __func__);
2377
2378         spin_lock_irqsave(&pxmitpriv->lock_sctx, irqL);
2379         pxmitbuf->sctx = NULL;
2380         spin_unlock_irqrestore(&pxmitpriv->lock_sctx, irqL);
2381
2382         return ret;
2383 }
2384
2385 s32 dump_mgntframe_and_wait_ack(struct adapter *padapter, struct xmit_frame *pmgntframe)
2386 {
2387         static u8 seq_no;
2388         s32 ret = _FAIL;
2389         u32 timeout_ms = 500;/*   500ms */
2390         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
2391
2392         if (padapter->bSurpriseRemoved ||
2393                 padapter->bDriverStopped) {
2394                 rtw_free_xmitbuf(&padapter->xmitpriv, pmgntframe->pxmitbuf);
2395                 rtw_free_xmitframe(&padapter->xmitpriv, pmgntframe);
2396                 return -1;
2397         }
2398
2399         if (mutex_lock_interruptible(&pxmitpriv->ack_tx_mutex) == 0) {
2400                 pxmitpriv->ack_tx = true;
2401                 pxmitpriv->seq_no = seq_no++;
2402                 pmgntframe->ack_report = 1;
2403                 if (rtw_hal_mgnt_xmit(padapter, pmgntframe) == _SUCCESS) {
2404                         ret = rtw_ack_tx_wait(pxmitpriv, timeout_ms);
2405                 }
2406
2407                 pxmitpriv->ack_tx = false;
2408                 mutex_unlock(&pxmitpriv->ack_tx_mutex);
2409         }
2410
2411         return ret;
2412 }
2413
2414 static int update_hidden_ssid(u8 *ies, u32 ies_len, u8 hidden_ssid_mode)
2415 {
2416         u8 *ssid_ie;
2417         sint ssid_len_ori;
2418         int len_diff = 0;
2419
2420         ssid_ie = rtw_get_ie(ies,  WLAN_EID_SSID, &ssid_len_ori, ies_len);
2421
2422         /* DBG_871X("%s hidden_ssid_mode:%u, ssid_ie:%p, ssid_len_ori:%d\n", __func__, hidden_ssid_mode, ssid_ie, ssid_len_ori); */
2423
2424         if (ssid_ie && ssid_len_ori > 0) {
2425                 switch (hidden_ssid_mode) {
2426                 case 1:
2427                 {
2428                         u8 *next_ie = ssid_ie + 2 + ssid_len_ori;
2429                         u32 remain_len = 0;
2430
2431                         remain_len = ies_len - (next_ie-ies);
2432
2433                         ssid_ie[1] = 0;
2434                         memcpy(ssid_ie+2, next_ie, remain_len);
2435                         len_diff -= ssid_len_ori;
2436
2437                         break;
2438                 }
2439                 case 2:
2440                         memset(&ssid_ie[2], 0, ssid_len_ori);
2441                         break;
2442                 default:
2443                         break;
2444         }
2445         }
2446
2447         return len_diff;
2448 }
2449
2450 void issue_beacon(struct adapter *padapter, int timeout_ms)
2451 {
2452         struct xmit_frame       *pmgntframe;
2453         struct pkt_attrib       *pattrib;
2454         unsigned char *pframe;
2455         struct ieee80211_hdr *pwlanhdr;
2456         __le16 *fctrl;
2457         unsigned int    rate_len;
2458         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
2459         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2460         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2461         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
2462         struct wlan_bssid_ex            *cur_network = &(pmlmeinfo->network);
2463         u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2464
2465         /* DBG_871X("%s\n", __func__); */
2466
2467         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2468         if (pmgntframe == NULL) {
2469                 DBG_871X("%s, alloc mgnt frame fail\n", __func__);
2470                 return;
2471         }
2472
2473         spin_lock_bh(&pmlmepriv->bcn_update_lock);
2474
2475         /* update attribute */
2476         pattrib = &pmgntframe->attrib;
2477         update_mgntframe_attrib(padapter, pattrib);
2478         pattrib->qsel = 0x10;
2479
2480         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2481
2482         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2483         pwlanhdr = (struct ieee80211_hdr *)pframe;
2484
2485
2486         fctrl = &(pwlanhdr->frame_control);
2487         *(fctrl) = 0;
2488
2489         memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN);
2490         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
2491         memcpy(pwlanhdr->addr3, get_my_bssid(cur_network), ETH_ALEN);
2492
2493         SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
2494         /* pmlmeext->mgnt_seq++; */
2495         SetFrameSubType(pframe, WIFI_BEACON);
2496
2497         pframe += sizeof(struct ieee80211_hdr_3addr);
2498         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
2499
2500         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
2501                 /* DBG_871X("ie len =%d\n", cur_network->IELength); */
2502                 {
2503                         int len_diff;
2504                         memcpy(pframe, cur_network->IEs, cur_network->IELength);
2505                         len_diff = update_hidden_ssid(
2506                                 pframe+_BEACON_IE_OFFSET_
2507                                 , cur_network->IELength-_BEACON_IE_OFFSET_
2508                                 , pmlmeinfo->hidden_ssid_mode
2509                         );
2510                         pframe += (cur_network->IELength+len_diff);
2511                         pattrib->pktlen += (cur_network->IELength+len_diff);
2512                 }
2513
2514                 {
2515                         u8 *wps_ie;
2516                         uint wps_ielen;
2517                         u8 sr = 0;
2518                         wps_ie = rtw_get_wps_ie(pmgntframe->buf_addr+TXDESC_OFFSET+sizeof(struct ieee80211_hdr_3addr)+_BEACON_IE_OFFSET_,
2519                                 pattrib->pktlen-sizeof(struct ieee80211_hdr_3addr)-_BEACON_IE_OFFSET_, NULL, &wps_ielen);
2520                         if (wps_ie && wps_ielen > 0) {
2521                                 rtw_get_wps_attr_content(wps_ie,  wps_ielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL);
2522                         }
2523                         if (sr != 0)
2524                                 set_fwstate(pmlmepriv, WIFI_UNDER_WPS);
2525                         else
2526                                 _clr_fwstate_(pmlmepriv, WIFI_UNDER_WPS);
2527                 }
2528
2529                 goto _issue_bcn;
2530
2531         }
2532
2533         /* below for ad-hoc mode */
2534
2535         /* timestamp will be inserted by hardware */
2536         pframe += 8;
2537         pattrib->pktlen += 8;
2538
2539         /*  beacon interval: 2 bytes */
2540
2541         memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2);
2542
2543         pframe += 2;
2544         pattrib->pktlen += 2;
2545
2546         /*  capability info: 2 bytes */
2547
2548         memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2);
2549
2550         pframe += 2;
2551         pattrib->pktlen += 2;
2552
2553         /*  SSID */
2554         pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pattrib->pktlen);
2555
2556         /*  supported rates... */
2557         rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
2558         pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, ((rate_len > 8) ? 8 : rate_len), cur_network->SupportedRates, &pattrib->pktlen);
2559
2560         /*  DS parameter set */
2561         pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pattrib->pktlen);
2562
2563         /* if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) */
2564         {
2565                 u8 erpinfo = 0;
2566                 u32 ATIMWindow;
2567                 /*  IBSS Parameter Set... */
2568                 /* ATIMWindow = cur->Configuration.ATIMWindow; */
2569                 ATIMWindow = 0;
2570                 pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pattrib->pktlen);
2571
2572                 /* ERP IE */
2573                 pframe = rtw_set_ie(pframe, _ERPINFO_IE_, 1, &erpinfo, &pattrib->pktlen);
2574         }
2575
2576
2577         /*  EXTERNDED SUPPORTED RATE */
2578         if (rate_len > 8) {
2579                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pattrib->pktlen);
2580         }
2581
2582
2583         /* todo:HT for adhoc */
2584
2585 _issue_bcn:
2586
2587         pmlmepriv->update_bcn = false;
2588
2589         spin_unlock_bh(&pmlmepriv->bcn_update_lock);
2590
2591         if ((pattrib->pktlen + TXDESC_SIZE) > 512) {
2592                 DBG_871X("beacon frame too large\n");
2593                 return;
2594         }
2595
2596         pattrib->last_txcmdsz = pattrib->pktlen;
2597
2598         /* DBG_871X("issue bcn_sz =%d\n", pattrib->last_txcmdsz); */
2599         if (timeout_ms > 0)
2600                 dump_mgntframe_and_wait(padapter, pmgntframe, timeout_ms);
2601         else
2602                 dump_mgntframe(padapter, pmgntframe);
2603
2604 }
2605
2606 void issue_probersp(struct adapter *padapter, unsigned char *da, u8 is_valid_p2p_probereq)
2607 {
2608         struct xmit_frame                       *pmgntframe;
2609         struct pkt_attrib                       *pattrib;
2610         unsigned char                           *pframe;
2611         struct ieee80211_hdr    *pwlanhdr;
2612         __le16 *fctrl;
2613         unsigned char                           *mac, *bssid;
2614         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
2615
2616         u8 *pwps_ie;
2617         uint wps_ielen;
2618         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
2619         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2620         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
2621         struct wlan_bssid_ex            *cur_network = &(pmlmeinfo->network);
2622         unsigned int    rate_len;
2623
2624         /* DBG_871X("%s\n", __func__); */
2625
2626         if (da == NULL)
2627                 return;
2628
2629         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2630         if (pmgntframe == NULL) {
2631                 DBG_871X("%s, alloc mgnt frame fail\n", __func__);
2632                 return;
2633         }
2634
2635
2636         /* update attribute */
2637         pattrib = &pmgntframe->attrib;
2638         update_mgntframe_attrib(padapter, pattrib);
2639
2640         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2641
2642         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2643         pwlanhdr = (struct ieee80211_hdr *)pframe;
2644
2645         mac = myid(&(padapter->eeprompriv));
2646         bssid = cur_network->MacAddress;
2647
2648         fctrl = &(pwlanhdr->frame_control);
2649         *(fctrl) = 0;
2650         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
2651         memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
2652         memcpy(pwlanhdr->addr3, bssid, ETH_ALEN);
2653
2654         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
2655         pmlmeext->mgnt_seq++;
2656         SetFrameSubType(fctrl, WIFI_PROBERSP);
2657
2658         pattrib->hdrlen = sizeof(struct ieee80211_hdr_3addr);
2659         pattrib->pktlen = pattrib->hdrlen;
2660         pframe += pattrib->hdrlen;
2661
2662
2663         if (cur_network->IELength > MAX_IE_SZ)
2664                 return;
2665
2666         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE) {
2667                 pwps_ie = rtw_get_wps_ie(cur_network->IEs+_FIXED_IE_LENGTH_, cur_network->IELength-_FIXED_IE_LENGTH_, NULL, &wps_ielen);
2668
2669                 /* inerset & update wps_probe_resp_ie */
2670                 if ((pmlmepriv->wps_probe_resp_ie != NULL) && pwps_ie && (wps_ielen > 0)) {
2671                         uint wps_offset, remainder_ielen;
2672                         u8 *premainder_ie;
2673
2674                         wps_offset = (uint)(pwps_ie - cur_network->IEs);
2675
2676                         premainder_ie = pwps_ie + wps_ielen;
2677
2678                         remainder_ielen = cur_network->IELength - wps_offset - wps_ielen;
2679
2680                         memcpy(pframe, cur_network->IEs, wps_offset);
2681                         pframe += wps_offset;
2682                         pattrib->pktlen += wps_offset;
2683
2684                         wps_ielen = (uint)pmlmepriv->wps_probe_resp_ie[1];/* to get ie data len */
2685                         if ((wps_offset+wps_ielen+2) <= MAX_IE_SZ) {
2686                                 memcpy(pframe, pmlmepriv->wps_probe_resp_ie, wps_ielen+2);
2687                                 pframe += wps_ielen+2;
2688                                 pattrib->pktlen += wps_ielen+2;
2689                         }
2690
2691                         if ((wps_offset+wps_ielen+2+remainder_ielen) <= MAX_IE_SZ) {
2692                                 memcpy(pframe, premainder_ie, remainder_ielen);
2693                                 pframe += remainder_ielen;
2694                                 pattrib->pktlen += remainder_ielen;
2695                         }
2696                 } else{
2697                         memcpy(pframe, cur_network->IEs, cur_network->IELength);
2698                         pframe += cur_network->IELength;
2699                         pattrib->pktlen += cur_network->IELength;
2700                 }
2701
2702                 /* retrieve SSID IE from cur_network->Ssid */
2703                 {
2704                         u8 *ssid_ie;
2705                         sint ssid_ielen;
2706                         sint ssid_ielen_diff;
2707                         u8 buf[MAX_IE_SZ];
2708                         u8 *ies = pmgntframe->buf_addr+TXDESC_OFFSET+sizeof(struct ieee80211_hdr_3addr);
2709
2710                         ssid_ie = rtw_get_ie(ies+_FIXED_IE_LENGTH_, _SSID_IE_, &ssid_ielen,
2711                                 (pframe-ies)-_FIXED_IE_LENGTH_);
2712
2713                         ssid_ielen_diff = cur_network->Ssid.SsidLength - ssid_ielen;
2714
2715                         if (ssid_ie &&  cur_network->Ssid.SsidLength) {
2716                                 uint remainder_ielen;
2717                                 u8 *remainder_ie;
2718                                 remainder_ie = ssid_ie+2;
2719                                 remainder_ielen = (pframe-remainder_ie);
2720
2721                                 if (remainder_ielen > MAX_IE_SZ) {
2722                                         DBG_871X_LEVEL(_drv_warning_, FUNC_ADPT_FMT" remainder_ielen > MAX_IE_SZ\n", FUNC_ADPT_ARG(padapter));
2723                                         remainder_ielen = MAX_IE_SZ;
2724                                 }
2725
2726                                 memcpy(buf, remainder_ie, remainder_ielen);
2727                                 memcpy(remainder_ie+ssid_ielen_diff, buf, remainder_ielen);
2728                                 *(ssid_ie+1) = cur_network->Ssid.SsidLength;
2729                                 memcpy(ssid_ie+2, cur_network->Ssid.Ssid, cur_network->Ssid.SsidLength);
2730
2731                                 pframe += ssid_ielen_diff;
2732                                 pattrib->pktlen += ssid_ielen_diff;
2733                         }
2734                 }
2735         } else{
2736                 /* timestamp will be inserted by hardware */
2737                 pframe += 8;
2738                 pattrib->pktlen += 8;
2739
2740                 /*  beacon interval: 2 bytes */
2741
2742                 memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2);
2743
2744                 pframe += 2;
2745                 pattrib->pktlen += 2;
2746
2747                 /*  capability info: 2 bytes */
2748
2749                 memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2);
2750
2751                 pframe += 2;
2752                 pattrib->pktlen += 2;
2753
2754                 /* below for ad-hoc mode */
2755
2756                 /*  SSID */
2757                 pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pattrib->pktlen);
2758
2759                 /*  supported rates... */
2760                 rate_len = rtw_get_rateset_len(cur_network->SupportedRates);
2761                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, ((rate_len > 8) ? 8 : rate_len), cur_network->SupportedRates, &pattrib->pktlen);
2762
2763                 /*  DS parameter set */
2764                 pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pattrib->pktlen);
2765
2766                 if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
2767                         u8 erpinfo = 0;
2768                         u32 ATIMWindow;
2769                         /*  IBSS Parameter Set... */
2770                         /* ATIMWindow = cur->Configuration.ATIMWindow; */
2771                         ATIMWindow = 0;
2772                         pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pattrib->pktlen);
2773
2774                         /* ERP IE */
2775                         pframe = rtw_set_ie(pframe, _ERPINFO_IE_, 1, &erpinfo, &pattrib->pktlen);
2776                 }
2777
2778
2779                 /*  EXTERNDED SUPPORTED RATE */
2780                 if (rate_len > 8) {
2781                         pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pattrib->pktlen);
2782                 }
2783
2784
2785                 /* todo:HT for adhoc */
2786
2787         }
2788
2789 #ifdef CONFIG_AUTO_AP_MODE
2790 {
2791         struct sta_info *psta;
2792         struct sta_priv *pstapriv = &padapter->stapriv;
2793
2794         DBG_871X("(%s)\n", __func__);
2795
2796         /* check rc station */
2797         psta = rtw_get_stainfo(pstapriv, da);
2798         if (psta && psta->isrc && psta->pid > 0) {
2799                 u8 RC_OUI[4] = {0x00, 0xE0, 0x4C, 0x0A};
2800                 u8 RC_INFO[14] = {0};
2801                 /* EID[1] + EID_LEN[1] + RC_OUI[4] + MAC[6] + PairingID[2] + ChannelNum[2] */
2802                 u16 cu_ch = (u16)cur_network->Configuration.DSConfig;
2803
2804                 DBG_871X("%s, reply rc(pid = 0x%x) device "MAC_FMT" in ch =%d\n", __func__,
2805                         psta->pid, MAC_ARG(psta->hwaddr), cu_ch);
2806
2807                 /* append vendor specific ie */
2808                 memcpy(RC_INFO, RC_OUI, sizeof(RC_OUI));
2809                 memcpy(&RC_INFO[4], mac, ETH_ALEN);
2810                 memcpy(&RC_INFO[10], (u8 *)&psta->pid, 2);
2811                 memcpy(&RC_INFO[12], (u8 *)&cu_ch, 2);
2812
2813                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, sizeof(RC_INFO), RC_INFO, &pattrib->pktlen);
2814         }
2815 }
2816 #endif /* CONFIG_AUTO_AP_MODE */
2817
2818
2819         pattrib->last_txcmdsz = pattrib->pktlen;
2820
2821
2822         dump_mgntframe(padapter, pmgntframe);
2823
2824         return;
2825
2826 }
2827
2828 static int _issue_probereq(struct adapter *padapter,
2829                            struct ndis_802_11_ssid *pssid,
2830                            u8 *da, u8 ch, bool append_wps, bool wait_ack)
2831 {
2832         int ret = _FAIL;
2833         struct xmit_frame               *pmgntframe;
2834         struct pkt_attrib               *pattrib;
2835         unsigned char           *pframe;
2836         struct ieee80211_hdr    *pwlanhdr;
2837         __le16 *fctrl;
2838         unsigned char           *mac;
2839         unsigned char           bssrate[NumRates];
2840         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
2841         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2842         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2843         int     bssrate_len = 0;
2844         u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2845
2846         RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_, ("+issue_probereq\n"));
2847
2848         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2849         if (pmgntframe == NULL)
2850                 goto exit;
2851
2852         /* update attribute */
2853         pattrib = &pmgntframe->attrib;
2854         update_mgntframe_attrib(padapter, pattrib);
2855
2856
2857         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2858
2859         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2860         pwlanhdr = (struct ieee80211_hdr *)pframe;
2861
2862         mac = myid(&(padapter->eeprompriv));
2863
2864         fctrl = &(pwlanhdr->frame_control);
2865         *(fctrl) = 0;
2866
2867         if (da) {
2868                 /*      unicast probe request frame */
2869                 memcpy(pwlanhdr->addr1, da, ETH_ALEN);
2870                 memcpy(pwlanhdr->addr3, da, ETH_ALEN);
2871         } else{
2872                 /*      broadcast probe request frame */
2873                 memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN);
2874                 memcpy(pwlanhdr->addr3, bc_addr, ETH_ALEN);
2875         }
2876
2877         memcpy(pwlanhdr->addr2, mac, ETH_ALEN);
2878
2879         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
2880         pmlmeext->mgnt_seq++;
2881         SetFrameSubType(pframe, WIFI_PROBEREQ);
2882
2883         pframe += sizeof(struct ieee80211_hdr_3addr);
2884         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
2885
2886         if (pssid)
2887                 pframe = rtw_set_ie(pframe, _SSID_IE_, pssid->SsidLength, pssid->Ssid, &(pattrib->pktlen));
2888         else
2889                 pframe = rtw_set_ie(pframe, _SSID_IE_, 0, NULL, &(pattrib->pktlen));
2890
2891         get_rate_set(padapter, bssrate, &bssrate_len);
2892
2893         if (bssrate_len > 8) {
2894                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, bssrate, &(pattrib->pktlen));
2895                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (bssrate_len - 8), (bssrate + 8), &(pattrib->pktlen));
2896         } else{
2897                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, bssrate_len, bssrate, &(pattrib->pktlen));
2898         }
2899
2900         if (ch)
2901                 pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, &ch, &pattrib->pktlen);
2902
2903         if (append_wps) {
2904                 /* add wps_ie for wps2.0 */
2905                 if (pmlmepriv->wps_probe_req_ie_len > 0 && pmlmepriv->wps_probe_req_ie) {
2906                         memcpy(pframe, pmlmepriv->wps_probe_req_ie, pmlmepriv->wps_probe_req_ie_len);
2907                         pframe += pmlmepriv->wps_probe_req_ie_len;
2908                         pattrib->pktlen += pmlmepriv->wps_probe_req_ie_len;
2909                 }
2910         }
2911
2912         pattrib->last_txcmdsz = pattrib->pktlen;
2913
2914         RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_, ("issuing probe_req, tx_len =%d\n", pattrib->last_txcmdsz));
2915
2916         if (wait_ack) {
2917                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
2918         } else {
2919                 dump_mgntframe(padapter, pmgntframe);
2920                 ret = _SUCCESS;
2921         }
2922
2923 exit:
2924         return ret;
2925 }
2926
2927 inline void issue_probereq(struct adapter *padapter, struct ndis_802_11_ssid *pssid, u8 *da)
2928 {
2929         _issue_probereq(padapter, pssid, da, 0, 1, false);
2930 }
2931
2932 int issue_probereq_ex(struct adapter *padapter, struct ndis_802_11_ssid *pssid, u8 *da, u8 ch, bool append_wps,
2933         int try_cnt, int wait_ms)
2934 {
2935         int ret;
2936         int i = 0;
2937
2938         do {
2939                 ret = _issue_probereq(padapter, pssid, da, ch, append_wps, wait_ms > 0?true:false);
2940
2941                 i++;
2942
2943                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
2944                         break;
2945
2946                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
2947                         msleep(wait_ms);
2948
2949         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
2950
2951         if (ret != _FAIL) {
2952                 ret = _SUCCESS;
2953                 #ifndef DBG_XMIT_ACK
2954                 goto exit;
2955                 #endif
2956         }
2957
2958         if (try_cnt && wait_ms) {
2959                 if (da)
2960                         DBG_871X(FUNC_ADPT_FMT" to "MAC_FMT", ch:%u%s, %d/%d in %u ms\n",
2961                                 FUNC_ADPT_ARG(padapter), MAC_ARG(da), rtw_get_oper_ch(padapter),
2962                                 ret == _SUCCESS?", acked":"", i, try_cnt, (i + 1) * wait_ms);
2963                 else
2964                         DBG_871X(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
2965                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
2966                                 ret == _SUCCESS?", acked":"", i, try_cnt, (i + 1) * wait_ms);
2967         }
2968 exit:
2969         return ret;
2970 }
2971
2972 /*  if psta == NULL, indiate we are station(client) now... */
2973 void issue_auth(struct adapter *padapter, struct sta_info *psta, unsigned short status)
2974 {
2975         struct xmit_frame                       *pmgntframe;
2976         struct pkt_attrib                       *pattrib;
2977         unsigned char                           *pframe;
2978         struct ieee80211_hdr    *pwlanhdr;
2979         __le16 *fctrl;
2980         unsigned int                                    val32;
2981         unsigned short                          val16;
2982         int use_shared_key = 0;
2983         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
2984         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2985         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
2986         __le16 le_tmp;
2987
2988         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2989         if (pmgntframe == NULL)
2990                 return;
2991
2992         /* update attribute */
2993         pattrib = &pmgntframe->attrib;
2994         update_mgntframe_attrib(padapter, pattrib);
2995
2996         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2997
2998         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2999         pwlanhdr = (struct ieee80211_hdr *)pframe;
3000
3001         fctrl = &(pwlanhdr->frame_control);
3002         *(fctrl) = 0;
3003
3004         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3005         pmlmeext->mgnt_seq++;
3006         SetFrameSubType(pframe, WIFI_AUTH);
3007
3008         pframe += sizeof(struct ieee80211_hdr_3addr);
3009         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
3010
3011
3012         if (psta) { /*  for AP mode */
3013                 memcpy(pwlanhdr->addr1, psta->hwaddr, ETH_ALEN);
3014                 memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
3015                 memcpy(pwlanhdr->addr3, myid(&(padapter->eeprompriv)), ETH_ALEN);
3016
3017                 /*  setting auth algo number */
3018                 val16 = (u16)psta->authalg;
3019
3020                 if (status != _STATS_SUCCESSFUL_)
3021                         val16 = 0;
3022
3023                 if (val16)
3024                         use_shared_key = 1;
3025
3026                 le_tmp = cpu_to_le16(val16);
3027
3028                 pframe = rtw_set_fixed_ie(pframe, _AUTH_ALGM_NUM_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
3029
3030                 /*  setting auth seq number */
3031                 val16 = (u16)psta->auth_seq;
3032                 le_tmp = cpu_to_le16(val16);
3033                 pframe = rtw_set_fixed_ie(pframe, _AUTH_SEQ_NUM_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
3034
3035                 /*  setting status code... */
3036                 val16 = status;
3037                 le_tmp = cpu_to_le16(val16);
3038                 pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
3039
3040                 /*  added challenging text... */
3041                 if ((psta->auth_seq == 2) && (psta->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1))
3042                         pframe = rtw_set_ie(pframe, _CHLGETXT_IE_, 128, psta->chg_txt, &(pattrib->pktlen));
3043
3044         } else{
3045                 memcpy(pwlanhdr->addr1, get_my_bssid(&pmlmeinfo->network), ETH_ALEN);
3046                 memcpy(pwlanhdr->addr2, myid(&padapter->eeprompriv), ETH_ALEN);
3047                 memcpy(pwlanhdr->addr3, get_my_bssid(&pmlmeinfo->network), ETH_ALEN);
3048
3049                 /*  setting auth algo number */
3050                 val16 = (pmlmeinfo->auth_algo == dot11AuthAlgrthm_Shared) ? 1 : 0;/*  0:OPEN System, 1:Shared key */
3051                 if (val16) {
3052                         use_shared_key = 1;
3053                 }
3054                 le_tmp = cpu_to_le16(val16);
3055                 /* DBG_871X("%s auth_algo = %s auth_seq =%d\n", __func__, (pmlmeinfo->auth_algo == 0)?"OPEN":"SHARED", pmlmeinfo->auth_seq); */
3056
3057                 /* setting IV for auth seq #3 */
3058                 if ((pmlmeinfo->auth_seq == 3) && (pmlmeinfo->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1)) {
3059                         __le32 le_tmp32;
3060
3061                         /* DBG_871X("==> iv(%d), key_index(%d)\n", pmlmeinfo->iv, pmlmeinfo->key_index); */
3062                         val32 = ((pmlmeinfo->iv++) | (pmlmeinfo->key_index << 30));
3063                         le_tmp32 = cpu_to_le32(val32);
3064                         pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *)&le_tmp32, &(pattrib->pktlen));
3065
3066                         pattrib->iv_len = 4;
3067                 }
3068
3069                 pframe = rtw_set_fixed_ie(pframe, _AUTH_ALGM_NUM_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
3070
3071                 /*  setting auth seq number */
3072                 le_tmp = cpu_to_le16(pmlmeinfo->auth_seq);
3073                 pframe = rtw_set_fixed_ie(pframe, _AUTH_SEQ_NUM_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
3074
3075
3076                 /*  setting status code... */
3077                 le_tmp = cpu_to_le16(status);
3078                 pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
3079
3080                 /*  then checking to see if sending challenging text... */
3081                 if ((pmlmeinfo->auth_seq == 3) && (pmlmeinfo->state & WIFI_FW_AUTH_STATE) && (use_shared_key == 1)) {
3082                         pframe = rtw_set_ie(pframe, _CHLGETXT_IE_, 128, pmlmeinfo->chg_txt, &(pattrib->pktlen));
3083
3084                         SetPrivacy(fctrl);
3085
3086                         pattrib->hdrlen = sizeof(struct ieee80211_hdr_3addr);
3087
3088                         pattrib->encrypt = _WEP40_;
3089
3090                         pattrib->icv_len = 4;
3091
3092                         pattrib->pktlen += pattrib->icv_len;
3093
3094                 }
3095
3096         }
3097
3098         pattrib->last_txcmdsz = pattrib->pktlen;
3099
3100         rtw_wep_encrypt(padapter, (u8 *)pmgntframe);
3101         DBG_871X("%s\n", __func__);
3102         dump_mgntframe(padapter, pmgntframe);
3103
3104         return;
3105 }
3106
3107
3108 void issue_asocrsp(struct adapter *padapter, unsigned short status, struct sta_info *pstat, int pkt_type)
3109 {
3110         struct xmit_frame       *pmgntframe;
3111         struct ieee80211_hdr    *pwlanhdr;
3112         struct pkt_attrib *pattrib;
3113         unsigned char *pbuf, *pframe;
3114         unsigned short val;
3115         __le16 *fctrl;
3116         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
3117         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3118         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3119         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
3120         struct wlan_bssid_ex *pnetwork = &(pmlmeinfo->network);
3121         u8 *ie = pnetwork->IEs;
3122         __le16 lestatus, le_tmp;
3123
3124         DBG_871X("%s\n", __func__);
3125
3126         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3127         if (pmgntframe == NULL)
3128                 return;
3129
3130         /* update attribute */
3131         pattrib = &pmgntframe->attrib;
3132         update_mgntframe_attrib(padapter, pattrib);
3133
3134
3135         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3136
3137         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3138         pwlanhdr = (struct ieee80211_hdr *)pframe;
3139
3140         fctrl = &(pwlanhdr->frame_control);
3141         *(fctrl) = 0;
3142
3143         memcpy((void *)GetAddr1Ptr(pwlanhdr), pstat->hwaddr, ETH_ALEN);
3144         memcpy((void *)GetAddr2Ptr(pwlanhdr), myid(&(padapter->eeprompriv)), ETH_ALEN);
3145         memcpy((void *)GetAddr3Ptr(pwlanhdr), get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3146
3147
3148         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3149         pmlmeext->mgnt_seq++;
3150         if ((pkt_type == WIFI_ASSOCRSP) || (pkt_type == WIFI_REASSOCRSP))
3151                 SetFrameSubType(pwlanhdr, pkt_type);
3152         else
3153                 return;
3154
3155         pattrib->hdrlen = sizeof(struct ieee80211_hdr_3addr);
3156         pattrib->pktlen += pattrib->hdrlen;
3157         pframe += pattrib->hdrlen;
3158
3159         /* capability */
3160         val = *(unsigned short *)rtw_get_capability_from_ie(ie);
3161
3162         pframe = rtw_set_fixed_ie(pframe, _CAPABILITY_, (unsigned char *)&val, &(pattrib->pktlen));
3163
3164         lestatus = cpu_to_le16(status);
3165         pframe = rtw_set_fixed_ie(pframe, _STATUS_CODE_, (unsigned char *)&lestatus, &(pattrib->pktlen));
3166
3167         le_tmp = cpu_to_le16(pstat->aid | BIT(14) | BIT(15));
3168         pframe = rtw_set_fixed_ie(pframe, _ASOC_ID_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
3169
3170         if (pstat->bssratelen <= 8) {
3171                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, pstat->bssratelen, pstat->bssrateset, &(pattrib->pktlen));
3172         } else{
3173                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, pstat->bssrateset, &(pattrib->pktlen));
3174                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (pstat->bssratelen-8), pstat->bssrateset+8, &(pattrib->pktlen));
3175         }
3176
3177         if ((pstat->flags & WLAN_STA_HT) && (pmlmepriv->htpriv.ht_option)) {
3178                 uint ie_len = 0;
3179
3180                 /* FILL HT CAP INFO IE */
3181                 /* p = hostapd_eid_ht_capabilities_info(hapd, p); */
3182                 pbuf = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_CAPABILITY_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
3183                 if (pbuf && ie_len > 0) {
3184                         memcpy(pframe, pbuf, ie_len+2);
3185                         pframe += (ie_len+2);
3186                         pattrib->pktlen += (ie_len+2);
3187                 }
3188
3189                 /* FILL HT ADD INFO IE */
3190                 /* p = hostapd_eid_ht_operation(hapd, p); */
3191                 pbuf = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _HT_ADD_INFO_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_));
3192                 if (pbuf && ie_len > 0) {
3193                         memcpy(pframe, pbuf, ie_len+2);
3194                         pframe += (ie_len+2);
3195                         pattrib->pktlen += (ie_len+2);
3196                 }
3197
3198         }
3199
3200         /* FILL WMM IE */
3201         if ((pstat->flags & WLAN_STA_WME) && (pmlmepriv->qospriv.qos_option)) {
3202                 uint ie_len = 0;
3203                 unsigned char WMM_PARA_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x01, 0x01};
3204
3205                 for (pbuf = ie + _BEACON_IE_OFFSET_; ; pbuf += (ie_len + 2)) {
3206                         pbuf = rtw_get_ie(pbuf, _VENDOR_SPECIFIC_IE_, &ie_len, (pnetwork->IELength - _BEACON_IE_OFFSET_ - (ie_len + 2)));
3207                         if (pbuf && !memcmp(pbuf+2, WMM_PARA_IE, 6)) {
3208                                 memcpy(pframe, pbuf, ie_len+2);
3209                                 pframe += (ie_len+2);
3210                                 pattrib->pktlen += (ie_len+2);
3211
3212                                 break;
3213                         }
3214
3215                         if ((pbuf == NULL) || (ie_len == 0)) {
3216                                 break;
3217                         }
3218                 }
3219
3220         }
3221
3222
3223         if (pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_REALTEK) {
3224                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, 6, REALTEK_96B_IE, &(pattrib->pktlen));
3225         }
3226
3227         /* add WPS IE ie for wps 2.0 */
3228         if (pmlmepriv->wps_assoc_resp_ie && pmlmepriv->wps_assoc_resp_ie_len > 0) {
3229                 memcpy(pframe, pmlmepriv->wps_assoc_resp_ie, pmlmepriv->wps_assoc_resp_ie_len);
3230
3231                 pframe += pmlmepriv->wps_assoc_resp_ie_len;
3232                 pattrib->pktlen += pmlmepriv->wps_assoc_resp_ie_len;
3233         }
3234
3235         pattrib->last_txcmdsz = pattrib->pktlen;
3236
3237         dump_mgntframe(padapter, pmgntframe);
3238 }
3239
3240 void issue_assocreq(struct adapter *padapter)
3241 {
3242         int ret = _FAIL;
3243         struct xmit_frame                               *pmgntframe;
3244         struct pkt_attrib                               *pattrib;
3245         unsigned char                           *pframe;
3246         struct ieee80211_hdr                    *pwlanhdr;
3247         __le16 *fctrl;
3248         __le16 val16;
3249         unsigned int                                    i, j, index = 0;
3250         unsigned char bssrate[NumRates], sta_bssrate[NumRates];
3251         struct ndis_80211_var_ie *pIE;
3252         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
3253         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
3254         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3255         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
3256         int     bssrate_len = 0, sta_bssrate_len = 0;
3257         u8 vs_ie_length = 0;
3258
3259         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3260         if (pmgntframe == NULL)
3261                 goto exit;
3262
3263         /* update attribute */
3264         pattrib = &pmgntframe->attrib;
3265         update_mgntframe_attrib(padapter, pattrib);
3266
3267
3268         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3269
3270         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3271         pwlanhdr = (struct ieee80211_hdr *)pframe;
3272
3273         fctrl = &(pwlanhdr->frame_control);
3274         *(fctrl) = 0;
3275         memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3276         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
3277         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3278
3279         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3280         pmlmeext->mgnt_seq++;
3281         SetFrameSubType(pframe, WIFI_ASSOCREQ);
3282
3283         pframe += sizeof(struct ieee80211_hdr_3addr);
3284         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
3285
3286         /* caps */
3287         memcpy(pframe, rtw_get_capability_from_ie(pmlmeinfo->network.IEs), 2);
3288
3289         pframe += 2;
3290         pattrib->pktlen += 2;
3291
3292         /* listen interval */
3293         /* todo: listen interval for power saving */
3294         val16 = cpu_to_le16(3);
3295         memcpy(pframe, (unsigned char *)&val16, 2);
3296         pframe += 2;
3297         pattrib->pktlen += 2;
3298
3299         /* SSID */
3300         pframe = rtw_set_ie(pframe, _SSID_IE_,  pmlmeinfo->network.Ssid.SsidLength, pmlmeinfo->network.Ssid.Ssid, &(pattrib->pktlen));
3301
3302         /* supported rate & extended supported rate */
3303
3304         /*  Check if the AP's supported rates are also supported by STA. */
3305         get_rate_set(padapter, sta_bssrate, &sta_bssrate_len);
3306         /* DBG_871X("sta_bssrate_len =%d\n", sta_bssrate_len); */
3307
3308         if (pmlmeext->cur_channel == 14) /*  for JAPAN, channel 14 can only uses B Mode(CCK) */
3309                 sta_bssrate_len = 4;
3310
3311
3312         /* for (i = 0; i < sta_bssrate_len; i++) { */
3313         /*      DBG_871X("sta_bssrate[%d]=%02X\n", i, sta_bssrate[i]); */
3314         /*  */
3315
3316         for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) {
3317                 if (pmlmeinfo->network.SupportedRates[i] == 0)
3318                         break;
3319                 DBG_871X("network.SupportedRates[%d]=%02X\n", i, pmlmeinfo->network.SupportedRates[i]);
3320         }
3321
3322
3323         for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) {
3324                 if (pmlmeinfo->network.SupportedRates[i] == 0)
3325                         break;
3326
3327
3328                 /*  Check if the AP's supported rates are also supported by STA. */
3329                 for (j = 0; j < sta_bssrate_len; j++) {
3330                          /*  Avoid the proprietary data rate (22Mbps) of Handlink WSG-4000 AP */
3331                         if ((pmlmeinfo->network.SupportedRates[i]|IEEE80211_BASIC_RATE_MASK)
3332                                         == (sta_bssrate[j]|IEEE80211_BASIC_RATE_MASK)) {
3333                                 /* DBG_871X("match i = %d, j =%d\n", i, j); */
3334                                 break;
3335                         } else {
3336                                 /* DBG_871X("not match: %02X != %02X\n", (pmlmeinfo->network.SupportedRates[i]|IEEE80211_BASIC_RATE_MASK), (sta_bssrate[j]|IEEE80211_BASIC_RATE_MASK)); */
3337                         }
3338                 }
3339
3340                 if (j == sta_bssrate_len) {
3341                         /*  the rate is not supported by STA */
3342                         DBG_871X("%s(): the rate[%d]=%02X is not supported by STA!\n", __func__, i, pmlmeinfo->network.SupportedRates[i]);
3343                 } else {
3344                         /*  the rate is supported by STA */
3345                         bssrate[index++] = pmlmeinfo->network.SupportedRates[i];
3346                 }
3347         }
3348
3349         bssrate_len = index;
3350         DBG_871X("bssrate_len = %d\n", bssrate_len);
3351
3352         if (bssrate_len == 0) {
3353                 rtw_free_xmitbuf(pxmitpriv, pmgntframe->pxmitbuf);
3354                 rtw_free_xmitframe(pxmitpriv, pmgntframe);
3355                 goto exit; /* don't connect to AP if no joint supported rate */
3356         }
3357
3358
3359         if (bssrate_len > 8) {
3360                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, bssrate, &(pattrib->pktlen));
3361                 pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (bssrate_len - 8), (bssrate + 8), &(pattrib->pktlen));
3362         } else
3363                 pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, bssrate_len, bssrate, &(pattrib->pktlen));
3364
3365         /* vendor specific IE, such as WPA, WMM, WPS */
3366         for (i = sizeof(struct ndis_802_11_fix_ie); i < pmlmeinfo->network.IELength;) {
3367                 pIE = (struct ndis_80211_var_ie *)(pmlmeinfo->network.IEs + i);
3368
3369                 switch (pIE->ElementID) {
3370                 case _VENDOR_SPECIFIC_IE_:
3371                         if ((!memcmp(pIE->data, RTW_WPA_OUI, 4)) ||
3372                                         (!memcmp(pIE->data, WMM_OUI, 4)) ||
3373                                         (!memcmp(pIE->data, WPS_OUI, 4))) {
3374                                 vs_ie_length = pIE->Length;
3375                                 if ((!padapter->registrypriv.wifi_spec) && (!memcmp(pIE->data, WPS_OUI, 4))) {
3376                                         /* Commented by Kurt 20110629 */
3377                                         /* In some older APs, WPS handshake */
3378                                         /* would be fail if we append vender extensions informations to AP */
3379
3380                                         vs_ie_length = 14;
3381                                 }
3382
3383                                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, vs_ie_length, pIE->data, &(pattrib->pktlen));
3384                         }
3385                         break;
3386
3387                 case EID_WPA2:
3388                         pframe = rtw_set_ie(pframe, EID_WPA2, pIE->Length, pIE->data, &(pattrib->pktlen));
3389                         break;
3390                 case EID_HTCapability:
3391                         if (padapter->mlmepriv.htpriv.ht_option) {
3392                                 if (!(is_ap_in_tkip(padapter))) {
3393                                         memcpy(&(pmlmeinfo->HT_caps), pIE->data, sizeof(struct HT_caps_element));
3394                                         pframe = rtw_set_ie(pframe, EID_HTCapability, pIE->Length, (u8 *)(&(pmlmeinfo->HT_caps)), &(pattrib->pktlen));
3395                                 }
3396                         }
3397                         break;
3398
3399                 case EID_EXTCapability:
3400                         if (padapter->mlmepriv.htpriv.ht_option)
3401                                 pframe = rtw_set_ie(pframe, EID_EXTCapability, pIE->Length, pIE->data, &(pattrib->pktlen));
3402                         break;
3403                 default:
3404                         break;
3405                 }
3406
3407                 i += (pIE->Length + 2);
3408         }
3409
3410         if (pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_REALTEK)
3411                 pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, 6, REALTEK_96B_IE, &(pattrib->pktlen));
3412
3413
3414         pattrib->last_txcmdsz = pattrib->pktlen;
3415         dump_mgntframe(padapter, pmgntframe);
3416
3417         ret = _SUCCESS;
3418
3419 exit:
3420         if (ret == _SUCCESS)
3421                 rtw_buf_update(&pmlmepriv->assoc_req, &pmlmepriv->assoc_req_len, (u8 *)pwlanhdr, pattrib->pktlen);
3422         else
3423                 rtw_buf_free(&pmlmepriv->assoc_req, &pmlmepriv->assoc_req_len);
3424
3425         return;
3426 }
3427
3428 /* when wait_ack is ture, this function shoule be called at process context */
3429 static int _issue_nulldata(struct adapter *padapter, unsigned char *da,
3430                            unsigned int power_mode, bool wait_ack)
3431 {
3432         int ret = _FAIL;
3433         struct xmit_frame                       *pmgntframe;
3434         struct pkt_attrib                       *pattrib;
3435         unsigned char                           *pframe;
3436         struct ieee80211_hdr    *pwlanhdr;
3437         __le16 *fctrl;
3438         struct xmit_priv *pxmitpriv;
3439         struct mlme_ext_priv *pmlmeext;
3440         struct mlme_ext_info *pmlmeinfo;
3441
3442         /* DBG_871X("%s:%d\n", __func__, power_mode); */
3443
3444         if (!padapter)
3445                 goto exit;
3446
3447         pxmitpriv = &(padapter->xmitpriv);
3448         pmlmeext = &(padapter->mlmeextpriv);
3449         pmlmeinfo = &(pmlmeext->mlmext_info);
3450
3451         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3452         if (pmgntframe == NULL)
3453                 goto exit;
3454
3455         /* update attribute */
3456         pattrib = &pmgntframe->attrib;
3457         update_mgntframe_attrib(padapter, pattrib);
3458         pattrib->retry_ctrl = false;
3459
3460         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3461
3462         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3463         pwlanhdr = (struct ieee80211_hdr *)pframe;
3464
3465         fctrl = &(pwlanhdr->frame_control);
3466         *(fctrl) = 0;
3467
3468         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
3469                 SetFrDs(fctrl);
3470         else if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
3471                 SetToDs(fctrl);
3472
3473         if (power_mode)
3474                 SetPwrMgt(fctrl);
3475
3476         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
3477         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
3478         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3479
3480         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3481         pmlmeext->mgnt_seq++;
3482         SetFrameSubType(pframe, WIFI_DATA_NULL);
3483
3484         pframe += sizeof(struct ieee80211_hdr_3addr);
3485         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
3486
3487         pattrib->last_txcmdsz = pattrib->pktlen;
3488
3489         if (wait_ack) {
3490                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
3491         } else{
3492                 dump_mgntframe(padapter, pmgntframe);
3493                 ret = _SUCCESS;
3494         }
3495
3496 exit:
3497         return ret;
3498 }
3499
3500 /*
3501  * [IMPORTANT] Don't call this function in interrupt context
3502  *
3503  * When wait_ms > 0, this function shoule be called at process context
3504  * da == NULL for station mode
3505  */
3506 int issue_nulldata(struct adapter *padapter, unsigned char *da, unsigned int power_mode, int try_cnt, int wait_ms)
3507 {
3508         int ret;
3509         int i = 0;
3510         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3511         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
3512         struct sta_info *psta;
3513
3514
3515         /* da == NULL, assum it's null data for sta to ap*/
3516         if (da == NULL)
3517                 da = get_my_bssid(&(pmlmeinfo->network));
3518
3519         psta = rtw_get_stainfo(&padapter->stapriv, da);
3520         if (psta) {
3521                 if (power_mode)
3522                         rtw_hal_macid_sleep(padapter, psta->mac_id);
3523                 else
3524                         rtw_hal_macid_wakeup(padapter, psta->mac_id);
3525         } else {
3526                 DBG_871X(FUNC_ADPT_FMT ": Can't find sta info for " MAC_FMT ", skip macid %s!!\n",
3527                         FUNC_ADPT_ARG(padapter), MAC_ARG(da), power_mode?"sleep":"wakeup");
3528                 rtw_warn_on(1);
3529         }
3530
3531         do {
3532                 ret = _issue_nulldata(padapter, da, power_mode, wait_ms > 0?true:false);
3533
3534                 i++;
3535
3536                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
3537                         break;
3538
3539                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
3540                         msleep(wait_ms);
3541
3542         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
3543
3544         if (ret != _FAIL) {
3545                 ret = _SUCCESS;
3546                 #ifndef DBG_XMIT_ACK
3547                 goto exit;
3548                 #endif
3549         }
3550
3551         if (try_cnt && wait_ms) {
3552                 if (da)
3553                         DBG_871X(FUNC_ADPT_FMT" to "MAC_FMT", ch:%u%s, %d/%d in %u ms\n",
3554                                 FUNC_ADPT_ARG(padapter), MAC_ARG(da), rtw_get_oper_ch(padapter),
3555                                 ret == _SUCCESS?", acked":"", i, try_cnt, (i + 1) * wait_ms);
3556                 else
3557                         DBG_871X(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
3558                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
3559                                 ret == _SUCCESS?", acked":"", i, try_cnt, (i + 1) * wait_ms);
3560         }
3561 exit:
3562         return ret;
3563 }
3564
3565 /*
3566  * [IMPORTANT] This function run in interrupt context
3567  *
3568  * The null data packet would be sent without power bit,
3569  * and not guarantee success.
3570  */
3571 s32 issue_nulldata_in_interrupt(struct adapter *padapter, u8 *da)
3572 {
3573         int ret;
3574         struct mlme_ext_priv *pmlmeext;
3575         struct mlme_ext_info *pmlmeinfo;
3576
3577
3578         pmlmeext = &padapter->mlmeextpriv;
3579         pmlmeinfo = &pmlmeext->mlmext_info;
3580
3581         /* da == NULL, assum it's null data for sta to ap*/
3582         if (da == NULL)
3583                 da = get_my_bssid(&(pmlmeinfo->network));
3584
3585         ret = _issue_nulldata(padapter, da, 0, false);
3586
3587         return ret;
3588 }
3589
3590 /* when wait_ack is ture, this function shoule be called at process context */
3591 static int _issue_qos_nulldata(struct adapter *padapter, unsigned char *da,
3592                                u16 tid, bool wait_ack)
3593 {
3594         int ret = _FAIL;
3595         struct xmit_frame                       *pmgntframe;
3596         struct pkt_attrib                       *pattrib;
3597         unsigned char                           *pframe;
3598         struct ieee80211_hdr    *pwlanhdr;
3599         __le16 *fctrl;
3600         u16 *qc;
3601         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
3602         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3603         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
3604
3605         DBG_871X("%s\n", __func__);
3606
3607         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3608         if (pmgntframe == NULL)
3609                 goto exit;
3610
3611         /* update attribute */
3612         pattrib = &pmgntframe->attrib;
3613         update_mgntframe_attrib(padapter, pattrib);
3614
3615         pattrib->hdrlen += 2;
3616         pattrib->qos_en = true;
3617         pattrib->eosp = 1;
3618         pattrib->ack_policy = 0;
3619         pattrib->mdata = 0;
3620
3621         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3622
3623         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3624         pwlanhdr = (struct ieee80211_hdr *)pframe;
3625
3626         fctrl = &(pwlanhdr->frame_control);
3627         *(fctrl) = 0;
3628
3629         if ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)
3630                 SetFrDs(fctrl);
3631         else if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE)
3632                 SetToDs(fctrl);
3633
3634         if (pattrib->mdata)
3635                 SetMData(fctrl);
3636
3637         qc = (unsigned short *)(pframe + pattrib->hdrlen - 2);
3638
3639         SetPriority(qc, tid);
3640
3641         SetEOSP(qc, pattrib->eosp);
3642
3643         SetAckpolicy(qc, pattrib->ack_policy);
3644
3645         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
3646         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
3647         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3648
3649         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3650         pmlmeext->mgnt_seq++;
3651         SetFrameSubType(pframe, WIFI_QOS_DATA_NULL);
3652
3653         pframe += sizeof(struct ieee80211_qos_hdr);
3654         pattrib->pktlen = sizeof(struct ieee80211_qos_hdr);
3655
3656         pattrib->last_txcmdsz = pattrib->pktlen;
3657
3658         if (wait_ack) {
3659                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
3660         } else{
3661                 dump_mgntframe(padapter, pmgntframe);
3662                 ret = _SUCCESS;
3663         }
3664
3665 exit:
3666         return ret;
3667 }
3668
3669 /* when wait_ms >0 , this function shoule be called at process context */
3670 /* da == NULL for station mode */
3671 int issue_qos_nulldata(struct adapter *padapter, unsigned char *da, u16 tid, int try_cnt, int wait_ms)
3672 {
3673         int ret;
3674         int i = 0;
3675         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3676         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
3677
3678         /* da == NULL, assum it's null data for sta to ap*/
3679         if (da == NULL)
3680                 da = get_my_bssid(&(pmlmeinfo->network));
3681
3682         do {
3683                 ret = _issue_qos_nulldata(padapter, da, tid, wait_ms > 0?true:false);
3684
3685                 i++;
3686
3687                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
3688                         break;
3689
3690                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
3691                         msleep(wait_ms);
3692
3693         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
3694
3695         if (ret != _FAIL) {
3696                 ret = _SUCCESS;
3697                 #ifndef DBG_XMIT_ACK
3698                 goto exit;
3699                 #endif
3700         }
3701
3702         if (try_cnt && wait_ms) {
3703                 if (da)
3704                         DBG_871X(FUNC_ADPT_FMT" to "MAC_FMT", ch:%u%s, %d/%d in %u ms\n",
3705                                 FUNC_ADPT_ARG(padapter), MAC_ARG(da), rtw_get_oper_ch(padapter),
3706                                 ret == _SUCCESS?", acked":"", i, try_cnt, (i + 1) * wait_ms);
3707                 else
3708                         DBG_871X(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
3709                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
3710                                 ret == _SUCCESS?", acked":"", i, try_cnt, (i + 1) * wait_ms);
3711         }
3712 exit:
3713         return ret;
3714 }
3715
3716 static int _issue_deauth(struct adapter *padapter, unsigned char *da,
3717                          unsigned short reason, bool wait_ack)
3718 {
3719         struct xmit_frame                       *pmgntframe;
3720         struct pkt_attrib                       *pattrib;
3721         unsigned char                           *pframe;
3722         struct ieee80211_hdr    *pwlanhdr;
3723         __le16 *fctrl;
3724         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
3725         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3726         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
3727         int ret = _FAIL;
3728         __le16 le_tmp;
3729
3730         /* DBG_871X("%s to "MAC_FMT"\n", __func__, MAC_ARG(da)); */
3731
3732         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3733         if (pmgntframe == NULL) {
3734                 goto exit;
3735         }
3736
3737         /* update attribute */
3738         pattrib = &pmgntframe->attrib;
3739         update_mgntframe_attrib(padapter, pattrib);
3740         pattrib->retry_ctrl = false;
3741
3742         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3743
3744         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3745         pwlanhdr = (struct ieee80211_hdr *)pframe;
3746
3747         fctrl = &(pwlanhdr->frame_control);
3748         *(fctrl) = 0;
3749
3750         memcpy(pwlanhdr->addr1, da, ETH_ALEN);
3751         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
3752         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3753
3754         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3755         pmlmeext->mgnt_seq++;
3756         SetFrameSubType(pframe, WIFI_DEAUTH);
3757
3758         pframe += sizeof(struct ieee80211_hdr_3addr);
3759         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
3760
3761         le_tmp = cpu_to_le16(reason);
3762         pframe = rtw_set_fixed_ie(pframe, _RSON_CODE_, (unsigned char *)&le_tmp, &(pattrib->pktlen));
3763
3764         pattrib->last_txcmdsz = pattrib->pktlen;
3765
3766
3767         if (wait_ack) {
3768                 ret = dump_mgntframe_and_wait_ack(padapter, pmgntframe);
3769         } else{
3770                 dump_mgntframe(padapter, pmgntframe);
3771                 ret = _SUCCESS;
3772         }
3773
3774 exit:
3775         return ret;
3776 }
3777
3778 int issue_deauth(struct adapter *padapter, unsigned char *da, unsigned short reason)
3779 {
3780         DBG_871X("%s to "MAC_FMT"\n", __func__, MAC_ARG(da));
3781         return _issue_deauth(padapter, da, reason, false);
3782 }
3783
3784 int issue_deauth_ex(struct adapter *padapter, u8 *da, unsigned short reason, int try_cnt,
3785         int wait_ms)
3786 {
3787         int ret;
3788         int i = 0;
3789
3790         do {
3791                 ret = _issue_deauth(padapter, da, reason, wait_ms > 0?true:false);
3792
3793                 i++;
3794
3795                 if (padapter->bDriverStopped || padapter->bSurpriseRemoved)
3796                         break;
3797
3798                 if (i < try_cnt && wait_ms > 0 && ret == _FAIL)
3799                         msleep(wait_ms);
3800
3801         } while ((i < try_cnt) && ((ret == _FAIL) || (wait_ms == 0)));
3802
3803         if (ret != _FAIL) {
3804                 ret = _SUCCESS;
3805                 #ifndef DBG_XMIT_ACK
3806                 goto exit;
3807                 #endif
3808         }
3809
3810         if (try_cnt && wait_ms) {
3811                 if (da)
3812                         DBG_871X(FUNC_ADPT_FMT" to "MAC_FMT", ch:%u%s, %d/%d in %u ms\n",
3813                                 FUNC_ADPT_ARG(padapter), MAC_ARG(da), rtw_get_oper_ch(padapter),
3814                                 ret == _SUCCESS?", acked":"", i, try_cnt, (i + 1) * wait_ms);
3815                 else
3816                         DBG_871X(FUNC_ADPT_FMT", ch:%u%s, %d/%d in %u ms\n",
3817                                 FUNC_ADPT_ARG(padapter), rtw_get_oper_ch(padapter),
3818                                 ret == _SUCCESS?", acked":"", i, try_cnt, (i + 1) * wait_ms);
3819         }
3820 exit:
3821         return ret;
3822 }
3823
3824 void issue_action_SA_Query(struct adapter *padapter, unsigned char *raddr, unsigned char action, unsigned short tid)
3825 {
3826         u8 category = RTW_WLAN_CATEGORY_SA_QUERY;
3827         struct xmit_frame               *pmgntframe;
3828         struct pkt_attrib               *pattrib;
3829         u8                      *pframe;
3830         struct ieee80211_hdr    *pwlanhdr;
3831         __le16 *fctrl;
3832         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
3833         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3834         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
3835         __le16 le_tmp;
3836
3837         DBG_871X("%s\n", __func__);
3838
3839         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3840         if (pmgntframe == NULL) {
3841                 DBG_871X("%s: alloc_mgtxmitframe fail\n", __func__);
3842                 return;
3843         }
3844
3845         /* update attribute */
3846         pattrib = &pmgntframe->attrib;
3847         update_mgntframe_attrib(padapter, pattrib);
3848
3849         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3850
3851         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3852         pwlanhdr = (struct ieee80211_hdr *)pframe;
3853
3854         fctrl = &(pwlanhdr->frame_control);
3855         *(fctrl) = 0;
3856
3857         if (raddr)
3858                 memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
3859         else
3860                 memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3861         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
3862         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3863
3864         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3865         pmlmeext->mgnt_seq++;
3866         SetFrameSubType(pframe, WIFI_ACTION);
3867
3868         pframe += sizeof(struct ieee80211_hdr_3addr);
3869         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
3870
3871         pframe = rtw_set_fixed_ie(pframe, 1, &category, &pattrib->pktlen);
3872         pframe = rtw_set_fixed_ie(pframe, 1, &action, &pattrib->pktlen);
3873
3874         switch (action) {
3875         case 0: /* SA Query req */
3876                 pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)&pmlmeext->sa_query_seq, &pattrib->pktlen);
3877                 pmlmeext->sa_query_seq++;
3878                 /* send sa query request to AP, AP should reply sa query response in 1 second */
3879                 set_sa_query_timer(pmlmeext, 1000);
3880                 break;
3881
3882         case 1: /* SA Query rsp */
3883                 le_tmp = cpu_to_le16(tid);
3884                 pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)&le_tmp, &pattrib->pktlen);
3885                 break;
3886         default:
3887                 break;
3888         }
3889
3890         pattrib->last_txcmdsz = pattrib->pktlen;
3891
3892         dump_mgntframe(padapter, pmgntframe);
3893 }
3894
3895 void issue_action_BA(struct adapter *padapter, unsigned char *raddr, unsigned char action, unsigned short status)
3896 {
3897         u8 category = RTW_WLAN_CATEGORY_BACK;
3898         u16 start_seq;
3899         u16 BA_para_set;
3900         u16 reason_code;
3901         u16 BA_timeout_value;
3902         u16 BA_starting_seqctrl = 0;
3903         enum HT_CAP_AMPDU_FACTOR max_rx_ampdu_factor;
3904         struct xmit_frame               *pmgntframe;
3905         struct pkt_attrib               *pattrib;
3906         u8                      *pframe;
3907         struct ieee80211_hdr    *pwlanhdr;
3908         __le16 *fctrl;
3909         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
3910         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
3911         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
3912         struct sta_info         *psta;
3913         struct sta_priv         *pstapriv = &padapter->stapriv;
3914         struct registry_priv    *pregpriv = &padapter->registrypriv;
3915         __le16 le_tmp;
3916
3917         DBG_871X("%s, category =%d, action =%d, status =%d\n", __func__, category, action, status);
3918
3919         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
3920         if (pmgntframe == NULL)
3921                 return;
3922
3923         /* update attribute */
3924         pattrib = &pmgntframe->attrib;
3925         update_mgntframe_attrib(padapter, pattrib);
3926
3927         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
3928
3929         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
3930         pwlanhdr = (struct ieee80211_hdr *)pframe;
3931
3932         fctrl = &(pwlanhdr->frame_control);
3933         *(fctrl) = 0;
3934
3935         /* memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); */
3936         memcpy(pwlanhdr->addr1, raddr, ETH_ALEN);
3937         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
3938         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
3939
3940         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
3941         pmlmeext->mgnt_seq++;
3942         SetFrameSubType(pframe, WIFI_ACTION);
3943
3944         pframe += sizeof(struct ieee80211_hdr_3addr);
3945         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
3946
3947         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
3948         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
3949
3950         if (category == 3) {
3951                 switch (action) {
3952                 case 0: /* ADDBA req */
3953                         do {
3954                                 pmlmeinfo->dialogToken++;
3955                         } while (pmlmeinfo->dialogToken == 0);
3956                         pframe = rtw_set_fixed_ie(pframe, 1, &(pmlmeinfo->dialogToken), &(pattrib->pktlen));
3957
3958                         if (rtw_btcoex_IsBTCoexCtrlAMPDUSize(padapter)) {
3959                                 /*  A-MSDU NOT Supported */
3960                                 BA_para_set = 0;
3961                                 /*  immediate Block Ack */
3962                                 BA_para_set |= (1 << 1) & IEEE80211_ADDBA_PARAM_POLICY_MASK;
3963                                 /*  TID */
3964                                 BA_para_set |= (status << 2) & IEEE80211_ADDBA_PARAM_TID_MASK;
3965                                 /*  max buffer size is 8 MSDU */
3966                                 BA_para_set |= (8 << 6) & RTW_IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK;
3967                         } else {
3968                                 BA_para_set = (0x1002 | ((status & 0xf) << 2)); /* immediate ack & 64 buffer size */
3969                         }
3970                         le_tmp = cpu_to_le16(BA_para_set);
3971                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
3972
3973                         BA_timeout_value = 5000;/*  5ms */
3974                         le_tmp = cpu_to_le16(BA_timeout_value);
3975                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
3976
3977                         /* if ((psta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.MacAddress)) != NULL) */
3978                         psta = rtw_get_stainfo(pstapriv, raddr);
3979                         if (psta != NULL) {
3980                                 start_seq = (psta->sta_xmitpriv.txseq_tid[status & 0x07]&0xfff) + 1;
3981
3982                                 DBG_871X("BA_starting_seqctrl = %d for TID =%d\n", start_seq, status & 0x07);
3983
3984                                 psta->BA_starting_seqctrl[status & 0x07] = start_seq;
3985
3986                                 BA_starting_seqctrl = start_seq << 4;
3987                         }
3988
3989                         le_tmp = cpu_to_le16(BA_starting_seqctrl);
3990                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
3991                         break;
3992
3993                 case 1: /* ADDBA rsp */
3994                         pframe = rtw_set_fixed_ie(pframe, 1, &(pmlmeinfo->ADDBA_req.dialog_token), &(pattrib->pktlen));
3995                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&status), &(pattrib->pktlen));
3996                         if (padapter->driver_rx_ampdu_factor != 0xFF)
3997                                 max_rx_ampdu_factor =
3998                                   (enum HT_CAP_AMPDU_FACTOR)padapter->driver_rx_ampdu_factor;
3999                         else
4000                                 rtw_hal_get_def_var(padapter,
4001                                                     HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor);
4002
4003                         if (MAX_AMPDU_FACTOR_64K == max_rx_ampdu_factor)
4004                                 BA_para_set = ((le16_to_cpu(pmlmeinfo->ADDBA_req.BA_para_set) & 0x3f) | 0x1000); /* 64 buffer size */
4005                         else if (MAX_AMPDU_FACTOR_32K == max_rx_ampdu_factor)
4006                                 BA_para_set = ((le16_to_cpu(pmlmeinfo->ADDBA_req.BA_para_set) & 0x3f) | 0x0800); /* 32 buffer size */
4007                         else if (MAX_AMPDU_FACTOR_16K == max_rx_ampdu_factor)
4008                                 BA_para_set = ((le16_to_cpu(pmlmeinfo->ADDBA_req.BA_para_set) & 0x3f) | 0x0400); /* 16 buffer size */
4009                         else if (MAX_AMPDU_FACTOR_8K == max_rx_ampdu_factor)
4010                                 BA_para_set = ((le16_to_cpu(pmlmeinfo->ADDBA_req.BA_para_set) & 0x3f) | 0x0200); /* 8 buffer size */
4011                         else
4012                                 BA_para_set = ((le16_to_cpu(pmlmeinfo->ADDBA_req.BA_para_set) & 0x3f) | 0x1000); /* 64 buffer size */
4013
4014                         if (rtw_btcoex_IsBTCoexCtrlAMPDUSize(padapter) &&
4015                             padapter->driver_rx_ampdu_factor == 0xFF) {
4016                                 /*  max buffer size is 8 MSDU */
4017                                 BA_para_set &= ~RTW_IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK;
4018                                 BA_para_set |= (8 << 6) & RTW_IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK;
4019                         }
4020
4021                         if (pregpriv->ampdu_amsdu == 0)/* disabled */
4022                                 le_tmp = cpu_to_le16(BA_para_set & ~BIT(0));
4023                         else if (pregpriv->ampdu_amsdu == 1)/* enabled */
4024                                 le_tmp = cpu_to_le16(BA_para_set | BIT(0));
4025                         else /* auto */
4026                                 le_tmp = cpu_to_le16(BA_para_set);
4027
4028                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
4029                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(pmlmeinfo->ADDBA_req.BA_timeout_value)), &(pattrib->pktlen));
4030                         break;
4031                 case 2:/* DELBA */
4032                         BA_para_set = (status & 0x1F) << 3;
4033                         le_tmp = cpu_to_le16(BA_para_set);
4034                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
4035
4036                         reason_code = 37;
4037                         le_tmp = cpu_to_le16(reason_code);
4038                         pframe = rtw_set_fixed_ie(pframe, 2, (unsigned char *)(&(le_tmp)), &(pattrib->pktlen));
4039                         break;
4040                 default:
4041                         break;
4042                 }
4043         }
4044
4045         pattrib->last_txcmdsz = pattrib->pktlen;
4046
4047         dump_mgntframe(padapter, pmgntframe);
4048 }
4049
4050 static void issue_action_BSSCoexistPacket(struct adapter *padapter)
4051 {
4052         struct list_head                *plist, *phead;
4053         unsigned char category, action;
4054         struct xmit_frame                       *pmgntframe;
4055         struct pkt_attrib                       *pattrib;
4056         unsigned char                   *pframe;
4057         struct ieee80211_hdr    *pwlanhdr;
4058         __le16 *fctrl;
4059         struct  wlan_network    *pnetwork = NULL;
4060         struct xmit_priv                *pxmitpriv = &(padapter->xmitpriv);
4061         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
4062         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
4063         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4064         struct __queue          *queue  = &(pmlmepriv->scanned_queue);
4065         u8 InfoContent[16] = {0};
4066         u8 ICS[8][15];
4067
4068         if ((pmlmepriv->num_FortyMHzIntolerant == 0) || (pmlmepriv->num_sta_no_ht == 0))
4069                 return;
4070
4071         if (true == pmlmeinfo->bwmode_updated)
4072                 return;
4073
4074
4075         DBG_871X("%s\n", __func__);
4076
4077
4078         category = RTW_WLAN_CATEGORY_PUBLIC;
4079         action = ACT_PUBLIC_BSSCOEXIST;
4080
4081         pmgntframe = alloc_mgtxmitframe(pxmitpriv);
4082         if (pmgntframe == NULL) {
4083                 return;
4084         }
4085
4086         /* update attribute */
4087         pattrib = &pmgntframe->attrib;
4088         update_mgntframe_attrib(padapter, pattrib);
4089
4090         memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
4091
4092         pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
4093         pwlanhdr = (struct ieee80211_hdr *)pframe;
4094
4095         fctrl = &(pwlanhdr->frame_control);
4096         *(fctrl) = 0;
4097
4098         memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
4099         memcpy(pwlanhdr->addr2, myid(&(padapter->eeprompriv)), ETH_ALEN);
4100         memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN);
4101
4102         SetSeqNum(pwlanhdr, pmlmeext->mgnt_seq);
4103         pmlmeext->mgnt_seq++;
4104         SetFrameSubType(pframe, WIFI_ACTION);
4105
4106         pframe += sizeof(struct ieee80211_hdr_3addr);
4107         pattrib->pktlen = sizeof(struct ieee80211_hdr_3addr);
4108
4109         pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pattrib->pktlen));
4110         pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pattrib->pktlen));
4111
4112
4113         /*  */
4114         if (pmlmepriv->num_FortyMHzIntolerant > 0) {
4115                 u8 iedata = 0;
4116
4117                 iedata |= BIT(2);/* 20 MHz BSS Width Request */
4118
4119                 pframe = rtw_set_ie(pframe, EID_BSSCoexistence,  1, &iedata, &(pattrib->pktlen));
4120
4121         }
4122
4123
4124         /*  */
4125         memset(ICS, 0, sizeof(ICS));
4126         if (pmlmepriv->num_sta_no_ht > 0) {
4127                 int i;
4128
4129                 spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
4130
4131                 phead = get_list_head(queue);
4132                 plist = get_next(phead);
4133
4134                 while (1) {
4135                         int len;
4136                         u8 *p;
4137                         struct wlan_bssid_ex *pbss_network;
4138
4139                         if (phead == plist)
4140                                 break;
4141
4142                         pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
4143
4144                         plist = get_next(plist);
4145
4146                         pbss_network = (struct wlan_bssid_ex *)&pnetwork->network;
4147
4148                         p = rtw_get_ie(pbss_network->IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, pbss_network->IELength - _FIXED_IE_LENGTH_);
4149                         if ((p == NULL) || (len == 0)) {/* non-HT */
4150
4151                                 if ((pbss_network->Configuration.DSConfig <= 0) || (pbss_network->Configuration.DSConfig > 14))
4152                                         continue;
4153
4154                                 ICS[0][pbss_network->Configuration.DSConfig] = 1;
4155
4156                                 if (ICS[0][0] == 0)
4157                                         ICS[0][0] = 1;
4158                         }
4159
4160                 }
4161
4162                 spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
4163
4164
4165                 for (i = 0; i < 8; i++) {
4166                         if (ICS[i][0] == 1) {
4167                                 int j, k = 0;
4168
4169                                 InfoContent[k] = i;
4170                                 /* SET_BSS_INTOLERANT_ELE_REG_CLASS(InfoContent, i); */
4171                                 k++;
4172
4173                                 for (j = 1; j <= 14; j++) {
4174                                         if (ICS[i][j] == 1) {
4175                                                 if (k < 16) {
4176                                                         InfoContent[k] = j; /* channel number */
4177                                                         /* SET_BSS_INTOLERANT_ELE_CHANNEL(InfoContent+k, j); */
4178                                                         k++;
4179                                                 }
4180                                         }
4181                                 }
4182
4183                                 pframe = rtw_set_ie(pframe, EID_BSSIntolerantChlReport, k, InfoContent, &(pattrib->pktlen));
4184
4185                         }
4186
4187                 }
4188
4189
4190         }
4191
4192
4193         pattrib->last_txcmdsz = pattrib->pktlen;
4194
4195         dump_mgntframe(padapter, pmgntframe);
4196 }
4197
4198 unsigned int send_delba(struct adapter *padapter, u8 initiator, u8 *addr)
4199 {
4200         struct sta_priv *pstapriv = &padapter->stapriv;
4201         struct sta_info *psta = NULL;
4202         /* struct recv_reorder_ctrl *preorder_ctrl; */
4203         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4204         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4205         u16 tid;
4206
4207         if ((pmlmeinfo->state&0x03) != WIFI_FW_AP_STATE)
4208                 if (!(pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS))
4209                         return _SUCCESS;
4210
4211         psta = rtw_get_stainfo(pstapriv, addr);
4212         if (psta == NULL)
4213                 return _SUCCESS;
4214
4215         /* DBG_871X("%s:%s\n", __func__, (initiator == 0)?"RX_DIR":"TX_DIR"); */
4216
4217         if (initiator == 0) {/*  recipient */
4218                 for (tid = 0; tid < MAXTID; tid++) {
4219                         if (psta->recvreorder_ctrl[tid].enable) {
4220                                 DBG_871X("rx agg disable tid(%d)\n", tid);
4221                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_DELBA, (((tid << 1) | initiator)&0x1F));
4222                                 psta->recvreorder_ctrl[tid].enable = false;
4223                                 psta->recvreorder_ctrl[tid].indicate_seq = 0xffff;
4224                                 #ifdef DBG_RX_SEQ
4225                                 DBG_871X("DBG_RX_SEQ %s:%d indicate_seq:%u\n", __func__, __LINE__,
4226                                         psta->recvreorder_ctrl[tid].indicate_seq);
4227                                 #endif
4228                         }
4229                 }
4230         } else if (initiator == 1) {/*  originator */
4231                 /* DBG_871X("tx agg_enable_bitmap(0x%08x)\n", psta->htpriv.agg_enable_bitmap); */
4232                 for (tid = 0; tid < MAXTID; tid++) {
4233                         if (psta->htpriv.agg_enable_bitmap & BIT(tid)) {
4234                                 DBG_871X("tx agg disable tid(%d)\n", tid);
4235                                 issue_action_BA(padapter, addr, RTW_WLAN_ACTION_DELBA, (((tid << 1) | initiator)&0x1F));
4236                                 psta->htpriv.agg_enable_bitmap &= ~BIT(tid);
4237                                 psta->htpriv.candidate_tid_bitmap &= ~BIT(tid);
4238
4239                         }
4240                 }
4241         }
4242
4243         return _SUCCESS;
4244
4245 }
4246
4247 unsigned int send_beacon(struct adapter *padapter)
4248 {
4249         u8 bxmitok = false;
4250         int     issue = 0;
4251         int poll = 0;
4252         unsigned long start = jiffies;
4253
4254         rtw_hal_set_hwreg(padapter, HW_VAR_BCN_VALID, NULL);
4255         rtw_hal_set_hwreg(padapter, HW_VAR_DL_BCN_SEL, NULL);
4256         do {
4257                 issue_beacon(padapter, 100);
4258                 issue++;
4259                 do {
4260                         cond_resched();
4261                         rtw_hal_get_hwreg(padapter, HW_VAR_BCN_VALID, (u8 *)(&bxmitok));
4262                         poll++;
4263                 } while ((poll%10) != 0 && false == bxmitok && !padapter->bSurpriseRemoved && !padapter->bDriverStopped);
4264
4265         } while (false == bxmitok && issue < 100 && !padapter->bSurpriseRemoved && !padapter->bDriverStopped);
4266
4267         if (padapter->bSurpriseRemoved || padapter->bDriverStopped) {
4268                 return _FAIL;
4269         }
4270
4271
4272         if (false == bxmitok) {
4273                 DBG_871X("%s fail! %u ms\n", __func__, jiffies_to_msecs(jiffies - start));
4274                 return _FAIL;
4275         } else{
4276                 unsigned long passing_time = jiffies_to_msecs(jiffies - start);
4277
4278                 if (passing_time > 100 || issue > 3)
4279                         DBG_871X("%s success, issue:%d, poll:%d, %lu ms\n", __func__, issue, poll, passing_time);
4280                 /* else */
4281                 /*      DBG_871X("%s success, issue:%d, poll:%d, %u ms\n", __func__, issue, poll, passing_time); */
4282
4283                 return _SUCCESS;
4284         }
4285 }
4286
4287 /****************************************************************************
4288
4289 Following are some utitity fuctions for WiFi MLME
4290
4291 *****************************************************************************/
4292
4293 void site_survey(struct adapter *padapter)
4294 {
4295         unsigned char   survey_channel = 0, val8;
4296         RT_SCAN_TYPE    ScanType = SCAN_PASSIVE;
4297         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4298         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4299         u32 initialgain = 0;
4300         u32 channel_scan_time_ms = 0;
4301
4302         {
4303                 struct rtw_ieee80211_channel *ch;
4304                 if (pmlmeext->sitesurvey_res.channel_idx < pmlmeext->sitesurvey_res.ch_num) {
4305                         ch = &pmlmeext->sitesurvey_res.ch[pmlmeext->sitesurvey_res.channel_idx];
4306                         survey_channel = ch->hw_value;
4307                         ScanType = (ch->flags & RTW_IEEE80211_CHAN_PASSIVE_SCAN) ? SCAN_PASSIVE : SCAN_ACTIVE;
4308                 }
4309         }
4310
4311         DBG_871X(FUNC_ADPT_FMT" ch:%u (cnt:%u) at %dms, %c%c%c\n"
4312                  , FUNC_ADPT_ARG(padapter)
4313                  , survey_channel
4314                  , pmlmeext->sitesurvey_res.channel_idx
4315                  , jiffies_to_msecs(jiffies - padapter->mlmepriv.scan_start_time)
4316                  , ScanType?'A':'P', pmlmeext->sitesurvey_res.scan_mode?'A':'P'
4317                  , pmlmeext->sitesurvey_res.ssid[0].SsidLength?'S':' '
4318                 );
4319 #ifdef DBG_FIXED_CHAN
4320         DBG_871X(FUNC_ADPT_FMT" fixed_chan:%u\n", pmlmeext->fixed_chan);
4321 #endif
4322
4323         if (survey_channel != 0) {
4324                 /* PAUSE 4-AC Queue when site_survey */
4325                 /* rtw_hal_get_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
4326                 /* val8 |= 0x0f; */
4327                 /* rtw_hal_set_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
4328                 if (pmlmeext->sitesurvey_res.channel_idx == 0) {
4329 #ifdef DBG_FIXED_CHAN
4330                         if (pmlmeext->fixed_chan != 0xff)
4331                                 set_channel_bwmode(padapter, pmlmeext->fixed_chan, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20);
4332                         else
4333 #endif
4334                                 set_channel_bwmode(padapter, survey_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20);
4335                 } else{
4336 #ifdef DBG_FIXED_CHAN
4337                         if (pmlmeext->fixed_chan != 0xff)
4338                                 SelectChannel(padapter, pmlmeext->fixed_chan);
4339                         else
4340 #endif
4341                                 SelectChannel(padapter, survey_channel);
4342                 }
4343
4344                 if (ScanType == SCAN_ACTIVE) { /* obey the channel plan setting... */
4345                         {
4346                                 int i;
4347                                 for (i = 0; i < RTW_SSID_SCAN_AMOUNT; i++) {
4348                                         if (pmlmeext->sitesurvey_res.ssid[i].SsidLength) {
4349                                                 /* IOT issue, When wifi_spec is not set, send one probe req without WPS IE. */
4350                                                 if (padapter->registrypriv.wifi_spec)
4351                                                         issue_probereq(padapter, &(pmlmeext->sitesurvey_res.ssid[i]), NULL);
4352                                                 else
4353                                                         issue_probereq_ex(padapter, &(pmlmeext->sitesurvey_res.ssid[i]), NULL, 0, 0, 0, 0);
4354                                                 issue_probereq(padapter, &(pmlmeext->sitesurvey_res.ssid[i]), NULL);
4355                                         }
4356                                 }
4357
4358                                 if (pmlmeext->sitesurvey_res.scan_mode == SCAN_ACTIVE) {
4359                                         /* IOT issue, When wifi_spec is not set, send one probe req without WPS IE. */
4360                                         if (padapter->registrypriv.wifi_spec)
4361                                                 issue_probereq(padapter, NULL, NULL);
4362                                         else
4363                                                 issue_probereq_ex(padapter, NULL, NULL, 0, 0, 0, 0);
4364                                         issue_probereq(padapter, NULL, NULL);
4365                                 }
4366                         }
4367                 }
4368
4369                 channel_scan_time_ms = pmlmeext->chan_scan_time;
4370
4371                 set_survey_timer(pmlmeext, channel_scan_time_ms);
4372 #if defined(CONFIG_SIGNAL_DISPLAY_DBM) && defined(CONFIG_BACKGROUND_NOISE_MONITOR)
4373                 {
4374                         struct noise_info info;
4375                         info.bPauseDIG = false;
4376                         info.IGIValue = 0;
4377                         info.max_time = channel_scan_time_ms/2;/* ms */
4378                         info.chan = survey_channel;
4379                         rtw_hal_set_odm_var(padapter, HAL_ODM_NOISE_MONITOR, &info, false);
4380                 }
4381 #endif
4382
4383         } else{
4384
4385                 /*      channel number is 0 or this channel is not valid. */
4386
4387                 {
4388                         pmlmeext->sitesurvey_res.state = SCAN_COMPLETE;
4389
4390                         /* switch back to the original channel */
4391                         /* SelectChannel(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset); */
4392
4393                         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
4394
4395                         /* flush 4-AC Queue after site_survey */
4396                         /* val8 = 0; */
4397                         /* rtw_hal_set_hwreg(padapter, HW_VAR_TXPAUSE, (u8 *)(&val8)); */
4398
4399                         /* config MSR */
4400                         Set_MSR(padapter, (pmlmeinfo->state & 0x3));
4401
4402                         initialgain = 0xff; /* restore RX GAIN */
4403                         rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
4404                         /* turn on dynamic functions */
4405                         Restore_DM_Func_Flag(padapter);
4406                         /* Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true); */
4407
4408                         if (is_client_associated_to_ap(padapter))
4409                                 issue_nulldata(padapter, NULL, 0, 3, 500);
4410
4411                         val8 = 0; /* survey done */
4412                         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_SITESURVEY, (u8 *)(&val8));
4413
4414                         report_surveydone_event(padapter);
4415
4416                         pmlmeext->chan_scan_time = SURVEY_TO;
4417                         pmlmeext->sitesurvey_res.state = SCAN_DISABLE;
4418
4419                         issue_action_BSSCoexistPacket(padapter);
4420                         issue_action_BSSCoexistPacket(padapter);
4421                         issue_action_BSSCoexistPacket(padapter);
4422                 }
4423         }
4424
4425         return;
4426
4427 }
4428
4429 /* collect bss info from Beacon and Probe request/response frames. */
4430 u8 collect_bss_info(struct adapter *padapter, union recv_frame *precv_frame, struct wlan_bssid_ex *bssid)
4431 {
4432         int     i;
4433         u32 len;
4434         u8 *p;
4435         u16 val16, subtype;
4436         u8 *pframe = precv_frame->u.hdr.rx_data;
4437         u32 packet_len = precv_frame->u.hdr.len;
4438         u8 ie_offset;
4439         struct registry_priv *pregistrypriv = &padapter->registrypriv;
4440         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4441         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4442         __le32 le32_tmp;
4443
4444         len = packet_len - sizeof(struct ieee80211_hdr_3addr);
4445
4446         if (len > MAX_IE_SZ) {
4447                 /* DBG_871X("IE too long for survey event\n"); */
4448                 return _FAIL;
4449         }
4450
4451         memset(bssid, 0, sizeof(struct wlan_bssid_ex));
4452
4453         subtype = GetFrameSubType(pframe);
4454
4455         if (subtype == WIFI_BEACON) {
4456                 bssid->Reserved[0] = 1;
4457                 ie_offset = _BEACON_IE_OFFSET_;
4458         } else {
4459                 /*  FIXME : more type */
4460                 if (subtype == WIFI_PROBERSP) {
4461                         ie_offset = _PROBERSP_IE_OFFSET_;
4462                         bssid->Reserved[0] = 3;
4463                 } else if (subtype == WIFI_PROBEREQ) {
4464                         ie_offset = _PROBEREQ_IE_OFFSET_;
4465                         bssid->Reserved[0] = 2;
4466                 } else {
4467                         bssid->Reserved[0] = 0;
4468                         ie_offset = _FIXED_IE_LENGTH_;
4469                 }
4470         }
4471
4472         bssid->Length = sizeof(struct wlan_bssid_ex) - MAX_IE_SZ + len;
4473
4474         /* below is to copy the information element */
4475         bssid->IELength = len;
4476         memcpy(bssid->IEs, (pframe + sizeof(struct ieee80211_hdr_3addr)), bssid->IELength);
4477
4478         /* get the signal strength */
4479         bssid->Rssi = precv_frame->u.hdr.attrib.phy_info.RecvSignalPower; /*  in dBM.raw data */
4480         bssid->PhyInfo.SignalQuality = precv_frame->u.hdr.attrib.phy_info.SignalQuality;/* in percentage */
4481         bssid->PhyInfo.SignalStrength = precv_frame->u.hdr.attrib.phy_info.SignalStrength;/* in percentage */
4482
4483         /*  checking SSID */
4484         p = rtw_get_ie(bssid->IEs + ie_offset, _SSID_IE_, &len, bssid->IELength - ie_offset);
4485         if (p == NULL) {
4486                 DBG_871X("marc: cannot find SSID for survey event\n");
4487                 return _FAIL;
4488         }
4489
4490         if (*(p + 1)) {
4491                 if (len > NDIS_802_11_LENGTH_SSID) {
4492                         DBG_871X("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
4493                         return _FAIL;
4494                 }
4495                 memcpy(bssid->Ssid.Ssid, (p + 2), *(p + 1));
4496                 bssid->Ssid.SsidLength = *(p + 1);
4497         } else
4498                 bssid->Ssid.SsidLength = 0;
4499
4500         memset(bssid->SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
4501
4502         /* checking rate info... */
4503         i = 0;
4504         p = rtw_get_ie(bssid->IEs + ie_offset, _SUPPORTEDRATES_IE_, &len, bssid->IELength - ie_offset);
4505         if (p != NULL) {
4506                 if (len > NDIS_802_11_LENGTH_RATES_EX) {
4507                         DBG_871X("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
4508                         return _FAIL;
4509                 }
4510                 memcpy(bssid->SupportedRates, (p + 2), len);
4511                 i = len;
4512         }
4513
4514         p = rtw_get_ie(bssid->IEs + ie_offset, _EXT_SUPPORTEDRATES_IE_, &len, bssid->IELength - ie_offset);
4515         if (p != NULL) {
4516                 if (len > (NDIS_802_11_LENGTH_RATES_EX-i)) {
4517                         DBG_871X("%s()-%d: IE too long (%d) for survey event\n", __func__, __LINE__, len);
4518                         return _FAIL;
4519                 }
4520                 memcpy(bssid->SupportedRates + i, (p + 2), len);
4521         }
4522
4523         bssid->NetworkTypeInUse = Ndis802_11OFDM24;
4524
4525         if (bssid->IELength < 12)
4526                 return _FAIL;
4527
4528         /*  Checking for DSConfig */
4529         p = rtw_get_ie(bssid->IEs + ie_offset, _DSSET_IE_, &len, bssid->IELength - ie_offset);
4530
4531         bssid->Configuration.DSConfig = 0;
4532         bssid->Configuration.Length = 0;
4533
4534         if (p) {
4535                 bssid->Configuration.DSConfig = *(p + 2);
4536         } else {
4537                 /*  In 5G, some ap do not have DSSET IE */
4538                 /*  checking HT info for channel */
4539                 p = rtw_get_ie(bssid->IEs + ie_offset, _HT_ADD_INFO_IE_, &len, bssid->IELength - ie_offset);
4540                 if (p) {
4541                         struct HT_info_element *HT_info = (struct HT_info_element *)(p + 2);
4542                         bssid->Configuration.DSConfig = HT_info->primary_channel;
4543                 } else { /*  use current channel */
4544                         bssid->Configuration.DSConfig = rtw_get_oper_ch(padapter);
4545                 }
4546         }
4547
4548         memcpy(&le32_tmp, rtw_get_beacon_interval_from_ie(bssid->IEs), 2);
4549         bssid->Configuration.BeaconPeriod = le32_to_cpu(le32_tmp);
4550
4551         val16 = rtw_get_capability((struct wlan_bssid_ex *)bssid);
4552
4553         if (val16 & BIT(0)) {
4554                 bssid->InfrastructureMode = Ndis802_11Infrastructure;
4555                 memcpy(bssid->MacAddress, GetAddr2Ptr(pframe), ETH_ALEN);
4556         } else {
4557                 bssid->InfrastructureMode = Ndis802_11IBSS;
4558                 memcpy(bssid->MacAddress, GetAddr3Ptr(pframe), ETH_ALEN);
4559         }
4560
4561         if (val16 & BIT(4))
4562                 bssid->Privacy = 1;
4563         else
4564                 bssid->Privacy = 0;
4565
4566         bssid->Configuration.ATIMWindow = 0;
4567
4568         /* 20/40 BSS Coexistence check */
4569         if ((pregistrypriv->wifi_spec == 1) && (false == pmlmeinfo->bwmode_updated)) {
4570                 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
4571
4572                 p = rtw_get_ie(bssid->IEs + ie_offset, _HT_CAPABILITY_IE_, &len, bssid->IELength - ie_offset);
4573                 if (p && len > 0) {
4574                         struct HT_caps_element  *pHT_caps;
4575                         pHT_caps = (struct HT_caps_element      *)(p + 2);
4576
4577                         if (le16_to_cpu(pHT_caps->u.HT_cap_element.HT_caps_info) & BIT(14))
4578                                 pmlmepriv->num_FortyMHzIntolerant++;
4579                 } else
4580                         pmlmepriv->num_sta_no_ht++;
4581         }
4582
4583 #ifdef CONFIG_INTEL_WIDI
4584         /* process_intel_widi_query_or_tigger(padapter, bssid); */
4585         if (process_intel_widi_query_or_tigger(padapter, bssid))
4586                 return _FAIL;
4587 #endif /*  CONFIG_INTEL_WIDI */
4588
4589         #if defined(DBG_RX_SIGNAL_DISPLAY_SSID_MONITORED) & 1
4590         if (strcmp(bssid->Ssid.Ssid, DBG_RX_SIGNAL_DISPLAY_SSID_MONITORED) == 0) {
4591                 DBG_871X("Receiving %s("MAC_FMT", DSConfig:%u) from ch%u with ss:%3u, sq:%3u, RawRSSI:%3ld\n"
4592                         , bssid->Ssid.Ssid, MAC_ARG(bssid->MacAddress), bssid->Configuration.DSConfig
4593                         , rtw_get_oper_ch(padapter)
4594                         , bssid->PhyInfo.SignalStrength, bssid->PhyInfo.SignalQuality, bssid->Rssi
4595                 );
4596         }
4597         #endif
4598
4599         /*  mark bss info receving from nearby channel as SignalQuality 101 */
4600         if (bssid->Configuration.DSConfig != rtw_get_oper_ch(padapter))
4601                 bssid->PhyInfo.SignalQuality = 101;
4602
4603         return _SUCCESS;
4604 }
4605
4606 void start_create_ibss(struct adapter *padapter)
4607 {
4608         unsigned short  caps;
4609         u8 val8;
4610         u8 join_type;
4611         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4612         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4613         struct wlan_bssid_ex            *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
4614         pmlmeext->cur_channel = (u8)pnetwork->Configuration.DSConfig;
4615         pmlmeinfo->bcn_interval = get_beacon_interval(pnetwork);
4616
4617         /* update wireless mode */
4618         update_wireless_mode(padapter);
4619
4620         /* udpate capability */
4621         caps = rtw_get_capability((struct wlan_bssid_ex *)pnetwork);
4622         update_capinfo(padapter, caps);
4623         if (caps&cap_IBSS) {/* adhoc master */
4624                 val8 = 0xcf;
4625                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
4626
4627                 rtw_hal_set_hwreg(padapter, HW_VAR_DO_IQK, NULL);
4628
4629                 /* switch channel */
4630                 /* SelectChannel(padapter, pmlmeext->cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE); */
4631                 set_channel_bwmode(padapter, pmlmeext->cur_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20);
4632
4633                 beacon_timing_control(padapter);
4634
4635                 /* set msr to WIFI_FW_ADHOC_STATE */
4636                 pmlmeinfo->state = WIFI_FW_ADHOC_STATE;
4637                 Set_MSR(padapter, (pmlmeinfo->state & 0x3));
4638
4639                 /* issue beacon */
4640                 if (send_beacon(padapter) == _FAIL) {
4641                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("issuing beacon frame fail....\n"));
4642
4643                         report_join_res(padapter, -1);
4644                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
4645                 } else{
4646                         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, padapter->registrypriv.dev_network.MacAddress);
4647                         join_type = 0;
4648                         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
4649
4650                         report_join_res(padapter, 1);
4651                         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
4652                         rtw_indicate_connect(padapter);
4653                 }
4654         } else{
4655                 DBG_871X("start_create_ibss, invalid cap:%x\n", caps);
4656                 return;
4657         }
4658         /* update bc/mc sta_info */
4659         update_bmc_sta(padapter);
4660
4661 }
4662
4663 void start_clnt_join(struct adapter *padapter)
4664 {
4665         unsigned short  caps;
4666         u8 val8;
4667         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4668         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4669         struct wlan_bssid_ex            *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
4670         int beacon_timeout;
4671
4672         /* update wireless mode */
4673         update_wireless_mode(padapter);
4674
4675         /* udpate capability */
4676         caps = rtw_get_capability((struct wlan_bssid_ex *)pnetwork);
4677         update_capinfo(padapter, caps);
4678         if (caps&cap_ESS) {
4679                 Set_MSR(padapter, WIFI_FW_STATION_STATE);
4680
4681                 val8 = (pmlmeinfo->auth_algo == dot11AuthAlgrthm_8021X) ? 0xcc : 0xcf;
4682
4683                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
4684
4685                 /*  Because of AP's not receiving deauth before */
4686                 /*  AP may: 1)not response auth or 2)deauth us after link is complete */
4687                 /*  issue deauth before issuing auth to deal with the situation */
4688
4689                 /*      Commented by Albert 2012/07/21 */
4690                 /*      For the Win8 P2P connection, it will be hard to have a successful connection if this Wi-Fi doesn't connect to it. */
4691                 {
4692                                 /* To avoid connecting to AP fail during resume process, change retry count from 5 to 1 */
4693                                 issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, 1, 100);
4694                 }
4695
4696                 /* here wait for receiving the beacon to start auth */
4697                 /* and enable a timer */
4698                 beacon_timeout = decide_wait_for_beacon_timeout(pmlmeinfo->bcn_interval);
4699                 set_link_timer(pmlmeext, beacon_timeout);
4700                 _set_timer(&padapter->mlmepriv.assoc_timer,
4701                         (REAUTH_TO * REAUTH_LIMIT) + (REASSOC_TO*REASSOC_LIMIT) + beacon_timeout);
4702
4703                 pmlmeinfo->state = WIFI_FW_AUTH_NULL | WIFI_FW_STATION_STATE;
4704         } else if (caps&cap_IBSS) { /* adhoc client */
4705                 Set_MSR(padapter, WIFI_FW_ADHOC_STATE);
4706
4707                 val8 = 0xcf;
4708                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_CFG, (u8 *)(&val8));
4709
4710                 beacon_timing_control(padapter);
4711
4712                 pmlmeinfo->state = WIFI_FW_ADHOC_STATE;
4713
4714                 report_join_res(padapter, 1);
4715         } else{
4716                 /* DBG_871X("marc: invalid cap:%x\n", caps); */
4717                 return;
4718         }
4719
4720 }
4721
4722 void start_clnt_auth(struct adapter *padapter)
4723 {
4724         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4725         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4726
4727         del_timer_sync(&pmlmeext->link_timer);
4728
4729         pmlmeinfo->state &= (~WIFI_FW_AUTH_NULL);
4730         pmlmeinfo->state |= WIFI_FW_AUTH_STATE;
4731
4732         pmlmeinfo->auth_seq = 1;
4733         pmlmeinfo->reauth_count = 0;
4734         pmlmeinfo->reassoc_count = 0;
4735         pmlmeinfo->link_count = 0;
4736         pmlmeext->retry = 0;
4737
4738
4739         DBG_871X_LEVEL(_drv_always_, "start auth\n");
4740         issue_auth(padapter, NULL, 0);
4741
4742         set_link_timer(pmlmeext, REAUTH_TO);
4743
4744 }
4745
4746
4747 void start_clnt_assoc(struct adapter *padapter)
4748 {
4749         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4750         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4751
4752         del_timer_sync(&pmlmeext->link_timer);
4753
4754         pmlmeinfo->state &= (~(WIFI_FW_AUTH_NULL | WIFI_FW_AUTH_STATE));
4755         pmlmeinfo->state |= (WIFI_FW_AUTH_SUCCESS | WIFI_FW_ASSOC_STATE);
4756
4757         issue_assocreq(padapter);
4758
4759         set_link_timer(pmlmeext, REASSOC_TO);
4760 }
4761
4762 unsigned int receive_disconnect(struct adapter *padapter, unsigned char *MacAddr, unsigned short reason)
4763 {
4764         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
4765         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
4766
4767         /* check A3 */
4768         if (!(!memcmp(MacAddr, get_my_bssid(&pmlmeinfo->network), ETH_ALEN)))
4769                 return _SUCCESS;
4770
4771         DBG_871X("%s\n", __func__);
4772
4773         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
4774                 if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {
4775                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
4776                         report_del_sta_event(padapter, MacAddr, reason);
4777
4778                 } else if (pmlmeinfo->state & WIFI_FW_LINKING_STATE) {
4779                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
4780                         report_join_res(padapter, -2);
4781                 }
4782         }
4783
4784         return _SUCCESS;
4785 }
4786
4787 static void process_80211d(struct adapter *padapter, struct wlan_bssid_ex *bssid)
4788 {
4789         struct registry_priv *pregistrypriv;
4790         struct mlme_ext_priv *pmlmeext;
4791         RT_CHANNEL_INFO *chplan_new;
4792         u8 channel;
4793         u8 i;
4794
4795
4796         pregistrypriv = &padapter->registrypriv;
4797         pmlmeext = &padapter->mlmeextpriv;
4798
4799         /*  Adjust channel plan by AP Country IE */
4800         if (pregistrypriv->enable80211d &&
4801                 (!pmlmeext->update_channel_plan_by_ap_done)) {
4802                 u8 *ie, *p;
4803                 u32 len;
4804                 RT_CHANNEL_PLAN chplan_ap;
4805                 RT_CHANNEL_INFO chplan_sta[MAX_CHANNEL_NUM];
4806                 u8 country[4];
4807                 u8 fcn; /*  first channel number */
4808                 u8 noc; /*  number of channel */
4809                 u8 j, k;
4810
4811                 ie = rtw_get_ie(bssid->IEs + _FIXED_IE_LENGTH_, _COUNTRY_IE_, &len, bssid->IELength - _FIXED_IE_LENGTH_);
4812                 if (!ie)
4813                         return;
4814                 if (len < 6)
4815                         return;
4816
4817                 ie += 2;
4818                 p = ie;
4819                 ie += len;
4820
4821                 memset(country, 0, 4);
4822                 memcpy(country, p, 3);
4823                 p += 3;
4824                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
4825                                 ("%s: 802.11d country =%s\n", __func__, country));
4826
4827                 i = 0;
4828                 while ((ie - p) >= 3) {
4829                         fcn = *(p++);
4830                         noc = *(p++);
4831                         p++;
4832
4833                         for (j = 0; j < noc; j++) {
4834                                 if (fcn <= 14)
4835                                         channel = fcn + j; /*  2.4 GHz */
4836                                 else
4837                                         channel = fcn + j*4; /*  5 GHz */
4838
4839                                 chplan_ap.Channel[i++] = channel;
4840                         }
4841                 }
4842                 chplan_ap.Len = i;
4843
4844 #ifdef DEBUG_RTL871X
4845                 i = 0;
4846                 DBG_871X("%s: AP[%s] channel plan {", __func__, bssid->Ssid.Ssid);
4847                 while ((i < chplan_ap.Len) && (chplan_ap.Channel[i] != 0)) {
4848                         DBG_8192C("%02d,", chplan_ap.Channel[i]);
4849                         i++;
4850                 }
4851                 DBG_871X("}\n");
4852 #endif
4853
4854                 memcpy(chplan_sta, pmlmeext->channel_set, sizeof(chplan_sta));
4855 #ifdef DEBUG_RTL871X
4856                 i = 0;
4857                 DBG_871X("%s: STA channel plan {", __func__);
4858                 while ((i < MAX_CHANNEL_NUM) && (chplan_sta[i].ChannelNum != 0)) {
4859                         DBG_871X("%02d(%c),", chplan_sta[i].ChannelNum, chplan_sta[i].ScanType == SCAN_PASSIVE?'p':'a');
4860                         i++;
4861                 }
4862                 DBG_871X("}\n");
4863 #endif
4864
4865                 memset(pmlmeext->channel_set, 0, sizeof(pmlmeext->channel_set));
4866                 chplan_new = pmlmeext->channel_set;
4867
4868                 i = j = k = 0;
4869                 if (pregistrypriv->wireless_mode & WIRELESS_11G) {
4870                         do {
4871                                 if ((i == MAX_CHANNEL_NUM) ||
4872                                         (chplan_sta[i].ChannelNum == 0) ||
4873                                         (chplan_sta[i].ChannelNum > 14))
4874                                         break;
4875
4876                                 if ((j == chplan_ap.Len) || (chplan_ap.Channel[j] > 14))
4877                                         break;
4878
4879                                 if (chplan_sta[i].ChannelNum == chplan_ap.Channel[j]) {
4880                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
4881                                         chplan_new[k].ScanType = SCAN_ACTIVE;
4882                                         i++;
4883                                         j++;
4884                                         k++;
4885                                 } else if (chplan_sta[i].ChannelNum < chplan_ap.Channel[j]) {
4886                                         chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
4887 /*                                      chplan_new[k].ScanType = chplan_sta[i].ScanType; */
4888                                         chplan_new[k].ScanType = SCAN_PASSIVE;
4889                                         i++;
4890                                         k++;
4891                                 } else if (chplan_sta[i].ChannelNum > chplan_ap.Channel[j]) {
4892                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
4893                                         chplan_new[k].ScanType = SCAN_ACTIVE;
4894                                         j++;
4895                                         k++;
4896                                 }
4897                         } while (1);
4898
4899                         /*  change AP not support channel to Passive scan */
4900                         while ((i < MAX_CHANNEL_NUM) &&
4901                                 (chplan_sta[i].ChannelNum != 0) &&
4902                                 (chplan_sta[i].ChannelNum <= 14)) {
4903
4904                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
4905 /*                              chplan_new[k].ScanType = chplan_sta[i].ScanType; */
4906                                 chplan_new[k].ScanType = SCAN_PASSIVE;
4907                                 i++;
4908                                 k++;
4909                         }
4910
4911                         /*  add channel AP supported */
4912                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] <= 14)) {
4913                                 chplan_new[k].ChannelNum = chplan_ap.Channel[j];
4914                                 chplan_new[k].ScanType = SCAN_ACTIVE;
4915                                 j++;
4916                                 k++;
4917                         }
4918                 } else{
4919                         /*  keep original STA 2.4G channel plan */
4920                         while ((i < MAX_CHANNEL_NUM) &&
4921                                 (chplan_sta[i].ChannelNum != 0) &&
4922                                 (chplan_sta[i].ChannelNum <= 14)) {
4923                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
4924                                 chplan_new[k].ScanType = chplan_sta[i].ScanType;
4925                                 i++;
4926                                 k++;
4927                         }
4928
4929                         /*  skip AP 2.4G channel plan */
4930                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] <= 14)) {
4931                                 j++;
4932                         }
4933                 }
4934
4935                 if (pregistrypriv->wireless_mode & WIRELESS_11A) {
4936                         do {
4937                                 if ((i == MAX_CHANNEL_NUM) ||
4938                                         (chplan_sta[i].ChannelNum == 0))
4939                                         break;
4940
4941                                 if ((j == chplan_ap.Len) || (chplan_ap.Channel[j] == 0))
4942                                         break;
4943
4944                                 if (chplan_sta[i].ChannelNum == chplan_ap.Channel[j]) {
4945                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
4946                                         chplan_new[k].ScanType = SCAN_ACTIVE;
4947                                         i++;
4948                                         j++;
4949                                         k++;
4950                                 } else if (chplan_sta[i].ChannelNum < chplan_ap.Channel[j]) {
4951                                         chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
4952 /*                                      chplan_new[k].ScanType = chplan_sta[i].ScanType; */
4953                                         chplan_new[k].ScanType = SCAN_PASSIVE;
4954                                         i++;
4955                                         k++;
4956                                 } else if (chplan_sta[i].ChannelNum > chplan_ap.Channel[j]) {
4957                                         chplan_new[k].ChannelNum = chplan_ap.Channel[j];
4958                                         chplan_new[k].ScanType = SCAN_ACTIVE;
4959                                         j++;
4960                                         k++;
4961                                 }
4962                         } while (1);
4963
4964                         /*  change AP not support channel to Passive scan */
4965                         while ((i < MAX_CHANNEL_NUM) && (chplan_sta[i].ChannelNum != 0)) {
4966                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
4967 /*                              chplan_new[k].ScanType = chplan_sta[i].ScanType; */
4968                                 chplan_new[k].ScanType = SCAN_PASSIVE;
4969                                 i++;
4970                                 k++;
4971                         }
4972
4973                         /*  add channel AP supported */
4974                         while ((j < chplan_ap.Len) && (chplan_ap.Channel[j] != 0)) {
4975                                 chplan_new[k].ChannelNum = chplan_ap.Channel[j];
4976                                 chplan_new[k].ScanType = SCAN_ACTIVE;
4977                                 j++;
4978                                 k++;
4979                         }
4980                 } else{
4981                         /*  keep original STA 5G channel plan */
4982                         while ((i < MAX_CHANNEL_NUM) && (chplan_sta[i].ChannelNum != 0)) {
4983                                 chplan_new[k].ChannelNum = chplan_sta[i].ChannelNum;
4984                                 chplan_new[k].ScanType = chplan_sta[i].ScanType;
4985                                 i++;
4986                                 k++;
4987                         }
4988                 }
4989
4990                 pmlmeext->update_channel_plan_by_ap_done = 1;
4991
4992 #ifdef DEBUG_RTL871X
4993                 k = 0;
4994                 DBG_871X("%s: new STA channel plan {", __func__);
4995                 while ((k < MAX_CHANNEL_NUM) && (chplan_new[k].ChannelNum != 0)) {
4996                         DBG_871X("%02d(%c),", chplan_new[k].ChannelNum, chplan_new[k].ScanType == SCAN_PASSIVE?'p':'c');
4997                         k++;
4998                 }
4999                 DBG_871X("}\n");
5000 #endif
5001         }
5002
5003         /*  If channel is used by AP, set channel scan type to active */
5004         channel = bssid->Configuration.DSConfig;
5005         chplan_new = pmlmeext->channel_set;
5006         i = 0;
5007         while ((i < MAX_CHANNEL_NUM) && (chplan_new[i].ChannelNum != 0)) {
5008                 if (chplan_new[i].ChannelNum == channel) {
5009                         if (chplan_new[i].ScanType == SCAN_PASSIVE) {
5010                                 /* 5G Bnad 2, 3 (DFS) doesn't change to active scan */
5011                                 if (channel >= 52 && channel <= 144)
5012                                         break;
5013
5014                                 chplan_new[i].ScanType = SCAN_ACTIVE;
5015                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_notice_,
5016                                                  ("%s: change channel %d scan type from passive to active\n",
5017                                                   __func__, channel));
5018                         }
5019                         break;
5020                 }
5021                 i++;
5022         }
5023 }
5024
5025 /****************************************************************************
5026
5027 Following are the functions to report events
5028
5029 *****************************************************************************/
5030
5031 void report_survey_event(struct adapter *padapter, union recv_frame *precv_frame)
5032 {
5033         struct cmd_obj *pcmd_obj;
5034         u8 *pevtcmd;
5035         u32 cmdsz;
5036         struct survey_event     *psurvey_evt;
5037         struct C2HEvent_Header *pc2h_evt_hdr;
5038         struct mlme_ext_priv *pmlmeext;
5039         struct cmd_priv *pcmdpriv;
5040         /* u8 *pframe = precv_frame->u.hdr.rx_data; */
5041         /* uint len = precv_frame->u.hdr.len; */
5042
5043         if (!padapter)
5044                 return;
5045
5046         pmlmeext = &padapter->mlmeextpriv;
5047         pcmdpriv = &padapter->cmdpriv;
5048
5049         pcmd_obj = rtw_zmalloc(sizeof(struct cmd_obj));
5050         if (pcmd_obj == NULL)
5051                 return;
5052
5053         cmdsz = (sizeof(struct survey_event) + sizeof(struct C2HEvent_Header));
5054         pevtcmd = rtw_zmalloc(cmdsz);
5055         if (pevtcmd == NULL) {
5056                 kfree((u8 *)pcmd_obj);
5057                 return;
5058         }
5059
5060         INIT_LIST_HEAD(&pcmd_obj->list);
5061
5062         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
5063         pcmd_obj->cmdsz = cmdsz;
5064         pcmd_obj->parmbuf = pevtcmd;
5065
5066         pcmd_obj->rsp = NULL;
5067         pcmd_obj->rspsz  = 0;
5068
5069         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
5070         pc2h_evt_hdr->len = sizeof(struct survey_event);
5071         pc2h_evt_hdr->ID = GEN_EVT_CODE(_Survey);
5072         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
5073
5074         psurvey_evt = (struct survey_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
5075
5076         if (collect_bss_info(padapter, precv_frame, (struct wlan_bssid_ex *)&psurvey_evt->bss) == _FAIL) {
5077                 kfree((u8 *)pcmd_obj);
5078                 kfree((u8 *)pevtcmd);
5079                 return;
5080         }
5081
5082         process_80211d(padapter, &psurvey_evt->bss);
5083
5084         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
5085
5086         pmlmeext->sitesurvey_res.bss_cnt++;
5087
5088         return;
5089
5090 }
5091
5092 void report_surveydone_event(struct adapter *padapter)
5093 {
5094         struct cmd_obj *pcmd_obj;
5095         u8 *pevtcmd;
5096         u32 cmdsz;
5097         struct surveydone_event *psurveydone_evt;
5098         struct C2HEvent_Header  *pc2h_evt_hdr;
5099         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5100         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
5101
5102         pcmd_obj = rtw_zmalloc(sizeof(struct cmd_obj));
5103         if (pcmd_obj == NULL)
5104                 return;
5105
5106         cmdsz = (sizeof(struct surveydone_event) + sizeof(struct C2HEvent_Header));
5107         pevtcmd = rtw_zmalloc(cmdsz);
5108         if (pevtcmd == NULL) {
5109                 kfree((u8 *)pcmd_obj);
5110                 return;
5111         }
5112
5113         INIT_LIST_HEAD(&pcmd_obj->list);
5114
5115         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
5116         pcmd_obj->cmdsz = cmdsz;
5117         pcmd_obj->parmbuf = pevtcmd;
5118
5119         pcmd_obj->rsp = NULL;
5120         pcmd_obj->rspsz  = 0;
5121
5122         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
5123         pc2h_evt_hdr->len = sizeof(struct surveydone_event);
5124         pc2h_evt_hdr->ID = GEN_EVT_CODE(_SurveyDone);
5125         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
5126
5127         psurveydone_evt = (struct surveydone_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
5128         psurveydone_evt->bss_cnt = pmlmeext->sitesurvey_res.bss_cnt;
5129
5130         DBG_871X("survey done event(%x) band:%d for "ADPT_FMT"\n", psurveydone_evt->bss_cnt, padapter->setband, ADPT_ARG(padapter));
5131
5132         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
5133
5134         return;
5135
5136 }
5137
5138 void report_join_res(struct adapter *padapter, int res)
5139 {
5140         struct cmd_obj *pcmd_obj;
5141         u8 *pevtcmd;
5142         u32 cmdsz;
5143         struct joinbss_event            *pjoinbss_evt;
5144         struct C2HEvent_Header  *pc2h_evt_hdr;
5145         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5146         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
5147         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
5148
5149         pcmd_obj = rtw_zmalloc(sizeof(struct cmd_obj));
5150         if (pcmd_obj == NULL)
5151                 return;
5152
5153         cmdsz = (sizeof(struct joinbss_event) + sizeof(struct C2HEvent_Header));
5154         pevtcmd = rtw_zmalloc(cmdsz);
5155         if (pevtcmd == NULL) {
5156                 kfree((u8 *)pcmd_obj);
5157                 return;
5158         }
5159
5160         INIT_LIST_HEAD(&pcmd_obj->list);
5161
5162         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
5163         pcmd_obj->cmdsz = cmdsz;
5164         pcmd_obj->parmbuf = pevtcmd;
5165
5166         pcmd_obj->rsp = NULL;
5167         pcmd_obj->rspsz  = 0;
5168
5169         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
5170         pc2h_evt_hdr->len = sizeof(struct joinbss_event);
5171         pc2h_evt_hdr->ID = GEN_EVT_CODE(_JoinBss);
5172         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
5173
5174         pjoinbss_evt = (struct joinbss_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
5175         memcpy((unsigned char *)(&(pjoinbss_evt->network.network)), &(pmlmeinfo->network), sizeof(struct wlan_bssid_ex));
5176         pjoinbss_evt->network.join_res  = pjoinbss_evt->network.aid = res;
5177
5178         DBG_871X("report_join_res(%d)\n", res);
5179
5180
5181         rtw_joinbss_event_prehandle(padapter, (u8 *)&pjoinbss_evt->network);
5182
5183
5184         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
5185
5186         return;
5187
5188 }
5189
5190 void report_wmm_edca_update(struct adapter *padapter)
5191 {
5192         struct cmd_obj *pcmd_obj;
5193         u8 *pevtcmd;
5194         u32 cmdsz;
5195         struct wmm_event                *pwmm_event;
5196         struct C2HEvent_Header  *pc2h_evt_hdr;
5197         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5198         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
5199
5200         pcmd_obj = rtw_zmalloc(sizeof(struct cmd_obj));
5201         if (pcmd_obj == NULL)
5202                 return;
5203
5204         cmdsz = (sizeof(struct wmm_event) + sizeof(struct C2HEvent_Header));
5205         pevtcmd = rtw_zmalloc(cmdsz);
5206         if (pevtcmd == NULL) {
5207                 kfree((u8 *)pcmd_obj);
5208                 return;
5209         }
5210
5211         INIT_LIST_HEAD(&pcmd_obj->list);
5212
5213         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
5214         pcmd_obj->cmdsz = cmdsz;
5215         pcmd_obj->parmbuf = pevtcmd;
5216
5217         pcmd_obj->rsp = NULL;
5218         pcmd_obj->rspsz  = 0;
5219
5220         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
5221         pc2h_evt_hdr->len = sizeof(struct wmm_event);
5222         pc2h_evt_hdr->ID = GEN_EVT_CODE(_WMM);
5223         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
5224
5225         pwmm_event = (struct wmm_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
5226         pwmm_event->wmm = 0;
5227
5228         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
5229
5230         return;
5231
5232 }
5233
5234 void report_del_sta_event(struct adapter *padapter, unsigned char *MacAddr, unsigned short reason)
5235 {
5236         struct cmd_obj *pcmd_obj;
5237         u8 *pevtcmd;
5238         u32 cmdsz;
5239         struct sta_info *psta;
5240         int     mac_id;
5241         struct stadel_event                     *pdel_sta_evt;
5242         struct C2HEvent_Header  *pc2h_evt_hdr;
5243         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5244         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
5245
5246         pcmd_obj = rtw_zmalloc(sizeof(struct cmd_obj));
5247         if (pcmd_obj == NULL) {
5248                 return;
5249         }
5250
5251         cmdsz = (sizeof(struct stadel_event) + sizeof(struct C2HEvent_Header));
5252         pevtcmd = rtw_zmalloc(cmdsz);
5253         if (pevtcmd == NULL) {
5254                 kfree((u8 *)pcmd_obj);
5255                 return;
5256         }
5257
5258         INIT_LIST_HEAD(&pcmd_obj->list);
5259
5260         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
5261         pcmd_obj->cmdsz = cmdsz;
5262         pcmd_obj->parmbuf = pevtcmd;
5263
5264         pcmd_obj->rsp = NULL;
5265         pcmd_obj->rspsz  = 0;
5266
5267         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
5268         pc2h_evt_hdr->len = sizeof(struct stadel_event);
5269         pc2h_evt_hdr->ID = GEN_EVT_CODE(_DelSTA);
5270         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
5271
5272         pdel_sta_evt = (struct stadel_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
5273         memcpy((unsigned char *)(&(pdel_sta_evt->macaddr)), MacAddr, ETH_ALEN);
5274         memcpy((unsigned char *)(pdel_sta_evt->rsvd), (unsigned char *)(&reason), 2);
5275
5276
5277         psta = rtw_get_stainfo(&padapter->stapriv, MacAddr);
5278         if (psta)
5279                 mac_id = (int)psta->mac_id;
5280         else
5281                 mac_id = (-1);
5282
5283         pdel_sta_evt->mac_id = mac_id;
5284
5285         DBG_871X("report_del_sta_event: delete STA, mac_id =%d\n", mac_id);
5286
5287         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
5288
5289         return;
5290 }
5291
5292 void report_add_sta_event(struct adapter *padapter, unsigned char *MacAddr, int cam_idx)
5293 {
5294         struct cmd_obj *pcmd_obj;
5295         u8 *pevtcmd;
5296         u32 cmdsz;
5297         struct stassoc_event            *padd_sta_evt;
5298         struct C2HEvent_Header  *pc2h_evt_hdr;
5299         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5300         struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
5301
5302         pcmd_obj = rtw_zmalloc(sizeof(struct cmd_obj));
5303         if (pcmd_obj == NULL)
5304                 return;
5305
5306         cmdsz = (sizeof(struct stassoc_event) + sizeof(struct C2HEvent_Header));
5307         pevtcmd = rtw_zmalloc(cmdsz);
5308         if (pevtcmd == NULL) {
5309                 kfree((u8 *)pcmd_obj);
5310                 return;
5311         }
5312
5313         INIT_LIST_HEAD(&pcmd_obj->list);
5314
5315         pcmd_obj->cmdcode = GEN_CMD_CODE(_Set_MLME_EVT);
5316         pcmd_obj->cmdsz = cmdsz;
5317         pcmd_obj->parmbuf = pevtcmd;
5318
5319         pcmd_obj->rsp = NULL;
5320         pcmd_obj->rspsz  = 0;
5321
5322         pc2h_evt_hdr = (struct C2HEvent_Header *)(pevtcmd);
5323         pc2h_evt_hdr->len = sizeof(struct stassoc_event);
5324         pc2h_evt_hdr->ID = GEN_EVT_CODE(_AddSTA);
5325         pc2h_evt_hdr->seq = atomic_inc_return(&pmlmeext->event_seq);
5326
5327         padd_sta_evt = (struct stassoc_event *)(pevtcmd + sizeof(struct C2HEvent_Header));
5328         memcpy((unsigned char *)(&(padd_sta_evt->macaddr)), MacAddr, ETH_ALEN);
5329         padd_sta_evt->cam_id = cam_idx;
5330
5331         DBG_871X("report_add_sta_event: add STA\n");
5332
5333         rtw_enqueue_cmd(pcmdpriv, pcmd_obj);
5334
5335         return;
5336 }
5337
5338
5339 bool rtw_port_switch_chk(struct adapter *adapter)
5340 {
5341         bool switch_needed = false;
5342         return switch_needed;
5343 }
5344
5345 /****************************************************************************
5346
5347 Following are the event callback functions
5348
5349 *****************************************************************************/
5350
5351 /* for sta/adhoc mode */
5352 void update_sta_info(struct adapter *padapter, struct sta_info *psta)
5353 {
5354         struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
5355         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
5356         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
5357
5358         /* ERP */
5359         VCS_update(padapter, psta);
5360
5361         /* HT */
5362         if (pmlmepriv->htpriv.ht_option) {
5363                 psta->htpriv.ht_option = true;
5364
5365                 psta->htpriv.ampdu_enable = pmlmepriv->htpriv.ampdu_enable;
5366
5367                 psta->htpriv.rx_ampdu_min_spacing = (pmlmeinfo->HT_caps.u.HT_cap_element.AMPDU_para&IEEE80211_HT_CAP_AMPDU_DENSITY)>>2;
5368
5369                 if (support_short_GI(padapter, &(pmlmeinfo->HT_caps), CHANNEL_WIDTH_20))
5370                         psta->htpriv.sgi_20m = true;
5371
5372                 if (support_short_GI(padapter, &(pmlmeinfo->HT_caps), CHANNEL_WIDTH_40))
5373                         psta->htpriv.sgi_40m = true;
5374
5375                 psta->qos_option = true;
5376
5377                 psta->htpriv.ldpc_cap = pmlmepriv->htpriv.ldpc_cap;
5378                 psta->htpriv.stbc_cap = pmlmepriv->htpriv.stbc_cap;
5379                 psta->htpriv.beamform_cap = pmlmepriv->htpriv.beamform_cap;
5380
5381                 memcpy(&psta->htpriv.ht_cap, &pmlmeinfo->HT_caps, sizeof(struct rtw_ieee80211_ht_cap));
5382         } else{
5383                 psta->htpriv.ht_option = false;
5384
5385                 psta->htpriv.ampdu_enable = false;
5386
5387                 psta->htpriv.sgi_20m = false;
5388                 psta->htpriv.sgi_40m = false;
5389                 psta->qos_option = false;
5390
5391         }
5392
5393         psta->htpriv.ch_offset = pmlmeext->cur_ch_offset;
5394
5395         psta->htpriv.agg_enable_bitmap = 0x0;/* reset */
5396         psta->htpriv.candidate_tid_bitmap = 0x0;/* reset */
5397
5398         psta->bw_mode = pmlmeext->cur_bwmode;
5399
5400         /* QoS */
5401         if (pmlmepriv->qospriv.qos_option)
5402                 psta->qos_option = true;
5403
5404         update_ldpc_stbc_cap(psta);
5405
5406         spin_lock_bh(&psta->lock);
5407         psta->state = _FW_LINKED;
5408         spin_unlock_bh(&psta->lock);
5409
5410 }
5411
5412 static void rtw_mlmeext_disconnect(struct adapter *padapter)
5413 {
5414         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
5415         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
5416         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
5417         struct wlan_bssid_ex            *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
5418         u8 state_backup = (pmlmeinfo->state&0x03);
5419
5420         /* set_opmode_cmd(padapter, infra_client_with_mlme); */
5421
5422         /*
5423          * For safety, prevent from keeping macid sleep.
5424          * If we can sure all power mode enter/leave are paired,
5425          * this check can be removed.
5426          * Lucas@20131113
5427          */
5428         /* wakeup macid after disconnect. */
5429         {
5430                 struct sta_info *psta;
5431                 psta = rtw_get_stainfo(&padapter->stapriv, get_my_bssid(pnetwork));
5432                 if (psta)
5433                         rtw_hal_macid_wakeup(padapter, psta->mac_id);
5434         }
5435
5436         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
5437         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
5438
5439         /* set MSR to no link state -> infra. mode */
5440         Set_MSR(padapter, _HW_STATE_STATION_);
5441
5442         pmlmeinfo->state = WIFI_FW_NULL_STATE;
5443
5444         if (state_backup == WIFI_FW_STATION_STATE) {
5445                 if (rtw_port_switch_chk(padapter)) {
5446                         rtw_hal_set_hwreg(padapter, HW_VAR_PORT_SWITCH, NULL);
5447                         {
5448                                 struct adapter *port0_iface = dvobj_get_port0_adapter(adapter_to_dvobj(padapter));
5449                                 if (port0_iface)
5450                                         rtw_lps_ctrl_wk_cmd(port0_iface, LPS_CTRL_CONNECT, 0);
5451                         }
5452                 }
5453         }
5454
5455         /* switch to the 20M Hz mode after disconnect */
5456         pmlmeext->cur_bwmode = CHANNEL_WIDTH_20;
5457         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
5458
5459         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
5460
5461         flush_all_cam_entry(padapter);
5462
5463         del_timer_sync(&pmlmeext->link_timer);
5464
5465         /* pmlmepriv->LinkDetectInfo.TrafficBusyState = false; */
5466         pmlmepriv->LinkDetectInfo.TrafficTransitionCount = 0;
5467         pmlmepriv->LinkDetectInfo.LowPowerTransitionCount = 0;
5468
5469 }
5470
5471 void mlmeext_joinbss_event_callback(struct adapter *padapter, int join_res)
5472 {
5473         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
5474         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
5475         struct wlan_bssid_ex            *cur_network = &(pmlmeinfo->network);
5476         struct sta_priv         *pstapriv = &padapter->stapriv;
5477         u8 join_type;
5478         struct sta_info *psta;
5479         if (join_res < 0) {
5480                 join_type = 1;
5481                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
5482                 rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, null_addr);
5483
5484                 goto exit_mlmeext_joinbss_event_callback;
5485         }
5486
5487         if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE)
5488                 /* update bc/mc sta_info */
5489                 update_bmc_sta(padapter);
5490
5491
5492         /* turn on dynamic functions */
5493         Switch_DM_Func(padapter, DYNAMIC_ALL_FUNC_ENABLE, true);
5494
5495         /*  update IOT-releated issue */
5496         update_IOT_info(padapter);
5497
5498         rtw_hal_set_hwreg(padapter, HW_VAR_BASIC_RATE, cur_network->SupportedRates);
5499
5500         /* BCN interval */
5501         rtw_hal_set_hwreg(padapter, HW_VAR_BEACON_INTERVAL, (u8 *)(&pmlmeinfo->bcn_interval));
5502
5503         /* udpate capability */
5504         update_capinfo(padapter, pmlmeinfo->capability);
5505
5506         /* WMM, Update EDCA param */
5507         WMMOnAssocRsp(padapter);
5508
5509         /* HT */
5510         HTOnAssocRsp(padapter);
5511
5512         /* Set cur_channel&cur_bwmode&cur_ch_offset */
5513         set_channel_bwmode(padapter, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode);
5514
5515         psta = rtw_get_stainfo(pstapriv, cur_network->MacAddress);
5516         if (psta) { /* only for infra. mode */
5517
5518                 pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
5519
5520                 /* DBG_871X("set_sta_rate\n"); */
5521
5522                 psta->wireless_mode = pmlmeext->cur_wireless_mode;
5523
5524                 /* set per sta rate after updating HT cap. */
5525                 set_sta_rate(padapter, psta);
5526
5527                 rtw_sta_media_status_rpt(padapter, psta, 1);
5528
5529                 /* wakeup macid after join bss successfully to ensure
5530                         the subsequent data frames can be sent out normally */
5531                 rtw_hal_macid_wakeup(padapter, psta->mac_id);
5532         }
5533
5534         if (rtw_port_switch_chk(padapter))
5535                 rtw_hal_set_hwreg(padapter, HW_VAR_PORT_SWITCH, NULL);
5536
5537         join_type = 2;
5538         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
5539
5540         if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
5541                 /*  correcting TSF */
5542                 correct_TSF(padapter, pmlmeext);
5543
5544                 /* set_link_timer(pmlmeext, DISCONNECT_TO); */
5545         }
5546
5547         if (get_iface_type(padapter) == IFACE_PORT0)
5548                 rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_CONNECT, 0);
5549
5550 exit_mlmeext_joinbss_event_callback:
5551
5552         DBG_871X("=>%s\n", __func__);
5553
5554 }
5555
5556 /* currently only adhoc mode will go here */
5557 void mlmeext_sta_add_event_callback(struct adapter *padapter, struct sta_info *psta)
5558 {
5559         struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
5560         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
5561         u8 join_type;
5562
5563         DBG_871X("%s\n", __func__);
5564
5565         if ((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) {
5566                 if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) { /* adhoc master or sta_count>1 */
5567
5568                         /* nothing to do */
5569                 } else{ /* adhoc client */
5570                         /* update TSF Value */
5571                         /* update_TSF(pmlmeext, pframe, len); */
5572
5573                         /*  correcting TSF */
5574                         correct_TSF(padapter, pmlmeext);
5575
5576                         /* start beacon */
5577                         if (send_beacon(padapter) == _FAIL) {
5578                                 pmlmeinfo->FW_sta_info[psta->mac_id].status = 0;
5579
5580                                 pmlmeinfo->state ^= WIFI_FW_ADHOC_STATE;
5581
5582                                 return;
5583                         }
5584
5585                         pmlmeinfo->state |= WIFI_FW_ASSOC_SUCCESS;
5586
5587                 }
5588
5589                 join_type = 2;
5590                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
5591         }
5592
5593         pmlmeinfo->FW_sta_info[psta->mac_id].psta = psta;
5594
5595         psta->bssratelen = rtw_get_rateset_len(pmlmeinfo->FW_sta_info[psta->mac_id].SupportedRates);
5596         memcpy(psta->bssrateset, pmlmeinfo->FW_sta_info[psta->mac_id].SupportedRates, psta->bssratelen);
5597
5598         /* update adhoc sta_info */
5599         update_sta_info(padapter, psta);
5600
5601         rtw_hal_update_sta_rate_mask(padapter, psta);
5602
5603         /*  ToDo: HT for Ad-hoc */
5604         psta->wireless_mode = rtw_check_network_type(psta->bssrateset, psta->bssratelen, pmlmeext->cur_channel);
5605         psta->raid = rtw_hal_networktype_to_raid(padapter, psta);
5606
5607         /* rate radaptive */
5608         Update_RA_Entry(padapter, psta);
5609 }
5610
5611 void mlmeext_sta_del_event_callback(struct adapter *padapter)
5612 {
5613         if (is_client_associated_to_ap(padapter) || is_IBSS_empty(padapter))
5614                 rtw_mlmeext_disconnect(padapter);
5615 }
5616
5617 /****************************************************************************
5618
5619 Following are the functions for the timer handlers
5620
5621 *****************************************************************************/
5622 void _linked_info_dump(struct adapter *padapter)
5623 {
5624         int i;
5625         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5626         struct mlme_ext_info    *pmlmeinfo = &(pmlmeext->mlmext_info);
5627         int UndecoratedSmoothedPWDB;
5628         struct dvobj_priv *pdvobj = adapter_to_dvobj(padapter);
5629
5630         if (padapter->bLinkInfoDump) {
5631
5632                 DBG_871X("\n ============["ADPT_FMT"] linked status check ===================\n", ADPT_ARG(padapter));
5633
5634                 if ((pmlmeinfo->state&0x03) == WIFI_FW_STATION_STATE) {
5635                         rtw_hal_get_def_var(padapter, HAL_DEF_UNDERCORATEDSMOOTHEDPWDB, &UndecoratedSmoothedPWDB);
5636
5637                         DBG_871X("AP[" MAC_FMT "] - UndecoratedSmoothedPWDB:%d\n",
5638                                 MAC_ARG(padapter->mlmepriv.cur_network.network.MacAddress), UndecoratedSmoothedPWDB);
5639                 } else if ((pmlmeinfo->state&0x03) == _HW_STATE_AP_) {
5640                         struct list_head        *phead, *plist;
5641
5642                         struct sta_info *psta = NULL;
5643                         struct sta_priv *pstapriv = &padapter->stapriv;
5644
5645                         spin_lock_bh(&pstapriv->asoc_list_lock);
5646                         phead = &pstapriv->asoc_list;
5647                         plist = get_next(phead);
5648                         while (phead != plist) {
5649                                 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
5650                                 plist = get_next(plist);
5651
5652                                 DBG_871X("STA[" MAC_FMT "]:UndecoratedSmoothedPWDB:%d\n",
5653                                         MAC_ARG(psta->hwaddr), psta->rssi_stat.UndecoratedSmoothedPWDB);
5654                         }
5655                         spin_unlock_bh(&pstapriv->asoc_list_lock);
5656
5657                 }
5658                 for (i = 0; i < NUM_STA; i++) {
5659                         if (pdvobj->macid[i]) {
5660                                 if (i != 1) /* skip bc/mc sta */
5661                                         /*   tx info ============ */
5662                                         rtw_hal_get_def_var(padapter, HW_DEF_RA_INFO_DUMP, &i);
5663                         }
5664                 }
5665                 rtw_hal_set_def_var(padapter, HAL_DEF_DBG_RX_INFO_DUMP, NULL);
5666
5667
5668         }
5669
5670
5671 }
5672
5673 static u8 chk_ap_is_alive(struct adapter *padapter, struct sta_info *psta)
5674 {
5675         u8 ret = false;
5676
5677         #ifdef DBG_EXPIRATION_CHK
5678         DBG_871X(FUNC_ADPT_FMT" rx:"STA_PKTS_FMT", beacon:%llu, probersp_to_self:%llu"
5679                                 /*", probersp_bm:%llu, probersp_uo:%llu, probereq:%llu, BI:%u"*/
5680                                 ", retry:%u\n"
5681                 , FUNC_ADPT_ARG(padapter)
5682                 , STA_RX_PKTS_DIFF_ARG(psta)
5683                 , psta->sta_stats.rx_beacon_pkts - psta->sta_stats.last_rx_beacon_pkts
5684                 , psta->sta_stats.rx_probersp_pkts - psta->sta_stats.last_rx_probersp_pkts
5685                 /*, psta->sta_stats.rx_probersp_bm_pkts - psta->sta_stats.last_rx_probersp_bm_pkts
5686                 , psta->sta_stats.rx_probersp_uo_pkts - psta->sta_stats.last_rx_probersp_uo_pkts
5687                 , psta->sta_stats.rx_probereq_pkts - psta->sta_stats.last_rx_probereq_pkts
5688                 , pmlmeinfo->bcn_interval*/
5689                 , pmlmeext->retry
5690         );
5691
5692         DBG_871X(FUNC_ADPT_FMT" tx_pkts:%llu, link_count:%u\n", FUNC_ADPT_ARG(padapter)
5693                 , padapter->xmitpriv.tx_pkts
5694                 , pmlmeinfo->link_count
5695         );
5696         #endif
5697
5698         if ((sta_rx_data_pkts(psta) == sta_last_rx_data_pkts(psta))
5699                 && sta_rx_beacon_pkts(psta) == sta_last_rx_beacon_pkts(psta)
5700                 && sta_rx_probersp_pkts(psta) == sta_last_rx_probersp_pkts(psta)
5701         ) {
5702                 ret = false;
5703         } else{
5704                 ret = true;
5705         }
5706
5707         sta_update_last_rx_pkts(psta);
5708
5709         return ret;
5710 }
5711
5712 void linked_status_chk(struct adapter *padapter)
5713 {
5714         u32 i;
5715         struct sta_info         *psta;
5716         struct xmit_priv        *pxmitpriv = &(padapter->xmitpriv);
5717         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
5718         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
5719         struct sta_priv         *pstapriv = &padapter->stapriv;
5720
5721
5722         if (is_client_associated_to_ap(padapter)) {
5723                 /* linked infrastructure client mode */
5724
5725                 int tx_chk = _SUCCESS, rx_chk = _SUCCESS;
5726                 int rx_chk_limit;
5727                 int link_count_limit;
5728
5729                 #if defined(DBG_ROAMING_TEST)
5730                 rx_chk_limit = 1;
5731                 #else
5732                 rx_chk_limit = 8;
5733                 #endif
5734                 link_count_limit = 7; /*  16 sec */
5735
5736                 /*  Marked by Kurt 20130715 */
5737                 /*  For WiDi 3.5 and latered on, they don't ask WiDi sink to do roaming, so we could not check rx limit that strictly. */
5738                 /*  todo: To check why we under miracast session, rx_chk would be false */
5739                 /* ifdef CONFIG_INTEL_WIDI */
5740                 /* if (padapter->mlmepriv.widi_state != INTEL_WIDI_STATE_NONE) */
5741                 /*      rx_chk_limit = 1; */
5742                 /* endif */
5743
5744                 psta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.MacAddress);
5745                 if (psta != NULL) {
5746                         if (chk_ap_is_alive(padapter, psta) == false)
5747                                 rx_chk = _FAIL;
5748
5749                         if (pxmitpriv->last_tx_pkts == pxmitpriv->tx_pkts)
5750                                 tx_chk = _FAIL;
5751
5752                         {
5753                                 if (rx_chk != _SUCCESS) {
5754                                         if (pmlmeext->retry == 0) {
5755                                                 #ifdef DBG_EXPIRATION_CHK
5756                                                 DBG_871X("issue_probereq to trigger probersp, retry =%d\n", pmlmeext->retry);
5757                                                 #endif
5758                                                 issue_probereq_ex(padapter, &pmlmeinfo->network.Ssid, pmlmeinfo->network.MacAddress, 0, 0, 0, 0);
5759                                                 issue_probereq_ex(padapter, &pmlmeinfo->network.Ssid, pmlmeinfo->network.MacAddress, 0, 0, 0, 0);
5760                                                 issue_probereq_ex(padapter, &pmlmeinfo->network.Ssid, pmlmeinfo->network.MacAddress, 0, 0, 0, 0);
5761                                         }
5762                                 }
5763
5764                                 if (tx_chk != _SUCCESS && pmlmeinfo->link_count++ == link_count_limit) {
5765                                         #ifdef DBG_EXPIRATION_CHK
5766                                         DBG_871X("%s issue_nulldata 0\n", __func__);
5767                                         #endif
5768                                         tx_chk = issue_nulldata_in_interrupt(padapter, NULL);
5769                                 }
5770                         }
5771
5772                         if (rx_chk == _FAIL) {
5773                                 pmlmeext->retry++;
5774                                 if (pmlmeext->retry > rx_chk_limit) {
5775                                         DBG_871X_LEVEL(_drv_always_, FUNC_ADPT_FMT" disconnect or roaming\n",
5776                                                 FUNC_ADPT_ARG(padapter));
5777                                         receive_disconnect(padapter, pmlmeinfo->network.MacAddress
5778                                                 , WLAN_REASON_EXPIRATION_CHK);
5779                                         return;
5780                                 }
5781                         } else {
5782                                 pmlmeext->retry = 0;
5783                         }
5784
5785                         if (tx_chk == _FAIL) {
5786                                 pmlmeinfo->link_count %= (link_count_limit+1);
5787                         } else {
5788                                 pxmitpriv->last_tx_pkts = pxmitpriv->tx_pkts;
5789                                 pmlmeinfo->link_count = 0;
5790                         }
5791
5792                 } /* end of if ((psta = rtw_get_stainfo(pstapriv, passoc_res->network.MacAddress)) != NULL) */
5793         } else if (is_client_associated_to_ibss(padapter)) {
5794                 /* linked IBSS mode */
5795                 /* for each assoc list entry to check the rx pkt counter */
5796                 for (i = IBSS_START_MAC_ID; i < NUM_STA; i++) {
5797                         if (pmlmeinfo->FW_sta_info[i].status == 1) {
5798                                 psta = pmlmeinfo->FW_sta_info[i].psta;
5799
5800                                 if (NULL == psta)
5801                                         continue;
5802
5803                                 if (pmlmeinfo->FW_sta_info[i].rx_pkt == sta_rx_pkts(psta)) {
5804
5805                                         if (pmlmeinfo->FW_sta_info[i].retry < 3) {
5806                                                 pmlmeinfo->FW_sta_info[i].retry++;
5807                                         } else{
5808                                                 pmlmeinfo->FW_sta_info[i].retry = 0;
5809                                                 pmlmeinfo->FW_sta_info[i].status = 0;
5810                                                 report_del_sta_event(padapter, psta->hwaddr
5811                                                         , 65535/*  indicate disconnect caused by no rx */
5812                                                 );
5813                                         }
5814                                 } else{
5815                                         pmlmeinfo->FW_sta_info[i].retry = 0;
5816                                         pmlmeinfo->FW_sta_info[i].rx_pkt = (u32)sta_rx_pkts(psta);
5817                                 }
5818                         }
5819                 }
5820
5821                 /* set_link_timer(pmlmeext, DISCONNECT_TO); */
5822
5823         }
5824
5825 }
5826
5827 void survey_timer_hdl(struct timer_list *t)
5828 {
5829         struct adapter *padapter =
5830                 from_timer(padapter, t, mlmeextpriv.survey_timer);
5831         struct cmd_obj  *ph2c;
5832         struct sitesurvey_parm  *psurveyPara;
5833         struct cmd_priv                                 *pcmdpriv = &padapter->cmdpriv;
5834         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
5835
5836         /* DBG_871X("marc: survey timer\n"); */
5837
5838         /* issue rtw_sitesurvey_cmd */
5839         if (pmlmeext->sitesurvey_res.state > SCAN_START) {
5840                 if (pmlmeext->sitesurvey_res.state ==  SCAN_PROCESS) {
5841                         pmlmeext->sitesurvey_res.channel_idx++;
5842                 }
5843
5844                 if (pmlmeext->scan_abort) {
5845                         {
5846                                 pmlmeext->sitesurvey_res.channel_idx = pmlmeext->sitesurvey_res.ch_num;
5847                                 DBG_871X("%s idx:%d\n", __func__
5848                                         , pmlmeext->sitesurvey_res.channel_idx
5849                                 );
5850                         }
5851
5852                         pmlmeext->scan_abort = false;/* reset */
5853                 }
5854
5855                 ph2c = rtw_zmalloc(sizeof(struct cmd_obj));
5856                 if (ph2c == NULL) {
5857                         goto exit_survey_timer_hdl;
5858                 }
5859
5860                 psurveyPara = rtw_zmalloc(sizeof(struct sitesurvey_parm));
5861                 if (psurveyPara == NULL) {
5862                         kfree((unsigned char *)ph2c);
5863                         goto exit_survey_timer_hdl;
5864                 }
5865
5866                 init_h2fwcmd_w_parm_no_rsp(ph2c, psurveyPara, GEN_CMD_CODE(_SiteSurvey));
5867                 rtw_enqueue_cmd(pcmdpriv, ph2c);
5868         }
5869
5870
5871 exit_survey_timer_hdl:
5872
5873         return;
5874 }
5875
5876 void link_timer_hdl(struct timer_list *t)
5877 {
5878         struct adapter *padapter =
5879                 from_timer(padapter, t, mlmeextpriv.link_timer);
5880         /* static unsigned int          rx_pkt = 0; */
5881         /* static u64                           tx_cnt = 0; */
5882         /* struct xmit_priv     *pxmitpriv = &(padapter->xmitpriv); */
5883         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
5884         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
5885         /* struct sta_priv      *pstapriv = &padapter->stapriv; */
5886
5887
5888         if (pmlmeinfo->state & WIFI_FW_AUTH_NULL) {
5889                 DBG_871X("link_timer_hdl:no beacon while connecting\n");
5890                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
5891                 report_join_res(padapter, -3);
5892         } else if (pmlmeinfo->state & WIFI_FW_AUTH_STATE) {
5893                 /* re-auth timer */
5894                 if (++pmlmeinfo->reauth_count > REAUTH_LIMIT) {
5895                         /* if (pmlmeinfo->auth_algo != dot11AuthAlgrthm_Auto) */
5896                         /*  */
5897                                 pmlmeinfo->state = 0;
5898                                 report_join_res(padapter, -1);
5899                                 return;
5900                         /*  */
5901                         /* else */
5902                         /*  */
5903                         /*      pmlmeinfo->auth_algo = dot11AuthAlgrthm_Shared; */
5904                         /*      pmlmeinfo->reauth_count = 0; */
5905                         /*  */
5906                 }
5907
5908                 DBG_871X("link_timer_hdl: auth timeout and try again\n");
5909                 pmlmeinfo->auth_seq = 1;
5910                 issue_auth(padapter, NULL, 0);
5911                 set_link_timer(pmlmeext, REAUTH_TO);
5912         } else if (pmlmeinfo->state & WIFI_FW_ASSOC_STATE) {
5913                 /* re-assoc timer */
5914                 if (++pmlmeinfo->reassoc_count > REASSOC_LIMIT) {
5915                         pmlmeinfo->state = WIFI_FW_NULL_STATE;
5916                         report_join_res(padapter, -2);
5917                         return;
5918                 }
5919
5920                 DBG_871X("link_timer_hdl: assoc timeout and try again\n");
5921                 issue_assocreq(padapter);
5922                 set_link_timer(pmlmeext, REASSOC_TO);
5923         }
5924
5925         return;
5926 }
5927
5928 void addba_timer_hdl(struct timer_list *t)
5929 {
5930         struct sta_info *psta = from_timer(psta, t, addba_retry_timer);
5931         struct ht_priv *phtpriv;
5932
5933         if (!psta)
5934                 return;
5935
5936         phtpriv = &psta->htpriv;
5937
5938         if (phtpriv->ht_option && phtpriv->ampdu_enable) {
5939                 if (phtpriv->candidate_tid_bitmap)
5940                         phtpriv->candidate_tid_bitmap = 0x0;
5941
5942         }
5943 }
5944
5945 void sa_query_timer_hdl(struct timer_list *t)
5946 {
5947         struct adapter *padapter =
5948                 from_timer(padapter, t, mlmeextpriv.sa_query_timer);
5949         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
5950         /* disconnect */
5951         spin_lock_bh(&pmlmepriv->lock);
5952
5953         if (check_fwstate(pmlmepriv, _FW_LINKED)) {
5954                 rtw_disassoc_cmd(padapter, 0, true);
5955                 rtw_indicate_disconnect(padapter);
5956                 rtw_free_assoc_resources(padapter, 1);
5957         }
5958
5959         spin_unlock_bh(&pmlmepriv->lock);
5960         DBG_871X("SA query timeout disconnect\n");
5961 }
5962
5963 u8 NULL_hdl(struct adapter *padapter, u8 *pbuf)
5964 {
5965         return H2C_SUCCESS;
5966 }
5967
5968 #ifdef CONFIG_AUTO_AP_MODE
5969 static int rtw_auto_ap_start_beacon(struct adapter *adapter)
5970 {
5971         int ret = 0;
5972         u8 *pbuf = NULL;
5973         uint len;
5974         u8 supportRate[16];
5975         int     sz = 0, rateLen;
5976         u8 *ie;
5977         u8 wireless_mode, oper_channel;
5978         u8 ssid[3] = {0}; /* hidden ssid */
5979         u32 ssid_len = sizeof(ssid);
5980         struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
5981
5982
5983         if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true)
5984                 return -EINVAL;
5985
5986
5987         len = 128;
5988         pbuf = rtw_zmalloc(len);
5989         if (!pbuf)
5990                 return -ENOMEM;
5991
5992
5993         /* generate beacon */
5994         ie = pbuf;
5995
5996         /* timestamp will be inserted by hardware */
5997         sz += 8;
5998         ie += sz;
5999
6000         /* beacon interval : 2bytes */
6001         *(u16 *)ie = cpu_to_le16((u16)100);/* BCN_INTERVAL = 100; */
6002         sz += 2;
6003         ie += 2;
6004
6005         /* capability info */
6006         *(u16 *)ie = 0;
6007         *(u16 *)ie |= cpu_to_le16(cap_ESS);
6008         *(u16 *)ie |= cpu_to_le16(cap_ShortPremble);
6009         /* u16*)ie |= cpu_to_le16(cap_Privacy); */
6010         sz += 2;
6011         ie += 2;
6012
6013         /* SSID */
6014         ie = rtw_set_ie(ie, _SSID_IE_, ssid_len, ssid, &sz);
6015
6016         /* supported rates */
6017         wireless_mode = WIRELESS_11BG_24N;
6018         rtw_set_supported_rate(supportRate, wireless_mode);
6019         rateLen = rtw_get_rateset_len(supportRate);
6020         if (rateLen > 8) {
6021                 ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, 8, supportRate, &sz);
6022         } else{
6023                 ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, rateLen, supportRate, &sz);
6024         }
6025
6026
6027         /* DS parameter set */
6028         if (check_buddy_fwstate(adapter, _FW_LINKED) &&
6029                 check_buddy_fwstate(adapter, WIFI_STATION_STATE)) {
6030                 struct adapter *pbuddystruct adapter = adapter->pbuddystruct adapter;
6031                 struct mlme_ext_priv *pbuddy_mlmeext  = &pbuddystruct adapter->mlmeextpriv;
6032
6033                 oper_channel = pbuddy_mlmeext->cur_channel;
6034         } else{
6035                 oper_channel = adapter_to_dvobj(adapter)->oper_channel;
6036         }
6037         ie = rtw_set_ie(ie, _DSSET_IE_, 1, &oper_channel, &sz);
6038
6039         /* ext supported rates */
6040         if (rateLen > 8) {
6041                 ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (supportRate + 8), &sz);
6042         }
6043
6044         DBG_871X("%s, start auto ap beacon sz =%d\n", __func__, sz);
6045
6046         /* lunch ap mode & start to issue beacon */
6047         if (rtw_check_beacon_data(adapter, pbuf,  sz) == _SUCCESS) {
6048
6049         } else{
6050                 ret = -EINVAL;
6051         }
6052
6053
6054         kfree(pbuf);
6055
6056         return ret;
6057
6058 }
6059 #endif/* CONFIG_AUTO_AP_MODE */
6060
6061 u8 setopmode_hdl(struct adapter *padapter, u8 *pbuf)
6062 {
6063         u8 type;
6064         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6065         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6066         struct setopmode_parm *psetop = (struct setopmode_parm *)pbuf;
6067
6068         if (psetop->mode == Ndis802_11APMode) {
6069                 pmlmeinfo->state = WIFI_FW_AP_STATE;
6070                 type = _HW_STATE_AP_;
6071                 /* start_ap_mode(padapter); */
6072         } else if (psetop->mode == Ndis802_11Infrastructure) {
6073                 pmlmeinfo->state &= ~(BIT(0)|BIT(1));/*  clear state */
6074                 pmlmeinfo->state |= WIFI_FW_STATION_STATE;/* set to     STATION_STATE */
6075                 type = _HW_STATE_STATION_;
6076         } else if (psetop->mode == Ndis802_11IBSS) {
6077                 type = _HW_STATE_ADHOC_;
6078         } else{
6079                 type = _HW_STATE_NOLINK_;
6080         }
6081
6082         rtw_hal_set_hwreg(padapter, HW_VAR_SET_OPMODE, (u8 *)(&type));
6083         /* Set_NETYPE0_MSR(padapter, type); */
6084
6085
6086 #ifdef CONFIG_AUTO_AP_MODE
6087         if (psetop->mode == Ndis802_11APMode)
6088                 rtw_auto_ap_start_beacon(padapter);
6089 #endif
6090
6091         if (rtw_port_switch_chk(padapter)) {
6092                 rtw_hal_set_hwreg(padapter, HW_VAR_PORT_SWITCH, NULL);
6093
6094                 if (psetop->mode == Ndis802_11APMode)
6095                         adapter_to_pwrctl(padapter)->fw_psmode_iface_id = 0xff; /* ap mode won't dowload rsvd pages */
6096                 else if (psetop->mode == Ndis802_11Infrastructure) {
6097                         struct adapter *port0_iface = dvobj_get_port0_adapter(adapter_to_dvobj(padapter));
6098                         if (port0_iface)
6099                                 rtw_lps_ctrl_wk_cmd(port0_iface, LPS_CTRL_CONNECT, 0);
6100                 }
6101         }
6102
6103         if (psetop->mode == Ndis802_11APMode) {
6104                 /*  Do this after port switch to */
6105                 /*  prevent from downloading rsvd page to wrong port */
6106                 rtw_btcoex_MediaStatusNotify(padapter, 1); /* connect */
6107         }
6108
6109         return H2C_SUCCESS;
6110
6111 }
6112
6113 u8 createbss_hdl(struct adapter *padapter, u8 *pbuf)
6114 {
6115         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6116         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6117         struct wlan_bssid_ex    *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
6118         struct joinbss_parm *pparm = (struct joinbss_parm *)pbuf;
6119         /* u32 initialgain; */
6120
6121         if (pmlmeinfo->state == WIFI_FW_AP_STATE) {
6122                 struct wlan_bssid_ex *network = &padapter->mlmepriv.cur_network.network;
6123                 start_bss_network(padapter, (u8 *)network);
6124                 return H2C_SUCCESS;
6125         }
6126
6127         /* below is for ad-hoc master */
6128         if (pparm->network.InfrastructureMode == Ndis802_11IBSS) {
6129                 rtw_joinbss_reset(padapter);
6130
6131                 pmlmeext->cur_bwmode = CHANNEL_WIDTH_20;
6132                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
6133                 pmlmeinfo->ERP_enable = 0;
6134                 pmlmeinfo->WMM_enable = 0;
6135                 pmlmeinfo->HT_enable = 0;
6136                 pmlmeinfo->HT_caps_enable = 0;
6137                 pmlmeinfo->HT_info_enable = 0;
6138                 pmlmeinfo->agg_enable_bitmap = 0;
6139                 pmlmeinfo->candidate_tid_bitmap = 0;
6140
6141                 /* disable dynamic functions, such as high power, DIG */
6142                 Save_DM_Func_Flag(padapter);
6143                 Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false);
6144
6145                 /* config the initial gain under linking, need to write the BB registers */
6146                 /* initialgain = 0x1E; */
6147                 /* rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain)); */
6148
6149                 /* cancel link timer */
6150                 del_timer_sync(&pmlmeext->link_timer);
6151
6152                 /* clear CAM */
6153                 flush_all_cam_entry(padapter);
6154
6155                 memcpy(pnetwork, pbuf, FIELD_OFFSET(struct wlan_bssid_ex, IELength));
6156                 pnetwork->IELength = ((struct wlan_bssid_ex *)pbuf)->IELength;
6157
6158                 if (pnetwork->IELength > MAX_IE_SZ)/* Check pbuf->IELength */
6159                         return H2C_PARAMETERS_ERROR;
6160
6161                 memcpy(pnetwork->IEs, ((struct wlan_bssid_ex *)pbuf)->IEs, pnetwork->IELength);
6162
6163                 start_create_ibss(padapter);
6164
6165         }
6166
6167         return H2C_SUCCESS;
6168
6169 }
6170
6171 u8 join_cmd_hdl(struct adapter *padapter, u8 *pbuf)
6172 {
6173         u8 join_type;
6174         struct ndis_80211_var_ie *pIE;
6175         struct registry_priv *pregpriv = &padapter->registrypriv;
6176         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6177         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6178         struct wlan_bssid_ex            *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
6179         u32 i;
6180         u8 cbw40_enable = 0;
6181         /* u32 initialgain; */
6182         /* u32 acparm; */
6183         u8 ch, bw, offset;
6184
6185         /* check already connecting to AP or not */
6186         if (pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) {
6187                 if (pmlmeinfo->state & WIFI_FW_STATION_STATE) {
6188                         issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, 1, 100);
6189                 }
6190                 pmlmeinfo->state = WIFI_FW_NULL_STATE;
6191
6192                 /* clear CAM */
6193                 flush_all_cam_entry(padapter);
6194
6195                 del_timer_sync(&pmlmeext->link_timer);
6196
6197                 /* set MSR to nolink -> infra. mode */
6198                 /* Set_MSR(padapter, _HW_STATE_NOLINK_); */
6199                 Set_MSR(padapter, _HW_STATE_STATION_);
6200
6201
6202                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_DISCONNECT, NULL);
6203         }
6204
6205         rtw_joinbss_reset(padapter);
6206
6207         pmlmeext->cur_bwmode = CHANNEL_WIDTH_20;
6208         pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
6209         pmlmeinfo->ERP_enable = 0;
6210         pmlmeinfo->WMM_enable = 0;
6211         pmlmeinfo->HT_enable = 0;
6212         pmlmeinfo->HT_caps_enable = 0;
6213         pmlmeinfo->HT_info_enable = 0;
6214         pmlmeinfo->agg_enable_bitmap = 0;
6215         pmlmeinfo->candidate_tid_bitmap = 0;
6216         pmlmeinfo->bwmode_updated = false;
6217         /* pmlmeinfo->assoc_AP_vendor = HT_IOT_PEER_MAX; */
6218         pmlmeinfo->VHT_enable = 0;
6219
6220         memcpy(pnetwork, pbuf, FIELD_OFFSET(struct wlan_bssid_ex, IELength));
6221         pnetwork->IELength = ((struct wlan_bssid_ex *)pbuf)->IELength;
6222
6223         if (pnetwork->IELength > MAX_IE_SZ)/* Check pbuf->IELength */
6224                 return H2C_PARAMETERS_ERROR;
6225
6226         memcpy(pnetwork->IEs, ((struct wlan_bssid_ex *)pbuf)->IEs, pnetwork->IELength);
6227
6228         pmlmeext->cur_channel = (u8)pnetwork->Configuration.DSConfig;
6229         pmlmeinfo->bcn_interval = get_beacon_interval(pnetwork);
6230
6231         /* Check AP vendor to move rtw_joinbss_cmd() */
6232         /* pmlmeinfo->assoc_AP_vendor = check_assoc_AP(pnetwork->IEs, pnetwork->IELength); */
6233
6234         /* sizeof(struct ndis_802_11_fix_ie) */
6235         for (i = _FIXED_IE_LENGTH_; i < pnetwork->IELength;) {
6236                 pIE = (struct ndis_80211_var_ie *)(pnetwork->IEs + i);
6237
6238                 switch (pIE->ElementID) {
6239                 case _VENDOR_SPECIFIC_IE_:/* Get WMM IE. */
6240                         if (!memcmp(pIE->data, WMM_OUI, 4))
6241                                 WMM_param_handler(padapter, pIE);
6242                         break;
6243
6244                 case _HT_CAPABILITY_IE_:        /* Get HT Cap IE. */
6245                         pmlmeinfo->HT_caps_enable = 1;
6246                         break;
6247
6248                 case _HT_EXTRA_INFO_IE_:        /* Get HT Info IE. */
6249                         pmlmeinfo->HT_info_enable = 1;
6250
6251                         /* spec case only for cisco's ap because cisco's ap issue assoc rsp using mcs rate @40MHz or @20MHz */
6252                         {
6253                                 struct HT_info_element *pht_info = (struct HT_info_element *)(pIE->data);
6254
6255                                 if (pnetwork->Configuration.DSConfig > 14) {
6256                                         if ((pregpriv->bw_mode >> 4) > CHANNEL_WIDTH_20)
6257                                                 cbw40_enable = 1;
6258                                 } else {
6259                                         if ((pregpriv->bw_mode & 0x0f) > CHANNEL_WIDTH_20)
6260                                                 cbw40_enable = 1;
6261                                 }
6262
6263                                 if ((cbw40_enable) && (pht_info->infos[0] & BIT(2))) {
6264                                         /* switch to the 40M Hz mode according to the AP */
6265                                         pmlmeext->cur_bwmode = CHANNEL_WIDTH_40;
6266                                         switch (pht_info->infos[0] & 0x3) {
6267                                         case 1:
6268                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
6269                                                 break;
6270
6271                                         case 3:
6272                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
6273                                                 break;
6274
6275                                         default:
6276                                                 pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
6277                                                 pmlmeext->cur_bwmode = CHANNEL_WIDTH_20;
6278                                                 break;
6279                                         }
6280
6281                                         DBG_871X("set HT ch/bw before connected\n");
6282                                 }
6283                         }
6284                         break;
6285                 default:
6286                         break;
6287                 }
6288
6289                 i += (pIE->Length + 2);
6290         }
6291
6292         /* check channel, bandwidth, offset and switch */
6293         if (rtw_chk_start_clnt_join(padapter, &ch, &bw, &offset) == _FAIL) {
6294                 report_join_res(padapter, (-4));
6295                 return H2C_SUCCESS;
6296         }
6297
6298         /* disable dynamic functions, such as high power, DIG */
6299         /* Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false); */
6300
6301         /* config the initial gain under linking, need to write the BB registers */
6302         /* initialgain = 0x1E; */
6303         /* rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain)); */
6304
6305         rtw_hal_set_hwreg(padapter, HW_VAR_BSSID, pmlmeinfo->network.MacAddress);
6306         join_type = 0;
6307         rtw_hal_set_hwreg(padapter, HW_VAR_MLME_JOIN, (u8 *)(&join_type));
6308         rtw_hal_set_hwreg(padapter, HW_VAR_DO_IQK, NULL);
6309
6310         set_channel_bwmode(padapter, ch, offset, bw);
6311
6312         /* cancel link timer */
6313         del_timer_sync(&pmlmeext->link_timer);
6314
6315         start_clnt_join(padapter);
6316
6317         return H2C_SUCCESS;
6318
6319 }
6320
6321 u8 disconnect_hdl(struct adapter *padapter, unsigned char *pbuf)
6322 {
6323         struct disconnect_parm *param = (struct disconnect_parm *)pbuf;
6324         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6325         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6326         struct wlan_bssid_ex            *pnetwork = (struct wlan_bssid_ex *)(&(pmlmeinfo->network));
6327         u8 val8;
6328
6329         if (is_client_associated_to_ap(padapter)) {
6330                         issue_deauth_ex(padapter, pnetwork->MacAddress, WLAN_REASON_DEAUTH_LEAVING, param->deauth_timeout_ms/100, 100);
6331         }
6332
6333         if (((pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) || ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)) {
6334                 /* Stop BCN */
6335                 val8 = 0;
6336                 rtw_hal_set_hwreg(padapter, HW_VAR_BCN_FUNC, (u8 *)(&val8));
6337         }
6338
6339         rtw_mlmeext_disconnect(padapter);
6340
6341         rtw_free_uc_swdec_pending_queue(padapter);
6342
6343         return  H2C_SUCCESS;
6344 }
6345
6346 static int rtw_scan_ch_decision(struct adapter *padapter, struct rtw_ieee80211_channel *out,
6347         u32 out_num, struct rtw_ieee80211_channel *in, u32 in_num)
6348 {
6349         int i, j;
6350         int set_idx;
6351         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6352
6353         /* clear first */
6354         memset(out, 0, sizeof(struct rtw_ieee80211_channel)*out_num);
6355
6356         /* acquire channels from in */
6357         j = 0;
6358         for (i = 0; i < in_num; i++) {
6359
6360                 DBG_871X(FUNC_ADPT_FMT" "CHAN_FMT"\n", FUNC_ADPT_ARG(padapter), CHAN_ARG(&in[i]));
6361
6362                 set_idx = rtw_ch_set_search_ch(pmlmeext->channel_set, in[i].hw_value);
6363                 if (in[i].hw_value && !(in[i].flags & RTW_IEEE80211_CHAN_DISABLED)
6364                         && set_idx >= 0
6365                         && rtw_mlme_band_check(padapter, in[i].hw_value)
6366                 ) {
6367                         if (j >= out_num) {
6368                                 DBG_871X_LEVEL(_drv_always_, FUNC_ADPT_FMT" out_num:%u not enough\n",
6369                                         FUNC_ADPT_ARG(padapter), out_num);
6370                                 break;
6371                         }
6372
6373                         memcpy(&out[j], &in[i], sizeof(struct rtw_ieee80211_channel));
6374
6375                         if (pmlmeext->channel_set[set_idx].ScanType == SCAN_PASSIVE)
6376                                 out[j].flags |= RTW_IEEE80211_CHAN_PASSIVE_SCAN;
6377
6378                         j++;
6379                 }
6380                 if (j >= out_num)
6381                         break;
6382         }
6383
6384         /* if out is empty, use channel_set as default */
6385         if (j == 0) {
6386                 for (i = 0; i < pmlmeext->max_chan_nums; i++) {
6387
6388                         DBG_871X(FUNC_ADPT_FMT" ch:%u\n", FUNC_ADPT_ARG(padapter), pmlmeext->channel_set[i].ChannelNum);
6389
6390                         if (rtw_mlme_band_check(padapter, pmlmeext->channel_set[i].ChannelNum)) {
6391
6392                                 if (j >= out_num) {
6393                                         DBG_871X_LEVEL(_drv_always_, FUNC_ADPT_FMT" out_num:%u not enough\n",
6394                                                 FUNC_ADPT_ARG(padapter), out_num);
6395                                         break;
6396                                 }
6397
6398                                 out[j].hw_value = pmlmeext->channel_set[i].ChannelNum;
6399
6400                                 if (pmlmeext->channel_set[i].ScanType == SCAN_PASSIVE)
6401                                         out[j].flags |= RTW_IEEE80211_CHAN_PASSIVE_SCAN;
6402
6403                                 j++;
6404                         }
6405                 }
6406         }
6407
6408         return j;
6409 }
6410
6411 u8 sitesurvey_cmd_hdl(struct adapter *padapter, u8 *pbuf)
6412 {
6413         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6414         struct sitesurvey_parm  *pparm = (struct sitesurvey_parm *)pbuf;
6415         u8 bdelayscan = false;
6416         u8 val8;
6417         u32 initialgain;
6418         u32 i;
6419
6420         if (pmlmeext->sitesurvey_res.state == SCAN_DISABLE) {
6421                 pmlmeext->sitesurvey_res.state = SCAN_START;
6422                 pmlmeext->sitesurvey_res.bss_cnt = 0;
6423                 pmlmeext->sitesurvey_res.channel_idx = 0;
6424
6425                 for (i = 0; i < RTW_SSID_SCAN_AMOUNT; i++) {
6426                         if (pparm->ssid[i].SsidLength) {
6427                                 memcpy(pmlmeext->sitesurvey_res.ssid[i].Ssid, pparm->ssid[i].Ssid, IW_ESSID_MAX_SIZE);
6428                                 pmlmeext->sitesurvey_res.ssid[i].SsidLength = pparm->ssid[i].SsidLength;
6429                         } else {
6430                                 pmlmeext->sitesurvey_res.ssid[i].SsidLength = 0;
6431                         }
6432                 }
6433
6434                 pmlmeext->sitesurvey_res.ch_num = rtw_scan_ch_decision(padapter
6435                         , pmlmeext->sitesurvey_res.ch, RTW_CHANNEL_SCAN_AMOUNT
6436                         , pparm->ch, pparm->ch_num
6437                 );
6438
6439                 pmlmeext->sitesurvey_res.scan_mode = pparm->scan_mode;
6440
6441                 /* issue null data if associating to the AP */
6442                 if (is_client_associated_to_ap(padapter)) {
6443                         pmlmeext->sitesurvey_res.state = SCAN_TXNULL;
6444
6445                         issue_nulldata(padapter, NULL, 1, 3, 500);
6446
6447                         bdelayscan = true;
6448                 }
6449                 if (bdelayscan) {
6450                         /* delay 50ms to protect nulldata(1). */
6451                         set_survey_timer(pmlmeext, 50);
6452                         return H2C_SUCCESS;
6453                 }
6454         }
6455
6456         if ((pmlmeext->sitesurvey_res.state == SCAN_START) || (pmlmeext->sitesurvey_res.state == SCAN_TXNULL)) {
6457                 /* disable dynamic functions, such as high power, DIG */
6458                 Save_DM_Func_Flag(padapter);
6459                 Switch_DM_Func(padapter, DYNAMIC_FUNC_DISABLE, false);
6460
6461                 /* config the initial gain under scaning, need to write the BB registers */
6462                 initialgain = 0x1e;
6463
6464                 rtw_hal_set_hwreg(padapter, HW_VAR_INITIAL_GAIN, (u8 *)(&initialgain));
6465
6466                 /* set MSR to no link state */
6467                 Set_MSR(padapter, _HW_STATE_NOLINK_);
6468
6469                 val8 = 1; /* under site survey */
6470                 rtw_hal_set_hwreg(padapter, HW_VAR_MLME_SITESURVEY, (u8 *)(&val8));
6471
6472                 pmlmeext->sitesurvey_res.state = SCAN_PROCESS;
6473         }
6474
6475         site_survey(padapter);
6476
6477         return H2C_SUCCESS;
6478
6479 }
6480
6481 u8 setauth_hdl(struct adapter *padapter, unsigned char *pbuf)
6482 {
6483         struct setauth_parm             *pparm = (struct setauth_parm *)pbuf;
6484         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6485         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6486
6487         if (pparm->mode < 4) {
6488                 pmlmeinfo->auth_algo = pparm->mode;
6489         }
6490
6491         return  H2C_SUCCESS;
6492 }
6493
6494 u8 setkey_hdl(struct adapter *padapter, u8 *pbuf)
6495 {
6496         u16 ctrl = 0;
6497         s16 cam_id = 0;
6498         struct setkey_parm              *pparm = (struct setkey_parm *)pbuf;
6499         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6500         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6501         unsigned char null_addr[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
6502         u8 *addr;
6503
6504         /* main tx key for wep. */
6505         if (pparm->set_tx)
6506                 pmlmeinfo->key_index = pparm->keyid;
6507
6508         cam_id = rtw_camid_alloc(padapter, NULL, pparm->keyid);
6509
6510         if (cam_id < 0) {
6511         } else {
6512                 if (cam_id > 3) /* not default key, searched by A2 */
6513                         addr = get_bssid(&padapter->mlmepriv);
6514                 else
6515                         addr = null_addr;
6516
6517                 ctrl = BIT(15) | BIT6 | ((pparm->algorithm) << 2) | pparm->keyid;
6518                 write_cam(padapter, cam_id, ctrl, addr, pparm->key);
6519                 DBG_871X_LEVEL(_drv_always_, "set group key camid:%d, addr:"MAC_FMT", kid:%d, type:%s\n"
6520                         , cam_id, MAC_ARG(addr), pparm->keyid, security_type_str(pparm->algorithm));
6521         }
6522
6523         if (cam_id >= 0 && cam_id <= 3)
6524                 rtw_hal_set_hwreg(padapter, HW_VAR_SEC_DK_CFG, (u8 *)true);
6525
6526         /* allow multicast packets to driver */
6527         padapter->HalFunc.SetHwRegHandler(padapter, HW_VAR_ON_RCR_AM, null_addr);
6528
6529         return H2C_SUCCESS;
6530 }
6531
6532 u8 set_stakey_hdl(struct adapter *padapter, u8 *pbuf)
6533 {
6534         u16 ctrl = 0;
6535         s16 cam_id = 0;
6536         u8 ret = H2C_SUCCESS;
6537         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6538         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6539         struct set_stakey_parm  *pparm = (struct set_stakey_parm *)pbuf;
6540         struct sta_priv *pstapriv = &padapter->stapriv;
6541         struct sta_info *psta;
6542
6543         if (pparm->algorithm == _NO_PRIVACY_)
6544                 goto write_to_cam;
6545
6546         psta = rtw_get_stainfo(pstapriv, pparm->addr);
6547         if (!psta) {
6548                 DBG_871X_LEVEL(_drv_always_, "%s sta:"MAC_FMT" not found\n", __func__, MAC_ARG(pparm->addr));
6549                 ret = H2C_REJECTED;
6550                 goto exit;
6551         }
6552
6553         pmlmeinfo->enc_algo = pparm->algorithm;
6554         cam_id = rtw_camid_alloc(padapter, psta, 0);
6555         if (cam_id < 0)
6556                 goto exit;
6557
6558 write_to_cam:
6559         if (pparm->algorithm == _NO_PRIVACY_) {
6560                 while ((cam_id = rtw_camid_search(padapter, pparm->addr, -1)) >= 0) {
6561                         DBG_871X_LEVEL(_drv_always_, "clear key for addr:"MAC_FMT", camid:%d\n", MAC_ARG(pparm->addr), cam_id);
6562                         clear_cam_entry(padapter, cam_id);
6563                         rtw_camid_free(padapter, cam_id);
6564                 }
6565         } else {
6566                 DBG_871X_LEVEL(_drv_always_, "set pairwise key camid:%d, addr:"MAC_FMT", kid:%d, type:%s\n",
6567                         cam_id, MAC_ARG(pparm->addr), pparm->keyid, security_type_str(pparm->algorithm));
6568                 ctrl = BIT(15) | ((pparm->algorithm) << 2) | pparm->keyid;
6569                 write_cam(padapter, cam_id, ctrl, pparm->addr, pparm->key);
6570         }
6571         ret = H2C_SUCCESS_RSP;
6572
6573 exit:
6574         return ret;
6575 }
6576
6577 u8 add_ba_hdl(struct adapter *padapter, unsigned char *pbuf)
6578 {
6579         struct addBaReq_parm    *pparm = (struct addBaReq_parm *)pbuf;
6580         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6581         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6582
6583         struct sta_info *psta = rtw_get_stainfo(&padapter->stapriv, pparm->addr);
6584
6585         if (!psta)
6586                 return  H2C_SUCCESS;
6587
6588         if (((pmlmeinfo->state & WIFI_FW_ASSOC_SUCCESS) && (pmlmeinfo->HT_enable)) ||
6589                 ((pmlmeinfo->state&0x03) == WIFI_FW_AP_STATE)) {
6590                 /* pmlmeinfo->ADDBA_retry_count = 0; */
6591                 /* pmlmeinfo->candidate_tid_bitmap |= (0x1 << pparm->tid); */
6592                 /* psta->htpriv.candidate_tid_bitmap |= BIT(pparm->tid); */
6593                 issue_action_BA(padapter, pparm->addr, RTW_WLAN_ACTION_ADDBA_REQ, (u16)pparm->tid);
6594                 /* _set_timer(&pmlmeext->ADDBA_timer, ADDBA_TO); */
6595                 _set_timer(&psta->addba_retry_timer, ADDBA_TO);
6596         } else{
6597                 psta->htpriv.candidate_tid_bitmap &= ~BIT(pparm->tid);
6598         }
6599         return  H2C_SUCCESS;
6600 }
6601
6602
6603 u8 chk_bmc_sleepq_cmd(struct adapter *padapter)
6604 {
6605         struct cmd_obj *ph2c;
6606         struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
6607         u8 res = _SUCCESS;
6608
6609         ph2c = rtw_zmalloc(sizeof(struct cmd_obj));
6610         if (ph2c == NULL) {
6611                 res = _FAIL;
6612                 goto exit;
6613         }
6614
6615         init_h2fwcmd_w_parm_no_parm_rsp(ph2c, GEN_CMD_CODE(_ChkBMCSleepq));
6616
6617         res = rtw_enqueue_cmd(pcmdpriv, ph2c);
6618
6619 exit:
6620         return res;
6621 }
6622
6623 u8 set_tx_beacon_cmd(struct adapter *padapter)
6624 {
6625         struct cmd_obj  *ph2c;
6626         struct Tx_Beacon_param  *ptxBeacon_parm;
6627         struct cmd_priv *pcmdpriv = &(padapter->cmdpriv);
6628         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6629         struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info);
6630         u8 res = _SUCCESS;
6631         int len_diff = 0;
6632
6633         ph2c = rtw_zmalloc(sizeof(struct cmd_obj));
6634         if (ph2c == NULL) {
6635                 res = _FAIL;
6636                 goto exit;
6637         }
6638
6639         ptxBeacon_parm = rtw_zmalloc(sizeof(struct Tx_Beacon_param));
6640         if (ptxBeacon_parm == NULL) {
6641                 kfree((unsigned char *)ph2c);
6642                 res = _FAIL;
6643                 goto exit;
6644         }
6645
6646         memcpy(&(ptxBeacon_parm->network), &(pmlmeinfo->network), sizeof(struct wlan_bssid_ex));
6647
6648         len_diff = update_hidden_ssid(
6649                 ptxBeacon_parm->network.IEs+_BEACON_IE_OFFSET_
6650                 , ptxBeacon_parm->network.IELength-_BEACON_IE_OFFSET_
6651                 , pmlmeinfo->hidden_ssid_mode
6652         );
6653         ptxBeacon_parm->network.IELength += len_diff;
6654
6655         init_h2fwcmd_w_parm_no_rsp(ph2c, ptxBeacon_parm, GEN_CMD_CODE(_TX_Beacon));
6656
6657         res = rtw_enqueue_cmd(pcmdpriv, ph2c);
6658
6659 exit:
6660         return res;
6661 }
6662
6663
6664 u8 mlme_evt_hdl(struct adapter *padapter, unsigned char *pbuf)
6665 {
6666         u8 evt_code, evt_seq;
6667         u16 evt_sz;
6668         uint    *peventbuf;
6669         void (*event_callback)(struct adapter *dev, u8 *pbuf);
6670         struct evt_priv *pevt_priv = &(padapter->evtpriv);
6671
6672         if (pbuf == NULL)
6673                 goto _abort_event_;
6674
6675         peventbuf = (uint *)pbuf;
6676         evt_sz = (u16)(*peventbuf&0xffff);
6677         evt_seq = (u8)((*peventbuf>>24)&0x7f);
6678         evt_code = (u8)((*peventbuf>>16)&0xff);
6679
6680
6681         #ifdef CHECK_EVENT_SEQ
6682         /*  checking event sequence... */
6683         if (evt_seq != (atomic_read(&pevt_priv->event_seq) & 0x7f)) {
6684                 RT_TRACE(_module_rtl871x_cmd_c_, _drv_info_,
6685                          ("Event Seq Error! %d vs %d\n", (evt_seq & 0x7f),
6686                           (atomic_read(&pevt_priv->event_seq) & 0x7f)));
6687
6688                 pevt_priv->event_seq = (evt_seq+1)&0x7f;
6689
6690                 goto _abort_event_;
6691         }
6692         #endif
6693
6694         /*  checking if event code is valid */
6695         if (evt_code >= MAX_C2HEVT) {
6696                 RT_TRACE(_module_rtl871x_cmd_c_, _drv_err_, ("\nEvent Code(%d) mismatch!\n", evt_code));
6697                 goto _abort_event_;
6698         }
6699
6700         /*  checking if event size match the event parm size */
6701         if ((wlanevents[evt_code].parmsize != 0) &&
6702                         (wlanevents[evt_code].parmsize != evt_sz)) {
6703
6704                 RT_TRACE(_module_rtl871x_cmd_c_, _drv_err_, ("\nEvent(%d) Parm Size mismatch (%d vs %d)!\n",
6705                         evt_code, wlanevents[evt_code].parmsize, evt_sz));
6706                 goto _abort_event_;
6707
6708         }
6709
6710         atomic_inc(&pevt_priv->event_seq);
6711
6712         peventbuf += 2;
6713
6714         if (peventbuf) {
6715                 event_callback = wlanevents[evt_code].event_callback;
6716                 event_callback(padapter, (u8 *)peventbuf);
6717
6718                 pevt_priv->evt_done_cnt++;
6719         }
6720
6721
6722 _abort_event_:
6723
6724
6725         return H2C_SUCCESS;
6726
6727 }
6728
6729 u8 h2c_msg_hdl(struct adapter *padapter, unsigned char *pbuf)
6730 {
6731         if (!pbuf)
6732                 return H2C_PARAMETERS_ERROR;
6733
6734         return H2C_SUCCESS;
6735 }
6736
6737 u8 chk_bmc_sleepq_hdl(struct adapter *padapter, unsigned char *pbuf)
6738 {
6739         struct sta_info *psta_bmc;
6740         struct list_head        *xmitframe_plist, *xmitframe_phead;
6741         struct xmit_frame *pxmitframe = NULL;
6742         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
6743         struct sta_priv  *pstapriv = &padapter->stapriv;
6744
6745         /* for BC/MC Frames */
6746         psta_bmc = rtw_get_bcmc_stainfo(padapter);
6747         if (!psta_bmc)
6748                 return H2C_SUCCESS;
6749
6750         if ((pstapriv->tim_bitmap&BIT(0)) && (psta_bmc->sleepq_len > 0)) {
6751                 msleep(10);/*  10ms, ATIM(HIQ) Windows */
6752
6753                 /* spin_lock_bh(&psta_bmc->sleep_q.lock); */
6754                 spin_lock_bh(&pxmitpriv->lock);
6755
6756                 xmitframe_phead = get_list_head(&psta_bmc->sleep_q);
6757                 xmitframe_plist = get_next(xmitframe_phead);
6758
6759                 while (xmitframe_phead != xmitframe_plist) {
6760                         pxmitframe = LIST_CONTAINOR(xmitframe_plist, struct xmit_frame, list);
6761
6762                         xmitframe_plist = get_next(xmitframe_plist);
6763
6764                         list_del_init(&pxmitframe->list);
6765
6766                         psta_bmc->sleepq_len--;
6767                         if (psta_bmc->sleepq_len > 0)
6768                                 pxmitframe->attrib.mdata = 1;
6769                         else
6770                                 pxmitframe->attrib.mdata = 0;
6771
6772                         pxmitframe->attrib.triggered = 1;
6773
6774                         if (xmitframe_hiq_filter(pxmitframe))
6775                                 pxmitframe->attrib.qsel = 0x11;/* HIQ */
6776
6777                         rtw_hal_xmitframe_enqueue(padapter, pxmitframe);
6778                 }
6779
6780                 /* spin_unlock_bh(&psta_bmc->sleep_q.lock); */
6781                 spin_unlock_bh(&pxmitpriv->lock);
6782
6783                 /* check hi queue and bmc_sleepq */
6784                 rtw_chk_hi_queue_cmd(padapter);
6785         }
6786
6787         return H2C_SUCCESS;
6788 }
6789
6790 u8 tx_beacon_hdl(struct adapter *padapter, unsigned char *pbuf)
6791 {
6792         if (send_beacon(padapter) == _FAIL) {
6793                 DBG_871X("issue_beacon, fail!\n");
6794                 return H2C_PARAMETERS_ERROR;
6795         }
6796
6797         /* tx bc/mc frames after update TIM */
6798         chk_bmc_sleepq_hdl(padapter, NULL);
6799
6800         return H2C_SUCCESS;
6801 }
6802
6803 int rtw_chk_start_clnt_join(struct adapter *padapter, u8 *ch, u8 *bw, u8 *offset)
6804 {
6805         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6806         unsigned char cur_ch = pmlmeext->cur_channel;
6807         unsigned char cur_bw = pmlmeext->cur_bwmode;
6808         unsigned char cur_ch_offset = pmlmeext->cur_ch_offset;
6809         bool connect_allow = true;
6810
6811         if (!ch || !bw || !offset) {
6812                 rtw_warn_on(1);
6813                 connect_allow = false;
6814         }
6815
6816         if (connect_allow) {
6817                 DBG_871X("start_join_set_ch_bw: ch =%d, bwmode =%d, ch_offset =%d\n", cur_ch, cur_bw, cur_ch_offset);
6818                 *ch = cur_ch;
6819                 *bw = cur_bw;
6820                 *offset = cur_ch_offset;
6821         }
6822
6823         return connect_allow == true ? _SUCCESS : _FAIL;
6824 }
6825
6826 /* Find union about ch, bw, ch_offset of all linked/linking interfaces */
6827 int rtw_get_ch_setting_union(struct adapter *adapter, u8 *ch, u8 *bw, u8 *offset)
6828 {
6829         struct dvobj_priv *dvobj = adapter_to_dvobj(adapter);
6830         struct adapter *iface;
6831         struct mlme_ext_priv *mlmeext;
6832         u8 ch_ret = 0;
6833         u8 bw_ret = CHANNEL_WIDTH_20;
6834         u8 offset_ret = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
6835
6836         if (ch)
6837                 *ch = 0;
6838         if (bw)
6839                 *bw = CHANNEL_WIDTH_20;
6840         if (offset)
6841                 *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
6842
6843         iface = dvobj->padapters;
6844         mlmeext = &iface->mlmeextpriv;
6845
6846         if (!check_fwstate(&iface->mlmepriv, _FW_LINKED|_FW_UNDER_LINKING))
6847                 return 0;
6848
6849         ch_ret = mlmeext->cur_channel;
6850         bw_ret = mlmeext->cur_bwmode;
6851         offset_ret = mlmeext->cur_ch_offset;
6852
6853         return 1;
6854 }
6855
6856 u8 set_ch_hdl(struct adapter *padapter, u8 *pbuf)
6857 {
6858         struct set_ch_parm *set_ch_parm;
6859         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6860
6861         if (!pbuf)
6862                 return H2C_PARAMETERS_ERROR;
6863
6864         set_ch_parm = (struct set_ch_parm *)pbuf;
6865
6866         DBG_871X(FUNC_NDEV_FMT" ch:%u, bw:%u, ch_offset:%u\n",
6867                 FUNC_NDEV_ARG(padapter->pnetdev),
6868                 set_ch_parm->ch, set_ch_parm->bw, set_ch_parm->ch_offset);
6869
6870         pmlmeext->cur_channel = set_ch_parm->ch;
6871         pmlmeext->cur_ch_offset = set_ch_parm->ch_offset;
6872         pmlmeext->cur_bwmode = set_ch_parm->bw;
6873
6874         set_channel_bwmode(padapter, set_ch_parm->ch, set_ch_parm->ch_offset, set_ch_parm->bw);
6875
6876         return  H2C_SUCCESS;
6877 }
6878
6879 u8 set_chplan_hdl(struct adapter *padapter, unsigned char *pbuf)
6880 {
6881         struct SetChannelPlan_param *setChannelPlan_param;
6882         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
6883
6884         if (!pbuf)
6885                 return H2C_PARAMETERS_ERROR;
6886
6887         setChannelPlan_param = (struct SetChannelPlan_param *)pbuf;
6888
6889         pmlmeext->max_chan_nums = init_channel_set(padapter, setChannelPlan_param->channel_plan, pmlmeext->channel_set);
6890         init_channel_list(padapter, pmlmeext->channel_set, pmlmeext->max_chan_nums, &pmlmeext->channel_list);
6891
6892         if ((padapter->rtw_wdev != NULL) && (padapter->rtw_wdev->wiphy)) {
6893                 struct regulatory_request request;
6894                 request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
6895                 rtw_reg_notifier(padapter->rtw_wdev->wiphy, &request);
6896         }
6897
6898         return  H2C_SUCCESS;
6899 }
6900
6901 u8 led_blink_hdl(struct adapter *padapter, unsigned char *pbuf)
6902 {
6903         struct LedBlink_param *ledBlink_param;
6904
6905         if (!pbuf)
6906                 return H2C_PARAMETERS_ERROR;
6907
6908         ledBlink_param = (struct LedBlink_param *)pbuf;
6909         return  H2C_SUCCESS;
6910 }
6911
6912 u8 set_csa_hdl(struct adapter *padapter, unsigned char *pbuf)
6913 {
6914         return  H2C_REJECTED;
6915 }
6916
6917 /*  TDLS_ESTABLISHED    : write RCR DATA BIT */
6918 /*  TDLS_CS_OFF         : go back to the channel linked with AP, terminating channel switch procedure */
6919 /*  TDLS_INIT_CH_SEN    : init channel sensing, receive all data and mgnt frame */
6920 /*  TDLS_DONE_CH_SEN: channel sensing and report candidate channel */
6921 /*  TDLS_OFF_CH         : first time set channel to off channel */
6922 /*  TDLS_BASE_CH                : go back tp the channel linked with AP when set base channel as target channel */
6923 /*  TDLS_P_OFF_CH       : periodically go to off channel */
6924 /*  TDLS_P_BASE_CH      : periodically go back to base channel */
6925 /*  TDLS_RS_RCR         : restore RCR */
6926 /*  TDLS_TEAR_STA       : free tdls sta */
6927 u8 tdls_hdl(struct adapter *padapter, unsigned char *pbuf)
6928 {
6929         return H2C_REJECTED;
6930 }
6931
6932 u8 run_in_thread_hdl(struct adapter *padapter, u8 *pbuf)
6933 {
6934         struct RunInThread_param *p;
6935
6936
6937         if (NULL == pbuf)
6938                 return H2C_PARAMETERS_ERROR;
6939         p = (struct RunInThread_param *)pbuf;
6940
6941         if (p->func)
6942                 p->func(p->context);
6943
6944         return H2C_SUCCESS;
6945 }