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"
37 #define IS_MAC_ADDRESS_BROADCAST(addr) \
39 ((addr[0] == 0xff) && (addr[1] == 0xff) && \
40 (addr[2] == 0xff) && (addr[3] == 0xff) && \
41 (addr[4] == 0xff) && (addr[5] == 0xff)) ? true : false \
44 static u8 validate_ssid(struct ndis_802_11_ssid *ssid)
48 if (ssid->SsidLength > 32)
50 for (i = 0; i < ssid->SsidLength; i++) {
51 /* wifi, printable ascii code must be supported */
52 if (!((ssid->Ssid[i] >= 0x20) && (ssid->Ssid[i] <= 0x7e)))
58 static u8 do_join(struct _adapter *padapter)
60 struct list_head *plist, *phead;
62 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
63 struct __queue *queue = &(pmlmepriv->scanned_queue);
65 phead = get_list_head(queue);
66 plist = get_next(phead);
67 pmlmepriv->cur_network.join_res = -2;
68 pmlmepriv->fw_state |= _FW_UNDER_LINKING;
69 pmlmepriv->pscanned = plist;
70 pmlmepriv->to_join = true;
72 /* adhoc mode will start with an empty queue, but skip checking */
73 if (!check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) &&
74 _queue_empty(queue)) {
75 if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
76 pmlmepriv->fw_state ^= _FW_UNDER_LINKING;
77 /* when set_ssid/set_bssid for do_join(), but scanning queue
78 * is empty we try to issue sitesurvey firstly
80 if (pmlmepriv->sitesurveyctrl.traffic_busy == false)
81 r8712_sitesurvey_cmd(padapter, &pmlmepriv->assoc_ssid);
86 ret = r8712_select_and_join_from_scan(pmlmepriv);
88 _set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
90 if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
91 /* submit r8712_createbss_cmd to change to an
92 * ADHOC_MASTER pmlmepriv->lock has been
93 * acquired by caller...
95 struct wlan_bssid_ex *pdev_network =
96 &(padapter->registrypriv.dev_network);
97 pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
98 pibss = padapter->registrypriv.dev_network.
100 memcpy(&pdev_network->Ssid,
101 &pmlmepriv->assoc_ssid,
102 sizeof(struct ndis_802_11_ssid));
103 r8712_update_registrypriv_dev_network(padapter);
104 r8712_generate_random_ibss(pibss);
105 if (r8712_createbss_cmd(padapter) != _SUCCESS)
107 pmlmepriv->to_join = false;
109 /* can't associate ; reset under-linking */
110 if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
111 pmlmepriv->fw_state ^=
113 /* when set_ssid/set_bssid for do_join(), but
114 * there are no desired bss in scanning queue
115 * we try to issue sitesurvey first
117 if (!pmlmepriv->sitesurveyctrl.traffic_busy)
118 r8712_sitesurvey_cmd(padapter,
119 &pmlmepriv->assoc_ssid);
126 u8 r8712_set_802_11_bssid(struct _adapter *padapter, u8 *bssid)
130 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
132 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid)) {
136 spin_lock_irqsave(&pmlmepriv->lock, irqL);
137 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY |
138 _FW_UNDER_LINKING) == true) {
139 status = check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
140 goto _Abort_Set_BSSID;
142 if (check_fwstate(pmlmepriv,
143 _FW_LINKED|WIFI_ADHOC_MASTER_STATE) == true) {
144 if (!memcmp(&pmlmepriv->cur_network.network.MacAddress, bssid,
146 if (check_fwstate(pmlmepriv,
147 WIFI_STATION_STATE) == false)
148 goto _Abort_Set_BSSID; /* driver is in
149 * WIFI_ADHOC_MASTER_STATE */
151 r8712_disassoc_cmd(padapter);
152 if (check_fwstate(pmlmepriv, _FW_LINKED) == true)
153 r8712_ind_disconnect(padapter);
154 r8712_free_assoc_resources(padapter);
155 if ((check_fwstate(pmlmepriv,
156 WIFI_ADHOC_MASTER_STATE))) {
157 _clr_fwstate_(pmlmepriv,
158 WIFI_ADHOC_MASTER_STATE);
159 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
163 memcpy(&pmlmepriv->assoc_bssid, bssid, ETH_ALEN);
164 pmlmepriv->assoc_by_bssid = true;
165 status = do_join(padapter);
169 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
173 void r8712_set_802_11_ssid(struct _adapter *padapter,
174 struct ndis_802_11_ssid *ssid)
177 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
178 struct wlan_network *pnetwork = &pmlmepriv->cur_network;
180 if (padapter->hw_init_completed == false)
182 spin_lock_irqsave(&pmlmepriv->lock, irqL);
183 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING)) {
184 check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
185 goto _Abort_Set_SSID;
187 if (check_fwstate(pmlmepriv, _FW_LINKED|WIFI_ADHOC_MASTER_STATE)) {
188 if ((pmlmepriv->assoc_ssid.SsidLength == ssid->SsidLength) &&
189 (!memcmp(&pmlmepriv->assoc_ssid.Ssid, ssid->Ssid,
190 ssid->SsidLength))) {
191 if ((check_fwstate(pmlmepriv,
192 WIFI_STATION_STATE) == false)) {
193 if (r8712_is_same_ibss(padapter,
194 pnetwork) == false) {
195 /* if in WIFI_ADHOC_MASTER_STATE or
196 * WIFI_ADHOC_STATE, create bss or
199 r8712_disassoc_cmd(padapter);
200 if (check_fwstate(pmlmepriv,
202 r8712_ind_disconnect(padapter);
203 r8712_free_assoc_resources(padapter);
204 if (check_fwstate(pmlmepriv,
205 WIFI_ADHOC_MASTER_STATE)) {
206 _clr_fwstate_(pmlmepriv,
207 WIFI_ADHOC_MASTER_STATE);
208 set_fwstate(pmlmepriv,
212 goto _Abort_Set_SSID; /* driver is in
213 * WIFI_ADHOC_MASTER_STATE */
216 r8712_disassoc_cmd(padapter);
217 if (check_fwstate(pmlmepriv, _FW_LINKED) == true)
218 r8712_ind_disconnect(padapter);
219 r8712_free_assoc_resources(padapter);
220 if (check_fwstate(pmlmepriv,
221 WIFI_ADHOC_MASTER_STATE) == true) {
222 _clr_fwstate_(pmlmepriv,
223 WIFI_ADHOC_MASTER_STATE);
224 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
228 if (padapter->securitypriv.btkip_countermeasure == true)
229 goto _Abort_Set_SSID;
230 if (validate_ssid(ssid) == false)
231 goto _Abort_Set_SSID;
232 memcpy(&pmlmepriv->assoc_ssid, ssid, sizeof(struct ndis_802_11_ssid));
233 pmlmepriv->assoc_by_bssid = false;
238 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
241 void r8712_set_802_11_infrastructure_mode(struct _adapter *padapter,
242 enum NDIS_802_11_NETWORK_INFRASTRUCTURE networktype)
245 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
246 struct wlan_network *cur_network = &pmlmepriv->cur_network;
247 enum NDIS_802_11_NETWORK_INFRASTRUCTURE *pold_state =
248 &(cur_network->network.InfrastructureMode);
250 if (*pold_state != networktype) {
251 spin_lock_irqsave(&pmlmepriv->lock, irqL);
252 if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) ||
253 (*pold_state == Ndis802_11IBSS))
254 r8712_disassoc_cmd(padapter);
255 if (check_fwstate(pmlmepriv,
256 _FW_LINKED|WIFI_ADHOC_MASTER_STATE) == true)
257 r8712_free_assoc_resources(padapter);
258 if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) ||
259 (*pold_state == Ndis802_11Infrastructure) ||
260 (*pold_state == Ndis802_11IBSS)) {
261 /* will clr Linked_state before this function,
262 * we must have checked whether issue dis-assoc_cmd or
264 r8712_ind_disconnect(padapter);
266 *pold_state = networktype;
267 /* clear WIFI_STATION_STATE; WIFI_AP_STATE; WIFI_ADHOC_STATE;
268 * WIFI_ADHOC_MASTER_STATE */
269 _clr_fwstate_(pmlmepriv, WIFI_STATION_STATE | WIFI_AP_STATE |
270 WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE |
272 switch (networktype) {
274 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
276 case Ndis802_11Infrastructure:
277 set_fwstate(pmlmepriv, WIFI_STATION_STATE);
279 case Ndis802_11APMode:
280 set_fwstate(pmlmepriv, WIFI_AP_STATE);
282 case Ndis802_11AutoUnknown:
283 case Ndis802_11InfrastructureMax:
286 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
290 u8 r8712_set_802_11_disassociate(struct _adapter *padapter)
293 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
295 spin_lock_irqsave(&pmlmepriv->lock, irqL);
296 if (check_fwstate(pmlmepriv, _FW_LINKED) == true) {
297 r8712_disassoc_cmd(padapter);
298 r8712_ind_disconnect(padapter);
299 r8712_free_assoc_resources(padapter);
301 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
305 u8 r8712_set_802_11_bssid_list_scan(struct _adapter *padapter)
307 struct mlme_priv *pmlmepriv = NULL;
311 if (padapter == NULL)
313 pmlmepriv = &padapter->mlmepriv;
314 if (padapter->hw_init_completed == false)
316 spin_lock_irqsave(&pmlmepriv->lock, irqL);
317 if ((check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING)) ||
318 (pmlmepriv->sitesurveyctrl.traffic_busy == true)) {
319 /* Scan or linking is in progress, do nothing. */
320 ret = (u8)check_fwstate(pmlmepriv, _FW_UNDER_SURVEY);
322 r8712_free_network_queue(padapter);
323 ret = r8712_sitesurvey_cmd(padapter, NULL);
325 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
329 u8 r8712_set_802_11_authentication_mode(struct _adapter *padapter,
330 enum NDIS_802_11_AUTHENTICATION_MODE authmode)
332 struct security_priv *psecuritypriv = &padapter->securitypriv;
335 psecuritypriv->ndisauthtype = authmode;
336 if (psecuritypriv->ndisauthtype > 3)
337 psecuritypriv->AuthAlgrthm = 2; /* 802.1x */
338 if (r8712_set_auth(padapter, psecuritypriv) == _SUCCESS)
345 u8 r8712_set_802_11_add_wep(struct _adapter *padapter,
346 struct NDIS_802_11_WEP *wep)
351 struct security_priv *psecuritypriv = &padapter->securitypriv;
353 bdefaultkey = (wep->KeyIndex & 0x40000000) > 0 ? false : true;
354 btransmitkey = (wep->KeyIndex & 0x80000000) > 0 ? true : false;
355 keyid = wep->KeyIndex & 0x3fffffff;
356 if (keyid >= WEP_KEYS)
358 switch (wep->KeyLength) {
360 psecuritypriv->PrivacyAlgrthm = _WEP40_;
363 psecuritypriv->PrivacyAlgrthm = _WEP104_;
366 psecuritypriv->PrivacyAlgrthm = _NO_PRIVACY_;
369 memcpy(psecuritypriv->DefKey[keyid].skey, &wep->KeyMaterial,
371 psecuritypriv->DefKeylen[keyid] = wep->KeyLength;
372 psecuritypriv->PrivacyKeyIndex = keyid;
373 if (r8712_set_key(padapter, psecuritypriv, keyid) == _FAIL)