1 /******************************************************************************
4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
5 * Linux device driver for RTL8192SU
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
23 * Contact information:
24 * WLAN FAE <wlanfae@realtek.com>
25 * Larry Finger <Larry.Finger@lwfinger.net>
27 ******************************************************************************/
29 #define _RTL871X_IOCTL_SET_C_
31 #include "osdep_service.h"
32 #include "drv_types.h"
33 #include "rtl871x_ioctl_set.h"
34 #include "usb_osintf.h"
38 static u8 validate_ssid(struct ndis_802_11_ssid *ssid)
42 if (ssid->SsidLength > 32)
44 for (i = 0; i < ssid->SsidLength; i++) {
45 /* wifi, printable ascii code must be supported */
46 if (!((ssid->Ssid[i] >= 0x20) && (ssid->Ssid[i] <= 0x7e)))
52 static u8 do_join(struct _adapter *padapter)
54 struct list_head *plist, *phead;
56 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
57 struct __queue *queue = &(pmlmepriv->scanned_queue);
59 phead = &queue->queue;
61 pmlmepriv->cur_network.join_res = -2;
62 pmlmepriv->fw_state |= _FW_UNDER_LINKING;
63 pmlmepriv->pscanned = plist;
64 pmlmepriv->to_join = true;
66 /* adhoc mode will start with an empty queue, but skip checking */
67 if (!check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) &&
68 list_empty(&queue->queue)) {
69 if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
70 pmlmepriv->fw_state ^= _FW_UNDER_LINKING;
71 /* when set_ssid/set_bssid for do_join(), but scanning queue
72 * is empty we try to issue sitesurvey firstly
74 if (!pmlmepriv->sitesurveyctrl.traffic_busy)
75 r8712_sitesurvey_cmd(padapter, &pmlmepriv->assoc_ssid);
80 ret = r8712_select_and_join_from_scan(pmlmepriv);
82 mod_timer(&pmlmepriv->assoc_timer,
83 jiffies + msecs_to_jiffies(MAX_JOIN_TIMEOUT));
85 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
86 /* submit r8712_createbss_cmd to change to an
87 * ADHOC_MASTER pmlmepriv->lock has been
88 * acquired by caller...
90 struct wlan_bssid_ex *pdev_network =
91 &(padapter->registrypriv.dev_network);
92 pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
93 pibss = padapter->registrypriv.dev_network.
95 memcpy(&pdev_network->Ssid,
96 &pmlmepriv->assoc_ssid,
97 sizeof(struct ndis_802_11_ssid));
98 r8712_update_registrypriv_dev_network(padapter);
99 r8712_generate_random_ibss(pibss);
100 if (r8712_createbss_cmd(padapter) != _SUCCESS)
102 pmlmepriv->to_join = false;
104 /* can't associate ; reset under-linking */
105 if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
106 pmlmepriv->fw_state ^=
108 /* when set_ssid/set_bssid for do_join(), but
109 * there are no desired bss in scanning queue
110 * we try to issue sitesurvey first
112 if (!pmlmepriv->sitesurveyctrl.traffic_busy)
113 r8712_sitesurvey_cmd(padapter,
114 &pmlmepriv->assoc_ssid);
121 u8 r8712_set_802_11_bssid(struct _adapter *padapter, u8 *bssid)
125 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
127 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid)) {
131 spin_lock_irqsave(&pmlmepriv->lock, irqL);
132 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY |
133 _FW_UNDER_LINKING)) {
134 status = check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
135 goto _Abort_Set_BSSID;
137 if (check_fwstate(pmlmepriv,
138 _FW_LINKED | WIFI_ADHOC_MASTER_STATE)) {
139 if (!memcmp(&pmlmepriv->cur_network.network.MacAddress, bssid,
141 if (!check_fwstate(pmlmepriv, WIFI_STATION_STATE))
143 * WIFI_ADHOC_MASTER_STATE
145 goto _Abort_Set_BSSID;
147 r8712_disassoc_cmd(padapter);
148 if (check_fwstate(pmlmepriv, _FW_LINKED))
149 r8712_ind_disconnect(padapter);
150 r8712_free_assoc_resources(padapter);
151 if ((check_fwstate(pmlmepriv,
152 WIFI_ADHOC_MASTER_STATE))) {
153 _clr_fwstate_(pmlmepriv,
154 WIFI_ADHOC_MASTER_STATE);
155 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
159 memcpy(&pmlmepriv->assoc_bssid, bssid, ETH_ALEN);
160 pmlmepriv->assoc_by_bssid = true;
161 status = do_join(padapter);
165 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
169 void r8712_set_802_11_ssid(struct _adapter *padapter,
170 struct ndis_802_11_ssid *ssid)
173 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
174 struct wlan_network *pnetwork = &pmlmepriv->cur_network;
176 if (!padapter->hw_init_completed)
178 spin_lock_irqsave(&pmlmepriv->lock, irqL);
179 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY | _FW_UNDER_LINKING)) {
180 check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
181 goto _Abort_Set_SSID;
183 if (check_fwstate(pmlmepriv, _FW_LINKED | WIFI_ADHOC_MASTER_STATE)) {
184 if ((pmlmepriv->assoc_ssid.SsidLength == ssid->SsidLength) &&
185 (!memcmp(&pmlmepriv->assoc_ssid.Ssid, ssid->Ssid,
186 ssid->SsidLength))) {
187 if (!check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
188 if (!r8712_is_same_ibss(padapter,
190 /* if in WIFI_ADHOC_MASTER_STATE or
191 * WIFI_ADHOC_STATE, create bss or
194 r8712_disassoc_cmd(padapter);
195 if (check_fwstate(pmlmepriv,
197 r8712_ind_disconnect(padapter);
198 r8712_free_assoc_resources(padapter);
199 if (check_fwstate(pmlmepriv,
200 WIFI_ADHOC_MASTER_STATE)) {
201 _clr_fwstate_(pmlmepriv,
202 WIFI_ADHOC_MASTER_STATE);
203 set_fwstate(pmlmepriv,
208 * WIFI_ADHOC_MASTER_STATE
210 goto _Abort_Set_SSID;
214 r8712_disassoc_cmd(padapter);
215 if (check_fwstate(pmlmepriv, _FW_LINKED))
216 r8712_ind_disconnect(padapter);
217 r8712_free_assoc_resources(padapter);
218 if (check_fwstate(pmlmepriv,
219 WIFI_ADHOC_MASTER_STATE)) {
220 _clr_fwstate_(pmlmepriv,
221 WIFI_ADHOC_MASTER_STATE);
222 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
226 if (padapter->securitypriv.btkip_countermeasure)
227 goto _Abort_Set_SSID;
228 if (!validate_ssid(ssid))
229 goto _Abort_Set_SSID;
230 memcpy(&pmlmepriv->assoc_ssid, ssid, sizeof(struct ndis_802_11_ssid));
231 pmlmepriv->assoc_by_bssid = false;
236 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
239 void r8712_set_802_11_infrastructure_mode(struct _adapter *padapter,
240 enum NDIS_802_11_NETWORK_INFRASTRUCTURE networktype)
243 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
244 struct wlan_network *cur_network = &pmlmepriv->cur_network;
245 enum NDIS_802_11_NETWORK_INFRASTRUCTURE *pold_state =
246 &(cur_network->network.InfrastructureMode);
248 if (*pold_state != networktype) {
249 spin_lock_irqsave(&pmlmepriv->lock, irqL);
250 if (check_fwstate(pmlmepriv, _FW_LINKED) ||
251 (*pold_state == Ndis802_11IBSS))
252 r8712_disassoc_cmd(padapter);
253 if (check_fwstate(pmlmepriv,
254 _FW_LINKED | WIFI_ADHOC_MASTER_STATE))
255 r8712_free_assoc_resources(padapter);
256 if (check_fwstate(pmlmepriv, _FW_LINKED) ||
257 (*pold_state == Ndis802_11Infrastructure) ||
258 (*pold_state == Ndis802_11IBSS)) {
259 /* will clr Linked_state before this function,
260 * we must have checked whether issue dis-assoc_cmd or
263 r8712_ind_disconnect(padapter);
265 *pold_state = networktype;
266 /* clear WIFI_STATION_STATE; WIFI_AP_STATE; WIFI_ADHOC_STATE;
267 * WIFI_ADHOC_MASTER_STATE
269 _clr_fwstate_(pmlmepriv, WIFI_STATION_STATE | WIFI_AP_STATE |
270 WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE);
271 switch (networktype) {
273 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
275 case Ndis802_11Infrastructure:
276 set_fwstate(pmlmepriv, WIFI_STATION_STATE);
278 case Ndis802_11APMode:
279 set_fwstate(pmlmepriv, WIFI_AP_STATE);
281 case Ndis802_11AutoUnknown:
282 case Ndis802_11InfrastructureMax:
285 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
289 u8 r8712_set_802_11_disassociate(struct _adapter *padapter)
292 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
294 spin_lock_irqsave(&pmlmepriv->lock, irqL);
295 if (check_fwstate(pmlmepriv, _FW_LINKED)) {
296 r8712_disassoc_cmd(padapter);
297 r8712_ind_disconnect(padapter);
298 r8712_free_assoc_resources(padapter);
300 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
304 u8 r8712_set_802_11_bssid_list_scan(struct _adapter *padapter)
306 struct mlme_priv *pmlmepriv = NULL;
312 pmlmepriv = &padapter->mlmepriv;
313 if (!padapter->hw_init_completed)
315 spin_lock_irqsave(&pmlmepriv->lock, irqL);
316 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY | _FW_UNDER_LINKING) ||
317 pmlmepriv->sitesurveyctrl.traffic_busy) {
318 /* Scan or linking is in progress, do nothing. */
319 ret = (u8)check_fwstate(pmlmepriv, _FW_UNDER_SURVEY);
321 r8712_free_network_queue(padapter);
322 ret = r8712_sitesurvey_cmd(padapter, NULL);
324 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
328 u8 r8712_set_802_11_authentication_mode(struct _adapter *padapter,
329 enum NDIS_802_11_AUTHENTICATION_MODE authmode)
331 struct security_priv *psecuritypriv = &padapter->securitypriv;
334 psecuritypriv->ndisauthtype = authmode;
335 if (psecuritypriv->ndisauthtype > 3)
336 psecuritypriv->AuthAlgrthm = 2; /* 802.1x */
337 if (r8712_set_auth(padapter, psecuritypriv) == _SUCCESS)
344 u8 r8712_set_802_11_add_wep(struct _adapter *padapter,
345 struct NDIS_802_11_WEP *wep)
348 struct security_priv *psecuritypriv = &padapter->securitypriv;
350 keyid = wep->KeyIndex & 0x3fffffff;
351 if (keyid >= WEP_KEYS)
353 switch (wep->KeyLength) {
355 psecuritypriv->PrivacyAlgrthm = _WEP40_;
358 psecuritypriv->PrivacyAlgrthm = _WEP104_;
361 psecuritypriv->PrivacyAlgrthm = _NO_PRIVACY_;
364 memcpy(psecuritypriv->DefKey[keyid].skey, &wep->KeyMaterial,
366 psecuritypriv->DefKeylen[keyid] = wep->KeyLength;
367 psecuritypriv->PrivacyKeyIndex = keyid;
368 if (r8712_set_key(padapter, psecuritypriv, keyid) == _FAIL)