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 memset(&pdev_network->Ssid, 0,
101 sizeof(struct ndis_802_11_ssid));
102 memcpy(&pdev_network->Ssid,
103 &pmlmepriv->assoc_ssid,
104 sizeof(struct ndis_802_11_ssid));
105 r8712_update_registrypriv_dev_network(padapter);
106 r8712_generate_random_ibss(pibss);
107 if (r8712_createbss_cmd(padapter) != _SUCCESS)
109 pmlmepriv->to_join = false;
111 /* can't associate ; reset under-linking */
112 if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
113 pmlmepriv->fw_state ^=
115 /* when set_ssid/set_bssid for do_join(), but
116 * there are no desired bss in scanning queue
117 * we try to issue sitesurvey first
119 if (!pmlmepriv->sitesurveyctrl.traffic_busy)
120 r8712_sitesurvey_cmd(padapter,
121 &pmlmepriv->assoc_ssid);
128 u8 r8712_set_802_11_bssid(struct _adapter *padapter, u8 *bssid)
132 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
134 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid)) {
138 spin_lock_irqsave(&pmlmepriv->lock, irqL);
139 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY |
140 _FW_UNDER_LINKING) == true) {
141 status = check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
142 goto _Abort_Set_BSSID;
144 if (check_fwstate(pmlmepriv,
145 _FW_LINKED|WIFI_ADHOC_MASTER_STATE) == true) {
146 if (!memcmp(&pmlmepriv->cur_network.network.MacAddress, bssid,
148 if (check_fwstate(pmlmepriv,
149 WIFI_STATION_STATE) == false)
150 goto _Abort_Set_BSSID; /* driver is in
151 * WIFI_ADHOC_MASTER_STATE */
153 r8712_disassoc_cmd(padapter);
154 if (check_fwstate(pmlmepriv, _FW_LINKED) == true)
155 r8712_ind_disconnect(padapter);
156 r8712_free_assoc_resources(padapter);
157 if ((check_fwstate(pmlmepriv,
158 WIFI_ADHOC_MASTER_STATE))) {
159 _clr_fwstate_(pmlmepriv,
160 WIFI_ADHOC_MASTER_STATE);
161 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
165 memcpy(&pmlmepriv->assoc_bssid, bssid, ETH_ALEN);
166 pmlmepriv->assoc_by_bssid = true;
167 status = do_join(padapter);
171 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
175 void r8712_set_802_11_ssid(struct _adapter *padapter,
176 struct ndis_802_11_ssid *ssid)
179 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
180 struct wlan_network *pnetwork = &pmlmepriv->cur_network;
182 if (padapter->hw_init_completed == false)
184 spin_lock_irqsave(&pmlmepriv->lock, irqL);
185 if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING)) {
186 check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
187 goto _Abort_Set_SSID;
189 if (check_fwstate(pmlmepriv, _FW_LINKED|WIFI_ADHOC_MASTER_STATE)) {
190 if ((pmlmepriv->assoc_ssid.SsidLength == ssid->SsidLength) &&
191 (!memcmp(&pmlmepriv->assoc_ssid.Ssid, ssid->Ssid,
192 ssid->SsidLength))) {
193 if ((check_fwstate(pmlmepriv,
194 WIFI_STATION_STATE) == false)) {
195 if (r8712_is_same_ibss(padapter,
196 pnetwork) == false) {
197 /* if in WIFI_ADHOC_MASTER_STATE or
198 * WIFI_ADHOC_STATE, create bss or
201 r8712_disassoc_cmd(padapter);
202 if (check_fwstate(pmlmepriv,
204 r8712_ind_disconnect(padapter);
205 r8712_free_assoc_resources(padapter);
206 if (check_fwstate(pmlmepriv,
207 WIFI_ADHOC_MASTER_STATE)) {
208 _clr_fwstate_(pmlmepriv,
209 WIFI_ADHOC_MASTER_STATE);
210 set_fwstate(pmlmepriv,
214 goto _Abort_Set_SSID; /* driver is in
215 * WIFI_ADHOC_MASTER_STATE */
218 r8712_disassoc_cmd(padapter);
219 if (check_fwstate(pmlmepriv, _FW_LINKED) == true)
220 r8712_ind_disconnect(padapter);
221 r8712_free_assoc_resources(padapter);
222 if (check_fwstate(pmlmepriv,
223 WIFI_ADHOC_MASTER_STATE) == true) {
224 _clr_fwstate_(pmlmepriv,
225 WIFI_ADHOC_MASTER_STATE);
226 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
230 if (padapter->securitypriv.btkip_countermeasure == true)
231 goto _Abort_Set_SSID;
232 if (validate_ssid(ssid) == false)
233 goto _Abort_Set_SSID;
234 memcpy(&pmlmepriv->assoc_ssid, ssid, sizeof(struct ndis_802_11_ssid));
235 pmlmepriv->assoc_by_bssid = false;
240 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
243 void r8712_set_802_11_infrastructure_mode(struct _adapter *padapter,
244 enum NDIS_802_11_NETWORK_INFRASTRUCTURE networktype)
247 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
248 struct wlan_network *cur_network = &pmlmepriv->cur_network;
249 enum NDIS_802_11_NETWORK_INFRASTRUCTURE *pold_state =
250 &(cur_network->network.InfrastructureMode);
252 if (*pold_state != networktype) {
253 spin_lock_irqsave(&pmlmepriv->lock, irqL);
254 if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) ||
255 (*pold_state == Ndis802_11IBSS))
256 r8712_disassoc_cmd(padapter);
257 if (check_fwstate(pmlmepriv,
258 _FW_LINKED|WIFI_ADHOC_MASTER_STATE) == true)
259 r8712_free_assoc_resources(padapter);
260 if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) ||
261 (*pold_state == Ndis802_11Infrastructure) ||
262 (*pold_state == Ndis802_11IBSS)) {
263 /* will clr Linked_state before this function,
264 * we must have checked whether issue dis-assoc_cmd or
266 r8712_ind_disconnect(padapter);
268 *pold_state = networktype;
269 /* clear WIFI_STATION_STATE; WIFI_AP_STATE; WIFI_ADHOC_STATE;
270 * WIFI_ADHOC_MASTER_STATE */
271 _clr_fwstate_(pmlmepriv, WIFI_STATION_STATE | WIFI_AP_STATE |
272 WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE |
274 switch (networktype) {
276 set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
278 case Ndis802_11Infrastructure:
279 set_fwstate(pmlmepriv, WIFI_STATION_STATE);
281 case Ndis802_11APMode:
282 set_fwstate(pmlmepriv, WIFI_AP_STATE);
284 case Ndis802_11AutoUnknown:
285 case Ndis802_11InfrastructureMax:
288 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
292 u8 r8712_set_802_11_disassociate(struct _adapter *padapter)
295 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
297 spin_lock_irqsave(&pmlmepriv->lock, irqL);
298 if (check_fwstate(pmlmepriv, _FW_LINKED) == true) {
299 r8712_disassoc_cmd(padapter);
300 r8712_ind_disconnect(padapter);
301 r8712_free_assoc_resources(padapter);
303 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
307 u8 r8712_set_802_11_bssid_list_scan(struct _adapter *padapter)
309 struct mlme_priv *pmlmepriv = NULL;
313 if (padapter == NULL)
315 pmlmepriv = &padapter->mlmepriv;
316 if (padapter->hw_init_completed == false)
318 spin_lock_irqsave(&pmlmepriv->lock, irqL);
319 if ((check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING)) ||
320 (pmlmepriv->sitesurveyctrl.traffic_busy == true)) {
321 /* Scan or linking is in progress, do nothing. */
322 ret = (u8)check_fwstate(pmlmepriv, _FW_UNDER_SURVEY);
324 r8712_free_network_queue(padapter);
325 ret = r8712_sitesurvey_cmd(padapter, NULL);
327 spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
331 u8 r8712_set_802_11_authentication_mode(struct _adapter *padapter,
332 enum NDIS_802_11_AUTHENTICATION_MODE authmode)
334 struct security_priv *psecuritypriv = &padapter->securitypriv;
337 psecuritypriv->ndisauthtype = authmode;
338 if (psecuritypriv->ndisauthtype > 3)
339 psecuritypriv->AuthAlgrthm = 2; /* 802.1x */
340 if (r8712_set_auth(padapter, psecuritypriv) == _SUCCESS)
347 u8 r8712_set_802_11_add_wep(struct _adapter *padapter,
348 struct NDIS_802_11_WEP *wep)
353 struct security_priv *psecuritypriv = &padapter->securitypriv;
355 bdefaultkey = (wep->KeyIndex & 0x40000000) > 0 ? false : true;
356 btransmitkey = (wep->KeyIndex & 0x80000000) > 0 ? true : false;
357 keyid = wep->KeyIndex & 0x3fffffff;
358 if (keyid >= WEP_KEYS)
360 switch (wep->KeyLength) {
362 psecuritypriv->PrivacyAlgrthm = _WEP40_;
365 psecuritypriv->PrivacyAlgrthm = _WEP104_;
368 psecuritypriv->PrivacyAlgrthm = _NO_PRIVACY_;
371 memcpy(psecuritypriv->DefKey[keyid].skey, &wep->KeyMaterial,
373 psecuritypriv->DefKeylen[keyid] = wep->KeyLength;
374 psecuritypriv->PrivacyKeyIndex = keyid;
375 if (r8712_set_key(padapter, psecuritypriv, keyid) == _FAIL)