Merge 5.19-rc3 into staging-next
[linux-2.6-microblaze.git] / drivers / staging / r8188eu / os_dep / ioctl_linux.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
3
4 #include "../include/osdep_service.h"
5 #include "../include/drv_types.h"
6 #include "../include/wlan_bssdef.h"
7 #include "../include/wifi.h"
8 #include "../include/rtw_mlme.h"
9 #include "../include/rtw_mlme_ext.h"
10 #include "../include/rtw_ioctl.h"
11 #include "../include/rtw_ioctl_set.h"
12 #include "../include/usb_ops.h"
13 #include "../include/rtl8188e_hal.h"
14 #include "../include/rtw_led.h"
15
16 #include "../include/rtw_iol.h"
17
18 #define RTL_IOCTL_WPA_SUPPLICANT        (SIOCIWFIRSTPRIV + 30)
19
20 #define SCAN_ITEM_SIZE 768
21 #define MAX_CUSTOM_LEN 64
22 #define RATE_COUNT 4
23
24 /*  combo scan */
25 #define WEXT_CSCAN_AMOUNT 9
26 #define WEXT_CSCAN_BUF_LEN              360
27 #define WEXT_CSCAN_HEADER               "CSCAN S\x01\x00\x00S\x00"
28 #define WEXT_CSCAN_HEADER_SIZE          12
29 #define WEXT_CSCAN_SSID_SECTION         'S'
30 #define WEXT_CSCAN_CHANNEL_SECTION      'C'
31 #define WEXT_CSCAN_NPROBE_SECTION       'N'
32 #define WEXT_CSCAN_ACTV_DWELL_SECTION   'A'
33 #define WEXT_CSCAN_PASV_DWELL_SECTION   'P'
34 #define WEXT_CSCAN_HOME_DWELL_SECTION   'H'
35 #define WEXT_CSCAN_TYPE_SECTION         'T'
36
37 static u32 rtw_rates[] = {1000000, 2000000, 5500000, 11000000,
38         6000000, 9000000, 12000000, 18000000, 24000000, 36000000,
39         48000000, 54000000};
40
41 void indicate_wx_scan_complete_event(struct adapter *padapter)
42 {
43         union iwreq_data wrqu;
44
45         memset(&wrqu, 0, sizeof(union iwreq_data));
46         wireless_send_event(padapter->pnetdev, SIOCGIWSCAN, &wrqu, NULL);
47 }
48
49 void rtw_indicate_wx_assoc_event(struct adapter *padapter)
50 {
51         union iwreq_data wrqu;
52         struct  mlme_priv *pmlmepriv = &padapter->mlmepriv;
53
54         memset(&wrqu, 0, sizeof(union iwreq_data));
55
56         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
57
58         memcpy(wrqu.ap_addr.sa_data, pmlmepriv->cur_network.network.MacAddress, ETH_ALEN);
59
60         wireless_send_event(padapter->pnetdev, SIOCGIWAP, &wrqu, NULL);
61 }
62
63 void rtw_indicate_wx_disassoc_event(struct adapter *padapter)
64 {
65         union iwreq_data wrqu;
66
67         memset(&wrqu, 0, sizeof(union iwreq_data));
68
69         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
70         memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
71
72         wireless_send_event(padapter->pnetdev, SIOCGIWAP, &wrqu, NULL);
73 }
74
75 static char *translate_scan(struct adapter *padapter,
76                             struct iw_request_info *info,
77                             struct wlan_network *pnetwork,
78                             char *start, char *stop)
79 {
80         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
81         struct iw_event iwe;
82         u16 cap;
83         __le16 le_tmp;
84         u32 ht_ielen = 0;
85         char *custom;
86         char *p;
87         u16 max_rate = 0, rate, ht_cap = false;
88         u32 i = 0;
89         u8 bw_40MHz = 0, short_GI = 0;
90         u16 mcs_rate = 0;
91         u8 ss, sq;
92         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
93
94         if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) {
95                 u32     blnGotP2PIE = false;
96
97                 /*      User is doing the P2P device discovery */
98                 /*      The prefix of SSID should be "DIRECT-" and the IE should contains the P2P IE. */
99                 /*      If not, the driver should ignore this AP and go to the next AP. */
100
101                 /*      Verifying the SSID */
102                 if (!memcmp(pnetwork->network.Ssid.Ssid, pwdinfo->p2p_wildcard_ssid, P2P_WILDCARD_SSID_LEN)) {
103                         u32     p2pielen = 0;
104
105                         if (pnetwork->network.Reserved[0] == 2) {/*  Probe Request */
106                                 /*      Verifying the P2P IE */
107                                 if (rtw_get_p2p_ie(pnetwork->network.IEs, pnetwork->network.IELength, NULL, &p2pielen))
108                                         blnGotP2PIE = true;
109                         } else {/*  Beacon or Probe Respones */
110                                 /*      Verifying the P2P IE */
111                                 if (rtw_get_p2p_ie(&pnetwork->network.IEs[12], pnetwork->network.IELength - 12, NULL, &p2pielen))
112                                         blnGotP2PIE = true;
113                         }
114                 }
115
116                 if (!blnGotP2PIE)
117                         return start;
118         }
119
120         /*  AP MAC address  */
121         iwe.cmd = SIOCGIWAP;
122         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
123
124         memcpy(iwe.u.ap_addr.sa_data, pnetwork->network.MacAddress, ETH_ALEN);
125         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
126
127         /* Add the ESSID */
128         iwe.cmd = SIOCGIWESSID;
129         iwe.u.data.flags = 1;
130         iwe.u.data.length = min_t(u16, pnetwork->network.Ssid.SsidLength, 32);
131         start = iwe_stream_add_point(info, start, stop, &iwe, pnetwork->network.Ssid.Ssid);
132
133         /* parsing HT_CAP_IE */
134         p = rtw_get_ie(&pnetwork->network.IEs[12], _HT_CAPABILITY_IE_, &ht_ielen, pnetwork->network.IELength - 12);
135
136         if (p && ht_ielen > 0) {
137                 struct ieee80211_ht_cap *pht_capie;
138
139                 ht_cap = true;
140                 pht_capie = (struct ieee80211_ht_cap *)(p + 2);
141                 memcpy(&mcs_rate, pht_capie->mcs.rx_mask, 2);
142                 bw_40MHz = (le16_to_cpu(pht_capie->cap_info) &
143                             IEEE80211_HT_CAP_SUP_WIDTH_20_40) ? 1 : 0;
144                 short_GI = (le16_to_cpu(pht_capie->cap_info) &
145                             (IEEE80211_HT_CAP_SGI_20 | IEEE80211_HT_CAP_SGI_40)) ? 1 : 0;
146         }
147
148         /* Add the protocol name */
149         iwe.cmd = SIOCGIWNAME;
150         if ((rtw_is_cckratesonly_included((u8 *)&pnetwork->network.SupportedRates))) {
151                 if (ht_cap)
152                         snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11bn");
153                 else
154                 snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11b");
155         } else if ((rtw_is_cckrates_included((u8 *)&pnetwork->network.SupportedRates))) {
156                 if (ht_cap)
157                         snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11bgn");
158                 else
159                         snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11bg");
160         } else {
161                 if (ht_cap)
162                         snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11gn");
163                 else
164                         snprintf(iwe.u.name, IFNAMSIZ, "IEEE 802.11g");
165         }
166
167         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_CHAR_LEN);
168
169           /* Add mode */
170         iwe.cmd = SIOCGIWMODE;
171         memcpy(&le_tmp, rtw_get_capability_from_ie(pnetwork->network.IEs), 2);
172
173         cap = le16_to_cpu(le_tmp);
174
175         if (cap & (WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_BSS)) {
176                 if (cap & WLAN_CAPABILITY_BSS)
177                         iwe.u.mode = IW_MODE_MASTER;
178                 else
179                         iwe.u.mode = IW_MODE_ADHOC;
180
181                 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
182         }
183
184         if (pnetwork->network.Configuration.DSConfig < 1)
185                 pnetwork->network.Configuration.DSConfig = 1;
186
187          /* Add frequency/channel */
188         iwe.cmd = SIOCGIWFREQ;
189         iwe.u.freq.m = rtw_ch2freq(pnetwork->network.Configuration.DSConfig) * 100000;
190         iwe.u.freq.e = 1;
191         iwe.u.freq.i = pnetwork->network.Configuration.DSConfig;
192         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
193
194         /* Add encryption capability */
195         iwe.cmd = SIOCGIWENCODE;
196         if (cap & WLAN_CAPABILITY_PRIVACY)
197                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
198         else
199                 iwe.u.data.flags = IW_ENCODE_DISABLED;
200         iwe.u.data.length = 0;
201         start = iwe_stream_add_point(info, start, stop, &iwe, pnetwork->network.Ssid.Ssid);
202
203         /*Add basic and extended rates */
204         max_rate = 0;
205         custom = kzalloc(MAX_CUSTOM_LEN, GFP_ATOMIC);
206         if (!custom)
207                 return start;
208         p = custom;
209         p += snprintf(p, MAX_CUSTOM_LEN - (p - custom), " Rates (Mb/s): ");
210         while (pnetwork->network.SupportedRates[i] != 0) {
211                 rate = pnetwork->network.SupportedRates[i] & 0x7F;
212                 if (rate > max_rate)
213                         max_rate = rate;
214                 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
215                               "%d%s ", rate >> 1, (rate & 1) ? ".5" : "");
216                 i++;
217         }
218
219         if (ht_cap) {
220                 if (mcs_rate & 0x8000)/* MCS15 */
221                         max_rate = (bw_40MHz) ? ((short_GI) ? 300 : 270) : ((short_GI) ? 144 : 130);
222                 else if (mcs_rate & 0x0080)/* MCS7 */
223                         ;
224                 else/* default MCS7 */
225                         max_rate = (bw_40MHz) ? ((short_GI) ? 150 : 135) : ((short_GI) ? 72 : 65);
226
227                 max_rate = max_rate * 2;/* Mbps/2; */
228         }
229
230         iwe.cmd = SIOCGIWRATE;
231         iwe.u.bitrate.fixed = 0;
232         iwe.u.bitrate.disabled = 0;
233         iwe.u.bitrate.value = max_rate * 500000;
234         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_PARAM_LEN);
235
236         /* parsing WPA/WPA2 IE */
237         {
238                 u8 *buf;
239                 u8 *wpa_ie, *rsn_ie;
240                 u16 wpa_len = 0, rsn_len = 0;
241                 u8 *p;
242
243                 buf = kzalloc(MAX_WPA_IE_LEN, GFP_ATOMIC);
244                 if (!buf)
245                         goto exit;
246                 wpa_ie = kzalloc(255, GFP_ATOMIC);
247                 if (!wpa_ie) {
248                         kfree(buf);
249                         goto exit;
250                 }
251                 rsn_ie = kzalloc(255, GFP_ATOMIC);
252                 if (!rsn_ie) {
253                         kfree(buf);
254                         kfree(wpa_ie);
255                         goto exit;
256                 }
257                 rtw_get_sec_ie(pnetwork->network.IEs, pnetwork->network.IELength, rsn_ie, &rsn_len, wpa_ie, &wpa_len);
258
259                 if (wpa_len > 0) {
260                         p = buf;
261                         memset(buf, 0, MAX_WPA_IE_LEN);
262                         p += sprintf(p, "wpa_ie =");
263                         for (i = 0; i < wpa_len; i++)
264                                 p += sprintf(p, "%02x", wpa_ie[i]);
265
266                         memset(&iwe, 0, sizeof(iwe));
267                         iwe.cmd = IWEVCUSTOM;
268                         iwe.u.data.length = strlen(buf);
269                         start = iwe_stream_add_point(info, start, stop, &iwe, buf);
270
271                         memset(&iwe, 0, sizeof(iwe));
272                         iwe.cmd = IWEVGENIE;
273                         iwe.u.data.length = wpa_len;
274                         start = iwe_stream_add_point(info, start, stop, &iwe, wpa_ie);
275                 }
276                 if (rsn_len > 0) {
277                         p = buf;
278                         memset(buf, 0, MAX_WPA_IE_LEN);
279                         p += sprintf(p, "rsn_ie =");
280                         for (i = 0; i < rsn_len; i++)
281                                 p += sprintf(p, "%02x", rsn_ie[i]);
282                         memset(&iwe, 0, sizeof(iwe));
283                         iwe.cmd = IWEVCUSTOM;
284                         iwe.u.data.length = strlen(buf);
285                         start = iwe_stream_add_point(info, start, stop, &iwe, buf);
286
287                         memset(&iwe, 0, sizeof(iwe));
288                         iwe.cmd = IWEVGENIE;
289                         iwe.u.data.length = rsn_len;
290                         start = iwe_stream_add_point(info, start, stop, &iwe, rsn_ie);
291                 }
292                 kfree(buf);
293                 kfree(wpa_ie);
294                 kfree(rsn_ie);
295         }
296
297         {/* parsing WPS IE */
298                 uint cnt = 0, total_ielen;
299                 u8 *wpsie_ptr = NULL;
300                 uint wps_ielen = 0;
301
302                 u8 *ie_ptr = pnetwork->network.IEs + _FIXED_IE_LENGTH_;
303                 total_ielen = pnetwork->network.IELength - _FIXED_IE_LENGTH_;
304
305                 while (cnt < total_ielen) {
306                         if (rtw_is_wps_ie(&ie_ptr[cnt], &wps_ielen) && (wps_ielen > 2)) {
307                                 wpsie_ptr = &ie_ptr[cnt];
308                                 iwe.cmd = IWEVGENIE;
309                                 iwe.u.data.length = (u16)wps_ielen;
310                                 start = iwe_stream_add_point(info, start, stop, &iwe, wpsie_ptr);
311                         }
312                         cnt += ie_ptr[cnt + 1] + 2; /* goto next */
313                 }
314         }
315
316         /* Add quality statistics */
317         iwe.cmd = IWEVQUAL;
318         iwe.u.qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED | IW_QUAL_NOISE_INVALID;
319
320         if (check_fwstate(pmlmepriv, _FW_LINKED) &&
321             is_same_network(&pmlmepriv->cur_network.network, &pnetwork->network)) {
322                 ss = padapter->recvpriv.signal_strength;
323                 sq = padapter->recvpriv.signal_qual;
324         } else {
325                 ss = pnetwork->network.PhyInfo.SignalStrength;
326                 sq = pnetwork->network.PhyInfo.SignalQuality;
327         }
328
329         iwe.u.qual.level = (u8)ss;
330         iwe.u.qual.qual = (u8)sq;   /*  signal quality */
331         iwe.u.qual.noise = 0; /*  noise level */
332         start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
333 exit:
334         kfree(custom);
335         return start;
336 }
337
338 static int wpa_set_auth_algs(struct net_device *dev, u32 value)
339 {
340         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
341         int ret = 0;
342
343         if ((value & AUTH_ALG_SHARED_KEY) && (value & AUTH_ALG_OPEN_SYSTEM)) {
344                 padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
345                 padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeAutoSwitch;
346                 padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Auto;
347         } else if (value & AUTH_ALG_SHARED_KEY) {
348                 padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
349
350                 padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeShared;
351                 padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Shared;
352         } else if (value & AUTH_ALG_OPEN_SYSTEM) {
353                 if (padapter->securitypriv.ndisauthtype < Ndis802_11AuthModeWPAPSK) {
354                         padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeOpen;
355                         padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
356                 }
357         } else if (!(value & AUTH_ALG_LEAP)) {
358                 ret = -EINVAL;
359         }
360         return ret;
361 }
362
363 static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len)
364 {
365         int ret = 0;
366         u32 wep_key_idx, wep_key_len, wep_total_len;
367         struct ndis_802_11_wep   *pwep = NULL;
368         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
369         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
370         struct security_priv *psecuritypriv = &padapter->securitypriv;
371         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
372
373         param->u.crypt.err = 0;
374         param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
375
376         if (param_len < (u32)((u8 *)param->u.crypt.key - (u8 *)param) + param->u.crypt.key_len) {
377                 ret =  -EINVAL;
378                 goto exit;
379         }
380
381         if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
382             param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
383             param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
384                 if (param->u.crypt.idx >= WEP_KEYS) {
385                         ret = -EINVAL;
386                         goto exit;
387                 }
388         } else {
389                 ret = -EINVAL;
390                 goto exit;
391         }
392
393         if (strcmp(param->u.crypt.alg, "WEP") == 0) {
394                 padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
395                 padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_;
396                 padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_;
397
398                 wep_key_idx = param->u.crypt.idx;
399                 wep_key_len = param->u.crypt.key_len;
400
401                 if (wep_key_idx > WEP_KEYS)
402                         return -EINVAL;
403
404                 if (wep_key_len > 0) {
405                         wep_key_len = wep_key_len <= 5 ? 5 : 13;
406                         wep_total_len = wep_key_len + sizeof(*pwep);
407                         pwep = kzalloc(wep_total_len, GFP_KERNEL);
408                         if (!pwep)
409                                 goto exit;
410
411                         pwep->KeyLength = wep_key_len;
412                         pwep->Length = wep_total_len;
413                         if (wep_key_len == 13) {
414                                 padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
415                                 padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_;
416                         }
417                 } else {
418                         ret = -EINVAL;
419                         goto exit;
420                 }
421                 pwep->KeyIndex = wep_key_idx;
422                 pwep->KeyIndex |= 0x80000000;
423                 memcpy(pwep->KeyMaterial,  param->u.crypt.key, pwep->KeyLength);
424                 if (param->u.crypt.set_tx) {
425                         if (rtw_set_802_11_add_wep(padapter, pwep) == (u8)_FAIL)
426                                 ret = -EOPNOTSUPP;
427                 } else {
428                         if (wep_key_idx >= WEP_KEYS) {
429                                 ret = -EOPNOTSUPP;
430                                 goto exit;
431                         }
432                         memcpy(&psecuritypriv->dot11DefKey[wep_key_idx].skey[0], pwep->KeyMaterial, pwep->KeyLength);
433                         psecuritypriv->dot11DefKeylen[wep_key_idx] = pwep->KeyLength;
434                         rtw_set_key(padapter, psecuritypriv, wep_key_idx, 0);
435                 }
436                 goto exit;
437         }
438
439         if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) { /*  802_1x */
440                 struct sta_info *psta, *pbcmc_sta;
441                 struct sta_priv *pstapriv = &padapter->stapriv;
442
443                 if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_MP_STATE)) { /* sta mode */
444                         psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv));
445                         if (!psta) {
446                                 ;
447                         } else {
448                                 if (strcmp(param->u.crypt.alg, "none") != 0)
449                                         psta->ieee8021x_blocked = false;
450
451                                 if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) ||
452                                     (padapter->securitypriv.ndisencryptstatus ==  Ndis802_11Encryption3Enabled))
453                                         psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
454
455                                 if (param->u.crypt.set_tx == 1) { /* pairwise key */
456                                         memcpy(psta->dot118021x_UncstKey.skey,  param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
457
458                                         if (strcmp(param->u.crypt.alg, "TKIP") == 0) { /* set mic key */
459                                                 memcpy(psta->dot11tkiptxmickey.skey, &param->u.crypt.key[16], 8);
460                                                 memcpy(psta->dot11tkiprxmickey.skey, &param->u.crypt.key[24], 8);
461                                                 padapter->securitypriv.busetkipkey = false;
462                                         }
463
464                                         rtw_setstakey_cmd(padapter, (unsigned char *)psta, true);
465                                 } else { /* group key */
466                                         memcpy(padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey,  param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
467                                         memcpy(padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey, &param->u.crypt.key[16], 8);
468                                         memcpy(padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey, &param->u.crypt.key[24], 8);
469                                         padapter->securitypriv.binstallGrpkey = true;
470
471                                         padapter->securitypriv.dot118021XGrpKeyid = param->u.crypt.idx;
472
473                                         rtw_set_key(padapter, &padapter->securitypriv, param->u.crypt.idx, 1);
474                                         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_PROVISIONING_ING))
475                                                 rtw_p2p_set_state(pwdinfo, P2P_STATE_PROVISIONING_DONE);
476                                 }
477                         }
478                         pbcmc_sta = rtw_get_bcmc_stainfo(padapter);
479                         if (!pbcmc_sta) {
480                                 ;
481                         } else {
482                                 /* Jeff: don't disable ieee8021x_blocked while clearing key */
483                                 if (strcmp(param->u.crypt.alg, "none") != 0)
484                                         pbcmc_sta->ieee8021x_blocked = false;
485
486                                 if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) ||
487                                     (padapter->securitypriv.ndisencryptstatus ==  Ndis802_11Encryption3Enabled))
488                                         pbcmc_sta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
489                         }
490                 }
491         }
492
493 exit:
494
495         kfree(pwep);
496
497         return ret;
498 }
499
500 static int rtw_set_wpa_ie(struct adapter *padapter, char *pie, unsigned short ielen)
501 {
502         u8 *buf = NULL;
503         int group_cipher = 0, pairwise_cipher = 0;
504         int ret = 0;
505         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
506
507         if (ielen > MAX_WPA_IE_LEN || !pie) {
508                 _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
509                 if (!pie)
510                         return ret;
511                 else
512                         return -EINVAL;
513         }
514
515         if (ielen) {
516                 buf = kmemdup(pie, ielen, GFP_KERNEL);
517                 if (!buf) {
518                         ret =  -ENOMEM;
519                         goto exit;
520                 }
521
522                 if (ielen < RSN_HEADER_LEN) {
523                         ret  = -1;
524                         goto exit;
525                 }
526
527                 if (rtw_parse_wpa_ie(buf, ielen, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
528                         padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
529                         padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPAPSK;
530                         memcpy(padapter->securitypriv.supplicant_ie, &buf[0], ielen);
531                 }
532
533                 if (rtw_parse_wpa2_ie(buf, ielen, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
534                         padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
535                         padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK;
536                         memcpy(padapter->securitypriv.supplicant_ie, &buf[0], ielen);
537                 }
538
539                 switch (group_cipher) {
540                 case WPA_CIPHER_NONE:
541                         padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_;
542                         padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
543                         break;
544                 case WPA_CIPHER_WEP40:
545                         padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_;
546                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
547                         break;
548                 case WPA_CIPHER_TKIP:
549                         padapter->securitypriv.dot118021XGrpPrivacy = _TKIP_;
550                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled;
551                         break;
552                 case WPA_CIPHER_CCMP:
553                         padapter->securitypriv.dot118021XGrpPrivacy = _AES_;
554                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled;
555                         break;
556                 case WPA_CIPHER_WEP104:
557                         padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_;
558                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
559                         break;
560                 }
561
562                 switch (pairwise_cipher) {
563                 case WPA_CIPHER_NONE:
564                         padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
565                         padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
566                         break;
567                 case WPA_CIPHER_WEP40:
568                         padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_;
569                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
570                         break;
571                 case WPA_CIPHER_TKIP:
572                         padapter->securitypriv.dot11PrivacyAlgrthm = _TKIP_;
573                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled;
574                         break;
575                 case WPA_CIPHER_CCMP:
576                         padapter->securitypriv.dot11PrivacyAlgrthm = _AES_;
577                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled;
578                         break;
579                 case WPA_CIPHER_WEP104:
580                         padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
581                         padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
582                         break;
583                 }
584
585                 _clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
586                 {/* set wps_ie */
587                         u16 cnt = 0;
588                         u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
589
590                         while (cnt < ielen) {
591                                 eid = buf[cnt];
592                                 if ((eid == _VENDOR_SPECIFIC_IE_) && (!memcmp(&buf[cnt + 2], wps_oui, 4))) {
593                                         padapter->securitypriv.wps_ie_len = ((buf[cnt + 1] + 2) < (MAX_WPA_IE_LEN << 2)) ? (buf[cnt + 1] + 2) : (MAX_WPA_IE_LEN << 2);
594
595                                         memcpy(padapter->securitypriv.wps_ie, &buf[cnt], padapter->securitypriv.wps_ie_len);
596
597                                         set_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS);
598                                         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_GONEGO_OK))
599                                                 rtw_p2p_set_state(pwdinfo, P2P_STATE_PROVISIONING_ING);
600                                         cnt += buf[cnt + 1] + 2;
601                                         break;
602                                 } else {
603                                         cnt += buf[cnt + 1] + 2; /* goto next */
604                                 }
605                         }
606                 }
607         }
608
609 exit:
610         kfree(buf);
611         return ret;
612 }
613
614 typedef unsigned char   NDIS_802_11_RATES_EX[NDIS_802_11_LENGTH_RATES_EX];
615
616 static int rtw_wx_get_name(struct net_device *dev,
617                              struct iw_request_info *info,
618                              union iwreq_data *wrqu, char *extra)
619 {
620         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
621         u32 ht_ielen = 0;
622         char *p;
623         u8 ht_cap = false;
624         struct  mlme_priv       *pmlmepriv = &padapter->mlmepriv;
625         struct wlan_bssid_ex  *pcur_bss = &pmlmepriv->cur_network.network;
626         NDIS_802_11_RATES_EX *prates = NULL;
627
628         if (check_fwstate(pmlmepriv, _FW_LINKED | WIFI_ADHOC_MASTER_STATE)) {
629                 /* parsing HT_CAP_IE */
630                 p = rtw_get_ie(&pcur_bss->IEs[12], _HT_CAPABILITY_IE_, &ht_ielen, pcur_bss->IELength - 12);
631                 if (p && ht_ielen > 0)
632                         ht_cap = true;
633
634                 prates = &pcur_bss->SupportedRates;
635
636                 if (rtw_is_cckratesonly_included((u8 *)prates)) {
637                         if (ht_cap)
638                                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11bn");
639                         else
640                                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11b");
641                 } else if (rtw_is_cckrates_included((u8 *)prates)) {
642                         if (ht_cap)
643                                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11bgn");
644                         else
645                                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11bg");
646                 } else {
647                         if (ht_cap)
648                                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11gn");
649                         else
650                                 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11g");
651                 }
652         } else {
653                 snprintf(wrqu->name, IFNAMSIZ, "unassociated");
654         }
655
656
657
658         return 0;
659 }
660
661 static int rtw_wx_get_freq(struct net_device *dev,
662                              struct iw_request_info *info,
663                              union iwreq_data *wrqu, char *extra)
664 {
665         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
666         struct  mlme_priv       *pmlmepriv = &padapter->mlmepriv;
667         struct wlan_bssid_ex  *pcur_bss = &pmlmepriv->cur_network.network;
668
669         if (check_fwstate(pmlmepriv, _FW_LINKED)) {
670                 /* wrqu->freq.m = ieee80211_wlan_frequencies[pcur_bss->Configuration.DSConfig-1] * 100000; */
671                 wrqu->freq.m = rtw_ch2freq(pcur_bss->Configuration.DSConfig) * 100000;
672                 wrqu->freq.e = 1;
673                 wrqu->freq.i = pcur_bss->Configuration.DSConfig;
674         } else {
675                 wrqu->freq.m = rtw_ch2freq(padapter->mlmeextpriv.cur_channel) * 100000;
676                 wrqu->freq.e = 1;
677                 wrqu->freq.i = padapter->mlmeextpriv.cur_channel;
678         }
679
680         return 0;
681 }
682
683 static int rtw_wx_set_mode(struct net_device *dev, struct iw_request_info *a,
684                              union iwreq_data *wrqu, char *b)
685 {
686         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
687         enum ndis_802_11_network_infra networkType;
688         int ret = 0;
689
690
691
692         if (_FAIL == rtw_pwr_wakeup(padapter)) {
693                 ret = -EPERM;
694                 goto exit;
695         }
696
697         if (!padapter->hw_init_completed) {
698                 ret = -EPERM;
699                 goto exit;
700         }
701
702         switch (wrqu->mode) {
703         case IW_MODE_AUTO:
704                 networkType = Ndis802_11AutoUnknown;
705                 break;
706         case IW_MODE_ADHOC:
707                 networkType = Ndis802_11IBSS;
708                 break;
709         case IW_MODE_MASTER:
710                 networkType = Ndis802_11APMode;
711                 break;
712         case IW_MODE_INFRA:
713                 networkType = Ndis802_11Infrastructure;
714                 break;
715         default:
716                 ret = -EINVAL;
717                 goto exit;
718         }
719         if (!rtw_set_802_11_infrastructure_mode(padapter, networkType)) {
720                 ret = -EPERM;
721                 goto exit;
722         }
723         rtw_setopmode_cmd(padapter, networkType);
724 exit:
725
726         return ret;
727 }
728
729 static int rtw_wx_get_mode(struct net_device *dev, struct iw_request_info *a,
730                              union iwreq_data *wrqu, char *b)
731 {
732         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
733         struct  mlme_priv       *pmlmepriv = &padapter->mlmepriv;
734
735         if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
736                 wrqu->mode = IW_MODE_INFRA;
737         else if  ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) ||
738                   (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)))
739                 wrqu->mode = IW_MODE_ADHOC;
740         else if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
741                 wrqu->mode = IW_MODE_MASTER;
742         else
743                 wrqu->mode = IW_MODE_AUTO;
744
745
746
747         return 0;
748 }
749
750 static int rtw_wx_set_pmkid(struct net_device *dev,
751                             struct iw_request_info *a,
752                             union iwreq_data *wrqu, char *extra)
753 {
754         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
755         u8   j, blInserted = false;
756         int  ret = false;
757         struct security_priv *psecuritypriv = &padapter->securitypriv;
758         struct iw_pmksa *pPMK = (struct iw_pmksa *)extra;
759         u8     strZeroMacAddress[ETH_ALEN] = {0x00};
760         u8     strIssueBssid[ETH_ALEN] = {0x00};
761
762         memcpy(strIssueBssid, pPMK->bssid.sa_data, ETH_ALEN);
763         if (pPMK->cmd == IW_PMKSA_ADD) {
764                 if (!memcmp(strIssueBssid, strZeroMacAddress, ETH_ALEN))
765                         return ret;
766                 else
767                         ret = true;
768                 blInserted = false;
769
770                 /* overwrite PMKID */
771                 for (j = 0; j < NUM_PMKID_CACHE; j++) {
772                         if (!memcmp(psecuritypriv->PMKIDList[j].Bssid, strIssueBssid, ETH_ALEN)) {
773                                 /*  BSSID is matched, the same AP => rewrite with new PMKID. */
774                                 memcpy(psecuritypriv->PMKIDList[j].PMKID, pPMK->pmkid, IW_PMKID_LEN);
775                                 psecuritypriv->PMKIDList[j].bUsed = true;
776                                 psecuritypriv->PMKIDIndex = j + 1;
777                                 blInserted = true;
778                                 break;
779                         }
780                 }
781
782                 if (!blInserted) {
783                         /*  Find a new entry */
784                         memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].Bssid, strIssueBssid, ETH_ALEN);
785                         memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].PMKID, pPMK->pmkid, IW_PMKID_LEN);
786
787                         psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].bUsed = true;
788                         psecuritypriv->PMKIDIndex++;
789                         if (psecuritypriv->PMKIDIndex == 16)
790                                 psecuritypriv->PMKIDIndex = 0;
791                 }
792         } else if (pPMK->cmd == IW_PMKSA_REMOVE) {
793                 ret = true;
794                 for (j = 0; j < NUM_PMKID_CACHE; j++) {
795                         if (!memcmp(psecuritypriv->PMKIDList[j].Bssid, strIssueBssid, ETH_ALEN)) {
796                                 /*  BSSID is matched, the same AP => Remove this PMKID information and reset it. */
797                                 memset(psecuritypriv->PMKIDList[j].Bssid, 0x00, ETH_ALEN);
798                                 psecuritypriv->PMKIDList[j].bUsed = false;
799                                 break;
800                         }
801                }
802         } else if (pPMK->cmd == IW_PMKSA_FLUSH) {
803                 memset(&psecuritypriv->PMKIDList[0], 0x00, sizeof(struct rt_pmkid_list) * NUM_PMKID_CACHE);
804                 psecuritypriv->PMKIDIndex = 0;
805                 ret = true;
806         }
807         return ret;
808 }
809
810 static int rtw_wx_get_sens(struct net_device *dev,
811                              struct iw_request_info *info,
812                              union iwreq_data *wrqu, char *extra)
813 {
814         wrqu->sens.value = 0;
815         wrqu->sens.fixed = 0;   /* no auto select */
816         wrqu->sens.disabled = 1;
817         return 0;
818 }
819
820 static int rtw_wx_get_range(struct net_device *dev,
821                                 struct iw_request_info *info,
822                                 union iwreq_data *wrqu, char *extra)
823 {
824         struct iw_range *range = (struct iw_range *)extra;
825         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
826         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
827
828         u16 val;
829         int i;
830
831         wrqu->data.length = sizeof(*range);
832         memset(range, 0, sizeof(*range));
833
834         /* Let's try to keep this struct in the same order as in
835          * linux/include/wireless.h
836          */
837
838         /* TODO: See what values we can set, and remove the ones we can't
839          * set, or fill them with some default data.
840          */
841
842         /* ~5 Mb/s real (802.11b) */
843         range->throughput = 5 * 1000 * 1000;
844
845         /* signal level threshold range */
846
847         /* percent values between 0 and 100. */
848         range->max_qual.qual = 100;
849         range->max_qual.level = 100;
850         range->max_qual.noise = 100;
851         range->max_qual.updated = 7; /* Updated all three */
852
853         range->avg_qual.qual = 92; /* > 8% missed beacons is 'bad' */
854         /* TODO: Find real 'good' to 'bad' threshol value for RSSI */
855         range->avg_qual.level = 178; /* -78 dBm */
856         range->avg_qual.noise = 0;
857         range->avg_qual.updated = 7; /* Updated all three */
858
859         range->num_bitrates = RATE_COUNT;
860
861         for (i = 0; i < RATE_COUNT && i < IW_MAX_BITRATES; i++)
862                 range->bitrate[i] = rtw_rates[i];
863
864         range->min_frag = MIN_FRAG_THRESHOLD;
865         range->max_frag = MAX_FRAG_THRESHOLD;
866
867         range->pm_capa = 0;
868
869         range->we_version_compiled = WIRELESS_EXT;
870         range->we_version_source = 16;
871
872         for (i = 0, val = 0; i < MAX_CHANNEL_NUM; i++) {
873                 /*  Include only legal frequencies for some countries */
874                 if (pmlmeext->channel_set[i].ChannelNum != 0) {
875                         range->freq[val].i = pmlmeext->channel_set[i].ChannelNum;
876                         range->freq[val].m = rtw_ch2freq(pmlmeext->channel_set[i].ChannelNum) * 100000;
877                         range->freq[val].e = 1;
878                         val++;
879                 }
880
881                 if (val == IW_MAX_FREQUENCIES)
882                         break;
883         }
884
885         range->num_channels = val;
886         range->num_frequency = val;
887
888 /*  The following code will proivde the security capability to network manager. */
889 /*  If the driver doesn't provide this capability to network manager, */
890 /*  the WPA/WPA2 routers can't be chosen in the network manager. */
891
892 /*
893 #define IW_SCAN_CAPA_NONE               0x00
894 #define IW_SCAN_CAPA_ESSID              0x01
895 #define IW_SCAN_CAPA_BSSID              0x02
896 #define IW_SCAN_CAPA_CHANNEL            0x04
897 #define IW_SCAN_CAPA_MODE               0x08
898 #define IW_SCAN_CAPA_RATE               0x10
899 #define IW_SCAN_CAPA_TYPE               0x20
900 #define IW_SCAN_CAPA_TIME               0x40
901 */
902
903         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
904                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
905
906         range->scan_capa = IW_SCAN_CAPA_ESSID | IW_SCAN_CAPA_TYPE |
907                            IW_SCAN_CAPA_BSSID | IW_SCAN_CAPA_CHANNEL |
908                            IW_SCAN_CAPA_MODE | IW_SCAN_CAPA_RATE;
909
910
911         return 0;
912 }
913
914 /* set bssid flow */
915 /* s1. rtw_set_802_11_infrastructure_mode() */
916 /* s2. rtw_set_802_11_authentication_mode() */
917 /* s3. set_802_11_encryption_mode() */
918 /* s4. rtw_set_802_11_bssid() */
919 static int rtw_wx_set_wap(struct net_device *dev,
920                          struct iw_request_info *info,
921                          union iwreq_data *awrq,
922                          char *extra)
923 {
924         uint ret = 0;
925         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
926         struct sockaddr *temp = (struct sockaddr *)awrq;
927         struct  mlme_priv       *pmlmepriv = &padapter->mlmepriv;
928         struct list_head *phead;
929         u8 *dst_bssid, *src_bssid;
930         struct __queue *queue   = &pmlmepriv->scanned_queue;
931         struct  wlan_network    *pnetwork = NULL;
932         enum ndis_802_11_auth_mode      authmode;
933
934
935
936         if (_FAIL == rtw_pwr_wakeup(padapter)) {
937                 ret = -1;
938                 goto exit;
939         }
940
941         if (!padapter->bup) {
942                 ret = -1;
943                 goto exit;
944         }
945
946         if (temp->sa_family != ARPHRD_ETHER) {
947                 ret = -EINVAL;
948                 goto exit;
949         }
950
951         authmode = padapter->securitypriv.ndisauthtype;
952         spin_lock_bh(&queue->lock);
953         phead = get_list_head(queue);
954         pmlmepriv->pscanned = phead->next;
955
956         while (phead != pmlmepriv->pscanned) {
957
958                 pnetwork = container_of(pmlmepriv->pscanned, struct wlan_network, list);
959
960                 pmlmepriv->pscanned = pmlmepriv->pscanned->next;
961
962                 dst_bssid = pnetwork->network.MacAddress;
963
964                 src_bssid = temp->sa_data;
965
966                 if ((!memcmp(dst_bssid, src_bssid, ETH_ALEN))) {
967                         if (!rtw_set_802_11_infrastructure_mode(padapter, pnetwork->network.InfrastructureMode)) {
968                                 ret = -1;
969                                 spin_unlock_bh(&queue->lock);
970                                 goto exit;
971                         }
972
973                         break;
974                 }
975         }
976         spin_unlock_bh(&queue->lock);
977
978         rtw_set_802_11_authentication_mode(padapter, authmode);
979         /* set_802_11_encryption_mode(padapter, padapter->securitypriv.ndisencryptstatus); */
980         if (!rtw_set_802_11_bssid(padapter, temp->sa_data)) {
981                 ret = -1;
982                 goto exit;
983         }
984
985 exit:
986
987
988
989         return ret;
990 }
991
992 static int rtw_wx_get_wap(struct net_device *dev,
993                             struct iw_request_info *info,
994                             union iwreq_data *wrqu, char *extra)
995 {
996         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
997         struct  mlme_priv       *pmlmepriv = &padapter->mlmepriv;
998         struct wlan_bssid_ex  *pcur_bss = &pmlmepriv->cur_network.network;
999
1000         wrqu->ap_addr.sa_family = ARPHRD_ETHER;
1001
1002         memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
1003
1004         if (check_fwstate(pmlmepriv, _FW_LINKED) ||
1005             check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) ||
1006             check_fwstate(pmlmepriv, WIFI_AP_STATE))
1007                 memcpy(wrqu->ap_addr.sa_data, pcur_bss->MacAddress, ETH_ALEN);
1008         else
1009                 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
1010
1011
1012
1013         return 0;
1014 }
1015
1016 static int rtw_wx_set_mlme(struct net_device *dev,
1017                              struct iw_request_info *info,
1018                              union iwreq_data *wrqu, char *extra)
1019 {
1020         int ret = 0;
1021         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1022         struct iw_mlme *mlme = (struct iw_mlme *)extra;
1023
1024         if (!mlme)
1025                 return -1;
1026
1027         switch (mlme->cmd) {
1028         case IW_MLME_DEAUTH:
1029                 if (!rtw_set_802_11_disassociate(padapter))
1030                         ret = -1;
1031                 break;
1032         case IW_MLME_DISASSOC:
1033                 if (!rtw_set_802_11_disassociate(padapter))
1034                         ret = -1;
1035                 break;
1036         default:
1037                 return -EOPNOTSUPP;
1038         }
1039         return ret;
1040 }
1041
1042 static int rtw_wx_set_scan(struct net_device *dev, struct iw_request_info *a,
1043                              union iwreq_data *wrqu, char *extra)
1044 {
1045         u8 _status = false;
1046         int ret = 0;
1047         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1048         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1049         struct ndis_802_11_ssid ssid[RTW_SSID_SCAN_AMOUNT];
1050         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
1051
1052         if (_FAIL == rtw_pwr_wakeup(padapter)) {
1053                 ret = -1;
1054                 goto exit;
1055         }
1056
1057         if (padapter->bDriverStopped) {
1058                 ret = -1;
1059                 goto exit;
1060         }
1061
1062         if (!padapter->bup) {
1063                 ret = -1;
1064                 goto exit;
1065         }
1066
1067         if (!padapter->hw_init_completed) {
1068                 ret = -1;
1069                 goto exit;
1070         }
1071
1072         /*  When Busy Traffic, driver do not site survey. So driver return success. */
1073         /*  wpa_supplicant will not issue SIOCSIWSCAN cmd again after scan timeout. */
1074         /*  modify by thomas 2011-02-22. */
1075         if (pmlmepriv->LinkDetectInfo.bBusyTraffic) {
1076                 indicate_wx_scan_complete_event(padapter);
1077                 goto exit;
1078         }
1079
1080         if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY | _FW_UNDER_LINKING)) {
1081                 indicate_wx_scan_complete_event(padapter);
1082                 goto exit;
1083         }
1084
1085 /*      For the DMP WiFi Display project, the driver won't to scan because */
1086 /*      the pmlmepriv->scan_interval is always equal to 3. */
1087 /*      So, the wpa_supplicant won't find out the WPS SoftAP. */
1088
1089         if (pwdinfo->p2p_state != P2P_STATE_NONE) {
1090                 rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo));
1091                 rtw_p2p_set_state(pwdinfo, P2P_STATE_FIND_PHASE_SEARCH);
1092                 rtw_p2p_findphase_ex_set(pwdinfo, P2P_FINDPHASE_EX_FULL);
1093                 rtw_free_network_queue(padapter, true);
1094         }
1095
1096         memset(ssid, 0, sizeof(struct ndis_802_11_ssid) * RTW_SSID_SCAN_AMOUNT);
1097
1098         if (wrqu->data.length == sizeof(struct iw_scan_req)) {
1099                 struct iw_scan_req *req = (struct iw_scan_req *)extra;
1100
1101                 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1102                         int len = min((int)req->essid_len, IW_ESSID_MAX_SIZE);
1103
1104                         memcpy(ssid[0].Ssid, req->essid, len);
1105                         ssid[0].SsidLength = len;
1106
1107                         spin_lock_bh(&pmlmepriv->lock);
1108
1109                         _status = rtw_sitesurvey_cmd(padapter, ssid, 1, NULL, 0);
1110
1111                         spin_unlock_bh(&pmlmepriv->lock);
1112                 }
1113         } else {
1114                 if (wrqu->data.length >= WEXT_CSCAN_HEADER_SIZE &&
1115                     !memcmp(extra, WEXT_CSCAN_HEADER, WEXT_CSCAN_HEADER_SIZE)) {
1116                         int len = wrqu->data.length - WEXT_CSCAN_HEADER_SIZE;
1117                         char *pos = extra + WEXT_CSCAN_HEADER_SIZE;
1118                         char section;
1119                         char sec_len;
1120                         int ssid_index = 0;
1121
1122                         while (len >= 1) {
1123                                 section = *(pos++);
1124                                 len -= 1;
1125
1126                                 switch (section) {
1127                                 case WEXT_CSCAN_SSID_SECTION:
1128                                         if (len < 1) {
1129                                                 len = 0;
1130                                                 break;
1131                                         }
1132                                         sec_len = *(pos++); len -= 1;
1133                                         if (sec_len > 0 &&
1134                                             sec_len <= len &&
1135                                             sec_len <= 32) {
1136                                                 ssid[ssid_index].SsidLength = sec_len;
1137                                                 memcpy(ssid[ssid_index].Ssid, pos, sec_len);
1138                                                 ssid_index++;
1139                                         }
1140                                         pos += sec_len;
1141                                         len -= sec_len;
1142                                         break;
1143                                 case WEXT_CSCAN_TYPE_SECTION:
1144                                 case WEXT_CSCAN_CHANNEL_SECTION:
1145                                         pos += 1;
1146                                         len -= 1;
1147                                         break;
1148                                 case WEXT_CSCAN_PASV_DWELL_SECTION:
1149                                 case WEXT_CSCAN_HOME_DWELL_SECTION:
1150                                 case WEXT_CSCAN_ACTV_DWELL_SECTION:
1151                                         pos += 2;
1152                                         len -= 2;
1153                                         break;
1154                                 default:
1155                                         len = 0; /*  stop parsing */
1156                                 }
1157                         }
1158
1159                         /* it has still some scan parameter to parse, we only do this now... */
1160                         _status = rtw_set_802_11_bssid_list_scan(padapter, ssid, RTW_SSID_SCAN_AMOUNT);
1161                 } else {
1162                         _status = rtw_set_802_11_bssid_list_scan(padapter, NULL, 0);
1163                 }
1164         }
1165
1166         if (!_status)
1167                 ret = -1;
1168
1169 exit:
1170
1171         return ret;
1172 }
1173
1174 static int rtw_wx_get_scan(struct net_device *dev, struct iw_request_info *a,
1175                              union iwreq_data *wrqu, char *extra)
1176 {
1177         struct list_head *plist, *phead;
1178         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1179         struct  mlme_priv       *pmlmepriv = &padapter->mlmepriv;
1180         struct __queue *queue   = &pmlmepriv->scanned_queue;
1181         struct  wlan_network    *pnetwork = NULL;
1182         char *ev = extra;
1183         char *stop = ev + wrqu->data.length;
1184         u32 ret = 0;
1185         u32 cnt = 0;
1186         u32 wait_for_surveydone;
1187         int wait_status;
1188         struct  wifidirect_info *pwdinfo = &padapter->wdinfo;
1189
1190         if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) {
1191                 /*      P2P is enabled */
1192                 wait_for_surveydone = 200;
1193         } else {
1194                 /*      P2P is disabled */
1195                 wait_for_surveydone = 100;
1196         }
1197
1198         wait_status = _FW_UNDER_SURVEY | _FW_UNDER_LINKING;
1199
1200         while (check_fwstate(pmlmepriv, wait_status)) {
1201                 msleep(30);
1202                 cnt++;
1203                 if (cnt > wait_for_surveydone)
1204                         break;
1205         }
1206
1207         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1208
1209         phead = get_list_head(queue);
1210         plist = phead->next;
1211
1212         while (phead != plist) {
1213                 if ((stop - ev) < SCAN_ITEM_SIZE) {
1214                         ret = -E2BIG;
1215                         break;
1216                 }
1217
1218                 pnetwork = container_of(plist, struct wlan_network, list);
1219
1220                 /* report network only if the current channel set contains the channel to which this network belongs */
1221                 if (rtw_ch_set_search_ch(padapter->mlmeextpriv.channel_set, pnetwork->network.Configuration.DSConfig) >= 0)
1222                         ev = translate_scan(padapter, a, pnetwork, ev, stop);
1223
1224                 plist = plist->next;
1225         }
1226
1227         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1228
1229         wrqu->data.length = ev - extra;
1230         wrqu->data.flags = 0;
1231
1232         return ret;
1233 }
1234
1235 /* set ssid flow */
1236 /* s1. rtw_set_802_11_infrastructure_mode() */
1237 /* s2. set_802_11_authenticaion_mode() */
1238 /* s3. set_802_11_encryption_mode() */
1239 /* s4. rtw_set_802_11_ssid() */
1240 static int rtw_wx_set_essid(struct net_device *dev,
1241                               struct iw_request_info *a,
1242                               union iwreq_data *wrqu, char *extra)
1243 {
1244         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1245         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1246         struct __queue *queue = &pmlmepriv->scanned_queue;
1247         struct list_head *phead;
1248         struct wlan_network *pnetwork = NULL;
1249         enum ndis_802_11_auth_mode authmode;
1250         struct ndis_802_11_ssid ndis_ssid;
1251         u8 *dst_ssid, *src_ssid;
1252
1253         uint ret = 0, len;
1254
1255         if (_FAIL == rtw_pwr_wakeup(padapter)) {
1256                 ret = -1;
1257                 goto exit;
1258         }
1259
1260         if (!padapter->bup) {
1261                 ret = -1;
1262                 goto exit;
1263         }
1264
1265         if (wrqu->essid.length > IW_ESSID_MAX_SIZE) {
1266                 ret = -E2BIG;
1267                 goto exit;
1268         }
1269
1270         if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1271                 ret = -1;
1272                 goto exit;
1273         }
1274
1275         authmode = padapter->securitypriv.ndisauthtype;
1276         if (wrqu->essid.flags && wrqu->essid.length) {
1277                 len = (wrqu->essid.length < IW_ESSID_MAX_SIZE) ? wrqu->essid.length : IW_ESSID_MAX_SIZE;
1278
1279                 memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid));
1280                 ndis_ssid.SsidLength = len;
1281                 memcpy(ndis_ssid.Ssid, extra, len);
1282                 src_ssid = ndis_ssid.Ssid;
1283
1284                 spin_lock_bh(&queue->lock);
1285                 phead = get_list_head(queue);
1286                 pmlmepriv->pscanned = phead->next;
1287
1288                 while (phead != pmlmepriv->pscanned) {
1289                         pnetwork = container_of(pmlmepriv->pscanned, struct wlan_network, list);
1290
1291                         pmlmepriv->pscanned = pmlmepriv->pscanned->next;
1292
1293                         dst_ssid = pnetwork->network.Ssid.Ssid;
1294
1295                         if ((!memcmp(dst_ssid, src_ssid, ndis_ssid.SsidLength)) &&
1296                             (pnetwork->network.Ssid.SsidLength == ndis_ssid.SsidLength)) {
1297
1298                                 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1299                                         if (pnetwork->network.InfrastructureMode != pmlmepriv->cur_network.network.InfrastructureMode)
1300                                                 continue;
1301                                 }
1302
1303                                 if (!rtw_set_802_11_infrastructure_mode(padapter, pnetwork->network.InfrastructureMode)) {
1304                                         ret = -1;
1305                                         spin_unlock_bh(&queue->lock);
1306                                         goto exit;
1307                                 }
1308
1309                                 break;
1310                         }
1311                 }
1312                 spin_unlock_bh(&queue->lock);
1313                 rtw_set_802_11_authentication_mode(padapter, authmode);
1314                 if (!rtw_set_802_11_ssid(padapter, &ndis_ssid)) {
1315                         ret = -1;
1316                         goto exit;
1317                 }
1318         }
1319
1320 exit:
1321         return ret;
1322 }
1323
1324 static int rtw_wx_get_essid(struct net_device *dev,
1325                               struct iw_request_info *a,
1326                               union iwreq_data *wrqu, char *extra)
1327 {
1328         u32 len;
1329         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1330         struct  mlme_priv       *pmlmepriv = &padapter->mlmepriv;
1331         struct wlan_bssid_ex  *pcur_bss = &pmlmepriv->cur_network.network;
1332
1333         if ((check_fwstate(pmlmepriv, _FW_LINKED)) ||
1334             (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE))) {
1335                 len = pcur_bss->Ssid.SsidLength;
1336                 memcpy(extra, pcur_bss->Ssid.Ssid, len);
1337         } else {
1338                 len = 0;
1339                 *extra = 0;
1340         }
1341         wrqu->essid.length = len;
1342         wrqu->essid.flags = 1;
1343
1344         return 0;
1345 }
1346
1347 static int rtw_wx_set_rate(struct net_device *dev,
1348                               struct iw_request_info *a,
1349                               union iwreq_data *wrqu, char *extra)
1350 {
1351         int i, ret = 0;
1352         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1353         u8 datarates[NumRates];
1354         u32     target_rate = wrqu->bitrate.value;
1355         u32     fixed = wrqu->bitrate.fixed;
1356         u32     ratevalue = 0;
1357         u8 mpdatarate[NumRates] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0xff};
1358
1359         if (target_rate == -1) {
1360                 ratevalue = 11;
1361                 goto set_rate;
1362         }
1363         target_rate = target_rate / 100000;
1364
1365         switch (target_rate) {
1366         case 10:
1367                 ratevalue = 0;
1368                 break;
1369         case 20:
1370                 ratevalue = 1;
1371                 break;
1372         case 55:
1373                 ratevalue = 2;
1374                 break;
1375         case 60:
1376                 ratevalue = 3;
1377                 break;
1378         case 90:
1379                 ratevalue = 4;
1380                 break;
1381         case 110:
1382                 ratevalue = 5;
1383                 break;
1384         case 120:
1385                 ratevalue = 6;
1386                 break;
1387         case 180:
1388                 ratevalue = 7;
1389                 break;
1390         case 240:
1391                 ratevalue = 8;
1392                 break;
1393         case 360:
1394                 ratevalue = 9;
1395                 break;
1396         case 480:
1397                 ratevalue = 10;
1398                 break;
1399         case 540:
1400                 ratevalue = 11;
1401                 break;
1402         default:
1403                 ratevalue = 11;
1404                 break;
1405         }
1406
1407 set_rate:
1408
1409         for (i = 0; i < NumRates; i++) {
1410                 if (ratevalue == mpdatarate[i]) {
1411                         datarates[i] = mpdatarate[i];
1412                         if (fixed == 0)
1413                                 break;
1414                 } else {
1415                         datarates[i] = 0xff;
1416                 }
1417         }
1418
1419         if (rtw_setdatarate_cmd(padapter, datarates) != _SUCCESS)
1420                 ret = -1;
1421
1422         return ret;
1423 }
1424
1425 static int rtw_wx_get_rate(struct net_device *dev,
1426                              struct iw_request_info *info,
1427                              union iwreq_data *wrqu, char *extra)
1428 {
1429         u16 max_rate = 0;
1430
1431         max_rate = rtw_get_cur_max_rate((struct adapter *)rtw_netdev_priv(dev));
1432
1433         if (max_rate == 0)
1434                 return -EPERM;
1435
1436         wrqu->bitrate.fixed = 0;        /* no auto select */
1437         wrqu->bitrate.value = max_rate * 100000;
1438
1439         return 0;
1440 }
1441
1442 static int rtw_wx_set_rts(struct net_device *dev,
1443                              struct iw_request_info *info,
1444                              union iwreq_data *wrqu, char *extra)
1445 {
1446         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1447
1448
1449
1450         if (wrqu->rts.disabled) {
1451                 padapter->registrypriv.rts_thresh = 2347;
1452         } else {
1453                 if (wrqu->rts.value < 0 ||
1454                     wrqu->rts.value > 2347)
1455                         return -EINVAL;
1456
1457                 padapter->registrypriv.rts_thresh = wrqu->rts.value;
1458         }
1459
1460         return 0;
1461 }
1462
1463 static int rtw_wx_get_rts(struct net_device *dev,
1464                              struct iw_request_info *info,
1465                              union iwreq_data *wrqu, char *extra)
1466 {
1467         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1468
1469
1470
1471         wrqu->rts.value = padapter->registrypriv.rts_thresh;
1472         wrqu->rts.fixed = 0;    /* no auto select */
1473         /* wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD); */
1474
1475
1476
1477         return 0;
1478 }
1479
1480 static int rtw_wx_set_frag(struct net_device *dev,
1481                              struct iw_request_info *info,
1482                              union iwreq_data *wrqu, char *extra)
1483 {
1484         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1485
1486
1487
1488         if (wrqu->frag.disabled) {
1489                 padapter->xmitpriv.frag_len = MAX_FRAG_THRESHOLD;
1490         } else {
1491                 if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
1492                     wrqu->frag.value > MAX_FRAG_THRESHOLD)
1493                         return -EINVAL;
1494
1495                 padapter->xmitpriv.frag_len = wrqu->frag.value & ~0x1;
1496         }
1497
1498         return 0;
1499 }
1500
1501 static int rtw_wx_get_frag(struct net_device *dev,
1502                              struct iw_request_info *info,
1503                              union iwreq_data *wrqu, char *extra)
1504 {
1505         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1506
1507
1508
1509         wrqu->frag.value = padapter->xmitpriv.frag_len;
1510         wrqu->frag.fixed = 0;   /* no auto select */
1511
1512
1513
1514         return 0;
1515 }
1516
1517 static int rtw_wx_get_retry(struct net_device *dev,
1518                              struct iw_request_info *info,
1519                              union iwreq_data *wrqu, char *extra)
1520 {
1521         wrqu->retry.value = 7;
1522         wrqu->retry.fixed = 0;  /* no auto select */
1523         wrqu->retry.disabled = 1;
1524
1525         return 0;
1526 }
1527
1528 static int rtw_wx_set_enc(struct net_device *dev,
1529                             struct iw_request_info *info,
1530                             union iwreq_data *wrqu, char *keybuf)
1531 {
1532         u32 key, ret = 0;
1533         u32 keyindex_provided;
1534         struct ndis_802_11_wep   wep;
1535         enum ndis_802_11_auth_mode authmode;
1536
1537         struct iw_point *erq = &wrqu->encoding;
1538         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1539         struct pwrctrl_priv *pwrpriv = &padapter->pwrctrlpriv;
1540
1541         memset(&wep, 0, sizeof(struct ndis_802_11_wep));
1542
1543         key = erq->flags & IW_ENCODE_INDEX;
1544
1545
1546
1547         if (erq->flags & IW_ENCODE_DISABLED) {
1548                 padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
1549                 padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
1550                 padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_;
1551                 padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1552                 authmode = Ndis802_11AuthModeOpen;
1553                 padapter->securitypriv.ndisauthtype = authmode;
1554
1555                 goto exit;
1556         }
1557
1558         if (key) {
1559                 if (key > WEP_KEYS)
1560                         return -EINVAL;
1561                 key--;
1562                 keyindex_provided = 1;
1563         } else {
1564                 keyindex_provided = 0;
1565                 key = padapter->securitypriv.dot11PrivacyKeyIndex;
1566         }
1567
1568         /* set authentication mode */
1569         if (erq->flags & IW_ENCODE_OPEN) {
1570                 padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;/* Ndis802_11EncryptionDisabled; */
1571                 padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
1572                 padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
1573                 padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_;
1574                 authmode = Ndis802_11AuthModeOpen;
1575                 padapter->securitypriv.ndisauthtype = authmode;
1576         } else if (erq->flags & IW_ENCODE_RESTRICTED) {
1577                 padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1578                 padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Shared;
1579                 padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_;
1580                 padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_;
1581                 authmode = Ndis802_11AuthModeShared;
1582                 padapter->securitypriv.ndisauthtype = authmode;
1583         } else {
1584                 padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;/* Ndis802_11EncryptionDisabled; */
1585                 padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1586                 padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
1587                 padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_;
1588                 authmode = Ndis802_11AuthModeOpen;
1589                 padapter->securitypriv.ndisauthtype = authmode;
1590         }
1591
1592         wep.KeyIndex = key;
1593         if (erq->length > 0) {
1594                 wep.KeyLength = erq->length <= 5 ? 5 : 13;
1595
1596                 wep.Length = wep.KeyLength + offsetof(struct ndis_802_11_wep, KeyMaterial);
1597         } else {
1598                 wep.KeyLength = 0;
1599
1600                 if (keyindex_provided == 1) {
1601                         /*  set key_id only, no given KeyMaterial(erq->length == 0). */
1602                         padapter->securitypriv.dot11PrivacyKeyIndex = key;
1603
1604                         switch (padapter->securitypriv.dot11DefKeylen[key]) {
1605                         case 5:
1606                                 padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_;
1607                                 break;
1608                         case 13:
1609                                 padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
1610                                 break;
1611                         default:
1612                                 padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
1613                                 break;
1614                         }
1615
1616                         goto exit;
1617                 }
1618         }
1619
1620         wep.KeyIndex |= 0x80000000;
1621
1622         memcpy(wep.KeyMaterial, keybuf, wep.KeyLength);
1623
1624         if (!rtw_set_802_11_add_wep(padapter, &wep)) {
1625                 if (rf_on == pwrpriv->rf_pwrstate)
1626                         ret = -EOPNOTSUPP;
1627                 goto exit;
1628         }
1629
1630 exit:
1631
1632
1633
1634         return ret;
1635 }
1636
1637 static int rtw_wx_get_enc(struct net_device *dev,
1638                             struct iw_request_info *info,
1639                             union iwreq_data *wrqu, char *keybuf)
1640 {
1641         uint key;
1642         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1643         struct iw_point *erq = &wrqu->encoding;
1644         struct  mlme_priv       *pmlmepriv = &padapter->mlmepriv;
1645
1646
1647
1648         if (check_fwstate(pmlmepriv, _FW_LINKED) != true) {
1649                 if (!check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
1650                         erq->length = 0;
1651                         erq->flags |= IW_ENCODE_DISABLED;
1652                         return 0;
1653                 }
1654         }
1655
1656         key = erq->flags & IW_ENCODE_INDEX;
1657
1658         if (key) {
1659                 if (key > WEP_KEYS)
1660                         return -EINVAL;
1661                 key--;
1662         } else {
1663                 key = padapter->securitypriv.dot11PrivacyKeyIndex;
1664         }
1665
1666         erq->flags = key + 1;
1667
1668         switch (padapter->securitypriv.ndisencryptstatus) {
1669         case Ndis802_11EncryptionNotSupported:
1670         case Ndis802_11EncryptionDisabled:
1671                 erq->length = 0;
1672                 erq->flags |= IW_ENCODE_DISABLED;
1673                 break;
1674         case Ndis802_11Encryption1Enabled:
1675                 erq->length = padapter->securitypriv.dot11DefKeylen[key];
1676                 if (erq->length) {
1677                         memcpy(keybuf, padapter->securitypriv.dot11DefKey[key].skey, padapter->securitypriv.dot11DefKeylen[key]);
1678
1679                         erq->flags |= IW_ENCODE_ENABLED;
1680
1681                         if (padapter->securitypriv.ndisauthtype == Ndis802_11AuthModeOpen)
1682                                 erq->flags |= IW_ENCODE_OPEN;
1683                         else if (padapter->securitypriv.ndisauthtype == Ndis802_11AuthModeShared)
1684                                 erq->flags |= IW_ENCODE_RESTRICTED;
1685                 } else {
1686                         erq->length = 0;
1687                         erq->flags |= IW_ENCODE_DISABLED;
1688                 }
1689                 break;
1690         case Ndis802_11Encryption2Enabled:
1691         case Ndis802_11Encryption3Enabled:
1692                 erq->length = 16;
1693                 erq->flags |= (IW_ENCODE_ENABLED | IW_ENCODE_OPEN | IW_ENCODE_NOKEY);
1694                 break;
1695         default:
1696                 erq->length = 0;
1697                 erq->flags |= IW_ENCODE_DISABLED;
1698                 break;
1699         }
1700
1701
1702         return 0;
1703 }
1704
1705 static int rtw_wx_get_power(struct net_device *dev,
1706                              struct iw_request_info *info,
1707                              union iwreq_data *wrqu, char *extra)
1708 {
1709         wrqu->power.value = 0;
1710         wrqu->power.fixed = 0;  /* no auto select */
1711         wrqu->power.disabled = 1;
1712
1713         return 0;
1714 }
1715
1716 static int rtw_wx_set_gen_ie(struct net_device *dev,
1717                              struct iw_request_info *info,
1718                              union iwreq_data *wrqu, char *extra)
1719 {
1720         int ret;
1721         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1722
1723         ret = rtw_set_wpa_ie(padapter, extra, wrqu->data.length);
1724         return ret;
1725 }
1726
1727 static int rtw_wx_set_auth(struct net_device *dev,
1728                              struct iw_request_info *info,
1729                              union iwreq_data *wrqu, char *extra)
1730 {
1731         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1732         struct iw_param *param = (struct iw_param *)&wrqu->param;
1733         int ret = 0;
1734
1735         switch (param->flags & IW_AUTH_INDEX) {
1736         case IW_AUTH_WPA_VERSION:
1737                 break;
1738         case IW_AUTH_CIPHER_PAIRWISE:
1739
1740                 break;
1741         case IW_AUTH_CIPHER_GROUP:
1742
1743                 break;
1744         case IW_AUTH_KEY_MGMT:
1745                 /*
1746                  *  ??? does not use these parameters
1747                  */
1748                 break;
1749         case IW_AUTH_TKIP_COUNTERMEASURES:
1750                 if (param->value) {
1751                         /*  wpa_supplicant is enabling the tkip countermeasure. */
1752                         padapter->securitypriv.btkip_countermeasure = true;
1753                 } else {
1754                         /*  wpa_supplicant is disabling the tkip countermeasure. */
1755                         padapter->securitypriv.btkip_countermeasure = false;
1756                 }
1757                 break;
1758         case IW_AUTH_DROP_UNENCRYPTED:
1759                 /* HACK:
1760                  *
1761                  * wpa_supplicant calls set_wpa_enabled when the driver
1762                  * is loaded and unloaded, regardless of if WPA is being
1763                  * used.  No other calls are made which can be used to
1764                  * determine if encryption will be used or not prior to
1765                  * association being expected.  If encryption is not being
1766                  * used, drop_unencrypted is set to false, else true -- we
1767                  * can use this to determine if the CAP_PRIVACY_ON bit should
1768                  * be set.
1769                  */
1770
1771                 if (padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption1Enabled)
1772                         break;/* it means init value, or using wep, ndisencryptstatus = Ndis802_11Encryption1Enabled, */
1773                                         /*  then it needn't reset it; */
1774
1775                 if (param->value) {
1776                         padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
1777                         padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
1778                         padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_;
1779                         padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1780                         padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeOpen;
1781                 }
1782
1783                 break;
1784         case IW_AUTH_80211_AUTH_ALG:
1785                 /*
1786                  *  It's the starting point of a link layer connection using wpa_supplicant
1787                 */
1788                 if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
1789                         LeaveAllPowerSaveMode(padapter);
1790                         rtw_disassoc_cmd(padapter, 500, false);
1791                         rtw_indicate_disconnect(padapter);
1792                         rtw_free_assoc_resources(padapter, 1);
1793                 }
1794                 ret = wpa_set_auth_algs(dev, (u32)param->value);
1795                 break;
1796         case IW_AUTH_WPA_ENABLED:
1797                 break;
1798         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1799                 break;
1800         case IW_AUTH_PRIVACY_INVOKED:
1801                 break;
1802         default:
1803                 return -EOPNOTSUPP;
1804         }
1805
1806         return ret;
1807 }
1808
1809 static int rtw_wx_set_enc_ext(struct net_device *dev,
1810                              struct iw_request_info *info,
1811                              union iwreq_data *wrqu, char *extra)
1812 {
1813         char *alg_name;
1814         u32 param_len;
1815         struct ieee_param *param = NULL;
1816         struct iw_point *pencoding = &wrqu->encoding;
1817         struct iw_encode_ext *pext = (struct iw_encode_ext *)extra;
1818         int ret = -1;
1819
1820         param_len = sizeof(struct ieee_param) + pext->key_len;
1821         param = kzalloc(param_len, GFP_KERNEL);
1822         if (!param)
1823                 return -ENOMEM;
1824
1825         param->cmd = IEEE_CMD_SET_ENCRYPTION;
1826         memset(param->sta_addr, 0xff, ETH_ALEN);
1827
1828         switch (pext->alg) {
1829         case IW_ENCODE_ALG_NONE:
1830                 /* todo: remove key */
1831                 /* remove = 1; */
1832                 alg_name = "none";
1833                 break;
1834         case IW_ENCODE_ALG_WEP:
1835                 alg_name = "WEP";
1836                 break;
1837         case IW_ENCODE_ALG_TKIP:
1838                 alg_name = "TKIP";
1839                 break;
1840         case IW_ENCODE_ALG_CCMP:
1841                 alg_name = "CCMP";
1842                 break;
1843         default:
1844                 goto out;
1845         }
1846
1847         strlcpy((char *)param->u.crypt.alg, alg_name, IEEE_CRYPT_ALG_NAME_LEN);
1848
1849         if (pext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1850                 param->u.crypt.set_tx = 1;
1851
1852         /* cliW: WEP does not have group key
1853          * just not checking GROUP key setting
1854          */
1855         if ((pext->alg != IW_ENCODE_ALG_WEP) &&
1856             (pext->ext_flags & IW_ENCODE_EXT_GROUP_KEY))
1857                 param->u.crypt.set_tx = 0;
1858
1859         param->u.crypt.idx = (pencoding->flags & 0x00FF) - 1;
1860
1861         if (pext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID)
1862                 memcpy(param->u.crypt.seq, pext->rx_seq, 8);
1863
1864         if (pext->key_len) {
1865                 param->u.crypt.key_len = pext->key_len;
1866                 memcpy(param->u.crypt.key, pext + 1, pext->key_len);
1867         }
1868
1869         ret =  wpa_set_encryption(dev, param, param_len);
1870
1871 out:
1872         kfree(param);
1873         return ret;
1874 }
1875
1876 static int rtw_wx_get_nick(struct net_device *dev,
1877                            struct iw_request_info *info,
1878                            union iwreq_data *wrqu, char *extra)
1879 {
1880         if (extra) {
1881                 wrqu->data.length = 14;
1882                 wrqu->data.flags = 1;
1883                 memcpy(extra, "<WIFI@REALTEK>", 14);
1884         }
1885
1886         /* dump debug info here */
1887         return 0;
1888 }
1889
1890 static int rtw_wx_read_rf(struct net_device *dev,
1891                             struct iw_request_info *info,
1892                             union iwreq_data *wrqu, char *extra)
1893 {
1894         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1895         u32 path, addr, data32;
1896
1897         path = *(u32 *)extra;
1898         if (path != RF_PATH_A)
1899                 return -EINVAL;
1900
1901         addr = *((u32 *)extra + 1);
1902         data32 = rtl8188e_PHY_QueryRFReg(padapter, addr, 0xFFFFF);
1903         /*
1904          * IMPORTANT!!
1905          * Only when wireless private ioctl is at odd order,
1906          * "extra" would be copied to user space.
1907          */
1908         sprintf(extra, "0x%05x", data32);
1909
1910         return 0;
1911 }
1912
1913 static int rtw_wx_write_rf(struct net_device *dev,
1914                             struct iw_request_info *info,
1915                             union iwreq_data *wrqu, char *extra)
1916 {
1917         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1918         u32 path, addr, data32;
1919
1920         path = *(u32 *)extra;
1921         if (path != RF_PATH_A)
1922                 return -EINVAL;
1923
1924         addr = *((u32 *)extra + 1);
1925         data32 = *((u32 *)extra + 2);
1926         rtl8188e_PHY_SetRFReg(padapter, addr, 0xFFFFF, data32);
1927
1928         return 0;
1929 }
1930
1931 static int rtw_wx_set_channel_plan(struct net_device *dev,
1932                                struct iw_request_info *info,
1933                                union iwreq_data *wrqu, char *extra)
1934 {
1935         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1936         u8 channel_plan_req = (u8)(*((int *)wrqu));
1937
1938         if (rtw_set_chplan_cmd(padapter, channel_plan_req) != _SUCCESS)
1939                 return -EPERM;
1940
1941         return 0;
1942 }
1943
1944 static int rtw_get_ap_info(struct net_device *dev,
1945                                struct iw_request_info *info,
1946                                union iwreq_data *wrqu, char *extra)
1947 {
1948         int ret = 0;
1949         u32 cnt = 0, wpa_ielen;
1950         struct list_head *plist, *phead;
1951         unsigned char *pbuf;
1952         u8 bssid[ETH_ALEN];
1953         char data[32];
1954         struct wlan_network *pnetwork = NULL;
1955         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
1956         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1957         struct __queue *queue = &pmlmepriv->scanned_queue;
1958         struct iw_point *pdata = &wrqu->data;
1959
1960         if (padapter->bDriverStopped || !pdata) {
1961                 ret = -EINVAL;
1962                 goto exit;
1963         }
1964
1965         while ((check_fwstate(pmlmepriv, (_FW_UNDER_SURVEY | _FW_UNDER_LINKING)))) {
1966                 msleep(30);
1967                 cnt++;
1968                 if (cnt > 100)
1969                         break;
1970         }
1971         pdata->flags = 0;
1972         if (pdata->length >= 32) {
1973                 if (copy_from_user(data, pdata->pointer, 32)) {
1974                         ret = -EINVAL;
1975                         goto exit;
1976                 }
1977         } else {
1978                 ret = -EINVAL;
1979                 goto exit;
1980         }
1981
1982         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1983
1984         phead = get_list_head(queue);
1985         plist = phead->next;
1986
1987         while (phead != plist) {
1988                 pnetwork = container_of(plist, struct wlan_network, list);
1989
1990                 if (!mac_pton(data, bssid)) {
1991                         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1992                         return -EINVAL;
1993                 }
1994
1995                 if (!memcmp(bssid, pnetwork->network.MacAddress, ETH_ALEN)) {
1996                         /* BSSID match, then check if supporting wpa/wpa2 */
1997                         pbuf = rtw_get_wpa_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength - 12);
1998                         if (pbuf && (wpa_ielen > 0)) {
1999                                 pdata->flags = 1;
2000                                 break;
2001                         }
2002
2003                         pbuf = rtw_get_wpa2_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength - 12);
2004                         if (pbuf && (wpa_ielen > 0)) {
2005                                 pdata->flags = 2;
2006                                 break;
2007                         }
2008                 }
2009
2010                 plist = plist->next;
2011         }
2012
2013         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2014
2015         if (pdata->length >= 34) {
2016                 if (copy_to_user(pdata->pointer + 32, (u8 *)&pdata->flags, 1)) {
2017                         ret = -EINVAL;
2018                         goto exit;
2019                 }
2020         }
2021
2022 exit:
2023
2024         return ret;
2025 }
2026
2027 static int rtw_set_pid(struct net_device *dev,
2028                                struct iw_request_info *info,
2029                                union iwreq_data *wrqu, char *extra)
2030 {
2031         int ret = 0;
2032         struct adapter *padapter = rtw_netdev_priv(dev);
2033         int *pdata = (int *)wrqu;
2034         int selector;
2035
2036         if (padapter->bDriverStopped || !pdata) {
2037                 ret = -EINVAL;
2038                 goto exit;
2039         }
2040
2041         selector = *pdata;
2042         if (selector < 3 && selector >= 0) {
2043                 padapter->pid[selector] = *(pdata + 1);
2044                 ui_pid[selector] = *(pdata + 1);
2045         }
2046 exit:
2047         return ret;
2048 }
2049
2050 static int rtw_wps_start(struct net_device *dev,
2051                          struct iw_request_info *info,
2052                          union iwreq_data *wrqu, char *extra)
2053 {
2054         int ret = 0;
2055         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2056         struct iw_point *pdata = &wrqu->data;
2057         u32   u32wps_start = 0;
2058
2059         if (!pdata)
2060                 return -EINVAL;
2061         ret = copy_from_user((void *)&u32wps_start, pdata->pointer, 4);
2062         if (ret) {
2063                 ret = -EINVAL;
2064                 goto exit;
2065         }
2066
2067         if (padapter->bDriverStopped) {
2068                 ret = -EINVAL;
2069                 goto exit;
2070         }
2071
2072         if (u32wps_start == 0)
2073                 u32wps_start = *extra;
2074
2075         if (u32wps_start == 1) /*  WPS Start */
2076                 rtw_led_control(padapter, LED_CTL_START_WPS);
2077         else if (u32wps_start == 2) /*  WPS Stop because of wps success */
2078                 rtw_led_control(padapter, LED_CTL_STOP_WPS);
2079         else if (u32wps_start == 3) /*  WPS Stop because of wps fail */
2080                 rtw_led_control(padapter, LED_CTL_STOP_WPS_FAIL);
2081
2082 exit:
2083         return ret;
2084 }
2085
2086 static int rtw_wext_p2p_enable(struct net_device *dev,
2087                                struct iw_request_info *info,
2088                                union iwreq_data *wrqu, char *extra)
2089 {
2090         int ret = 0;
2091         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2092         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2093         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
2094         enum P2P_ROLE init_role = P2P_ROLE_DISABLE;
2095
2096         if (*extra == '0')
2097                 init_role = P2P_ROLE_DISABLE;
2098         else if (*extra == '1')
2099                 init_role = P2P_ROLE_DEVICE;
2100         else if (*extra == '2')
2101                 init_role = P2P_ROLE_CLIENT;
2102         else if (*extra == '3')
2103                 init_role = P2P_ROLE_GO;
2104
2105         if (_FAIL == rtw_p2p_enable(padapter, init_role)) {
2106                 ret = -EFAULT;
2107                 goto exit;
2108         }
2109
2110         /* set channel/bandwidth */
2111         if (init_role != P2P_ROLE_DISABLE) {
2112                 u8 channel, ch_offset;
2113                 u16 bwmode;
2114
2115                 if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_LISTEN)) {
2116                         /*      Stay at the listen state and wait for discovery. */
2117                         channel = pwdinfo->listen_channel;
2118                         pwdinfo->operating_channel = pwdinfo->listen_channel;
2119                         ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
2120                         bwmode = HT_CHANNEL_WIDTH_20;
2121                 } else {
2122                         pwdinfo->operating_channel = pmlmeext->cur_channel;
2123
2124                         channel = pwdinfo->operating_channel;
2125                         ch_offset = pmlmeext->cur_ch_offset;
2126                         bwmode = pmlmeext->cur_bwmode;
2127                 }
2128
2129                 set_channel_bwmode(padapter, channel, ch_offset, bwmode);
2130         }
2131
2132 exit:
2133         return ret;
2134 }
2135
2136 static void rtw_p2p_set_go_nego_ssid(struct net_device *dev,
2137                                      struct iw_request_info *info,
2138                                      union iwreq_data *wrqu, char *extra)
2139 {
2140         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2141         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2142
2143         memcpy(pwdinfo->nego_ssid, extra, strlen(extra));
2144         pwdinfo->nego_ssidlen = strlen(extra);
2145 }
2146
2147 static int rtw_p2p_set_intent(struct net_device *dev,
2148                               struct iw_request_info *info,
2149                               union iwreq_data *wrqu, char *extra)
2150 {
2151         int ret = 0;
2152         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2153         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2154         u8 intent = pwdinfo->intent;
2155
2156         switch (wrqu->data.length) {
2157         case 1:
2158                 intent = extra[0] - '0';
2159                 break;
2160         case 2:
2161                 intent = str_2char2num(extra[0], extra[1]);
2162                 break;
2163         }
2164         if (intent <= 15)
2165                 pwdinfo->intent = intent;
2166         else
2167                 ret = -1;
2168         return ret;
2169 }
2170
2171 static int rtw_p2p_set_listen_ch(struct net_device *dev,
2172                                struct iw_request_info *info,
2173                                union iwreq_data *wrqu, char *extra)
2174 {
2175         int ret = 0;
2176         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2177         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2178         u8 listen_ch = pwdinfo->listen_channel; /*      Listen channel number */
2179
2180         switch (wrqu->data.length) {
2181         case 1:
2182                 listen_ch = extra[0] - '0';
2183                 break;
2184         case 2:
2185                 listen_ch = str_2char2num(extra[0], extra[1]);
2186                 break;
2187         }
2188
2189         if ((listen_ch == 1) || (listen_ch == 6) || (listen_ch == 11)) {
2190                 pwdinfo->listen_channel = listen_ch;
2191                 set_channel_bwmode(padapter, pwdinfo->listen_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
2192         } else {
2193                 ret = -1;
2194         }
2195
2196         return ret;
2197 }
2198
2199 static int rtw_p2p_set_op_ch(struct net_device *dev,
2200                                struct iw_request_info *info,
2201                                union iwreq_data *wrqu, char *extra)
2202 {
2203 /*      Commented by Albert 20110524 */
2204 /*      This function is used to set the operating channel if the driver will become the group owner */
2205
2206         int ret = 0;
2207         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2208         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2209         u8 op_ch = pwdinfo->operating_channel;  /*      Operating channel number */
2210
2211         switch (wrqu->data.length) {
2212         case 1:
2213                 op_ch = extra[0] - '0';
2214                 break;
2215         case 2:
2216                 op_ch = str_2char2num(extra[0], extra[1]);
2217                 break;
2218         }
2219
2220         if (op_ch > 0)
2221                 pwdinfo->operating_channel = op_ch;
2222         else
2223                 ret = -1;
2224
2225         return ret;
2226 }
2227
2228 static int rtw_p2p_profilefound(struct net_device *dev,
2229                                struct iw_request_info *info,
2230                                union iwreq_data *wrqu, char *extra)
2231 {
2232         int ret = 0;
2233         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2234         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2235
2236         /*      Comment by Albert 2010/10/13 */
2237         /*      Input data format: */
2238         /*      Ex:  0 */
2239         /*      Ex:  1XX:XX:XX:XX:XX:XXYYSSID */
2240         /*      0 => Reflush the profile record list. */
2241         /*      1 => Add the profile list */
2242         /*      XX:XX:XX:XX:XX:XX => peer's MAC Address (ex: 00:E0:4C:00:00:01) */
2243         /*      YY => SSID Length */
2244         /*      SSID => SSID for persistence group */
2245
2246         /*      The upper application should pass the SSID to driver by using this rtw_p2p_profilefound function. */
2247         if (!rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) {
2248                 if (extra[0] == '0') {
2249                         /*      Remove all the profile information of wifidirect_info structure. */
2250                         memset(&pwdinfo->profileinfo[0], 0x00, sizeof(struct profile_info) * P2P_MAX_PERSISTENT_GROUP_NUM);
2251                         pwdinfo->profileindex = 0;
2252                 } else {
2253                         if (pwdinfo->profileindex >= P2P_MAX_PERSISTENT_GROUP_NUM) {
2254                                 ret = -1;
2255                         } else {
2256                                 int jj, kk;
2257
2258                                 /*      Add this profile information into pwdinfo->profileinfo */
2259                                 /*      Ex:  1XX:XX:XX:XX:XX:XXYYSSID */
2260                                 for (jj = 0, kk = 1; jj < ETH_ALEN; jj++, kk += 3)
2261                                         pwdinfo->profileinfo[pwdinfo->profileindex].peermac[jj] = key_2char2num(extra[kk], extra[kk + 1]);
2262
2263                                 pwdinfo->profileinfo[pwdinfo->profileindex].ssidlen = (extra[18] - '0') * 10 + (extra[19] - '0');
2264                                 memcpy(pwdinfo->profileinfo[pwdinfo->profileindex].ssid, &extra[20], pwdinfo->profileinfo[pwdinfo->profileindex].ssidlen);
2265                                 pwdinfo->profileindex++;
2266                         }
2267                 }
2268         }
2269
2270         return ret;
2271 }
2272
2273 static void rtw_p2p_setDN(struct net_device *dev,
2274                           struct iw_request_info *info,
2275                           union iwreq_data *wrqu, char *extra)
2276 {
2277         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2278         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2279
2280         memset(pwdinfo->device_name, 0x00, WPS_MAX_DEVICE_NAME_LEN);
2281         memcpy(pwdinfo->device_name, extra, wrqu->data.length - 1);
2282         pwdinfo->device_name_len = wrqu->data.length - 1;
2283 }
2284
2285 static int rtw_p2p_get_wps_configmethod(struct net_device *dev,
2286                                struct iw_request_info *info,
2287                                union iwreq_data *wrqu, char *extra)
2288 {
2289         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2290         u8 peerMAC[ETH_ALEN] = {0x00};
2291         int jj, kk;
2292         u8 peerMACStr[17] = {0x00};
2293         struct mlme_priv                *pmlmepriv = &padapter->mlmepriv;
2294         struct list_head *plist, *phead;
2295         struct __queue *queue   = &pmlmepriv->scanned_queue;
2296         struct  wlan_network    *pnetwork = NULL;
2297         u8 blnMatch = 0;
2298         u16     attr_content = 0;
2299         uint attr_contentlen = 0;
2300         /* 6 is the string "wpsCM =", 17 is the MAC addr, we have to clear it at wrqu->data.pointer */
2301         u8 attr_content_str[6 + 17] = {0x00};
2302
2303         /*      Commented by Albert 20110727 */
2304         /*      The input data is the MAC address which the application wants to know its WPS config method. */
2305         /*      After knowing its WPS config method, the application can decide the config method for provisioning discovery. */
2306         /*      Format: iwpriv wlanx p2p_get_wpsCM 00:E0:4C:00:00:05 */
2307
2308         if (copy_from_user(peerMACStr, wrqu->data.pointer + 6, 17))
2309                 return -EFAULT;
2310
2311         for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
2312                 peerMAC[jj] = key_2char2num(peerMACStr[kk], peerMACStr[kk + 1]);
2313
2314         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
2315
2316         phead = get_list_head(queue);
2317         plist = phead->next;
2318
2319         while (phead != plist) {
2320                 pnetwork = container_of(plist, struct wlan_network, list);
2321                 if (!memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) {
2322                         u8 *wpsie;
2323                         uint wpsie_len = 0;
2324                         __be16 be_tmp;
2325
2326                         /*  The mac address is matched. */
2327                         wpsie = rtw_get_wps_ie(&pnetwork->network.IEs[12], pnetwork->network.IELength - 12, NULL, &wpsie_len);
2328                         if (wpsie) {
2329                                 rtw_get_wps_attr_content(wpsie, wpsie_len, WPS_ATTR_CONF_METHOD, (u8 *)&be_tmp, &attr_contentlen);
2330                                 if (attr_contentlen) {
2331                                         attr_content = be16_to_cpu(be_tmp);
2332                                         sprintf(attr_content_str, "\n\nM =%.4d", attr_content);
2333                                         blnMatch = 1;
2334                                 }
2335                         }
2336                         break;
2337                 }
2338                 plist = plist->next;
2339         }
2340
2341         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2342
2343         if (!blnMatch)
2344                 sprintf(attr_content_str, "\n\nM = 0000");
2345
2346         if (copy_to_user(wrqu->data.pointer, attr_content_str, 6 + 17))
2347                 return -EFAULT;
2348         return 0;
2349 }
2350
2351 static int rtw_p2p_get_go_device_address(struct net_device *dev,
2352                                struct iw_request_info *info,
2353                                union iwreq_data *wrqu, char *extra)
2354 {
2355         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2356         u8 peerMAC[ETH_ALEN] = {0x00};
2357         int jj, kk;
2358         u8 peerMACStr[17] = {0x00};
2359         struct mlme_priv                *pmlmepriv = &padapter->mlmepriv;
2360         struct list_head *plist, *phead;
2361         struct __queue *queue   = &pmlmepriv->scanned_queue;
2362         struct  wlan_network    *pnetwork = NULL;
2363         u8 blnMatch = 0;
2364         u8 *p2pie;
2365         uint p2pielen = 0, attr_contentlen = 0;
2366         u8 attr_content[100] = {0x00};
2367
2368         u8 go_devadd_str[100 + 10] = {0x00};
2369         /*  +10 is for the str "go_devadd =", we have to clear it at wrqu->data.pointer */
2370
2371         /*      Commented by Albert 20121209 */
2372         /*      The input data is the GO's interface address which the application wants to know its device address. */
2373         /*      Format: iwpriv wlanx p2p_get2 go_devadd = 00:E0:4C:00:00:05 */
2374
2375         if (copy_from_user(peerMACStr, wrqu->data.pointer + 10, 17))
2376                 return -EFAULT;
2377
2378         for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
2379                 peerMAC[jj] = key_2char2num(peerMACStr[kk], peerMACStr[kk + 1]);
2380
2381         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
2382
2383         phead = get_list_head(queue);
2384         plist = phead->next;
2385
2386         while (phead != plist) {
2387                 pnetwork = container_of(plist, struct wlan_network, list);
2388                 if (!memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) {
2389                         /*      Commented by Albert 2011/05/18 */
2390                         /*      Match the device address located in the P2P IE */
2391                         /*      This is for the case that the P2P device address is not the same as the P2P interface address. */
2392
2393                         p2pie = rtw_get_p2p_ie(&pnetwork->network.IEs[12], pnetwork->network.IELength - 12, NULL, &p2pielen);
2394                         if (p2pie) {
2395                                 while (p2pie) {
2396                                         /*      The P2P Device ID attribute is included in the Beacon frame. */
2397                                         /*      The P2P Device Info attribute is included in the probe response frame. */
2398
2399                                         memset(attr_content, 0x00, 100);
2400                                         if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_ID, attr_content, &attr_contentlen)) {
2401                                                 /*      Handle the P2P Device ID attribute of Beacon first */
2402                                                 blnMatch = 1;
2403                                                 break;
2404                                         } else if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_INFO, attr_content, &attr_contentlen)) {
2405                                                 /*      Handle the P2P Device Info attribute of probe response */
2406                                                 blnMatch = 1;
2407                                                 break;
2408                                         }
2409
2410                                         /* Get the next P2P IE */
2411                                         p2pie = rtw_get_p2p_ie(p2pie + p2pielen, pnetwork->network.IELength - 12 - (p2pie - &pnetwork->network.IEs[12] + p2pielen), NULL, &p2pielen);
2412                                 }
2413                         }
2414              }
2415
2416                 plist = plist->next;
2417         }
2418
2419         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2420
2421         if (!blnMatch)
2422                 sprintf(go_devadd_str, "\n\ndev_add = NULL");
2423         else
2424                 sprintf(go_devadd_str, "\ndev_add =%.2X:%.2X:%.2X:%.2X:%.2X:%.2X",
2425                         attr_content[0], attr_content[1], attr_content[2], attr_content[3], attr_content[4], attr_content[5]);
2426
2427         if (copy_to_user(wrqu->data.pointer, go_devadd_str, 10 + 17))
2428                 return -EFAULT;
2429         return 0;
2430 }
2431
2432 static int rtw_p2p_get_device_type(struct net_device *dev,
2433                                struct iw_request_info *info,
2434                                union iwreq_data *wrqu, char *extra)
2435 {
2436         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2437         u8 peerMAC[ETH_ALEN] = {0x00};
2438         int jj, kk;
2439         u8 peerMACStr[17] = {0x00};
2440         struct mlme_priv                *pmlmepriv = &padapter->mlmepriv;
2441         struct list_head *plist, *phead;
2442         struct __queue *queue   = &pmlmepriv->scanned_queue;
2443         struct  wlan_network    *pnetwork = NULL;
2444         u8 blnMatch = 0;
2445         u8 dev_type[8] = {0x00};
2446         uint dev_type_len = 0;
2447         u8 dev_type_str[17 + 9] = {0x00};       /*  +9 is for the str "dev_type =", we have to clear it at wrqu->data.pointer */
2448
2449         /*      Commented by Albert 20121209 */
2450         /*      The input data is the MAC address which the application wants to know its device type. */
2451         /*      Such user interface could know the device type. */
2452         /*      Format: iwpriv wlanx p2p_get2 dev_type = 00:E0:4C:00:00:05 */
2453
2454         if (copy_from_user(peerMACStr, wrqu->data.pointer + 9, 17))
2455                 return -EFAULT;
2456
2457         for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
2458                 peerMAC[jj] = key_2char2num(peerMACStr[kk], peerMACStr[kk + 1]);
2459
2460         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
2461
2462         phead = get_list_head(queue);
2463         plist = phead->next;
2464
2465         while (phead != plist) {
2466                 pnetwork = container_of(plist, struct wlan_network, list);
2467                 if (!memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) {
2468                         u8 *wpsie;
2469                         uint wpsie_len = 0;
2470
2471                 /*      The mac address is matched. */
2472
2473                         wpsie = rtw_get_wps_ie(&pnetwork->network.IEs[12],
2474                                                pnetwork->network.IELength - 12,
2475                                                NULL, &wpsie_len);
2476                         if (wpsie) {
2477                                 rtw_get_wps_attr_content(wpsie, wpsie_len, WPS_ATTR_PRIMARY_DEV_TYPE, dev_type, &dev_type_len);
2478                                 if (dev_type_len) {
2479                                         u16     type = 0;
2480                                         __be16 be_tmp;
2481
2482                                         memcpy(&be_tmp, dev_type, 2);
2483                                         type = be16_to_cpu(be_tmp);
2484                                         sprintf(dev_type_str, "\n\nN =%.2d", type);
2485                                         blnMatch = 1;
2486                                 }
2487                         }
2488                         break;
2489              }
2490
2491                 plist = plist->next;
2492         }
2493
2494         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2495
2496         if (!blnMatch)
2497                 sprintf(dev_type_str, "\n\nN = 00");
2498
2499         if (copy_to_user(wrqu->data.pointer, dev_type_str, 9 + 17)) {
2500                 return -EFAULT;
2501         }
2502
2503         return 0;
2504 }
2505
2506 static int rtw_p2p_get_device_name(struct net_device *dev,
2507                                struct iw_request_info *info,
2508                                union iwreq_data *wrqu, char *extra)
2509 {
2510         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2511         u8 peerMAC[ETH_ALEN] = {0x00};
2512         int jj, kk;
2513         u8 peerMACStr[17] = {0x00};
2514         struct mlme_priv                *pmlmepriv = &padapter->mlmepriv;
2515         struct list_head *plist, *phead;
2516         struct __queue *queue   = &pmlmepriv->scanned_queue;
2517         struct  wlan_network    *pnetwork = NULL;
2518         u8 blnMatch = 0;
2519         u8 dev_name[WPS_MAX_DEVICE_NAME_LEN] = {0x00};
2520         uint dev_len = 0;
2521         u8 dev_name_str[WPS_MAX_DEVICE_NAME_LEN + 5] = {0x00};  /*  +5 is for the str "devN =", we have to clear it at wrqu->data.pointer */
2522
2523         /*      Commented by Albert 20121225 */
2524         /*      The input data is the MAC address which the application wants to know its device name. */
2525         /*      Such user interface could show peer device's device name instead of ssid. */
2526         /*      Format: iwpriv wlanx p2p_get2 devN = 00:E0:4C:00:00:05 */
2527
2528         if (copy_from_user(peerMACStr, wrqu->data.pointer + 5, 17))
2529                 return -EFAULT;
2530
2531         for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
2532                 peerMAC[jj] = key_2char2num(peerMACStr[kk], peerMACStr[kk + 1]);
2533
2534         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
2535
2536         phead = get_list_head(queue);
2537         plist = phead->next;
2538
2539         while (phead != plist) {
2540                 pnetwork = container_of(plist, struct wlan_network, list);
2541                 if (!memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) {
2542                         u8 *wpsie;
2543                         uint wpsie_len = 0;
2544
2545                         /*      The mac address is matched. */
2546                         wpsie = rtw_get_wps_ie(&pnetwork->network.IEs[12], pnetwork->network.IELength - 12, NULL, &wpsie_len);
2547                         if (wpsie) {
2548                                 rtw_get_wps_attr_content(wpsie, wpsie_len, WPS_ATTR_DEVICE_NAME, dev_name, &dev_len);
2549                                 if (dev_len) {
2550                                         sprintf(dev_name_str, "\n\nN =%s", dev_name);
2551                                         blnMatch = 1;
2552                                 }
2553                         }
2554                         break;
2555                 }
2556
2557                 plist = plist->next;
2558         }
2559
2560         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2561
2562         if (!blnMatch)
2563                 sprintf(dev_name_str, "\n\nN = 0000");
2564
2565         if (copy_to_user(wrqu->data.pointer, dev_name_str, 5 + ((dev_len > 17) ? dev_len : 17)))
2566                 return -EFAULT;
2567         return 0;
2568 }
2569
2570 static int rtw_p2p_get_invitation_procedure(struct net_device *dev,
2571                                struct iw_request_info *info,
2572                                union iwreq_data *wrqu, char *extra)
2573 {
2574         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2575         u8 peerMAC[ETH_ALEN] = {0x00};
2576         int jj, kk;
2577         u8 peerMACStr[17] = {0x00};
2578         struct mlme_priv                *pmlmepriv = &padapter->mlmepriv;
2579         struct list_head *plist, *phead;
2580         struct __queue *queue   = &pmlmepriv->scanned_queue;
2581         struct  wlan_network    *pnetwork = NULL;
2582         u8 blnMatch = 0;
2583         u8 *p2pie;
2584         uint p2pielen = 0, attr_contentlen = 0;
2585         u8 attr_content[2] = {0x00};
2586
2587         u8 inv_proc_str[17 + 8] = {0x00};
2588         /*  +8 is for the str "InvProc =", we have to clear it at wrqu->data.pointer */
2589
2590         /*      Commented by Ouden 20121226 */
2591         /*      The application wants to know P2P initiation procedure is supported or not. */
2592         /*      Format: iwpriv wlanx p2p_get2 InvProc = 00:E0:4C:00:00:05 */
2593
2594         if (copy_from_user(peerMACStr, wrqu->data.pointer + 8, 17))
2595                 return -EFAULT;
2596
2597         for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
2598                 peerMAC[jj] = key_2char2num(peerMACStr[kk], peerMACStr[kk + 1]);
2599
2600         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
2601
2602         phead = get_list_head(queue);
2603         plist = phead->next;
2604
2605         while (phead != plist) {
2606                 pnetwork = container_of(plist, struct wlan_network, list);
2607                 if (!memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) {
2608                         /*      Commented by Albert 20121226 */
2609                         /*      Match the device address located in the P2P IE */
2610                         /*      This is for the case that the P2P device address is not the same as the P2P interface address. */
2611
2612                         p2pie = rtw_get_p2p_ie(&pnetwork->network.IEs[12], pnetwork->network.IELength - 12, NULL, &p2pielen);
2613                         if (p2pie) {
2614                                 while (p2pie) {
2615                                         if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_CAPABILITY, attr_content, &attr_contentlen)) {
2616                                                 /*      Handle the P2P capability attribute */
2617                                                 blnMatch = 1;
2618                                                 break;
2619                                         }
2620
2621                                         /* Get the next P2P IE */
2622                                         p2pie = rtw_get_p2p_ie(p2pie + p2pielen, pnetwork->network.IELength - 12 - (p2pie - &pnetwork->network.IEs[12] + p2pielen), NULL, &p2pielen);
2623                                 }
2624                         }
2625                 }
2626                 plist = plist->next;
2627         }
2628
2629         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2630
2631         if (!blnMatch) {
2632                 sprintf(inv_proc_str, "\nIP =-1");
2633         } else {
2634                 if (attr_content[0] & 0x20)
2635                         sprintf(inv_proc_str, "\nIP = 1");
2636                 else
2637                         sprintf(inv_proc_str, "\nIP = 0");
2638         }
2639         if (copy_to_user(wrqu->data.pointer, inv_proc_str, 8 + 17))
2640                 return -EFAULT;
2641         return 0;
2642 }
2643
2644 static int rtw_p2p_connect(struct net_device *dev,
2645                                struct iw_request_info *info,
2646                                union iwreq_data *wrqu, char *extra)
2647 {
2648         int ret = 0;
2649         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2650         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2651         u8 peerMAC[ETH_ALEN] = {0x00};
2652         int jj, kk;
2653         struct mlme_priv                *pmlmepriv = &padapter->mlmepriv;
2654         struct list_head *plist, *phead;
2655         struct __queue *queue   = &pmlmepriv->scanned_queue;
2656         struct  wlan_network    *pnetwork = NULL;
2657         u32 peer_channel = 0;
2658
2659         /*      Commented by Albert 20110304 */
2660         /*      The input data contains two informations. */
2661         /*      1. First information is the MAC address which wants to formate with */
2662         /*      2. Second information is the WPS PINCode or "pbc" string for push button method */
2663         /*      Format: 00:E0:4C:00:00:05 */
2664         /*      Format: 00:E0:4C:00:00:05 */
2665
2666         if (pwdinfo->p2p_state == P2P_STATE_NONE)
2667                 return ret;
2668
2669         if (pwdinfo->ui_got_wps_info == P2P_NO_WPSINFO)
2670                 return -1;
2671
2672         for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
2673                 peerMAC[jj] = key_2char2num(extra[kk], extra[kk + 1]);
2674
2675         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
2676
2677         phead = get_list_head(queue);
2678         plist = phead->next;
2679
2680         while (phead != plist) {
2681                 pnetwork = container_of(plist, struct wlan_network, list);
2682                 if (!memcmp(pnetwork->network.MacAddress, peerMAC, ETH_ALEN)) {
2683                         peer_channel = pnetwork->network.Configuration.DSConfig;
2684                         break;
2685                 }
2686
2687                 plist = plist->next;
2688         }
2689
2690         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2691
2692         if (peer_channel) {
2693                 memset(&pwdinfo->nego_req_info, 0x00, sizeof(struct tx_nego_req_info));
2694                 memset(&pwdinfo->groupid_info, 0x00, sizeof(struct group_id_info));
2695
2696                 pwdinfo->nego_req_info.peer_channel_num[0] = peer_channel;
2697                 memcpy(pwdinfo->nego_req_info.peerDevAddr, pnetwork->network.MacAddress, ETH_ALEN);
2698                 pwdinfo->nego_req_info.benable = true;
2699
2700                 _cancel_timer_ex(&pwdinfo->restore_p2p_state_timer);
2701                 if (rtw_p2p_state(pwdinfo) != P2P_STATE_GONEGO_OK) {
2702                         /*      Restore to the listen state if the current p2p state is not nego OK */
2703                         rtw_p2p_set_state(pwdinfo, P2P_STATE_LISTEN);
2704                 }
2705
2706                 rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo));
2707                 rtw_p2p_set_state(pwdinfo, P2P_STATE_GONEGO_ING);
2708
2709                 _set_timer(&pwdinfo->pre_tx_scan_timer, P2P_TX_PRESCAN_TIMEOUT);
2710                 _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_GO_NEGO_TIMEOUT);
2711         } else {
2712                 ret = -1;
2713         }
2714         return ret;
2715 }
2716
2717 static void rtw_p2p_invite_req(struct net_device *dev,
2718                                struct iw_request_info *info,
2719                                union iwreq_data *wrqu, char *extra)
2720 {
2721         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2722         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2723         int jj, kk;
2724         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
2725         struct list_head *plist, *phead;
2726         struct __queue *queue   = &pmlmepriv->scanned_queue;
2727         struct  wlan_network    *pnetwork = NULL;
2728         uint peer_channel = 0;
2729         u8 attr_content[50] = {0x00};
2730         u8 *p2pie;
2731         uint p2pielen = 0, attr_contentlen = 0;
2732         struct tx_invite_req_info *pinvite_req_info = &pwdinfo->invitereq_info;
2733
2734         /*      The input data contains two informations. */
2735         /*      1. First information is the P2P device address which you want to send to. */
2736         /*      2. Second information is the group id which combines with GO's mac address, space and GO's ssid. */
2737         /*      Command line sample: iwpriv wlan0 p2p_set invite ="00:11:22:33:44:55 00:E0:4C:00:00:05 DIRECT-xy" */
2738         /*      Format: 00:11:22:33:44:55 00:E0:4C:00:00:05 DIRECT-xy */
2739
2740         if (wrqu->data.length <=  37)
2741                 return;
2742
2743         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) {
2744                 return;
2745         } else {
2746                 /*      Reset the content of struct tx_invite_req_info */
2747                 pinvite_req_info->benable = false;
2748                 memset(pinvite_req_info->go_bssid, 0x00, ETH_ALEN);
2749                 memset(pinvite_req_info->go_ssid, 0x00, WLAN_SSID_MAXLEN);
2750                 pinvite_req_info->ssidlen = 0x00;
2751                 pinvite_req_info->operating_ch = pwdinfo->operating_channel;
2752                 memset(pinvite_req_info->peer_macaddr, 0x00, ETH_ALEN);
2753                 pinvite_req_info->token = 3;
2754         }
2755
2756         for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
2757                 pinvite_req_info->peer_macaddr[jj] = key_2char2num(extra[kk], extra[kk + 1]);
2758
2759         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
2760
2761         phead = get_list_head(queue);
2762         plist = phead->next;
2763
2764         while (phead != plist) {
2765                 pnetwork = container_of(plist, struct wlan_network, list);
2766
2767                 /*      Commented by Albert 2011/05/18 */
2768                 /*      Match the device address located in the P2P IE */
2769                 /*      This is for the case that the P2P device address is not the same as the P2P interface address. */
2770
2771                 p2pie = rtw_get_p2p_ie(&pnetwork->network.IEs[12], pnetwork->network.IELength - 12, NULL, &p2pielen);
2772                 if (p2pie) {
2773                         /*      The P2P Device ID attribute is included in the Beacon frame. */
2774                         /*      The P2P Device Info attribute is included in the probe response frame. */
2775
2776                         if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_ID, attr_content, &attr_contentlen)) {
2777                                 /*      Handle the P2P Device ID attribute of Beacon first */
2778                                 if (!memcmp(attr_content, pinvite_req_info->peer_macaddr, ETH_ALEN)) {
2779                                         peer_channel = pnetwork->network.Configuration.DSConfig;
2780                                         break;
2781                                 }
2782                         } else if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_INFO, attr_content, &attr_contentlen)) {
2783                                 /*      Handle the P2P Device Info attribute of probe response */
2784                                 if (!memcmp(attr_content, pinvite_req_info->peer_macaddr, ETH_ALEN)) {
2785                                         peer_channel = pnetwork->network.Configuration.DSConfig;
2786                                         break;
2787                                 }
2788                         }
2789                 }
2790                 plist = plist->next;
2791         }
2792
2793         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2794
2795         if (peer_channel) {
2796                 /*      Store the GO's bssid */
2797                 for (jj = 0, kk = 18; jj < ETH_ALEN; jj++, kk += 3)
2798                         pinvite_req_info->go_bssid[jj] = key_2char2num(extra[kk], extra[kk + 1]);
2799
2800                 /*      Store the GO's ssid */
2801                 pinvite_req_info->ssidlen = wrqu->data.length - 36;
2802                 memcpy(pinvite_req_info->go_ssid, &extra[36], (u32)pinvite_req_info->ssidlen);
2803                 pinvite_req_info->benable = true;
2804                 pinvite_req_info->peer_ch = peer_channel;
2805
2806                 rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo));
2807                 rtw_p2p_set_state(pwdinfo, P2P_STATE_TX_INVITE_REQ);
2808
2809                 set_channel_bwmode(padapter, peer_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
2810
2811                 _set_timer(&pwdinfo->pre_tx_scan_timer, P2P_TX_PRESCAN_TIMEOUT);
2812
2813                 _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_INVITE_TIMEOUT);
2814         }
2815 }
2816
2817 static void rtw_p2p_set_persistent(struct net_device *dev,
2818                                    struct iw_request_info *info,
2819                                    union iwreq_data *wrqu, char *extra)
2820 {
2821         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2822         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2823
2824         /*      The input data is 0 or 1 */
2825         /*      0: disable persistent group functionality */
2826         /*      1: enable persistent group founctionality */
2827
2828         if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE)) {
2829                 return;
2830         } else {
2831                 if (extra[0] == '0')    /*      Disable the persistent group function. */
2832                         pwdinfo->persistent_supported = false;
2833                 else if (extra[0] == '1')       /*      Enable the persistent group function. */
2834                         pwdinfo->persistent_supported = true;
2835                 else
2836                         pwdinfo->persistent_supported = false;
2837         }
2838         pr_info("[%s] persistent_supported = %d\n", __func__, pwdinfo->persistent_supported);
2839 }
2840
2841 static void rtw_p2p_prov_disc(struct net_device *dev,
2842                               struct iw_request_info *info,
2843                               union iwreq_data *wrqu, char *extra)
2844 {
2845         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2846         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2847         u8 peerMAC[ETH_ALEN] = {0x00};
2848         int jj, kk;
2849         struct mlme_priv                *pmlmepriv = &padapter->mlmepriv;
2850         struct list_head *plist, *phead;
2851         struct __queue *queue   = &pmlmepriv->scanned_queue;
2852         struct  wlan_network    *pnetwork = NULL;
2853         uint peer_channel = 0;
2854         u8 attr_content[100] = {0x00};
2855         u8 *p2pie;
2856         uint p2pielen = 0, attr_contentlen = 0;
2857
2858         /*      The input data contains two informations. */
2859         /*      1. First information is the MAC address which wants to issue the provisioning discovery request frame. */
2860         /*      2. Second information is the WPS configuration method which wants to discovery */
2861         /*      Format: 00:E0:4C:00:00:05_display */
2862         /*      Format: 00:E0:4C:00:00:05_keypad */
2863         /*      Format: 00:E0:4C:00:00:05_pbc */
2864         /*      Format: 00:E0:4C:00:00:05_label */
2865
2866         if (pwdinfo->p2p_state == P2P_STATE_NONE) {
2867                 return;
2868         } else {
2869                 /*      Reset the content of struct tx_provdisc_req_info excluded the wps_config_method_request. */
2870                 memset(pwdinfo->tx_prov_disc_info.peerDevAddr, 0x00, ETH_ALEN);
2871                 memset(pwdinfo->tx_prov_disc_info.peerIFAddr, 0x00, ETH_ALEN);
2872                 memset(&pwdinfo->tx_prov_disc_info.ssid, 0x00, sizeof(struct ndis_802_11_ssid));
2873                 pwdinfo->tx_prov_disc_info.peer_channel_num[0] = 0;
2874                 pwdinfo->tx_prov_disc_info.peer_channel_num[1] = 0;
2875                 pwdinfo->tx_prov_disc_info.benable = false;
2876         }
2877
2878         for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
2879                 peerMAC[jj] = key_2char2num(extra[kk], extra[kk + 1]);
2880
2881         if (!memcmp(&extra[18], "display", 7))
2882                 pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_DISPLYA;
2883         else if (!memcmp(&extra[18], "keypad", 7))
2884                 pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_KEYPAD;
2885         else if (!memcmp(&extra[18], "pbc", 3))
2886                 pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_PUSH_BUTTON;
2887         else if (!memcmp(&extra[18], "label", 5))
2888                 pwdinfo->tx_prov_disc_info.wps_config_method_request = WPS_CM_LABEL;
2889         else
2890                 return;
2891
2892         spin_lock_bh(&pmlmepriv->scanned_queue.lock);
2893
2894         phead = get_list_head(queue);
2895         plist = phead->next;
2896
2897         while (phead != plist) {
2898                 if (peer_channel != 0)
2899                         break;
2900
2901                 pnetwork = container_of(plist, struct wlan_network, list);
2902
2903                 /*      Commented by Albert 2011/05/18 */
2904                 /*      Match the device address located in the P2P IE */
2905                 /*      This is for the case that the P2P device address is not the same as the P2P interface address. */
2906
2907                 p2pie = rtw_get_p2p_ie(&pnetwork->network.IEs[12], pnetwork->network.IELength - 12, NULL, &p2pielen);
2908                 if (p2pie) {
2909                         while (p2pie) {
2910                                 /*      The P2P Device ID attribute is included in the Beacon frame. */
2911                                 /*      The P2P Device Info attribute is included in the probe response frame. */
2912
2913                                 if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_ID, attr_content, &attr_contentlen)) {
2914                                         /*      Handle the P2P Device ID attribute of Beacon first */
2915                                         if (!memcmp(attr_content, peerMAC, ETH_ALEN)) {
2916                                                 peer_channel = pnetwork->network.Configuration.DSConfig;
2917                                                 break;
2918                                         }
2919                                 } else if (rtw_get_p2p_attr_content(p2pie, p2pielen, P2P_ATTR_DEVICE_INFO, attr_content, &attr_contentlen)) {
2920                                         /*      Handle the P2P Device Info attribute of probe response */
2921                                         if (!memcmp(attr_content, peerMAC, ETH_ALEN)) {
2922                                                 peer_channel = pnetwork->network.Configuration.DSConfig;
2923                                                 break;
2924                                         }
2925                                 }
2926
2927                                 /* Get the next P2P IE */
2928                                 p2pie = rtw_get_p2p_ie(p2pie + p2pielen, pnetwork->network.IELength - 12 - (p2pie - &pnetwork->network.IEs[12] + p2pielen), NULL, &p2pielen);
2929                         }
2930                 }
2931
2932                 plist = plist->next;
2933         }
2934
2935         spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
2936
2937         if (peer_channel) {
2938                 memcpy(pwdinfo->tx_prov_disc_info.peerIFAddr, pnetwork->network.MacAddress, ETH_ALEN);
2939                 memcpy(pwdinfo->tx_prov_disc_info.peerDevAddr, peerMAC, ETH_ALEN);
2940                 pwdinfo->tx_prov_disc_info.peer_channel_num[0] = (u16)peer_channel;
2941                 pwdinfo->tx_prov_disc_info.benable = true;
2942                 rtw_p2p_set_pre_state(pwdinfo, rtw_p2p_state(pwdinfo));
2943                 rtw_p2p_set_state(pwdinfo, P2P_STATE_TX_PROVISION_DIS_REQ);
2944
2945                 if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT)) {
2946                         memcpy(&pwdinfo->tx_prov_disc_info.ssid, &pnetwork->network.Ssid, sizeof(struct ndis_802_11_ssid));
2947                 } else if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_DEVICE) || rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) {
2948                         memcpy(pwdinfo->tx_prov_disc_info.ssid.Ssid, pwdinfo->p2p_wildcard_ssid, P2P_WILDCARD_SSID_LEN);
2949                         pwdinfo->tx_prov_disc_info.ssid.SsidLength = P2P_WILDCARD_SSID_LEN;
2950                 }
2951
2952                 set_channel_bwmode(padapter, peer_channel, HAL_PRIME_CHNL_OFFSET_DONT_CARE, HT_CHANNEL_WIDTH_20);
2953
2954                 _set_timer(&pwdinfo->pre_tx_scan_timer, P2P_TX_PRESCAN_TIMEOUT);
2955
2956                 _set_timer(&pwdinfo->restore_p2p_state_timer, P2P_PROVISION_TIMEOUT);
2957         }
2958 }
2959
2960 /*      This function is used to inform the driver the user had specified the pin code value or pbc */
2961 /*      to application. */
2962
2963 static void rtw_p2p_got_wpsinfo(struct net_device *dev,
2964                                 struct iw_request_info *info,
2965                                 union iwreq_data *wrqu, char *extra)
2966 {
2967         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
2968         struct wifidirect_info *pwdinfo = &padapter->wdinfo;
2969
2970         /*      Added by Albert 20110328 */
2971         /*      if the input data is P2P_NO_WPSINFO -> reset the wpsinfo */
2972         /*      if the input data is P2P_GOT_WPSINFO_PEER_DISPLAY_PIN -> the utility just input the PIN code got from the peer P2P device. */
2973         /*      if the input data is P2P_GOT_WPSINFO_SELF_DISPLAY_PIN -> the utility just got the PIN code from itself. */
2974         /*      if the input data is P2P_GOT_WPSINFO_PBC -> the utility just determine to use the PBC */
2975
2976         if (*extra == '0')
2977                 pwdinfo->ui_got_wps_info = P2P_NO_WPSINFO;
2978         else if (*extra == '1')
2979                 pwdinfo->ui_got_wps_info = P2P_GOT_WPSINFO_PEER_DISPLAY_PIN;
2980         else if (*extra == '2')
2981                 pwdinfo->ui_got_wps_info = P2P_GOT_WPSINFO_SELF_DISPLAY_PIN;
2982         else if (*extra == '3')
2983                 pwdinfo->ui_got_wps_info = P2P_GOT_WPSINFO_PBC;
2984         else
2985                 pwdinfo->ui_got_wps_info = P2P_NO_WPSINFO;
2986 }
2987
2988 static int rtw_p2p_set(struct net_device *dev,
2989                                struct iw_request_info *info,
2990                                union iwreq_data *wrqu, char *extra)
2991 {
2992         int ret = 0;
2993
2994         if (!memcmp(extra, "enable =", 7)) {
2995                 rtw_wext_p2p_enable(dev, info, wrqu, &extra[7]);
2996         } else if (!memcmp(extra, "setDN =", 6)) {
2997                 wrqu->data.length -= 6;
2998                 rtw_p2p_setDN(dev, info, wrqu, &extra[6]);
2999         } else if (!memcmp(extra, "profilefound =", 13)) {
3000                 wrqu->data.length -= 13;
3001                 rtw_p2p_profilefound(dev, info, wrqu, &extra[13]);
3002         } else if (!memcmp(extra, "prov_disc =", 10)) {
3003                 wrqu->data.length -= 10;
3004                 rtw_p2p_prov_disc(dev, info, wrqu, &extra[10]);
3005         } else if (!memcmp(extra, "nego =", 5)) {
3006                 wrqu->data.length -= 5;
3007                 rtw_p2p_connect(dev, info, wrqu, &extra[5]);
3008         } else if (!memcmp(extra, "intent =", 7)) {
3009                 /*      Commented by Albert 2011/03/23 */
3010                 /*      The wrqu->data.length will include the null character */
3011                 /*      So, we will decrease 7 + 1 */
3012                 wrqu->data.length -= 8;
3013                 rtw_p2p_set_intent(dev, info, wrqu, &extra[7]);
3014         } else if (!memcmp(extra, "ssid =", 5)) {
3015                 wrqu->data.length -= 5;
3016                 rtw_p2p_set_go_nego_ssid(dev, info, wrqu, &extra[5]);
3017         } else if (!memcmp(extra, "got_wpsinfo =", 12)) {
3018                 wrqu->data.length -= 12;
3019                 rtw_p2p_got_wpsinfo(dev, info, wrqu, &extra[12]);
3020         } else if (!memcmp(extra, "listen_ch =", 10)) {
3021                 /*      Commented by Albert 2011/05/24 */
3022                 /*      The wrqu->data.length will include the null character */
3023                 /*      So, we will decrease (10 + 1) */
3024                 wrqu->data.length -= 11;
3025                 rtw_p2p_set_listen_ch(dev, info, wrqu, &extra[10]);
3026         } else if (!memcmp(extra, "op_ch =", 6)) {
3027                 /*      Commented by Albert 2011/05/24 */
3028                 /*      The wrqu->data.length will include the null character */
3029                 /*      So, we will decrease (6 + 1) */
3030                 wrqu->data.length -= 7;
3031                 rtw_p2p_set_op_ch(dev, info, wrqu, &extra[6]);
3032         } else if (!memcmp(extra, "invite =", 7)) {
3033                 wrqu->data.length -= 8;
3034                 rtw_p2p_invite_req(dev, info, wrqu, &extra[7]);
3035         } else if (!memcmp(extra, "persistent =", 11)) {
3036                 wrqu->data.length -= 11;
3037                 rtw_p2p_set_persistent(dev, info, wrqu, &extra[11]);
3038         }
3039
3040         return ret;
3041 }
3042
3043 static int rtw_p2p_get2(struct net_device *dev,
3044                                struct iw_request_info *info,
3045                                union iwreq_data *wrqu, char *extra)
3046 {
3047         int ret = 0;
3048
3049         if (!memcmp(extra, "wpsCM =", 6)) {
3050                 wrqu->data.length -= 6;
3051                 ret = rtw_p2p_get_wps_configmethod(dev, info, wrqu,  &extra[6]);
3052         } else if (!memcmp(extra, "devN =", 5)) {
3053                 wrqu->data.length -= 5;
3054                 ret = rtw_p2p_get_device_name(dev, info, wrqu, &extra[5]);
3055         } else if (!memcmp(extra, "dev_type =", 9)) {
3056                 wrqu->data.length -= 9;
3057                 ret = rtw_p2p_get_device_type(dev, info, wrqu, &extra[9]);
3058         } else if (!memcmp(extra, "go_devadd =", 10)) {
3059                 wrqu->data.length -= 10;
3060                 ret = rtw_p2p_get_go_device_address(dev, info, wrqu, &extra[10]);
3061         } else if (!memcmp(extra, "InvProc =", 8)) {
3062                 wrqu->data.length -= 8;
3063                 ret = rtw_p2p_get_invitation_procedure(dev, info, wrqu, &extra[8]);
3064         }
3065
3066         return ret;
3067 }
3068
3069 static int rtw_rereg_nd_name(struct net_device *dev,
3070                                struct iw_request_info *info,
3071                                union iwreq_data *wrqu, char *extra)
3072 {
3073         int ret = 0;
3074         struct adapter *padapter = rtw_netdev_priv(dev);
3075         struct rereg_nd_name_data *rereg_priv = &padapter->rereg_nd_name_priv;
3076         char new_ifname[IFNAMSIZ];
3077
3078         if (rereg_priv->old_ifname[0] == 0) {
3079                 char *reg_ifname;
3080                 reg_ifname = padapter->registrypriv.if2name;
3081
3082                 strncpy(rereg_priv->old_ifname, reg_ifname, IFNAMSIZ);
3083                 rereg_priv->old_ifname[IFNAMSIZ - 1] = 0;
3084         }
3085
3086         if (wrqu->data.length > IFNAMSIZ)
3087                 return -EFAULT;
3088
3089         if (copy_from_user(new_ifname, wrqu->data.pointer, IFNAMSIZ))
3090                 return -EFAULT;
3091
3092         if (0 == strcmp(rereg_priv->old_ifname, new_ifname))
3093                 return ret;
3094
3095         ret = rtw_change_ifname(padapter, new_ifname);
3096         if (0 != ret)
3097                 goto exit;
3098
3099         if (!memcmp(rereg_priv->old_ifname, "disable%d", 9)) {
3100                 padapter->ledpriv.bRegUseLed = rereg_priv->old_bRegUseLed;
3101                 rtl8188eu_InitSwLeds(padapter);
3102                 rtw_ips_mode_req(&padapter->pwrctrlpriv, rereg_priv->old_ips_mode);
3103         }
3104
3105         strncpy(rereg_priv->old_ifname, new_ifname, IFNAMSIZ);
3106         rereg_priv->old_ifname[IFNAMSIZ - 1] = 0;
3107
3108         if (!memcmp(new_ifname, "disable%d", 9)) {
3109                 /*  free network queue for Android's timming issue */
3110                 rtw_free_network_queue(padapter, true);
3111
3112                 /*  close led */
3113                 rtw_led_control(padapter, LED_CTL_POWER_OFF);
3114                 rereg_priv->old_bRegUseLed = padapter->ledpriv.bRegUseLed;
3115                 padapter->ledpriv.bRegUseLed = false;
3116                 rtl8188eu_DeInitSwLeds(padapter);
3117
3118                 /*  the interface is being "disabled", we can do deeper IPS */
3119                 rereg_priv->old_ips_mode = rtw_get_ips_mode_req(&padapter->pwrctrlpriv);
3120                 rtw_ips_mode_req(&padapter->pwrctrlpriv, IPS_NORMAL);
3121         }
3122 exit:
3123         return ret;
3124 }
3125
3126 static void mac_reg_dump(struct adapter *padapter)
3127 {
3128         int i, j = 1;
3129         u32 reg;
3130         int res;
3131
3132         pr_info("\n ======= MAC REG =======\n");
3133         for (i = 0x0; i < 0x300; i += 4) {
3134                 if (j % 4 == 1)
3135                         pr_info("0x%02x", i);
3136
3137                 res = rtw_read32(padapter, i, &reg);
3138                 if (!res)
3139                         pr_info(" 0x%08x ", reg);
3140
3141                 if ((j++) % 4 == 0)
3142                         pr_info("\n");
3143         }
3144         for (i = 0x400; i < 0x800; i += 4) {
3145                 if (j % 4 == 1)
3146                         pr_info("0x%02x", i);
3147
3148                 res = rtw_read32(padapter, i, &reg);
3149                 if (!res)
3150                         pr_info(" 0x%08x ", reg);
3151
3152                 if ((j++) % 4 == 0)
3153                         pr_info("\n");
3154         }
3155 }
3156
3157 static void bb_reg_dump(struct adapter *padapter)
3158 {
3159         int i, j = 1, res;
3160         u32 reg;
3161
3162         pr_info("\n ======= BB REG =======\n");
3163         for (i = 0x800; i < 0x1000; i += 4) {
3164                 if (j % 4 == 1)
3165                         pr_info("0x%02x", i);
3166
3167                 res = rtw_read32(padapter, i, &reg);
3168                 if (!res)
3169                         pr_info(" 0x%08x ", reg);
3170
3171                 if ((j++) % 4 == 0)
3172                         pr_info("\n");
3173         }
3174 }
3175
3176 static void rf_reg_dump(struct adapter *padapter)
3177 {
3178         int i, j = 1;
3179         u32 value;
3180
3181         pr_info("\n ======= RF REG =======\n");
3182         pr_info("\nRF_Path(%x)\n", RF_PATH_A);
3183         for (i = 0; i < 0x100; i++) {
3184                 value = rtl8188e_PHY_QueryRFReg(padapter, i, 0xffffffff);
3185                 if (j % 4 == 1)
3186                         pr_info("0x%02x ", i);
3187                 pr_info(" 0x%08x ", value);
3188                 if ((j++) % 4 == 0)
3189                         pr_info("\n");
3190         }
3191 }
3192
3193 static void rtw_set_dynamic_functions(struct adapter *adapter, u8 dm_func)
3194 {
3195         struct hal_data_8188e *haldata = &adapter->haldata;
3196         struct odm_dm_struct *odmpriv = &haldata->odmpriv;
3197         int res;
3198
3199         switch (dm_func) {
3200         case 0:
3201                 /* disable all dynamic func */
3202                 odmpriv->SupportAbility = DYNAMIC_FUNC_DISABLE;
3203                 break;
3204         case 1:
3205                 /* disable DIG */
3206                 odmpriv->SupportAbility &= (~DYNAMIC_BB_DIG);
3207                 break;
3208         case 6:
3209                 /* turn on all dynamic func */
3210                 if (!(odmpriv->SupportAbility & DYNAMIC_BB_DIG)) {
3211                         struct rtw_dig *digtable = &odmpriv->DM_DigTable;
3212
3213                         res = rtw_read8(adapter, 0xc50, &digtable->CurIGValue);
3214                         (void)res;
3215                         /* FIXME: return an error to caller */
3216                 }
3217                 odmpriv->SupportAbility = DYNAMIC_ALL_FUNC_ENABLE;
3218                 break;
3219         default:
3220                 break;
3221         }
3222 }
3223
3224 static int rtw_dbg_port(struct net_device *dev,
3225                                struct iw_request_info *info,
3226                                union iwreq_data *wrqu, char *extra)
3227 {
3228         int ret = 0;
3229         u8 major_cmd, minor_cmd;
3230         u16 arg;
3231         s32 extra_arg;
3232         u32 *pdata, val32;
3233         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
3234         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3235         struct mlme_ext_priv    *pmlmeext = &padapter->mlmeextpriv;
3236         struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info;
3237         struct wlan_network *cur_network = &pmlmepriv->cur_network;
3238         struct sta_priv *pstapriv = &padapter->stapriv;
3239
3240         pdata = (u32 *)&wrqu->data;
3241
3242         val32 = *pdata;
3243         arg = (u16)(val32 & 0x0000ffff);
3244         major_cmd = (u8)(val32 >> 24);
3245         minor_cmd = (u8)((val32 >> 16) & 0x00ff);
3246
3247         extra_arg = *(pdata + 1);
3248
3249         switch (major_cmd) {
3250         case 0x70:/* read_reg */
3251                 switch (minor_cmd) {
3252                 case 1:
3253                         break;
3254                 case 2:
3255                         break;
3256                 case 4:
3257                         break;
3258                 }
3259                 break;
3260         case 0x71:/* write_reg */
3261                 switch (minor_cmd) {
3262                 case 1:
3263                         rtw_write8(padapter, arg, extra_arg);
3264                         break;
3265                 case 2:
3266                         rtw_write16(padapter, arg, extra_arg);
3267                         break;
3268                 case 4:
3269                         rtw_write32(padapter, arg, extra_arg);
3270                         break;
3271                 }
3272                 break;
3273         case 0x72:/* read_bb */
3274                 break;
3275         case 0x73:/* write_bb */
3276                 rtl8188e_PHY_SetBBReg(padapter, arg, 0xffffffff, extra_arg);
3277                 break;
3278         case 0x74:/* read_rf */
3279                 if (minor_cmd != RF_PATH_A) {
3280                         ret = -EINVAL;
3281                         break;
3282                 }
3283                 break;
3284         case 0x75:/* write_rf */
3285                 if (minor_cmd != RF_PATH_A) {
3286                         ret = -EINVAL;
3287                         break;
3288                 }
3289                 rtl8188e_PHY_SetRFReg(padapter, arg, 0xffffffff, extra_arg);
3290                 break;
3291
3292         case 0x76:
3293                 switch (minor_cmd) {
3294                 case 0x00: /* normal mode, */
3295                         padapter->recvpriv.is_signal_dbg = 0;
3296                         break;
3297                 case 0x01: /* dbg mode */
3298                         padapter->recvpriv.is_signal_dbg = 1;
3299                         extra_arg = extra_arg > 100 ? 100 : extra_arg;
3300                         extra_arg = extra_arg < 0 ? 0 : extra_arg;
3301                         padapter->recvpriv.signal_strength_dbg = extra_arg;
3302                         break;
3303                 }
3304                 break;
3305         case 0x78: /* IOL test */
3306                 switch (minor_cmd) {
3307                 case 0x04: /* LLT table initialization test */
3308                 {
3309                         struct xmit_frame       *xmit_frame;
3310
3311                         xmit_frame = rtw_IOL_accquire_xmit_frame(padapter);
3312                         if (!xmit_frame) {
3313                                 ret = -ENOMEM;
3314                                 break;
3315                         }
3316
3317                         if (rtl8188e_IOL_exec_cmds_sync(padapter, xmit_frame, 500, 0) != _SUCCESS)
3318                                 ret = -EPERM;
3319                 }
3320                         break;
3321                 case 0x05: /* blink LED test */
3322                 {
3323                         u16 reg = 0x4c;
3324                         u32 blink_num = 50;
3325                         u32 blink_delay_ms = 200;
3326                         int i;
3327                         struct xmit_frame       *xmit_frame;
3328
3329                         xmit_frame = rtw_IOL_accquire_xmit_frame(padapter);
3330                         if (!xmit_frame) {
3331                                 ret = -ENOMEM;
3332                                 break;
3333                         }
3334
3335                         for (i = 0; i < blink_num; i++) {
3336                                 rtw_IOL_append_WB_cmd(xmit_frame, reg, 0x00, 0xff);
3337                                 rtw_IOL_append_DELAY_MS_cmd(xmit_frame, blink_delay_ms);
3338                                 rtw_IOL_append_WB_cmd(xmit_frame, reg, 0x08, 0xff);
3339                                 rtw_IOL_append_DELAY_MS_cmd(xmit_frame, blink_delay_ms);
3340                         }
3341                         if (rtl8188e_IOL_exec_cmds_sync(padapter, xmit_frame, (blink_delay_ms * blink_num * 2) + 200, 0) != _SUCCESS)
3342                                 ret = -EPERM;
3343                 }
3344                         break;
3345
3346                 case 0x06: /* continuous write byte test */
3347                 {
3348                         u16 reg = arg;
3349                         u16 start_value = 0;
3350                         u32 write_num = extra_arg;
3351                         int i, res;
3352                         struct xmit_frame       *xmit_frame;
3353                         u8 val8;
3354
3355                         xmit_frame = rtw_IOL_accquire_xmit_frame(padapter);
3356                         if (!xmit_frame) {
3357                                 ret = -ENOMEM;
3358                                 break;
3359                         }
3360
3361                         for (i = 0; i < write_num; i++)
3362                                 rtw_IOL_append_WB_cmd(xmit_frame, reg, i + start_value, 0xFF);
3363                         if (rtl8188e_IOL_exec_cmds_sync(padapter, xmit_frame, 5000, 0) != _SUCCESS)
3364                                 ret = -EPERM;
3365
3366                         /* FIXME: is this read necessary? */
3367                         res = rtw_read8(padapter, reg, &val8);
3368                         (void)res;
3369                 }
3370                         break;
3371
3372                 case 0x07: /* continuous write word test */
3373                 {
3374                         u16 reg = arg;
3375                         u16 start_value = 200;
3376                         u32 write_num = extra_arg;
3377                         u16 val16;
3378                         int i, res;
3379                         struct xmit_frame       *xmit_frame;
3380
3381                         xmit_frame = rtw_IOL_accquire_xmit_frame(padapter);
3382                         if (!xmit_frame) {
3383                                 ret = -ENOMEM;
3384                                 break;
3385                         }
3386
3387                         for (i = 0; i < write_num; i++)
3388                                 rtw_IOL_append_WW_cmd(xmit_frame, reg, i + start_value, 0xFFFF);
3389                         if (rtl8188e_IOL_exec_cmds_sync(padapter, xmit_frame, 5000, 0) != _SUCCESS)
3390                                 ret = -EPERM;
3391
3392                         /* FIXME: is this read necessary? */
3393                         res = rtw_read16(padapter, reg, &val16);
3394                         (void)res;
3395                 }
3396                         break;
3397                 case 0x08: /* continuous write dword test */
3398                 {
3399                         u16 reg = arg;
3400                         u32 start_value = 0x110000c7;
3401                         u32 write_num = extra_arg;
3402
3403                         int i;
3404                         struct xmit_frame       *xmit_frame;
3405
3406                         xmit_frame = rtw_IOL_accquire_xmit_frame(padapter);
3407                         if (!xmit_frame) {
3408                                 ret = -ENOMEM;
3409                                 break;
3410                         }
3411
3412                         for (i = 0; i < write_num; i++)
3413                                 rtw_IOL_append_WD_cmd(xmit_frame, reg, i + start_value, 0xFFFFFFFF);
3414                         if (rtl8188e_IOL_exec_cmds_sync(padapter, xmit_frame, 5000, 0) != _SUCCESS)
3415                                 ret = -EPERM;
3416
3417                         /* FIXME: is this read necessary? */
3418                         ret = rtw_read32(padapter, reg, &write_num);
3419                 }
3420                         break;
3421                 }
3422                 break;
3423         case 0x79:
3424                 {
3425                         /*
3426                         * dbg 0x79000000 [value], set RESP_TXAGC to + value, value:0~15
3427                         * dbg 0x79010000 [value], set RESP_TXAGC to - value, value:0~15
3428                         */
3429                         u8 value =  extra_arg & 0x0f;
3430                         u8 sign = minor_cmd;
3431                         u16 write_value = 0;
3432
3433                         if (sign)
3434                                 value = value | 0x10;
3435
3436                         write_value = value | (value << 5);
3437                         rtw_write16(padapter, 0x6d9, write_value);
3438                 }
3439                 break;
3440         case 0x7a:
3441                 receive_disconnect(padapter, pmlmeinfo->network.MacAddress
3442                         , WLAN_REASON_EXPIRATION_CHK);
3443                 break;
3444         case 0x7F:
3445                 switch (minor_cmd) {
3446                 case 0x0:
3447                         break;
3448                 case 0x01:
3449                         break;
3450                 case 0x02:
3451                         break;
3452                 case 0x03:
3453                         break;
3454                 case 0x04:
3455                         break;
3456                 case 0x05:
3457                         rtw_get_stainfo(pstapriv, cur_network->network.MacAddress);
3458                         break;
3459                 case 0x06:
3460                         {
3461                                 u32 ODMFlag = (u32)(0x0f & arg);
3462                                 SetHwReg8188EU(padapter, HW_VAR_DM_FLAG, (u8 *)(&ODMFlag));
3463                         }
3464                         break;
3465                 case 0x07:
3466                         break;
3467                 case 0x08:
3468                         break;
3469                 case 0x09:
3470                         break;
3471                 case 0x15:
3472                         break;
3473                 case 0x10:/*  driver version display */
3474                         break;
3475                 case 0x11:
3476                         padapter->bRxRSSIDisplay = extra_arg;
3477                         break;
3478                 case 0x12: /* set rx_stbc */
3479                 {
3480                         struct registry_priv    *pregpriv = &padapter->registrypriv;
3481                         /*  0: disable, bit(0):enable 2.4g, bit(1):enable 5g, 0x3: enable both 2.4g and 5g */
3482                         /* default is set to enable 2.4GHZ for IOT issue with bufflao's AP at 5GHZ */
3483                         if (extra_arg == 0 ||
3484                             extra_arg == 1 ||
3485                             extra_arg == 2 ||
3486                             extra_arg == 3)
3487                                 pregpriv->rx_stbc = extra_arg;
3488                 }
3489                         break;
3490                 case 0x13: /* set ampdu_enable */
3491                 {
3492                         struct registry_priv    *pregpriv = &padapter->registrypriv;
3493                         /*  0: disable, 0x1:enable (but wifi_spec should be 0), 0x2: force enable (don't care wifi_spec) */
3494                         if (extra_arg >= 0 && extra_arg < 3)
3495                                 pregpriv->ampdu_enable = extra_arg;
3496                 }
3497                         break;
3498                 case 0x14: /* get wifi_spec */
3499                         break;
3500                 case 0x23:
3501                         padapter->bNotifyChannelChange = extra_arg;
3502                         break;
3503                 case 0x24:
3504                         padapter->bShowGetP2PState = extra_arg;
3505                         break;
3506                 case 0xdd:/* registers dump, 0 for mac reg, 1 for bb reg, 2 for rf reg */
3507                         if (extra_arg == 0)
3508                                 mac_reg_dump(padapter);
3509                         else if (extra_arg == 1)
3510                                 bb_reg_dump(padapter);
3511                         else if (extra_arg == 2)
3512                                 rf_reg_dump(padapter);
3513                         break;
3514                 case 0xee:/* turn on/off dynamic funcs */
3515                         if (extra_arg != 0xf) {
3516                                 /* extra_arg = 0  - disable all dynamic func
3517                                  * extra_arg = 1  - disable DIG
3518                                  * extra_arg = 6  - turn on all dynamic func
3519                                  */
3520                                 rtw_set_dynamic_functions(padapter, extra_arg);
3521                         }
3522                         break;
3523                 case 0xfd:
3524                         rtw_write8(padapter, 0xc50, arg);
3525                         rtw_write8(padapter, 0xc58, arg);
3526                         break;
3527                 case 0xfe:
3528                         break;
3529                 case 0xff:
3530                         break;
3531                 }
3532                 break;
3533         default:
3534                 break;
3535         }
3536         return ret;
3537 }
3538
3539 static int rtw_wx_set_priv(struct net_device *dev,
3540                                 struct iw_request_info *info,
3541                                 union iwreq_data *awrq,
3542                                 char *extra)
3543 {
3544         int ret = 0;
3545         int len = 0;
3546         char *ext;
3547         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
3548         struct iw_point *dwrq = (struct iw_point *)awrq;
3549
3550         if (dwrq->length == 0)
3551                 return -EFAULT;
3552
3553         len = dwrq->length;
3554         ext = vmalloc(len);
3555         if (!ext)
3556                 return -ENOMEM;
3557
3558         if (copy_from_user(ext, dwrq->pointer, len)) {
3559                 vfree(ext);
3560                 return -EFAULT;
3561         }
3562
3563         /* added for wps2.0 @20110524 */
3564         if (dwrq->flags == 0x8766 && len > 8) {
3565                 u32 cp_sz;
3566                 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
3567                 u8 *probereq_wpsie = ext;
3568                 int probereq_wpsie_len = len;
3569                 u8 wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
3570
3571                 if ((_VENDOR_SPECIFIC_IE_ == probereq_wpsie[0]) &&
3572                     (!memcmp(&probereq_wpsie[2], wps_oui, 4))) {
3573                         cp_sz = probereq_wpsie_len > MAX_WPS_IE_LEN ? MAX_WPS_IE_LEN : probereq_wpsie_len;
3574
3575                         pmlmepriv->wps_probe_req_ie_len = 0;
3576                         kfree(pmlmepriv->wps_probe_req_ie);
3577                         pmlmepriv->wps_probe_req_ie = NULL;
3578
3579                         pmlmepriv->wps_probe_req_ie = kmemdup(probereq_wpsie, cp_sz, GFP_KERNEL);
3580                         if (!pmlmepriv->wps_probe_req_ie) {
3581                                 ret =  -EINVAL;
3582                                 goto FREE_EXT;
3583                         }
3584                         pmlmepriv->wps_probe_req_ie_len = cp_sz;
3585                 }
3586                 goto FREE_EXT;
3587         }
3588
3589         if (len >= WEXT_CSCAN_HEADER_SIZE &&
3590             !memcmp(ext, WEXT_CSCAN_HEADER, WEXT_CSCAN_HEADER_SIZE)) {
3591                 ret = rtw_wx_set_scan(dev, info, awrq, ext);
3592                 goto FREE_EXT;
3593         }
3594
3595 FREE_EXT:
3596
3597         vfree(ext);
3598
3599         return ret;
3600 }
3601
3602 static int rtw_pm_set(struct net_device *dev,
3603                                struct iw_request_info *info,
3604                                union iwreq_data *wrqu, char *extra)
3605 {
3606         int ret = 0;
3607         unsigned        mode = 0;
3608         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
3609
3610         if (!memcmp(extra, "lps =", 4)) {
3611                 sscanf(extra + 4, "%u", &mode);
3612                 ret = rtw_pm_set_lps(padapter, mode);
3613         } else if (!memcmp(extra, "ips =", 4)) {
3614                 sscanf(extra + 4, "%u", &mode);
3615                 ret = rtw_pm_set_ips(padapter, mode);
3616         } else {
3617                 ret = -EINVAL;
3618         }
3619
3620         return ret;
3621 }
3622
3623 static iw_handler rtw_handlers[] = {
3624         IW_HANDLER(SIOCGIWNAME, rtw_wx_get_name),
3625         IW_HANDLER(SIOCGIWFREQ, rtw_wx_get_freq),
3626         IW_HANDLER(SIOCSIWMODE, rtw_wx_set_mode),
3627         IW_HANDLER(SIOCGIWMODE, rtw_wx_get_mode),
3628         IW_HANDLER(SIOCGIWSENS, rtw_wx_get_sens),
3629         IW_HANDLER(SIOCGIWRANGE, rtw_wx_get_range),
3630         IW_HANDLER(SIOCSIWPRIV, rtw_wx_set_priv),
3631         IW_HANDLER(SIOCSIWAP, rtw_wx_set_wap),
3632         IW_HANDLER(SIOCGIWAP, rtw_wx_get_wap),
3633         IW_HANDLER(SIOCSIWMLME, rtw_wx_set_mlme),
3634         IW_HANDLER(SIOCSIWSCAN, rtw_wx_set_scan),
3635         IW_HANDLER(SIOCGIWSCAN, rtw_wx_get_scan),
3636         IW_HANDLER(SIOCSIWESSID, rtw_wx_set_essid),
3637         IW_HANDLER(SIOCGIWESSID, rtw_wx_get_essid),
3638         IW_HANDLER(SIOCGIWNICKN, rtw_wx_get_nick),
3639         IW_HANDLER(SIOCSIWRATE, rtw_wx_set_rate),
3640         IW_HANDLER(SIOCGIWRATE, rtw_wx_get_rate),
3641         IW_HANDLER(SIOCSIWRTS, rtw_wx_set_rts),
3642         IW_HANDLER(SIOCGIWRTS, rtw_wx_get_rts),
3643         IW_HANDLER(SIOCSIWFRAG, rtw_wx_set_frag),
3644         IW_HANDLER(SIOCGIWFRAG, rtw_wx_get_frag),
3645         IW_HANDLER(SIOCGIWRETRY, rtw_wx_get_retry),
3646         IW_HANDLER(SIOCSIWENCODE, rtw_wx_set_enc),
3647         IW_HANDLER(SIOCGIWENCODE, rtw_wx_get_enc),
3648         IW_HANDLER(SIOCGIWPOWER, rtw_wx_get_power),
3649         IW_HANDLER(SIOCSIWGENIE, rtw_wx_set_gen_ie),
3650         IW_HANDLER(SIOCSIWAUTH, rtw_wx_set_auth),
3651         IW_HANDLER(SIOCSIWENCODEEXT, rtw_wx_set_enc_ext),
3652         IW_HANDLER(SIOCSIWPMKSA, rtw_wx_set_pmkid),
3653 };
3654
3655 static const struct iw_priv_args rtw_private_args[] = {
3656         {
3657                 SIOCIWFIRSTPRIV + 0x0,
3658                 IW_PRIV_TYPE_CHAR | 0x7FF, 0, "write"
3659         },
3660         {
3661                 SIOCIWFIRSTPRIV + 0x1,
3662                 IW_PRIV_TYPE_CHAR | 0x7FF,
3663                 IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "read"
3664         },
3665         {
3666                 SIOCIWFIRSTPRIV + 0x2, 0, 0, "driver_ext"
3667         },
3668         {
3669                 SIOCIWFIRSTPRIV + 0x4,
3670                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "apinfo"
3671         },
3672         {
3673                 SIOCIWFIRSTPRIV + 0x5,
3674                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "setpid"
3675         },
3676         {
3677                 SIOCIWFIRSTPRIV + 0x6,
3678                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "wps_start"
3679         },
3680         {
3681                 SIOCIWFIRSTPRIV + 0xA,
3682                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, 0, "channel_plan"
3683         },
3684
3685         {
3686                 SIOCIWFIRSTPRIV + 0xB,
3687                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "dbg"
3688         },
3689         {
3690                 SIOCIWFIRSTPRIV + 0xC,
3691                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 3, 0, "rfw"
3692         },
3693         {
3694                 SIOCIWFIRSTPRIV + 0xD,
3695                 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ, "rfr"
3696         },
3697         {
3698                 SIOCIWFIRSTPRIV + 0x10,
3699                 IW_PRIV_TYPE_CHAR | P2P_PRIVATE_IOCTL_SET_LEN, 0, "p2p_set"
3700         },
3701         {
3702                 SIOCIWFIRSTPRIV + 0x11,
3703                 IW_PRIV_TYPE_CHAR | P2P_PRIVATE_IOCTL_SET_LEN, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | P2P_PRIVATE_IOCTL_SET_LEN, "p2p_get"
3704         },
3705         {
3706                 SIOCIWFIRSTPRIV + 0x12,
3707                 IW_PRIV_TYPE_CHAR | P2P_PRIVATE_IOCTL_SET_LEN, IW_PRIV_TYPE_CHAR | IFNAMSIZ, "p2p_get2"
3708         },
3709         {
3710                 SIOCIWFIRSTPRIV + 0x16,
3711                 IW_PRIV_TYPE_CHAR | 64, 0, "pm_set"
3712         },
3713
3714         {SIOCIWFIRSTPRIV + 0x18, IW_PRIV_TYPE_CHAR | IFNAMSIZ, 0, "rereg_nd_name"},
3715 };
3716
3717 static iw_handler rtw_private_handler[] = {
3718         NULL,                           /* 0x00 */
3719         NULL,                           /* 0x01 */
3720         NULL,                           /* 0x02 */
3721 NULL,                                   /* 0x03 */
3722 /*  for MM DTV platform */
3723         rtw_get_ap_info,                /* 0x04 */
3724
3725         rtw_set_pid,                    /* 0x05 */
3726         rtw_wps_start,                  /* 0x06 */
3727
3728         NULL,                           /* 0x07 */
3729         NULL,                           /* 0x08 */
3730         NULL,                           /* 0x09 */
3731
3732 /*  Set Channel depend on the country code */
3733         rtw_wx_set_channel_plan,        /* 0x0A */
3734
3735         rtw_dbg_port,                   /* 0x0B */
3736         rtw_wx_write_rf,                /* 0x0C */
3737         rtw_wx_read_rf,                 /* 0x0D */
3738         NULL,                           /* 0x0E */
3739         NULL,                           /* 0x0F */
3740
3741         rtw_p2p_set,                    /* 0x10 */
3742         NULL,                           /* 0x11 */
3743         rtw_p2p_get2,                   /* 0x12 */
3744
3745         NULL,                           /* 0x13 */
3746         NULL,                           /* 0x14 */
3747         NULL,                           /* 0x15 */
3748
3749         rtw_pm_set,                     /* 0x16 */
3750         NULL,                           /* 0x17 */
3751         rtw_rereg_nd_name,              /* 0x18 */
3752 };
3753
3754 static struct iw_statistics *rtw_get_wireless_stats(struct net_device *dev)
3755 {
3756         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(dev);
3757         struct iw_statistics *piwstats = &padapter->iwstats;
3758         int tmp_noise = 0;
3759         int tmp;
3760
3761         if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
3762                 piwstats->qual.qual = 0;
3763                 piwstats->qual.level = 0;
3764                 piwstats->qual.noise = 0;
3765         } else {
3766                 tmp_noise = padapter->recvpriv.noise;
3767
3768                 piwstats->qual.level = padapter->signal_strength;
3769                 tmp = 219 + 3 * padapter->signal_strength;
3770                 tmp = min(100, tmp);
3771                 tmp = max(0, tmp);
3772                 piwstats->qual.qual = tmp;
3773                 piwstats->qual.noise = tmp_noise;
3774         }
3775         piwstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
3776         return &padapter->iwstats;
3777 }
3778
3779 struct iw_handler_def rtw_handlers_def = {
3780         .standard = rtw_handlers,
3781         .num_standard = ARRAY_SIZE(rtw_handlers),
3782         .private = rtw_private_handler,
3783         .private_args = (struct iw_priv_args *)rtw_private_args,
3784         .num_private = ARRAY_SIZE(rtw_private_handler),
3785         .num_private_args = ARRAY_SIZE(rtw_private_args),
3786         .get_wireless_stats = rtw_get_wireless_stats,
3787 };