linux/drivers/staging/rtl8712/os_intfs.c
<<
>>
Prefs
   1/******************************************************************************
   2 * os_intfs.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 _OS_INTFS_C_
  30
  31#include <linux/module.h>
  32#include <linux/kthread.h>
  33#include <linux/firmware.h>
  34#include "osdep_service.h"
  35#include "drv_types.h"
  36#include "xmit_osdep.h"
  37#include "recv_osdep.h"
  38#include "rtl871x_ioctl.h"
  39#include "usb_osintf.h"
  40
  41MODULE_LICENSE("GPL");
  42MODULE_DESCRIPTION("rtl871x wireless lan driver");
  43MODULE_AUTHOR("Larry Finger");
  44
  45static char ifname[IFNAMSIZ] = "wlan%d";
  46
  47/* module param defaults */
  48static int chip_version = RTL8712_2ndCUT;
  49static int rfintfs = HWPI;
  50static int lbkmode = RTL8712_AIR_TRX;
  51static int hci = RTL8712_USB;
  52static int ampdu_enable = 1;/*for enable tx_ampdu*/
  53
  54/* The video_mode variable is for video mode.*/
  55/* It may be specify when inserting module with video_mode=1 parameter.*/
  56static int video_mode = 1;   /* enable video mode*/
  57
  58/*Ndis802_11Infrastructure; infra, ad-hoc, auto*/
  59static int network_mode = Ndis802_11IBSS;
  60static int channel = 1;/*ad-hoc support requirement*/
  61static int wireless_mode = WIRELESS_11BG;
  62static int vrtl_carrier_sense = AUTO_VCS;
  63static int vcs_type = RTS_CTS;
  64static int frag_thresh = 2346;
  65static int preamble = PREAMBLE_LONG;/*long, short, auto*/
  66static int scan_mode = 1;/*active, passive*/
  67static int adhoc_tx_pwr = 1;
  68static int soft_ap;
  69static int smart_ps = 1;
  70static int power_mgnt = PS_MODE_ACTIVE;
  71static int radio_enable = 1;
  72static int long_retry_lmt = 7;
  73static int short_retry_lmt = 7;
  74static int busy_thresh = 40;
  75static int ack_policy = NORMAL_ACK;
  76static int mp_mode;
  77static int software_encrypt;
  78static int software_decrypt;
  79
  80static int wmm_enable;/* default is set to disable the wmm.*/
  81static int uapsd_enable;
  82static int uapsd_max_sp = NO_LIMIT;
  83static int uapsd_acbk_en;
  84static int uapsd_acbe_en;
  85static int uapsd_acvi_en;
  86static int uapsd_acvo_en;
  87
  88static int ht_enable = 1;
  89static int cbw40_enable = 1;
  90static int rf_config = RTL8712_RF_1T2R;  /* 1T2R*/
  91static int low_power;
  92/* mac address to use instead of the one stored in Efuse */
  93char *r8712_initmac;
  94static char *initmac;
  95/* if wifi_test = 1, driver will disable the turbo mode and pass it to
  96 * firmware private.
  97 */
  98static int wifi_test;
  99
 100module_param_string(ifname, ifname, sizeof(ifname), S_IRUGO|S_IWUSR);
 101module_param(wifi_test, int, 0644);
 102module_param(initmac, charp, 0644);
 103module_param(video_mode, int, 0644);
 104module_param(chip_version, int, 0644);
 105module_param(rfintfs, int, 0644);
 106module_param(lbkmode, int, 0644);
 107module_param(hci, int, 0644);
 108module_param(network_mode, int, 0644);
 109module_param(channel, int, 0644);
 110module_param(mp_mode, int, 0644);
 111module_param(wmm_enable, int, 0644);
 112module_param(vrtl_carrier_sense, int, 0644);
 113module_param(vcs_type, int, 0644);
 114module_param(busy_thresh, int, 0644);
 115module_param(ht_enable, int, 0644);
 116module_param(cbw40_enable, int, 0644);
 117module_param(ampdu_enable, int, 0644);
 118module_param(rf_config, int, 0644);
 119module_param(power_mgnt, int, 0644);
 120module_param(low_power, int, 0644);
 121
 122MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
 123MODULE_PARM_DESC(initmac, "MAC-Address, default: use FUSE");
 124
 125static uint loadparam(struct _adapter *padapter, struct  net_device *pnetdev);
 126static int netdev_open(struct net_device *pnetdev);
 127static int netdev_close(struct net_device *pnetdev);
 128
 129static uint loadparam(struct _adapter *padapter, struct  net_device *pnetdev)
 130{
 131        uint status = _SUCCESS;
 132        struct registry_priv  *registry_par = &padapter->registrypriv;
 133
 134        registry_par->chip_version = (u8)chip_version;
 135        registry_par->rfintfs = (u8)rfintfs;
 136        registry_par->lbkmode = (u8)lbkmode;
 137        registry_par->hci = (u8)hci;
 138        registry_par->network_mode  = (u8)network_mode;
 139        memcpy(registry_par->ssid.Ssid, "ANY", 3);
 140        registry_par->ssid.SsidLength = 3;
 141        registry_par->channel = (u8)channel;
 142        registry_par->wireless_mode = (u8)wireless_mode;
 143        registry_par->vrtl_carrier_sense = (u8)vrtl_carrier_sense;
 144        registry_par->vcs_type = (u8)vcs_type;
 145        registry_par->frag_thresh = (u16)frag_thresh;
 146        registry_par->preamble = (u8)preamble;
 147        registry_par->scan_mode = (u8)scan_mode;
 148        registry_par->adhoc_tx_pwr = (u8)adhoc_tx_pwr;
 149        registry_par->soft_ap = (u8)soft_ap;
 150        registry_par->smart_ps = (u8)smart_ps;
 151        registry_par->power_mgnt = (u8)power_mgnt;
 152        registry_par->radio_enable = (u8)radio_enable;
 153        registry_par->long_retry_lmt = (u8)long_retry_lmt;
 154        registry_par->short_retry_lmt = (u8)short_retry_lmt;
 155        registry_par->busy_thresh = (u16)busy_thresh;
 156        registry_par->ack_policy = (u8)ack_policy;
 157        registry_par->mp_mode = (u8)mp_mode;
 158        registry_par->software_encrypt = (u8)software_encrypt;
 159        registry_par->software_decrypt = (u8)software_decrypt;
 160        /*UAPSD*/
 161        registry_par->wmm_enable = (u8)wmm_enable;
 162        registry_par->uapsd_enable = (u8)uapsd_enable;
 163        registry_par->uapsd_max_sp = (u8)uapsd_max_sp;
 164        registry_par->uapsd_acbk_en = (u8)uapsd_acbk_en;
 165        registry_par->uapsd_acbe_en = (u8)uapsd_acbe_en;
 166        registry_par->uapsd_acvi_en = (u8)uapsd_acvi_en;
 167        registry_par->uapsd_acvo_en = (u8)uapsd_acvo_en;
 168        registry_par->ht_enable = (u8)ht_enable;
 169        registry_par->cbw40_enable = (u8)cbw40_enable;
 170        registry_par->ampdu_enable = (u8)ampdu_enable;
 171        registry_par->rf_config = (u8)rf_config;
 172        registry_par->low_power = (u8)low_power;
 173        registry_par->wifi_test = (u8) wifi_test;
 174        r8712_initmac = initmac;
 175        return status;
 176}
 177
 178static int r871x_net_set_mac_address(struct net_device *pnetdev, void *p)
 179{
 180        struct _adapter *padapter = (struct _adapter *)netdev_priv(pnetdev);
 181        struct sockaddr *addr = p;
 182
 183        if (padapter->bup == false)
 184                memcpy(pnetdev->dev_addr, addr->sa_data, ETH_ALEN);
 185        return 0;
 186}
 187
 188static struct net_device_stats *r871x_net_get_stats(struct net_device *pnetdev)
 189{
 190        struct _adapter *padapter = (struct _adapter *) netdev_priv(pnetdev);
 191        struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
 192        struct recv_priv *precvpriv = &(padapter->recvpriv);
 193
 194        padapter->stats.tx_packets = pxmitpriv->tx_pkts;
 195        padapter->stats.rx_packets = precvpriv->rx_pkts;
 196        padapter->stats.tx_dropped = pxmitpriv->tx_drop;
 197        padapter->stats.rx_dropped = precvpriv->rx_drop;
 198        padapter->stats.tx_bytes = pxmitpriv->tx_bytes;
 199        padapter->stats.rx_bytes = precvpriv->rx_bytes;
 200        return &padapter->stats;
 201}
 202
 203static const struct net_device_ops rtl8712_netdev_ops = {
 204        .ndo_open = netdev_open,
 205        .ndo_stop = netdev_close,
 206        .ndo_start_xmit = r8712_xmit_entry,
 207        .ndo_set_mac_address = r871x_net_set_mac_address,
 208        .ndo_get_stats = r871x_net_get_stats,
 209        .ndo_do_ioctl = r871x_ioctl,
 210};
 211
 212struct net_device *r8712_init_netdev(void)
 213{
 214        struct _adapter *padapter;
 215        struct net_device *pnetdev;
 216
 217        pnetdev = alloc_etherdev(sizeof(struct _adapter));
 218        if (!pnetdev)
 219                return NULL;
 220        if (dev_alloc_name(pnetdev, ifname) < 0) {
 221                strcpy(ifname, "wlan%d");
 222                dev_alloc_name(pnetdev, ifname);
 223        }
 224        padapter = (struct _adapter *) netdev_priv(pnetdev);
 225        padapter->pnetdev = pnetdev;
 226        pr_info("r8712u: register rtl8712_netdev_ops to netdev_ops\n");
 227        pnetdev->netdev_ops = &rtl8712_netdev_ops;
 228        pnetdev->watchdog_timeo = HZ; /* 1 second timeout */
 229        pnetdev->wireless_handlers = (struct iw_handler_def *)
 230                                     &r871x_handlers_def;
 231        /*step 2.*/
 232        loadparam(padapter, pnetdev);
 233        netif_carrier_off(pnetdev);
 234        padapter->pid = 0;  /* Initial the PID value used for HW PBC.*/
 235        return pnetdev;
 236}
 237
 238static u32 start_drv_threads(struct _adapter *padapter)
 239{
 240        padapter->cmdThread = kthread_run(r8712_cmd_thread, padapter, "%s",
 241                              padapter->pnetdev->name);
 242        if (IS_ERR(padapter->cmdThread) < 0)
 243                return _FAIL;
 244        return _SUCCESS;
 245}
 246
 247void r8712_stop_drv_threads(struct _adapter *padapter)
 248{
 249        /*Below is to terminate r8712_cmd_thread & event_thread...*/
 250        up(&padapter->cmdpriv.cmd_queue_sema);
 251        if (padapter->cmdThread)
 252                _down_sema(&padapter->cmdpriv.terminate_cmdthread_sema);
 253        padapter->cmdpriv.cmd_seq = 1;
 254}
 255
 256static void start_drv_timers(struct _adapter *padapter)
 257{
 258        _set_timer(&padapter->mlmepriv.sitesurveyctrl.sitesurvey_ctrl_timer,
 259                   5000);
 260        _set_timer(&padapter->mlmepriv.wdg_timer, 2000);
 261}
 262
 263void r8712_stop_drv_timers(struct _adapter *padapter)
 264{
 265        _cancel_timer_ex(&padapter->mlmepriv.assoc_timer);
 266        _cancel_timer_ex(&padapter->securitypriv.tkip_timer);
 267        _cancel_timer_ex(&padapter->mlmepriv.scan_to_timer);
 268        _cancel_timer_ex(&padapter->mlmepriv.dhcp_timer);
 269        _cancel_timer_ex(&padapter->mlmepriv.wdg_timer);
 270        _cancel_timer_ex(&padapter->mlmepriv.sitesurveyctrl.
 271                         sitesurvey_ctrl_timer);
 272}
 273
 274static u8 init_default_value(struct _adapter *padapter)
 275{
 276        u8 ret  = _SUCCESS;
 277        struct registry_priv *pregistrypriv = &padapter->registrypriv;
 278        struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
 279        struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
 280        struct security_priv *psecuritypriv = &padapter->securitypriv;
 281
 282        /*xmit_priv*/
 283        pxmitpriv->vcs_setting = pregistrypriv->vrtl_carrier_sense;
 284        pxmitpriv->vcs = pregistrypriv->vcs_type;
 285        pxmitpriv->vcs_type = pregistrypriv->vcs_type;
 286        pxmitpriv->rts_thresh = pregistrypriv->rts_thresh;
 287        pxmitpriv->frag_len = pregistrypriv->frag_thresh;
 288        /* mlme_priv */
 289        /* Maybe someday we should rename this variable to "active_mode"(Jeff)*/
 290        pmlmepriv->passive_mode = 1; /* 1: active, 0: passive. */
 291        /*ht_priv*/
 292        {
 293                int i;
 294                struct ht_priv   *phtpriv = &pmlmepriv->htpriv;
 295
 296                phtpriv->ampdu_enable = false;/*set to disabled*/
 297                for (i = 0; i < 16; i++)
 298                        phtpriv->baddbareq_issued[i] = false;
 299        }
 300        /*security_priv*/
 301        psecuritypriv->sw_encrypt = pregistrypriv->software_encrypt;
 302        psecuritypriv->sw_decrypt = pregistrypriv->software_decrypt;
 303        psecuritypriv->binstallGrpkey = _FAIL;
 304        /*pwrctrl_priv*/
 305        /*registry_priv*/
 306        r8712_init_registrypriv_dev_network(padapter);
 307        r8712_update_registrypriv_dev_network(padapter);
 308        /*misc.*/
 309        return ret;
 310}
 311
 312u8 r8712_init_drv_sw(struct _adapter *padapter)
 313{
 314        if ((r8712_init_cmd_priv(&padapter->cmdpriv)) == _FAIL)
 315                return _FAIL;
 316        padapter->cmdpriv.padapter = padapter;
 317        if ((r8712_init_evt_priv(&padapter->evtpriv)) == _FAIL)
 318                return _FAIL;
 319        if (r8712_init_mlme_priv(padapter) == _FAIL)
 320                return _FAIL;
 321        _r8712_init_xmit_priv(&padapter->xmitpriv, padapter);
 322        _r8712_init_recv_priv(&padapter->recvpriv, padapter);
 323        memset((unsigned char *)&padapter->securitypriv, 0,
 324               sizeof(struct security_priv));
 325        _init_timer(&(padapter->securitypriv.tkip_timer), padapter->pnetdev,
 326                    r8712_use_tkipkey_handler, padapter);
 327        _r8712_init_sta_priv(&padapter->stapriv);
 328        padapter->stapriv.padapter = padapter;
 329        r8712_init_bcmc_stainfo(padapter);
 330        r8712_init_pwrctrl_priv(padapter);
 331        mp871xinit(padapter);
 332        if (init_default_value(padapter) != _SUCCESS)
 333                return _FAIL;
 334        r8712_InitSwLeds(padapter);
 335        return _SUCCESS;
 336}
 337
 338u8 r8712_free_drv_sw(struct _adapter *padapter)
 339{
 340        struct net_device *pnetdev = (struct net_device *)padapter->pnetdev;
 341
 342        r8712_free_cmd_priv(&padapter->cmdpriv);
 343        r8712_free_evt_priv(&padapter->evtpriv);
 344        r8712_DeInitSwLeds(padapter);
 345        r8712_free_mlme_priv(&padapter->mlmepriv);
 346        r8712_free_io_queue(padapter);
 347        _free_xmit_priv(&padapter->xmitpriv);
 348        if (padapter->fw_found)
 349                _r8712_free_sta_priv(&padapter->stapriv);
 350        _r8712_free_recv_priv(&padapter->recvpriv);
 351        mp871xdeinit(padapter);
 352        if (pnetdev)
 353                free_netdev(pnetdev);
 354        return _SUCCESS;
 355}
 356
 357
 358static void enable_video_mode(struct _adapter *padapter, int cbw40_value)
 359{
 360        /*   bit 8:
 361         *   1 -> enable video mode to 96B AP
 362         *   0 -> disable video mode to 96B AP
 363         *   bit 9:
 364         *   1 -> enable 40MHz mode
 365         *   0 -> disable 40MHz mode
 366         *   bit 10:
 367         *   1 -> enable STBC
 368         *   0 -> disable STBC
 369         */
 370        u32  intcmd = 0xf4000500;   /* enable bit8, bit10*/
 371
 372        if (cbw40_value) {
 373                /* if the driver supports the 40M bandwidth,
 374                 * we can enable the bit 9.*/
 375                intcmd |= 0x200;
 376        }
 377        r8712_fw_cmd(padapter, intcmd);
 378}
 379
 380/**
 381 *
 382 * This function intends to handle the activation of an interface
 383 * i.e. when it is brought Up/Active from a Down state.
 384 *
 385 */
 386static int netdev_open(struct net_device *pnetdev)
 387{
 388        struct _adapter *padapter = (struct _adapter *)netdev_priv(pnetdev);
 389
 390        mutex_lock(&padapter->mutex_start);
 391        if (padapter->bup == false) {
 392                padapter->bDriverStopped = false;
 393                padapter->bSurpriseRemoved = false;
 394                padapter->bup = true;
 395                if (rtl871x_hal_init(padapter) != _SUCCESS)
 396                        goto netdev_open_error;
 397                if (r8712_initmac == NULL)
 398                        /* Use the mac address stored in the Efuse */
 399                        memcpy(pnetdev->dev_addr,
 400                                padapter->eeprompriv.mac_addr, ETH_ALEN);
 401                else {
 402                        /* We have to inform f/w to use user-supplied MAC
 403                         * address.
 404                         */
 405                        msleep(200);
 406                        r8712_setMacAddr_cmd(padapter, (u8 *)pnetdev->dev_addr);
 407                        /*
 408                         * The "myid" function will get the wifi mac address
 409                         * from eeprompriv structure instead of netdev
 410                         * structure. So, we have to overwrite the mac_addr
 411                         * stored in the eeprompriv structure. In this case,
 412                         * the real mac address won't be used anymore. So that,
 413                         * the eeprompriv.mac_addr should store the mac which
 414                         * users specify.
 415                         */
 416                        memcpy(padapter->eeprompriv.mac_addr,
 417                                pnetdev->dev_addr, ETH_ALEN);
 418                }
 419                if (start_drv_threads(padapter) != _SUCCESS)
 420                        goto netdev_open_error;
 421                if (padapter->dvobjpriv.inirp_init == NULL)
 422                        goto netdev_open_error;
 423                else
 424                        padapter->dvobjpriv.inirp_init(padapter);
 425                r8712_set_ps_mode(padapter, padapter->registrypriv.power_mgnt,
 426                                  padapter->registrypriv.smart_ps);
 427        }
 428        if (!netif_queue_stopped(pnetdev))
 429                netif_start_queue(pnetdev);
 430        else
 431                netif_wake_queue(pnetdev);
 432
 433         if (video_mode)
 434                enable_video_mode(padapter, cbw40_enable);
 435        /* start driver mlme relation timer */
 436        start_drv_timers(padapter);
 437        padapter->ledpriv.LedControlHandler(padapter, LED_CTL_NO_LINK);
 438        mutex_unlock(&padapter->mutex_start);
 439        return 0;
 440netdev_open_error:
 441        padapter->bup = false;
 442        netif_carrier_off(pnetdev);
 443        netif_stop_queue(pnetdev);
 444        mutex_unlock(&padapter->mutex_start);
 445        return -1;
 446}
 447
 448/**
 449 *
 450 * This function intends to handle the shutdown of an interface
 451 * i.e. when it is brought Down from an Up/Active state.
 452 *
 453 */
 454static int netdev_close(struct net_device *pnetdev)
 455{
 456        struct _adapter *padapter = (struct _adapter *) netdev_priv(pnetdev);
 457
 458        /* Close LED*/
 459        padapter->ledpriv.LedControlHandler(padapter, LED_CTL_POWER_OFF);
 460        msleep(200);
 461
 462        /*s1.*/
 463        if (pnetdev) {
 464                if (!netif_queue_stopped(pnetdev))
 465                        netif_stop_queue(pnetdev);
 466        }
 467        /*s2.*/
 468        /*s2-1.  issue disassoc_cmd to fw*/
 469        r8712_disassoc_cmd(padapter);
 470        /*s2-2.  indicate disconnect to os*/
 471        r8712_ind_disconnect(padapter);
 472        /*s2-3.*/
 473        r8712_free_assoc_resources(padapter);
 474        /*s2-4.*/
 475        r8712_free_network_queue(padapter);
 476        return 0;
 477}
 478
 479#include "mlme_osdep.h"
 480