linux/drivers/staging/rtl8712/rtl871x_ioctl_set.c
<<
>>
Prefs
   1/******************************************************************************
   2 * rtl871x_ioctl_set.c
   3 *
   4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
   5 * Linux device driver for RTL8192SU
   6 *
   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.
  10 *
  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
  14 * more details.
  15 *
  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
  19 *
  20 * Modifications for inclusion into the Linux staging tree are
  21 * Copyright(c) 2010 Larry Finger. All rights reserved.
  22 *
  23 * Contact information:
  24 * WLAN FAE <wlanfae@realtek.com>
  25 * Larry Finger <Larry.Finger@lwfinger.net>
  26 *
  27 ******************************************************************************/
  28
  29#define _RTL871X_IOCTL_SET_C_
  30
  31#include "osdep_service.h"
  32#include "drv_types.h"
  33#include "rtl871x_ioctl_set.h"
  34#include "usb_osintf.h"
  35#include "usb_ops.h"
  36
  37#define IS_MAC_ADDRESS_BROADCAST(addr) \
  38( \
  39        ((addr[0] == 0xff) && (addr[1] == 0xff) && \
  40         (addr[2] == 0xff) && (addr[3] == 0xff) && \
  41         (addr[4] == 0xff) && (addr[5] == 0xff)) ? true : false \
  42)
  43
  44static u8 validate_ssid(struct ndis_802_11_ssid *ssid)
  45{
  46        u8 i;
  47
  48        if (ssid->SsidLength > 32)
  49                return false;
  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)))
  53                        return false;
  54        }
  55        return true;
  56}
  57
  58static u8 do_join(struct _adapter *padapter)
  59{
  60        struct list_head *plist, *phead;
  61        u8 *pibss = NULL;
  62        struct  mlme_priv       *pmlmepriv = &(padapter->mlmepriv);
  63        struct  __queue *queue  = &(pmlmepriv->scanned_queue);
  64
  65        phead = &queue->queue;
  66        plist = phead->next;
  67        pmlmepriv->cur_network.join_res = -2;
  68        pmlmepriv->fw_state |= _FW_UNDER_LINKING;
  69        pmlmepriv->pscanned = plist;
  70        pmlmepriv->to_join = true;
  71
  72        /* adhoc mode will start with an empty queue, but skip checking */
  73        if (!check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) &&
  74            list_empty(&queue->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
  79                 */
  80                if (!pmlmepriv->sitesurveyctrl.traffic_busy)
  81                        r8712_sitesurvey_cmd(padapter, &pmlmepriv->assoc_ssid);
  82                return true;
  83        } else {
  84                int ret;
  85
  86                ret = r8712_select_and_join_from_scan(pmlmepriv);
  87                if (ret == _SUCCESS)
  88                        mod_timer(&pmlmepriv->assoc_timer,
  89                                  jiffies + msecs_to_jiffies(MAX_JOIN_TIMEOUT));
  90                else {
  91                        if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
  92                                /* submit r8712_createbss_cmd to change to an
  93                                 * ADHOC_MASTER pmlmepriv->lock has been
  94                                 * acquired by caller...
  95                                 */
  96                                struct wlan_bssid_ex *pdev_network =
  97                                        &(padapter->registrypriv.dev_network);
  98                                pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
  99                                pibss = padapter->registrypriv.dev_network.
 100                                        MacAddress;
 101                                memcpy(&pdev_network->Ssid,
 102                                        &pmlmepriv->assoc_ssid,
 103                                        sizeof(struct ndis_802_11_ssid));
 104                                r8712_update_registrypriv_dev_network(padapter);
 105                                r8712_generate_random_ibss(pibss);
 106                                if (r8712_createbss_cmd(padapter) != _SUCCESS)
 107                                        return false;
 108                                pmlmepriv->to_join = false;
 109                        } else {
 110                                /* can't associate ; reset under-linking */
 111                                if (pmlmepriv->fw_state & _FW_UNDER_LINKING)
 112                                        pmlmepriv->fw_state ^=
 113                                                             _FW_UNDER_LINKING;
 114                                /* when set_ssid/set_bssid for do_join(), but
 115                                 * there are no desired bss in scanning queue
 116                                 * we try to issue sitesurvey first
 117                                 */
 118                                if (!pmlmepriv->sitesurveyctrl.traffic_busy)
 119                                        r8712_sitesurvey_cmd(padapter,
 120                                                       &pmlmepriv->assoc_ssid);
 121                        }
 122                }
 123        }
 124        return true;
 125}
 126
 127u8 r8712_set_802_11_bssid(struct _adapter *padapter, u8 *bssid)
 128{
 129        unsigned long irqL;
 130        u8 status = true;
 131        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 132
 133        if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid)) {
 134                status = false;
 135                return status;
 136        }
 137        spin_lock_irqsave(&pmlmepriv->lock, irqL);
 138        if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY |
 139            _FW_UNDER_LINKING) == true) {
 140                status = check_fwstate(pmlmepriv, _FW_UNDER_LINKING);
 141                goto _Abort_Set_BSSID;
 142        }
 143        if (check_fwstate(pmlmepriv,
 144            _FW_LINKED|WIFI_ADHOC_MASTER_STATE) == true) {
 145                if (!memcmp(&pmlmepriv->cur_network.network.MacAddress, bssid,
 146                    ETH_ALEN)) {
 147                        if (!check_fwstate(pmlmepriv, WIFI_STATION_STATE))
 148                                goto _Abort_Set_BSSID; /* driver is in
 149                                                * WIFI_ADHOC_MASTER_STATE */
 150                } else {
 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);
 160                        }
 161                }
 162        }
 163        memcpy(&pmlmepriv->assoc_bssid, bssid, ETH_ALEN);
 164        pmlmepriv->assoc_by_bssid = true;
 165        status = do_join(padapter);
 166        goto done;
 167_Abort_Set_BSSID:
 168done:
 169        spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
 170        return status;
 171}
 172
 173void r8712_set_802_11_ssid(struct _adapter *padapter,
 174                           struct ndis_802_11_ssid *ssid)
 175{
 176        unsigned long irqL;
 177        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 178        struct wlan_network *pnetwork = &pmlmepriv->cur_network;
 179
 180        if (!padapter->hw_init_completed)
 181                return;
 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;
 186        }
 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, WIFI_STATION_STATE)) {
 192                                if (!r8712_is_same_ibss(padapter,
 193                                     pnetwork)) {
 194                                        /* if in WIFI_ADHOC_MASTER_STATE or
 195                                         *  WIFI_ADHOC_STATE, create bss or
 196                                         * rejoin again
 197                                         */
 198                                        r8712_disassoc_cmd(padapter);
 199                                        if (check_fwstate(pmlmepriv,
 200                                            _FW_LINKED) == true)
 201                                                r8712_ind_disconnect(padapter);
 202                                        r8712_free_assoc_resources(padapter);
 203                                        if (check_fwstate(pmlmepriv,
 204                                             WIFI_ADHOC_MASTER_STATE)) {
 205                                                _clr_fwstate_(pmlmepriv,
 206                                                    WIFI_ADHOC_MASTER_STATE);
 207                                                set_fwstate(pmlmepriv,
 208                                                            WIFI_ADHOC_STATE);
 209                                        }
 210                                } else
 211                                        goto _Abort_Set_SSID; /* driver is in
 212                                                  * WIFI_ADHOC_MASTER_STATE */
 213                        }
 214                } else {
 215                        r8712_disassoc_cmd(padapter);
 216                        if (check_fwstate(pmlmepriv, _FW_LINKED) == true)
 217                                r8712_ind_disconnect(padapter);
 218                        r8712_free_assoc_resources(padapter);
 219                        if (check_fwstate(pmlmepriv,
 220                            WIFI_ADHOC_MASTER_STATE) == true) {
 221                                _clr_fwstate_(pmlmepriv,
 222                                              WIFI_ADHOC_MASTER_STATE);
 223                                set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
 224                        }
 225                }
 226        }
 227        if (padapter->securitypriv.btkip_countermeasure == true)
 228                goto _Abort_Set_SSID;
 229        if (!validate_ssid(ssid))
 230                goto _Abort_Set_SSID;
 231        memcpy(&pmlmepriv->assoc_ssid, ssid, sizeof(struct ndis_802_11_ssid));
 232        pmlmepriv->assoc_by_bssid = false;
 233        do_join(padapter);
 234        goto done;
 235_Abort_Set_SSID:
 236done:
 237        spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
 238}
 239
 240void r8712_set_802_11_infrastructure_mode(struct _adapter *padapter,
 241        enum NDIS_802_11_NETWORK_INFRASTRUCTURE networktype)
 242{
 243        unsigned long irqL;
 244        struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
 245        struct wlan_network     *cur_network = &pmlmepriv->cur_network;
 246        enum NDIS_802_11_NETWORK_INFRASTRUCTURE *pold_state =
 247                                &(cur_network->network.InfrastructureMode);
 248
 249        if (*pold_state != networktype) {
 250                spin_lock_irqsave(&pmlmepriv->lock, irqL);
 251                if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) ||
 252                    (*pold_state == Ndis802_11IBSS))
 253                        r8712_disassoc_cmd(padapter);
 254                if (check_fwstate(pmlmepriv,
 255                    _FW_LINKED|WIFI_ADHOC_MASTER_STATE) == true)
 256                        r8712_free_assoc_resources(padapter);
 257                if ((check_fwstate(pmlmepriv, _FW_LINKED) == true) ||
 258                    (*pold_state == Ndis802_11Infrastructure) ||
 259                    (*pold_state == Ndis802_11IBSS)) {
 260                        /* will clr Linked_state before this function,
 261                         * we must have checked whether issue dis-assoc_cmd or
 262                         * not */
 263                        r8712_ind_disconnect(padapter);
 264                }
 265                *pold_state = networktype;
 266                /* clear WIFI_STATION_STATE; WIFI_AP_STATE; WIFI_ADHOC_STATE;
 267                 * WIFI_ADHOC_MASTER_STATE */
 268                _clr_fwstate_(pmlmepriv, WIFI_STATION_STATE | WIFI_AP_STATE |
 269                              WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE);
 270                switch (networktype) {
 271                case Ndis802_11IBSS:
 272                        set_fwstate(pmlmepriv, WIFI_ADHOC_STATE);
 273                        break;
 274                case Ndis802_11Infrastructure:
 275                        set_fwstate(pmlmepriv, WIFI_STATION_STATE);
 276                        break;
 277                case Ndis802_11APMode:
 278                        set_fwstate(pmlmepriv, WIFI_AP_STATE);
 279                        break;
 280                case Ndis802_11AutoUnknown:
 281                case Ndis802_11InfrastructureMax:
 282                        break;
 283                }
 284                spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
 285        }
 286}
 287
 288u8 r8712_set_802_11_disassociate(struct _adapter *padapter)
 289{
 290        unsigned long irqL;
 291        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 292
 293        spin_lock_irqsave(&pmlmepriv->lock, irqL);
 294        if (check_fwstate(pmlmepriv, _FW_LINKED) == true) {
 295                r8712_disassoc_cmd(padapter);
 296                r8712_ind_disconnect(padapter);
 297                r8712_free_assoc_resources(padapter);
 298        }
 299        spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
 300        return true;
 301}
 302
 303u8 r8712_set_802_11_bssid_list_scan(struct _adapter *padapter)
 304{
 305        struct mlme_priv *pmlmepriv = NULL;
 306        unsigned long irqL;
 307        u8 ret = true;
 308
 309        if (!padapter)
 310                return false;
 311        pmlmepriv = &padapter->mlmepriv;
 312        if (!padapter->hw_init_completed)
 313                return false;
 314        spin_lock_irqsave(&pmlmepriv->lock, irqL);
 315        if ((check_fwstate(pmlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING)) ||
 316            (pmlmepriv->sitesurveyctrl.traffic_busy == true)) {
 317                /* Scan or linking is in progress, do nothing. */
 318                ret = (u8)check_fwstate(pmlmepriv, _FW_UNDER_SURVEY);
 319        } else {
 320                r8712_free_network_queue(padapter);
 321                ret = r8712_sitesurvey_cmd(padapter, NULL);
 322        }
 323        spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
 324        return ret;
 325}
 326
 327u8 r8712_set_802_11_authentication_mode(struct _adapter *padapter,
 328                                enum NDIS_802_11_AUTHENTICATION_MODE authmode)
 329{
 330        struct security_priv *psecuritypriv = &padapter->securitypriv;
 331        u8 ret;
 332
 333        psecuritypriv->ndisauthtype = authmode;
 334        if (psecuritypriv->ndisauthtype > 3)
 335                psecuritypriv->AuthAlgrthm = 2; /* 802.1x */
 336        if (r8712_set_auth(padapter, psecuritypriv) == _SUCCESS)
 337                ret = true;
 338        else
 339                ret = false;
 340        return ret;
 341}
 342
 343u8 r8712_set_802_11_add_wep(struct _adapter *padapter,
 344                            struct NDIS_802_11_WEP *wep)
 345{
 346        sint    keyid;
 347        struct security_priv *psecuritypriv = &padapter->securitypriv;
 348
 349        keyid = wep->KeyIndex & 0x3fffffff;
 350        if (keyid >= WEP_KEYS)
 351                return false;
 352        switch (wep->KeyLength) {
 353        case 5:
 354                psecuritypriv->PrivacyAlgrthm = _WEP40_;
 355                break;
 356        case 13:
 357                psecuritypriv->PrivacyAlgrthm = _WEP104_;
 358                break;
 359        default:
 360                psecuritypriv->PrivacyAlgrthm = _NO_PRIVACY_;
 361                break;
 362        }
 363        memcpy(psecuritypriv->DefKey[keyid].skey, &wep->KeyMaterial,
 364                wep->KeyLength);
 365        psecuritypriv->DefKeylen[keyid] = wep->KeyLength;
 366        psecuritypriv->PrivacyKeyIndex = keyid;
 367        if (r8712_set_key(padapter, psecuritypriv, keyid) == _FAIL)
 368                return false;
 369        return _SUCCESS;
 370}
 371