linux/drivers/staging/rtl8192e/rtl8192e/rtl_core.c
<<
>>
Prefs
   1/******************************************************************************
   2 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
   3 *
   4 * Based on the r8180 driver, which is:
   5 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of version 2 of the GNU General Public License as
   8 * published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful, but WITHOUT
  11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13 * more details.
  14 *
  15 * You should have received a copy of the GNU General Public License along with
  16 * this program; if not, write to the Free Software Foundation, Inc.,
  17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  18 *
  19 * The full GNU General Public License is included in this distribution in the
  20 * file called LICENSE.
  21 *
  22 * Contact Information:
  23 * wlanfae <wlanfae@realtek.com>
  24******************************************************************************/
  25#undef RX_DONT_PASS_UL
  26#undef DEBUG_EPROM
  27#undef DEBUG_RX_VERBOSE
  28#undef DUMMY_RX
  29#undef DEBUG_ZERO_RX
  30#undef DEBUG_RX_SKB
  31#undef DEBUG_TX_FRAG
  32#undef DEBUG_RX_FRAG
  33#undef DEBUG_TX_FILLDESC
  34#undef DEBUG_TX
  35#undef DEBUG_IRQ
  36#undef DEBUG_RX
  37#undef DEBUG_RXALLOC
  38#undef DEBUG_REGISTERS
  39#undef DEBUG_RING
  40#undef DEBUG_IRQ_TASKLET
  41#undef DEBUG_TX_ALLOC
  42#undef DEBUG_TX_DESC
  43
  44#include <linux/uaccess.h>
  45#include <linux/pci.h>
  46#include <linux/vmalloc.h>
  47#include "rtl_core.h"
  48#include "r8192E_phy.h"
  49#include "r8192E_phyreg.h"
  50#include "r8190P_rtl8256.h"
  51#include "r8192E_cmdpkt.h"
  52
  53#include "rtl_wx.h"
  54#include "rtl_dm.h"
  55
  56#include "rtl_pm.h"
  57
  58int hwwep = 1;
  59static int channels = 0x3fff;
  60static char *ifname = "wlan%d";
  61
  62
  63static struct rtl819x_ops rtl819xp_ops = {
  64        .nic_type                       = NIC_8192E,
  65        .get_eeprom_size                = rtl8192_get_eeprom_size,
  66        .init_adapter_variable          = rtl8192_InitializeVariables,
  67        .initialize_adapter             = rtl8192_adapter_start,
  68        .link_change                    = rtl8192_link_change,
  69        .tx_fill_descriptor             = rtl8192_tx_fill_desc,
  70        .tx_fill_cmd_descriptor         = rtl8192_tx_fill_cmd_desc,
  71        .rx_query_status_descriptor     = rtl8192_rx_query_status_desc,
  72        .rx_command_packet_handler = NULL,
  73        .stop_adapter                   = rtl8192_halt_adapter,
  74        .update_ratr_table              = rtl8192_update_ratr_table,
  75        .irq_enable                     = rtl8192_EnableInterrupt,
  76        .irq_disable                    = rtl8192_DisableInterrupt,
  77        .irq_clear                      = rtl8192_ClearInterrupt,
  78        .rx_enable                      = rtl8192_enable_rx,
  79        .tx_enable                      = rtl8192_enable_tx,
  80        .interrupt_recognized           = rtl8192_interrupt_recognized,
  81        .TxCheckStuckHandler            = rtl8192_HalTxCheckStuck,
  82        .RxCheckStuckHandler            = rtl8192_HalRxCheckStuck,
  83};
  84
  85static struct pci_device_id rtl8192_pci_id_tbl[] = {
  86        {RTL_PCI_DEVICE(0x10ec, 0x8192, rtl819xp_ops)},
  87        {RTL_PCI_DEVICE(0x07aa, 0x0044, rtl819xp_ops)},
  88        {RTL_PCI_DEVICE(0x07aa, 0x0047, rtl819xp_ops)},
  89        {}
  90};
  91
  92MODULE_DEVICE_TABLE(pci, rtl8192_pci_id_tbl);
  93
  94static int rtl8192_pci_probe(struct pci_dev *pdev,
  95                        const struct pci_device_id *id);
  96static void rtl8192_pci_disconnect(struct pci_dev *pdev);
  97static irqreturn_t rtl8192_interrupt(int irq, void *netdev);
  98
  99static struct pci_driver rtl8192_pci_driver = {
 100        .name = DRV_NAME,       /* Driver name   */
 101        .id_table = rtl8192_pci_id_tbl, /* PCI_ID table  */
 102        .probe  = rtl8192_pci_probe,    /* probe fn      */
 103        .remove  = rtl8192_pci_disconnect,      /* remove fn */
 104        .suspend = rtl8192E_suspend,    /* PM suspend fn */
 105        .resume = rtl8192E_resume,                 /* PM resume fn  */
 106};
 107
 108/****************************************************************************
 109   -----------------------------IO STUFF-------------------------
 110*****************************************************************************/
 111static bool PlatformIOCheckPageLegalAndGetRegMask(u32 u4bPage, u8 *pu1bPageMask)
 112{
 113        bool            bReturn = false;
 114
 115        *pu1bPageMask = 0xfe;
 116
 117        switch (u4bPage) {
 118        case 1: case 2: case 3: case 4:
 119        case 8: case 9: case 10: case 12: case 13:
 120                bReturn = true;
 121                *pu1bPageMask = 0xf0;
 122                break;
 123
 124        default:
 125                bReturn = false;
 126                break;
 127        }
 128
 129        return bReturn;
 130}
 131
 132void write_nic_io_byte(struct net_device *dev, int x, u8 y)
 133{
 134        u32 u4bPage = (x >> 8);
 135        u8 u1PageMask = 0;
 136        bool    bIsLegalPage = false;
 137
 138        if (u4bPage == 0) {
 139                outb(y&0xff, dev->base_addr + x);
 140
 141        } else {
 142                bIsLegalPage = PlatformIOCheckPageLegalAndGetRegMask(u4bPage,
 143                               &u1PageMask);
 144                if (bIsLegalPage) {
 145                        u8 u1bPsr = read_nic_io_byte(dev, PSR);
 146
 147                        write_nic_io_byte(dev, PSR, ((u1bPsr & u1PageMask) |
 148                                          (u8)u4bPage));
 149                        write_nic_io_byte(dev, (x & 0xff), y);
 150                        write_nic_io_byte(dev, PSR, (u1bPsr & u1PageMask));
 151                }
 152        }
 153}
 154
 155void write_nic_io_word(struct net_device *dev, int x, u16 y)
 156{
 157        u32 u4bPage = (x >> 8);
 158        u8 u1PageMask = 0;
 159        bool    bIsLegalPage = false;
 160
 161        if (u4bPage == 0) {
 162                outw(y, dev->base_addr + x);
 163        } else {
 164                bIsLegalPage = PlatformIOCheckPageLegalAndGetRegMask(u4bPage,
 165                                                         &u1PageMask);
 166                if (bIsLegalPage) {
 167                        u8 u1bPsr = read_nic_io_byte(dev, PSR);
 168
 169                        write_nic_io_byte(dev, PSR, ((u1bPsr & u1PageMask) |
 170                                          (u8)u4bPage));
 171                        write_nic_io_word(dev, (x & 0xff), y);
 172                        write_nic_io_byte(dev, PSR, (u1bPsr & u1PageMask));
 173
 174                }
 175        }
 176}
 177
 178void write_nic_io_dword(struct net_device *dev, int x, u32 y)
 179{
 180        u32 u4bPage = (x >> 8);
 181        u8 u1PageMask = 0;
 182        bool    bIsLegalPage = false;
 183
 184        if (u4bPage == 0) {
 185                outl(y, dev->base_addr + x);
 186        } else {
 187                bIsLegalPage = PlatformIOCheckPageLegalAndGetRegMask(u4bPage,
 188                                                 &u1PageMask);
 189                if (bIsLegalPage) {
 190                        u8 u1bPsr = read_nic_io_byte(dev, PSR);
 191
 192                        write_nic_io_byte(dev, PSR, ((u1bPsr & u1PageMask) |
 193                                          (u8)u4bPage));
 194                        write_nic_io_dword(dev, (x & 0xff), y);
 195                        write_nic_io_byte(dev, PSR, (u1bPsr & u1PageMask));
 196                }
 197        }
 198}
 199
 200u8 read_nic_io_byte(struct net_device *dev, int x)
 201{
 202        u32 u4bPage = (x >> 8);
 203        u8 u1PageMask = 0;
 204        bool    bIsLegalPage = false;
 205        u8      Data = 0;
 206
 207        if (u4bPage == 0) {
 208                return 0xff&inb(dev->base_addr + x);
 209        } else {
 210                bIsLegalPage = PlatformIOCheckPageLegalAndGetRegMask(u4bPage,
 211                                                        &u1PageMask);
 212                if (bIsLegalPage) {
 213                        u8 u1bPsr = read_nic_io_byte(dev, PSR);
 214
 215                        write_nic_io_byte(dev, PSR, ((u1bPsr & u1PageMask) |
 216                                          (u8)u4bPage));
 217                        Data = read_nic_io_byte(dev, (x & 0xff));
 218                        write_nic_io_byte(dev, PSR, (u1bPsr & u1PageMask));
 219                }
 220        }
 221
 222        return Data;
 223}
 224
 225u16 read_nic_io_word(struct net_device *dev, int x)
 226{
 227        u32 u4bPage = (x >> 8);
 228        u8 u1PageMask = 0;
 229        bool    bIsLegalPage = false;
 230        u16     Data = 0;
 231
 232        if (u4bPage == 0) {
 233                return inw(dev->base_addr + x);
 234        } else {
 235                bIsLegalPage = PlatformIOCheckPageLegalAndGetRegMask(u4bPage,
 236                               &u1PageMask);
 237                if (bIsLegalPage) {
 238                        u8 u1bPsr = read_nic_io_byte(dev, PSR);
 239
 240                        write_nic_io_byte(dev, PSR, ((u1bPsr & u1PageMask) |
 241                                          (u8)u4bPage));
 242                        Data = read_nic_io_word(dev, (x & 0xff));
 243                        write_nic_io_byte(dev, PSR, (u1bPsr & u1PageMask));
 244
 245                }
 246        }
 247
 248        return Data;
 249}
 250
 251u32 read_nic_io_dword(struct net_device *dev, int x)
 252{
 253        u32 u4bPage = (x >> 8);
 254        u8 u1PageMask = 0;
 255        bool    bIsLegalPage = false;
 256        u32     Data = 0;
 257
 258        if (u4bPage == 0) {
 259                return inl(dev->base_addr + x);
 260        } else {
 261                bIsLegalPage = PlatformIOCheckPageLegalAndGetRegMask(u4bPage,
 262                               &u1PageMask);
 263                if (bIsLegalPage) {
 264                        u8 u1bPsr = read_nic_io_byte(dev, PSR);
 265
 266                        write_nic_io_byte(dev, PSR, ((u1bPsr & u1PageMask) |
 267                                          (u8)u4bPage));
 268                        Data = read_nic_io_dword(dev, (x & 0xff));
 269                        write_nic_io_byte(dev, PSR, (u1bPsr & u1PageMask));
 270
 271                }
 272        }
 273
 274        return Data;
 275}
 276
 277u8 read_nic_byte(struct net_device *dev, int x)
 278{
 279        return 0xff & readb((u8 __iomem *)dev->mem_start + x);
 280}
 281
 282u32 read_nic_dword(struct net_device *dev, int x)
 283{
 284        return readl((u8 __iomem *)dev->mem_start + x);
 285}
 286
 287u16 read_nic_word(struct net_device *dev, int x)
 288{
 289        return readw((u8 __iomem *)dev->mem_start + x);
 290}
 291
 292void write_nic_byte(struct net_device *dev, int x, u8 y)
 293{
 294        writeb(y, (u8 __iomem *)dev->mem_start + x);
 295
 296        udelay(20);
 297}
 298
 299void write_nic_dword(struct net_device *dev, int x, u32 y)
 300{
 301        writel(y, (u8 __iomem *)dev->mem_start + x);
 302
 303        udelay(20);
 304}
 305
 306void write_nic_word(struct net_device *dev, int x, u16 y)
 307{
 308        writew(y, (u8 __iomem *)dev->mem_start + x);
 309
 310        udelay(20);
 311}
 312
 313/****************************************************************************
 314   -----------------------------GENERAL FUNCTION-------------------------
 315*****************************************************************************/
 316bool MgntActSet_RF_State(struct net_device *dev,
 317                         enum rt_rf_power_state StateToSet,
 318                         RT_RF_CHANGE_SOURCE ChangeSource,
 319                         bool   ProtectOrNot)
 320{
 321        struct r8192_priv *priv = rtllib_priv(dev);
 322        struct rtllib_device *ieee = priv->rtllib;
 323        bool                    bActionAllowed = false;
 324        bool                    bConnectBySSID = false;
 325        enum rt_rf_power_state rtState;
 326        u16                     RFWaitCounter = 0;
 327        unsigned long flag;
 328        RT_TRACE((COMP_PS | COMP_RF), "===>MgntActSet_RF_State(): "
 329                 "StateToSet(%d)\n", StateToSet);
 330
 331        ProtectOrNot = false;
 332
 333
 334        if (!ProtectOrNot) {
 335                while (true) {
 336                        spin_lock_irqsave(&priv->rf_ps_lock, flag);
 337                        if (priv->RFChangeInProgress) {
 338                                spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
 339                                RT_TRACE((COMP_PS | COMP_RF),
 340                                         "MgntActSet_RF_State(): RF Change in "
 341                                         "progress! Wait to set..StateToSet"
 342                                         "(%d).\n", StateToSet);
 343
 344                                while (priv->RFChangeInProgress) {
 345                                        RFWaitCounter++;
 346                                        RT_TRACE((COMP_PS | COMP_RF),
 347                                                 "MgntActSet_RF_State(): Wait 1"
 348                                                 " ms (%d times)...\n",
 349                                                 RFWaitCounter);
 350                                        mdelay(1);
 351
 352                                        if (RFWaitCounter > 100) {
 353                                                RT_TRACE(COMP_ERR, "MgntActSet_"
 354                                                         "RF_State(): Wait too "
 355                                                         "logn to set RF\n");
 356                                                return false;
 357                                        }
 358                                }
 359                        } else {
 360                                priv->RFChangeInProgress = true;
 361                                spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
 362                                break;
 363                        }
 364                }
 365        }
 366
 367        rtState = priv->rtllib->eRFPowerState;
 368
 369        switch (StateToSet) {
 370        case eRfOn:
 371                priv->rtllib->RfOffReason &= (~ChangeSource);
 372
 373                if ((ChangeSource == RF_CHANGE_BY_HW) &&
 374                    (priv->bHwRadioOff == true))
 375                        priv->bHwRadioOff = false;
 376
 377                if (!priv->rtllib->RfOffReason) {
 378                        priv->rtllib->RfOffReason = 0;
 379                        bActionAllowed = true;
 380
 381
 382                        if (rtState == eRfOff &&
 383                            ChangeSource >= RF_CHANGE_BY_HW)
 384                                bConnectBySSID = true;
 385                } else {
 386                        RT_TRACE((COMP_PS | COMP_RF), "MgntActSet_RF_State - "
 387                                 "eRfon reject pMgntInfo->RfOffReason= 0x%x,"
 388                                 " ChangeSource=0x%X\n",
 389                                  priv->rtllib->RfOffReason, ChangeSource);
 390        }
 391
 392                break;
 393
 394        case eRfOff:
 395
 396                if ((priv->rtllib->iw_mode == IW_MODE_INFRA) ||
 397                    (priv->rtllib->iw_mode == IW_MODE_ADHOC)) {
 398                        if ((priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) ||
 399                            (ChangeSource > RF_CHANGE_BY_IPS)) {
 400                                if (ieee->state == RTLLIB_LINKED)
 401                                        priv->blinked_ingpio = true;
 402                                else
 403                                        priv->blinked_ingpio = false;
 404                                rtllib_MgntDisconnect(priv->rtllib,
 405                                                      disas_lv_ss);
 406                        }
 407                }
 408                if ((ChangeSource == RF_CHANGE_BY_HW) &&
 409                     (priv->bHwRadioOff == false))
 410                        priv->bHwRadioOff = true;
 411                priv->rtllib->RfOffReason |= ChangeSource;
 412                bActionAllowed = true;
 413                break;
 414
 415        case eRfSleep:
 416                priv->rtllib->RfOffReason |= ChangeSource;
 417                bActionAllowed = true;
 418                break;
 419
 420        default:
 421                break;
 422        }
 423
 424        if (bActionAllowed) {
 425                RT_TRACE((COMP_PS | COMP_RF), "MgntActSet_RF_State(): Action is"
 426                         " allowed.... StateToSet(%d), RfOffReason(%#X)\n",
 427                         StateToSet, priv->rtllib->RfOffReason);
 428                PHY_SetRFPowerState(dev, StateToSet);
 429                if (StateToSet == eRfOn) {
 430
 431                        if (bConnectBySSID && (priv->blinked_ingpio == true)) {
 432                                queue_delayed_work_rsl(ieee->wq,
 433                                         &ieee->associate_procedure_wq, 0);
 434                                priv->blinked_ingpio = false;
 435                        }
 436                }
 437        } else {
 438                RT_TRACE((COMP_PS | COMP_RF), "MgntActSet_RF_State(): "
 439                         "Action is rejected.... StateToSet(%d), ChangeSource"
 440                         "(%#X), RfOffReason(%#X)\n", StateToSet, ChangeSource,
 441                         priv->rtllib->RfOffReason);
 442        }
 443
 444        if (!ProtectOrNot) {
 445                spin_lock_irqsave(&priv->rf_ps_lock, flag);
 446                priv->RFChangeInProgress = false;
 447                spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
 448        }
 449
 450        RT_TRACE((COMP_PS | COMP_RF), "<===MgntActSet_RF_State()\n");
 451        return bActionAllowed;
 452}
 453
 454
 455static short rtl8192_get_nic_desc_num(struct net_device *dev, int prio)
 456{
 457        struct r8192_priv *priv = rtllib_priv(dev);
 458        struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
 459
 460        /* For now, we reserved two free descriptor as a safety boundary
 461        * between the tail and the head
 462        */
 463        if ((prio == MGNT_QUEUE) && (skb_queue_len(&ring->queue) > 10))
 464                RT_TRACE(COMP_DBG, "-----[%d]---------ring->idx=%d "
 465                         "queue_len=%d---------\n", prio, ring->idx,
 466                         skb_queue_len(&ring->queue));
 467        return skb_queue_len(&ring->queue);
 468}
 469
 470static short rtl8192_check_nic_enough_desc(struct net_device *dev, int prio)
 471{
 472        struct r8192_priv *priv = rtllib_priv(dev);
 473        struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
 474
 475        if (ring->entries - skb_queue_len(&ring->queue) >= 2)
 476                return 1;
 477        return 0;
 478}
 479
 480void rtl8192_tx_timeout(struct net_device *dev)
 481{
 482        struct r8192_priv *priv = rtllib_priv(dev);
 483
 484        schedule_work(&priv->reset_wq);
 485        printk(KERN_INFO "TXTIMEOUT");
 486}
 487
 488void rtl8192_irq_enable(struct net_device *dev)
 489{
 490        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
 491        priv->irq_enabled = 1;
 492
 493        priv->ops->irq_enable(dev);
 494}
 495
 496void rtl8192_irq_disable(struct net_device *dev)
 497{
 498        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
 499
 500        priv->ops->irq_disable(dev);
 501
 502        priv->irq_enabled = 0;
 503}
 504
 505void rtl8192_set_chan(struct net_device *dev, short ch)
 506{
 507        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
 508
 509        RT_TRACE(COMP_CH, "=====>%s()====ch:%d\n", __func__, ch);
 510        if (priv->chan_forced)
 511                return;
 512
 513        priv->chan = ch;
 514
 515        if (priv->rf_set_chan)
 516                priv->rf_set_chan(dev, priv->chan);
 517}
 518
 519void rtl8192_update_cap(struct net_device *dev, u16 cap)
 520{
 521        struct r8192_priv *priv = rtllib_priv(dev);
 522        struct rtllib_network *net = &priv->rtllib->current_network;
 523        bool            ShortPreamble;
 524
 525        if (cap & WLAN_CAPABILITY_SHORT_PREAMBLE) {
 526                if (priv->dot11CurrentPreambleMode != PREAMBLE_SHORT) {
 527                        ShortPreamble = true;
 528                        priv->dot11CurrentPreambleMode = PREAMBLE_SHORT;
 529                        RT_TRACE(COMP_DBG, "%s(): WLAN_CAPABILITY_SHORT_"
 530                                 "PREAMBLE\n", __func__);
 531                        priv->rtllib->SetHwRegHandler(dev, HW_VAR_ACK_PREAMBLE,
 532                                        (unsigned char *)&ShortPreamble);
 533                }
 534        } else {
 535                if (priv->dot11CurrentPreambleMode != PREAMBLE_LONG) {
 536                        ShortPreamble = false;
 537                        priv->dot11CurrentPreambleMode = PREAMBLE_LONG;
 538                        RT_TRACE(COMP_DBG, "%s(): WLAN_CAPABILITY_LONG_"
 539                                 "PREAMBLE\n", __func__);
 540                        priv->rtllib->SetHwRegHandler(dev, HW_VAR_ACK_PREAMBLE,
 541                                              (unsigned char *)&ShortPreamble);
 542                }
 543        }
 544
 545        if (net->mode & (IEEE_G|IEEE_N_24G)) {
 546                u8      slot_time_val;
 547                u8      CurSlotTime = priv->slot_time;
 548
 549                if ((cap & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
 550                   (!priv->rtllib->pHTInfo->bCurrentRT2RTLongSlotTime)) {
 551                        if (CurSlotTime != SHORT_SLOT_TIME) {
 552                                slot_time_val = SHORT_SLOT_TIME;
 553                                priv->rtllib->SetHwRegHandler(dev,
 554                                         HW_VAR_SLOT_TIME, &slot_time_val);
 555                        }
 556                } else {
 557                        if (CurSlotTime != NON_SHORT_SLOT_TIME) {
 558                                slot_time_val = NON_SHORT_SLOT_TIME;
 559                                priv->rtllib->SetHwRegHandler(dev,
 560                                         HW_VAR_SLOT_TIME, &slot_time_val);
 561                        }
 562                }
 563        }
 564}
 565
 566static struct rtllib_qos_parameters def_qos_parameters = {
 567        {3, 3, 3, 3},
 568        {7, 7, 7, 7},
 569        {2, 2, 2, 2},
 570        {0, 0, 0, 0},
 571        {0, 0, 0, 0}
 572};
 573
 574static void rtl8192_update_beacon(void *data)
 575{
 576        struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
 577                                  update_beacon_wq.work);
 578        struct net_device *dev = priv->rtllib->dev;
 579        struct rtllib_device *ieee = priv->rtllib;
 580        struct rtllib_network *net = &ieee->current_network;
 581
 582        if (ieee->pHTInfo->bCurrentHTSupport)
 583                HT_update_self_and_peer_setting(ieee, net);
 584        ieee->pHTInfo->bCurrentRT2RTLongSlotTime =
 585                 net->bssht.bdRT2RTLongSlotTime;
 586        ieee->pHTInfo->RT2RT_HT_Mode = net->bssht.RT2RT_HT_Mode;
 587        rtl8192_update_cap(dev, net->capability);
 588}
 589
 590int WDCAPARA_ADD[] = {EDCAPARA_BE, EDCAPARA_BK, EDCAPARA_VI, EDCAPARA_VO};
 591
 592static void rtl8192_qos_activate(void *data)
 593{
 594        struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
 595                                  qos_activate);
 596        struct net_device *dev = priv->rtllib->dev;
 597        int i;
 598
 599        mutex_lock(&priv->mutex);
 600        if (priv->rtllib->state != RTLLIB_LINKED)
 601                goto success;
 602        RT_TRACE(COMP_QOS, "qos active process with associate response "
 603                 "received\n");
 604
 605        for (i = 0; i <  QOS_QUEUE_NUM; i++) {
 606                priv->rtllib->SetHwRegHandler(dev, HW_VAR_AC_PARAM, (u8 *)(&i));
 607        }
 608
 609success:
 610        mutex_unlock(&priv->mutex);
 611}
 612
 613static int rtl8192_qos_handle_probe_response(struct r8192_priv *priv,
 614                int active_network,
 615                struct rtllib_network *network)
 616{
 617        int ret = 0;
 618        u32 size = sizeof(struct rtllib_qos_parameters);
 619
 620        if (priv->rtllib->state != RTLLIB_LINKED)
 621                return ret;
 622
 623        if ((priv->rtllib->iw_mode != IW_MODE_INFRA))
 624                return ret;
 625
 626        if (network->flags & NETWORK_HAS_QOS_MASK) {
 627                if (active_network &&
 628                                (network->flags & NETWORK_HAS_QOS_PARAMETERS))
 629                        network->qos_data.active = network->qos_data.supported;
 630
 631                if ((network->qos_data.active == 1) && (active_network == 1) &&
 632                                (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
 633                                (network->qos_data.old_param_count !=
 634                                network->qos_data.param_count)) {
 635                        network->qos_data.old_param_count =
 636                                network->qos_data.param_count;
 637        priv->rtllib->wmm_acm = network->qos_data.wmm_acm;
 638                        queue_work_rsl(priv->priv_wq, &priv->qos_activate);
 639                        RT_TRACE(COMP_QOS, "QoS parameters change call "
 640                                        "qos_activate\n");
 641                }
 642        } else {
 643                memcpy(&priv->rtllib->current_network.qos_data.parameters,
 644                       &def_qos_parameters, size);
 645
 646                if ((network->qos_data.active == 1) && (active_network == 1)) {
 647                        queue_work_rsl(priv->priv_wq, &priv->qos_activate);
 648                        RT_TRACE(COMP_QOS, "QoS was disabled call qos_"
 649                                 "activate\n");
 650                }
 651                network->qos_data.active = 0;
 652                network->qos_data.supported = 0;
 653        }
 654
 655        return 0;
 656}
 657
 658static int rtl8192_handle_beacon(struct net_device *dev,
 659        struct rtllib_beacon *beacon,
 660        struct rtllib_network *network)
 661{
 662        struct r8192_priv *priv = rtllib_priv(dev);
 663
 664        rtl8192_qos_handle_probe_response(priv, 1, network);
 665
 666        queue_delayed_work_rsl(priv->priv_wq, &priv->update_beacon_wq, 0);
 667        return 0;
 668
 669}
 670
 671static int rtl8192_qos_association_resp(struct r8192_priv *priv,
 672        struct rtllib_network *network)
 673{
 674        int ret = 0;
 675        unsigned long flags;
 676        u32 size = sizeof(struct rtllib_qos_parameters);
 677        int set_qos_param = 0;
 678
 679        if ((priv == NULL) || (network == NULL))
 680                return ret;
 681
 682        if (priv->rtllib->state != RTLLIB_LINKED)
 683                return ret;
 684
 685        if ((priv->rtllib->iw_mode != IW_MODE_INFRA))
 686                return ret;
 687
 688        spin_lock_irqsave(&priv->rtllib->lock, flags);
 689        if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
 690                memcpy(&priv->rtllib->current_network.qos_data.parameters,
 691                       &network->qos_data.parameters,
 692                       sizeof(struct rtllib_qos_parameters));
 693                priv->rtllib->current_network.qos_data.active = 1;
 694                priv->rtllib->wmm_acm = network->qos_data.wmm_acm;
 695                set_qos_param = 1;
 696                priv->rtllib->current_network.qos_data.old_param_count =
 697                        priv->rtllib->current_network.qos_data.param_count;
 698                priv->rtllib->current_network.qos_data.param_count =
 699                        network->qos_data.param_count;
 700        } else {
 701                memcpy(&priv->rtllib->current_network.qos_data.parameters,
 702                &def_qos_parameters, size);
 703                priv->rtllib->current_network.qos_data.active = 0;
 704                priv->rtllib->current_network.qos_data.supported = 0;
 705                set_qos_param = 1;
 706        }
 707
 708        spin_unlock_irqrestore(&priv->rtllib->lock, flags);
 709
 710        RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n", __func__,
 711                 network->flags, priv->rtllib->current_network.qos_data.active);
 712        if (set_qos_param == 1) {
 713                dm_init_edca_turbo(priv->rtllib->dev);
 714                queue_work_rsl(priv->priv_wq, &priv->qos_activate);
 715        }
 716        return ret;
 717}
 718
 719static int rtl8192_handle_assoc_response(struct net_device *dev,
 720                                 struct rtllib_assoc_response_frame *resp,
 721                                 struct rtllib_network *network)
 722{
 723        struct r8192_priv *priv = rtllib_priv(dev);
 724        rtl8192_qos_association_resp(priv, network);
 725        return 0;
 726}
 727
 728static void rtl8192_prepare_beacon(struct r8192_priv *priv)
 729{
 730        struct net_device *dev = priv->rtllib->dev;
 731        struct sk_buff *pskb = NULL, *pnewskb = NULL;
 732        struct cb_desc *tcb_desc = NULL;
 733        struct rtl8192_tx_ring *ring = NULL;
 734        struct tx_desc *pdesc = NULL;
 735
 736        ring = &priv->tx_ring[BEACON_QUEUE];
 737        pskb = __skb_dequeue(&ring->queue);
 738        kfree_skb(pskb);
 739
 740        pnewskb = rtllib_get_beacon(priv->rtllib);
 741        if (!pnewskb)
 742                return;
 743
 744        tcb_desc = (struct cb_desc *)(pnewskb->cb + 8);
 745        tcb_desc->queue_index = BEACON_QUEUE;
 746        tcb_desc->data_rate = 2;
 747        tcb_desc->RATRIndex = 7;
 748        tcb_desc->bTxDisableRateFallBack = 1;
 749        tcb_desc->bTxUseDriverAssingedRate = 1;
 750        skb_push(pnewskb, priv->rtllib->tx_headroom);
 751
 752        pdesc = &ring->desc[0];
 753        priv->ops->tx_fill_descriptor(dev, pdesc, tcb_desc, pnewskb);
 754        __skb_queue_tail(&ring->queue, pnewskb);
 755        pdesc->OWN = 1;
 756
 757        return;
 758}
 759
 760static void rtl8192_stop_beacon(struct net_device *dev)
 761{
 762}
 763
 764void rtl8192_config_rate(struct net_device *dev, u16 *rate_config)
 765{
 766        struct r8192_priv *priv = rtllib_priv(dev);
 767        struct rtllib_network *net;
 768        u8 i = 0, basic_rate = 0;
 769        net = &priv->rtllib->current_network;
 770
 771        for (i = 0; i < net->rates_len; i++) {
 772                basic_rate = net->rates[i] & 0x7f;
 773                switch (basic_rate) {
 774                case MGN_1M:
 775                        *rate_config |= RRSR_1M;
 776                        break;
 777                case MGN_2M:
 778                        *rate_config |= RRSR_2M;
 779                        break;
 780                case MGN_5_5M:
 781                        *rate_config |= RRSR_5_5M;
 782                        break;
 783                case MGN_11M:
 784                        *rate_config |= RRSR_11M;
 785                        break;
 786                case MGN_6M:
 787                        *rate_config |= RRSR_6M;
 788                        break;
 789                case MGN_9M:
 790                        *rate_config |= RRSR_9M;
 791                        break;
 792                case MGN_12M:
 793                        *rate_config |= RRSR_12M;
 794                        break;
 795                case MGN_18M:
 796                        *rate_config |= RRSR_18M;
 797                        break;
 798                case MGN_24M:
 799                        *rate_config |= RRSR_24M;
 800                        break;
 801                case MGN_36M:
 802                        *rate_config |= RRSR_36M;
 803                        break;
 804                case MGN_48M:
 805                        *rate_config |= RRSR_48M;
 806                        break;
 807                case MGN_54M:
 808                        *rate_config |= RRSR_54M;
 809                        break;
 810                }
 811        }
 812
 813        for (i = 0; i < net->rates_ex_len; i++) {
 814                basic_rate = net->rates_ex[i] & 0x7f;
 815                switch (basic_rate) {
 816                case MGN_1M:
 817                        *rate_config |= RRSR_1M;
 818                        break;
 819                case MGN_2M:
 820                        *rate_config |= RRSR_2M;
 821                        break;
 822                case MGN_5_5M:
 823                        *rate_config |= RRSR_5_5M;
 824                        break;
 825                case MGN_11M:
 826                        *rate_config |= RRSR_11M;
 827                        break;
 828                case MGN_6M:
 829                        *rate_config |= RRSR_6M;
 830                        break;
 831                case MGN_9M:
 832                        *rate_config |= RRSR_9M;
 833                        break;
 834                case MGN_12M:
 835                        *rate_config |= RRSR_12M;
 836                        break;
 837                case MGN_18M:
 838                        *rate_config |= RRSR_18M;
 839                        break;
 840                case MGN_24M:
 841                        *rate_config |= RRSR_24M;
 842                        break;
 843                case MGN_36M:
 844                        *rate_config |= RRSR_36M;
 845                        break;
 846                case MGN_48M:
 847                        *rate_config |= RRSR_48M;
 848                        break;
 849                case MGN_54M:
 850                        *rate_config |= RRSR_54M;
 851                        break;
 852                }
 853        }
 854}
 855
 856static void rtl8192_refresh_supportrate(struct r8192_priv *priv)
 857{
 858        struct rtllib_device *ieee = priv->rtllib;
 859        if (ieee->mode == WIRELESS_MODE_N_24G ||
 860            ieee->mode == WIRELESS_MODE_N_5G) {
 861                memcpy(ieee->Regdot11HTOperationalRateSet,
 862                       ieee->RegHTSuppRateSet, 16);
 863                memcpy(ieee->Regdot11TxHTOperationalRateSet,
 864                       ieee->RegHTSuppRateSet, 16);
 865
 866        } else {
 867                memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
 868        }
 869        return;
 870}
 871
 872static u8 rtl8192_getSupportedWireleeMode(struct net_device *dev)
 873{
 874        struct r8192_priv *priv = rtllib_priv(dev);
 875        u8 ret = 0;
 876
 877        switch (priv->rf_chip) {
 878        case RF_8225:
 879        case RF_8256:
 880        case RF_6052:
 881        case RF_PSEUDO_11N:
 882                ret = (WIRELESS_MODE_N_24G|WIRELESS_MODE_G | WIRELESS_MODE_B);
 883                break;
 884        case RF_8258:
 885                ret = (WIRELESS_MODE_A | WIRELESS_MODE_N_5G);
 886                break;
 887        default:
 888                ret = WIRELESS_MODE_B;
 889                break;
 890        }
 891        return ret;
 892}
 893
 894void rtl8192_SetWirelessMode(struct net_device *dev, u8 wireless_mode)
 895{
 896        struct r8192_priv *priv = rtllib_priv(dev);
 897        u8 bSupportMode = rtl8192_getSupportedWireleeMode(dev);
 898
 899        if ((wireless_mode == WIRELESS_MODE_AUTO) ||
 900            ((wireless_mode & bSupportMode) == 0)) {
 901                if (bSupportMode & WIRELESS_MODE_N_24G) {
 902                        wireless_mode = WIRELESS_MODE_N_24G;
 903                } else if (bSupportMode & WIRELESS_MODE_N_5G) {
 904                        wireless_mode = WIRELESS_MODE_N_5G;
 905                } else if ((bSupportMode & WIRELESS_MODE_A)) {
 906                        wireless_mode = WIRELESS_MODE_A;
 907                } else if ((bSupportMode & WIRELESS_MODE_G)) {
 908                        wireless_mode = WIRELESS_MODE_G;
 909                } else if ((bSupportMode & WIRELESS_MODE_B)) {
 910                        wireless_mode = WIRELESS_MODE_B;
 911                } else {
 912                        RT_TRACE(COMP_ERR, "%s(), No valid wireless mode "
 913                                 "supported (%x)!!!\n", __func__, bSupportMode);
 914                        wireless_mode = WIRELESS_MODE_B;
 915                }
 916        }
 917
 918        if ((wireless_mode & (WIRELESS_MODE_B | WIRELESS_MODE_G)) ==
 919            (WIRELESS_MODE_G | WIRELESS_MODE_B))
 920                wireless_mode = WIRELESS_MODE_G;
 921
 922        priv->rtllib->mode = wireless_mode;
 923
 924        ActUpdateChannelAccessSetting(dev, wireless_mode,
 925                                      &priv->ChannelAccessSetting);
 926
 927        if ((wireless_mode == WIRELESS_MODE_N_24G) ||
 928            (wireless_mode == WIRELESS_MODE_N_5G)) {
 929                priv->rtllib->pHTInfo->bEnableHT = 1;
 930        RT_TRACE(COMP_DBG, "%s(), wireless_mode:%x, bEnableHT = 1\n",
 931                 __func__, wireless_mode);
 932        } else {
 933                priv->rtllib->pHTInfo->bEnableHT = 0;
 934                RT_TRACE(COMP_DBG, "%s(), wireless_mode:%x, bEnableHT = 0\n",
 935                         __func__, wireless_mode);
 936        }
 937
 938        RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
 939        rtl8192_refresh_supportrate(priv);
 940}
 941
 942static int _rtl8192_sta_up(struct net_device *dev, bool is_silent_reset)
 943{
 944        struct r8192_priv *priv = rtllib_priv(dev);
 945        struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
 946                                        (&(priv->rtllib->PowerSaveControl));
 947        bool init_status = true;
 948        priv->bDriverIsGoingToUnload = false;
 949        priv->bdisable_nic = false;
 950
 951        priv->up = 1;
 952        priv->rtllib->ieee_up = 1;
 953
 954        priv->up_first_time = 0;
 955        RT_TRACE(COMP_INIT, "Bringing up iface");
 956        priv->bfirst_init = true;
 957        init_status = priv->ops->initialize_adapter(dev);
 958        if (init_status != true) {
 959                RT_TRACE(COMP_ERR, "ERR!!! %s(): initialization is failed!\n",
 960                         __func__);
 961                priv->bfirst_init = false;
 962                return -1;
 963        }
 964
 965        RT_TRACE(COMP_INIT, "start adapter finished\n");
 966        RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
 967        priv->bfirst_init = false;
 968
 969        if (priv->polling_timer_on == 0)
 970                check_rfctrl_gpio_timer((unsigned long)dev);
 971
 972        if (priv->rtllib->state != RTLLIB_LINKED)
 973                rtllib_softmac_start_protocol(priv->rtllib, 0);
 974        rtllib_reset_queue(priv->rtllib);
 975        watch_dog_timer_callback((unsigned long) dev);
 976
 977        if (!netif_queue_stopped(dev))
 978                netif_start_queue(dev);
 979        else
 980                netif_wake_queue(dev);
 981
 982        return 0;
 983}
 984
 985static int rtl8192_sta_down(struct net_device *dev, bool shutdownrf)
 986{
 987        struct r8192_priv *priv = rtllib_priv(dev);
 988        unsigned long flags = 0;
 989        u8 RFInProgressTimeOut = 0;
 990
 991        if (priv->up == 0)
 992                return -1;
 993
 994        if (priv->rtllib->rtllib_ips_leave != NULL)
 995                priv->rtllib->rtllib_ips_leave(dev);
 996
 997        if (priv->rtllib->state == RTLLIB_LINKED)
 998                LeisurePSLeave(dev);
 999
1000        priv->bDriverIsGoingToUnload = true;
1001        priv->up = 0;
1002        priv->rtllib->ieee_up = 0;
1003        priv->bfirst_after_down = 1;
1004        RT_TRACE(COMP_DOWN, "==========>%s()\n", __func__);
1005        if (!netif_queue_stopped(dev))
1006                netif_stop_queue(dev);
1007
1008        priv->rtllib->wpa_ie_len = 0;
1009        kfree(priv->rtllib->wpa_ie);
1010        priv->rtllib->wpa_ie = NULL;
1011        CamResetAllEntry(dev);
1012        memset(priv->rtllib->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
1013        rtl8192_irq_disable(dev);
1014
1015        del_timer_sync(&priv->watch_dog_timer);
1016        rtl8192_cancel_deferred_work(priv);
1017        cancel_delayed_work(&priv->rtllib->hw_wakeup_wq);
1018
1019        rtllib_softmac_stop_protocol(priv->rtllib, 0, true);
1020        spin_lock_irqsave(&priv->rf_ps_lock, flags);
1021        while (priv->RFChangeInProgress) {
1022                spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
1023                if (RFInProgressTimeOut > 100) {
1024                        spin_lock_irqsave(&priv->rf_ps_lock, flags);
1025                        break;
1026                }
1027                RT_TRACE(COMP_DBG, "===>%s():RF is in progress, need to wait "
1028                         "until rf change is done.\n", __func__);
1029                mdelay(1);
1030                RFInProgressTimeOut++;
1031                spin_lock_irqsave(&priv->rf_ps_lock, flags);
1032        }
1033        priv->RFChangeInProgress = true;
1034        spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
1035        priv->ops->stop_adapter(dev, false);
1036        spin_lock_irqsave(&priv->rf_ps_lock, flags);
1037        priv->RFChangeInProgress = false;
1038        spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
1039        udelay(100);
1040        memset(&priv->rtllib->current_network, 0,
1041               offsetof(struct rtllib_network, list));
1042        RT_TRACE(COMP_DOWN, "<==========%s()\n", __func__);
1043
1044        return 0;
1045}
1046
1047static void rtl8192_init_priv_handler(struct net_device *dev)
1048{
1049        struct r8192_priv *priv = rtllib_priv(dev);
1050
1051        priv->rtllib->softmac_hard_start_xmit   = rtl8192_hard_start_xmit;
1052        priv->rtllib->set_chan                  = rtl8192_set_chan;
1053        priv->rtllib->link_change               = priv->ops->link_change;
1054        priv->rtllib->softmac_data_hard_start_xmit = rtl8192_hard_data_xmit;
1055        priv->rtllib->data_hard_stop            = rtl8192_data_hard_stop;
1056        priv->rtllib->data_hard_resume          = rtl8192_data_hard_resume;
1057        priv->rtllib->check_nic_enough_desc     = rtl8192_check_nic_enough_desc;
1058        priv->rtllib->get_nic_desc_num          = rtl8192_get_nic_desc_num;
1059        priv->rtllib->handle_assoc_response     = rtl8192_handle_assoc_response;
1060        priv->rtllib->handle_beacon             = rtl8192_handle_beacon;
1061        priv->rtllib->SetWirelessMode           = rtl8192_SetWirelessMode;
1062        priv->rtllib->LeisurePSLeave            = LeisurePSLeave;
1063        priv->rtllib->SetBWModeHandler          = rtl8192_SetBWMode;
1064        priv->rf_set_chan                       = rtl8192_phy_SwChnl;
1065
1066        priv->rtllib->start_send_beacons = rtl8192e_start_beacon;
1067        priv->rtllib->stop_send_beacons = rtl8192_stop_beacon;
1068
1069        priv->rtllib->sta_wake_up = rtl8192_hw_wakeup;
1070        priv->rtllib->enter_sleep_state = rtl8192_hw_to_sleep;
1071        priv->rtllib->ps_is_queue_empty = rtl8192_is_tx_queue_empty;
1072
1073        priv->rtllib->GetNmodeSupportBySecCfg = rtl8192_GetNmodeSupportBySecCfg;
1074        priv->rtllib->GetHalfNmodeSupportByAPsHandler =
1075                                         rtl8192_GetHalfNmodeSupportByAPs;
1076
1077        priv->rtllib->SetHwRegHandler = rtl8192e_SetHwReg;
1078        priv->rtllib->AllowAllDestAddrHandler = rtl8192_AllowAllDestAddr;
1079        priv->rtllib->SetFwCmdHandler = NULL;
1080        priv->rtllib->InitialGainHandler = InitialGain819xPci;
1081        priv->rtllib->rtllib_ips_leave_wq = rtllib_ips_leave_wq;
1082        priv->rtllib->rtllib_ips_leave = rtllib_ips_leave;
1083
1084        priv->rtllib->LedControlHandler = NULL;
1085        priv->rtllib->UpdateBeaconInterruptHandler = NULL;
1086
1087        priv->rtllib->ScanOperationBackupHandler = PHY_ScanOperationBackup8192;
1088
1089        priv->rtllib->rtllib_rfkill_poll = NULL;
1090}
1091
1092static void rtl8192_init_priv_constant(struct net_device *dev)
1093{
1094        struct r8192_priv *priv = rtllib_priv(dev);
1095        struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
1096                                        &(priv->rtllib->PowerSaveControl);
1097
1098        pPSC->RegMaxLPSAwakeIntvl = 5;
1099
1100        priv->RegPciASPM = 2;
1101
1102        priv->RegDevicePciASPMSetting = 0x03;
1103
1104        priv->RegHostPciASPMSetting = 0x02;
1105
1106        priv->RegHwSwRfOffD3 = 2;
1107
1108        priv->RegSupportPciASPM = 2;
1109}
1110
1111
1112static void rtl8192_init_priv_variable(struct net_device *dev)
1113{
1114        struct r8192_priv *priv = rtllib_priv(dev);
1115        u8 i;
1116
1117        priv->AcmMethod = eAcmWay2_SW;
1118        priv->dot11CurrentPreambleMode = PREAMBLE_AUTO;
1119        priv->rtllib->hwscan_sem_up = 1;
1120        priv->rtllib->status = 0;
1121        priv->H2CTxCmdSeq = 0;
1122        priv->bDisableFrameBursting = 0;
1123        priv->bDMInitialGainEnable = 1;
1124        priv->polling_timer_on = 0;
1125        priv->up_first_time = 1;
1126        priv->blinked_ingpio = false;
1127        priv->bDriverIsGoingToUnload = false;
1128        priv->being_init_adapter = false;
1129        priv->initialized_at_probe = false;
1130        priv->sw_radio_on = true;
1131        priv->bdisable_nic = false;
1132        priv->bfirst_init = false;
1133        priv->txringcount = 64;
1134        priv->rxbuffersize = 9100;
1135        priv->rxringcount = MAX_RX_COUNT;
1136        priv->irq_enabled = 0;
1137        priv->chan = 1;
1138        priv->RegWirelessMode = WIRELESS_MODE_AUTO;
1139        priv->RegChannelPlan = 0xf;
1140        priv->nrxAMPDU_size = 0;
1141        priv->nrxAMPDU_aggr_num = 0;
1142        priv->last_rxdesc_tsf_high = 0;
1143        priv->last_rxdesc_tsf_low = 0;
1144        priv->rtllib->mode = WIRELESS_MODE_AUTO;
1145        priv->rtllib->iw_mode = IW_MODE_INFRA;
1146        priv->rtllib->bNetPromiscuousMode = false;
1147        priv->rtllib->IntelPromiscuousModeInfo.bPromiscuousOn = false;
1148        priv->rtllib->IntelPromiscuousModeInfo.bFilterSourceStationFrame =
1149                                                                 false;
1150        priv->rtllib->ieee_up = 0;
1151        priv->retry_rts = DEFAULT_RETRY_RTS;
1152        priv->retry_data = DEFAULT_RETRY_DATA;
1153        priv->rtllib->rts = DEFAULT_RTS_THRESHOLD;
1154        priv->rtllib->rate = 110;
1155        priv->rtllib->short_slot = 1;
1156        priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
1157        priv->bcck_in_ch14 = false;
1158        priv->bfsync_processing  = false;
1159        priv->CCKPresentAttentuation = 0;
1160        priv->rfa_txpowertrackingindex = 0;
1161        priv->rfc_txpowertrackingindex = 0;
1162        priv->CckPwEnl = 6;
1163        priv->ScanDelay = 50;
1164        priv->ResetProgress = RESET_TYPE_NORESET;
1165        priv->bForcedSilentReset = 0;
1166        priv->bDisableNormalResetCheck = false;
1167        priv->force_reset = false;
1168        memset(priv->rtllib->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
1169
1170        memset(&priv->InterruptLog, 0, sizeof(struct log_int_8190));
1171        priv->RxCounter = 0;
1172        priv->rtllib->wx_set_enc = 0;
1173        priv->bHwRadioOff = false;
1174        priv->RegRfOff = 0;
1175        priv->isRFOff = false;
1176        priv->bInPowerSaveMode = false;
1177        priv->rtllib->RfOffReason = 0;
1178        priv->RFChangeInProgress = false;
1179        priv->bHwRfOffAction = 0;
1180        priv->SetRFPowerStateInProgress = false;
1181        priv->rtllib->PowerSaveControl.bInactivePs = true;
1182        priv->rtllib->PowerSaveControl.bIPSModeBackup = false;
1183        priv->rtllib->PowerSaveControl.bLeisurePs = true;
1184        priv->rtllib->PowerSaveControl.bFwCtrlLPS = false;
1185        priv->rtllib->LPSDelayCnt = 0;
1186        priv->rtllib->sta_sleep = LPS_IS_WAKE;
1187        priv->rtllib->eRFPowerState = eRfOn;
1188
1189        priv->txpower_checkcnt = 0;
1190        priv->thermal_readback_index = 0;
1191        priv->txpower_tracking_callback_cnt = 0;
1192        priv->ccktxpower_adjustcnt_ch14 = 0;
1193        priv->ccktxpower_adjustcnt_not_ch14 = 0;
1194
1195        priv->rtllib->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
1196        priv->rtllib->iw_mode = IW_MODE_INFRA;
1197        priv->rtllib->active_scan = 1;
1198        priv->rtllib->be_scan_inprogress = false;
1199        priv->rtllib->modulation = RTLLIB_CCK_MODULATION |
1200                                   RTLLIB_OFDM_MODULATION;
1201        priv->rtllib->host_encrypt = 1;
1202        priv->rtllib->host_decrypt = 1;
1203
1204        priv->rtllib->dot11PowerSaveMode = eActive;
1205        priv->rtllib->fts = DEFAULT_FRAG_THRESHOLD;
1206        priv->rtllib->MaxMssDensity = 0;
1207        priv->rtllib->MinSpaceCfg = 0;
1208
1209        priv->card_type = PCI;
1210
1211        priv->AcmControl = 0;
1212        priv->pFirmware = vzalloc(sizeof(struct rt_firmware));
1213        if (!priv->pFirmware)
1214                printk(KERN_ERR "rtl8192e: Unable to allocate space "
1215                       "for firmware\n");
1216
1217        skb_queue_head_init(&priv->rx_queue);
1218        skb_queue_head_init(&priv->skb_queue);
1219
1220        for (i = 0; i < MAX_QUEUE_SIZE; i++)
1221                skb_queue_head_init(&priv->rtllib->skb_waitQ[i]);
1222        for (i = 0; i < MAX_QUEUE_SIZE; i++)
1223                skb_queue_head_init(&priv->rtllib->skb_aggQ[i]);
1224}
1225
1226static void rtl8192_init_priv_lock(struct r8192_priv *priv)
1227{
1228        spin_lock_init(&priv->fw_scan_lock);
1229        spin_lock_init(&priv->tx_lock);
1230        spin_lock_init(&priv->irq_lock);
1231        spin_lock_init(&priv->irq_th_lock);
1232        spin_lock_init(&priv->rf_ps_lock);
1233        spin_lock_init(&priv->ps_lock);
1234        spin_lock_init(&priv->rf_lock);
1235        spin_lock_init(&priv->rt_h2c_lock);
1236        sema_init(&priv->wx_sem, 1);
1237        sema_init(&priv->rf_sem, 1);
1238        mutex_init(&priv->mutex);
1239}
1240
1241static void rtl8192_init_priv_task(struct net_device *dev)
1242{
1243        struct r8192_priv *priv = rtllib_priv(dev);
1244
1245        priv->priv_wq = create_workqueue(DRV_NAME);
1246        INIT_WORK_RSL(&priv->reset_wq, (void *)rtl8192_restart, dev);
1247        INIT_WORK_RSL(&priv->rtllib->ips_leave_wq, (void *)IPSLeave_wq, dev);
1248        INIT_DELAYED_WORK_RSL(&priv->watch_dog_wq,
1249                              (void *)rtl819x_watchdog_wqcallback, dev);
1250        INIT_DELAYED_WORK_RSL(&priv->txpower_tracking_wq,
1251                              (void *)dm_txpower_trackingcallback, dev);
1252        INIT_DELAYED_WORK_RSL(&priv->rfpath_check_wq,
1253                              (void *)dm_rf_pathcheck_workitemcallback, dev);
1254        INIT_DELAYED_WORK_RSL(&priv->update_beacon_wq,
1255                              (void *)rtl8192_update_beacon, dev);
1256        INIT_WORK_RSL(&priv->qos_activate, (void *)rtl8192_qos_activate, dev);
1257        INIT_DELAYED_WORK_RSL(&priv->rtllib->hw_wakeup_wq,
1258                              (void *) rtl8192_hw_wakeup_wq, dev);
1259        INIT_DELAYED_WORK_RSL(&priv->rtllib->hw_sleep_wq,
1260                              (void *) rtl8192_hw_sleep_wq, dev);
1261        tasklet_init(&priv->irq_rx_tasklet,
1262                     (void(*)(unsigned long))rtl8192_irq_rx_tasklet,
1263                     (unsigned long)priv);
1264        tasklet_init(&priv->irq_tx_tasklet,
1265                     (void(*)(unsigned long))rtl8192_irq_tx_tasklet,
1266                     (unsigned long)priv);
1267        tasklet_init(&priv->irq_prepare_beacon_tasklet,
1268                     (void(*)(unsigned long))rtl8192_prepare_beacon,
1269                     (unsigned long)priv);
1270}
1271
1272static short rtl8192_get_channel_map(struct net_device *dev)
1273{
1274        int i;
1275
1276        struct r8192_priv *priv = rtllib_priv(dev);
1277        if ((priv->rf_chip != RF_8225) && (priv->rf_chip != RF_8256)
1278                        && (priv->rf_chip != RF_6052)) {
1279                RT_TRACE(COMP_ERR, "%s: unknown rf chip, can't set channel "
1280                         "map\n", __func__);
1281                return -1;
1282        }
1283
1284        if (priv->ChannelPlan >= COUNTRY_CODE_MAX) {
1285                printk(KERN_INFO "rtl819x_init:Error channel plan! Set to "
1286                       "default.\n");
1287                priv->ChannelPlan = COUNTRY_CODE_FCC;
1288        }
1289        RT_TRACE(COMP_INIT, "Channel plan is %d\n", priv->ChannelPlan);
1290        dot11d_init(priv->rtllib);
1291        Dot11d_Channelmap(priv->ChannelPlan, priv->rtllib);
1292        for (i = 1; i <= 11; i++)
1293                (priv->rtllib->active_channel_map)[i] = 1;
1294        (priv->rtllib->active_channel_map)[12] = 2;
1295        (priv->rtllib->active_channel_map)[13] = 2;
1296
1297        return 0;
1298}
1299
1300static short rtl8192_init(struct net_device *dev)
1301{
1302        struct r8192_priv *priv = rtllib_priv(dev);
1303
1304        memset(&(priv->stats), 0, sizeof(struct rt_stats));
1305
1306        rtl8192_init_priv_handler(dev);
1307        rtl8192_init_priv_constant(dev);
1308        rtl8192_init_priv_variable(dev);
1309        rtl8192_init_priv_lock(priv);
1310        rtl8192_init_priv_task(dev);
1311        priv->ops->get_eeprom_size(dev);
1312        priv->ops->init_adapter_variable(dev);
1313        rtl8192_get_channel_map(dev);
1314
1315        init_hal_dm(dev);
1316
1317        init_timer(&priv->watch_dog_timer);
1318        setup_timer(&priv->watch_dog_timer,
1319                    watch_dog_timer_callback,
1320                    (unsigned long) dev);
1321
1322        init_timer(&priv->gpio_polling_timer);
1323        setup_timer(&priv->gpio_polling_timer,
1324                    check_rfctrl_gpio_timer,
1325                    (unsigned long)dev);
1326
1327        rtl8192_irq_disable(dev);
1328        if (request_irq(dev->irq, rtl8192_interrupt, IRQF_SHARED,
1329            dev->name, dev)) {
1330                printk(KERN_ERR "Error allocating IRQ %d", dev->irq);
1331                return -1;
1332        } else {
1333                priv->irq = dev->irq;
1334                RT_TRACE(COMP_INIT, "IRQ %d\n", dev->irq);
1335        }
1336
1337        if (rtl8192_pci_initdescring(dev) != 0) {
1338                printk(KERN_ERR "Endopoints initialization failed");
1339                free_irq(dev->irq, dev);
1340                return -1;
1341        }
1342
1343        return 0;
1344}
1345
1346/***************************************************************************
1347        -------------------------------WATCHDOG STUFF---------------------------
1348***************************************************************************/
1349short rtl8192_is_tx_queue_empty(struct net_device *dev)
1350{
1351        int i = 0;
1352        struct r8192_priv *priv = rtllib_priv(dev);
1353        for (i = 0; i <= MGNT_QUEUE; i++) {
1354                if ((i == TXCMD_QUEUE) || (i == HCCA_QUEUE))
1355                        continue;
1356                if (skb_queue_len(&(&priv->tx_ring[i])->queue) > 0) {
1357                        printk(KERN_INFO "===>tx queue is not empty:%d, %d\n",
1358                               i, skb_queue_len(&(&priv->tx_ring[i])->queue));
1359                        return 0;
1360                }
1361        }
1362        return 1;
1363}
1364
1365static enum reset_type rtl819x_TxCheckStuck(struct net_device *dev)
1366{
1367        struct r8192_priv *priv = rtllib_priv(dev);
1368        u8      QueueID;
1369        u8      ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
1370        bool    bCheckFwTxCnt = false;
1371        struct rtl8192_tx_ring  *ring = NULL;
1372        struct sk_buff *skb = NULL;
1373        struct cb_desc *tcb_desc = NULL;
1374        unsigned long flags = 0;
1375
1376        switch (priv->rtllib->ps) {
1377        case RTLLIB_PS_DISABLED:
1378                ResetThreshold = NIC_SEND_HANG_THRESHOLD_NORMAL;
1379                break;
1380        case (RTLLIB_PS_MBCAST|RTLLIB_PS_UNICAST):
1381                ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
1382                break;
1383        default:
1384                ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
1385                break;
1386        }
1387        spin_lock_irqsave(&priv->irq_th_lock, flags);
1388        for (QueueID = 0; QueueID < MAX_TX_QUEUE; QueueID++) {
1389                if (QueueID == TXCMD_QUEUE)
1390                        continue;
1391
1392                if (QueueID == BEACON_QUEUE)
1393                        continue;
1394
1395                ring = &priv->tx_ring[QueueID];
1396
1397                if (skb_queue_len(&ring->queue) == 0) {
1398                        continue;
1399                } else {
1400                        skb = (&ring->queue)->next;
1401                        tcb_desc = (struct cb_desc *)(skb->cb +
1402                                    MAX_DEV_ADDR_SIZE);
1403                        tcb_desc->nStuckCount++;
1404                        bCheckFwTxCnt = true;
1405                        if (tcb_desc->nStuckCount > 1)
1406                                printk(KERN_INFO "%s: QueueID=%d tcb_desc->n"
1407                                       "StuckCount=%d\n", __func__, QueueID,
1408                                       tcb_desc->nStuckCount);
1409                }
1410        }
1411        spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1412
1413        if (bCheckFwTxCnt) {
1414                if (priv->ops->TxCheckStuckHandler(dev)) {
1415                        RT_TRACE(COMP_RESET, "TxCheckStuck(): Fw indicates no"
1416                                 " Tx condition!\n");
1417                        return RESET_TYPE_SILENT;
1418                }
1419        }
1420
1421        return RESET_TYPE_NORESET;
1422}
1423
1424static enum reset_type rtl819x_RxCheckStuck(struct net_device *dev)
1425{
1426        struct r8192_priv *priv = rtllib_priv(dev);
1427
1428        if (priv->ops->RxCheckStuckHandler(dev)) {
1429                RT_TRACE(COMP_RESET, "RxStuck Condition\n");
1430                return RESET_TYPE_SILENT;
1431        }
1432
1433        return RESET_TYPE_NORESET;
1434}
1435
1436static enum reset_type rtl819x_ifcheck_resetornot(struct net_device *dev)
1437{
1438        struct r8192_priv *priv = rtllib_priv(dev);
1439        enum reset_type TxResetType = RESET_TYPE_NORESET;
1440        enum reset_type RxResetType = RESET_TYPE_NORESET;
1441        enum rt_rf_power_state rfState;
1442
1443        rfState = priv->rtllib->eRFPowerState;
1444
1445        if (rfState == eRfOn)
1446                TxResetType = rtl819x_TxCheckStuck(dev);
1447
1448        if (rfState == eRfOn &&
1449            (priv->rtllib->iw_mode == IW_MODE_INFRA) &&
1450            (priv->rtllib->state == RTLLIB_LINKED))
1451                RxResetType = rtl819x_RxCheckStuck(dev);
1452
1453        if (TxResetType == RESET_TYPE_NORMAL ||
1454            RxResetType == RESET_TYPE_NORMAL) {
1455                printk(KERN_INFO "%s(): TxResetType is %d, RxResetType is %d\n",
1456                       __func__, TxResetType, RxResetType);
1457                return RESET_TYPE_NORMAL;
1458        } else if (TxResetType == RESET_TYPE_SILENT ||
1459                   RxResetType == RESET_TYPE_SILENT) {
1460                printk(KERN_INFO "%s(): TxResetType is %d, RxResetType is %d\n",
1461                       __func__, TxResetType, RxResetType);
1462                return RESET_TYPE_SILENT;
1463        } else {
1464                return RESET_TYPE_NORESET;
1465        }
1466
1467}
1468
1469static void rtl819x_silentreset_mesh_bk(struct net_device *dev, u8 IsPortal)
1470{
1471}
1472
1473static void rtl819x_ifsilentreset(struct net_device *dev)
1474{
1475        struct r8192_priv *priv = rtllib_priv(dev);
1476        u8      reset_times = 0;
1477        int reset_status = 0;
1478        struct rtllib_device *ieee = priv->rtllib;
1479        unsigned long flag;
1480
1481        u8 IsPortal = 0;
1482
1483
1484        if (priv->ResetProgress == RESET_TYPE_NORESET) {
1485
1486                RT_TRACE(COMP_RESET, "=========>Reset progress!!\n");
1487
1488                priv->ResetProgress = RESET_TYPE_SILENT;
1489
1490                spin_lock_irqsave(&priv->rf_ps_lock, flag);
1491                if (priv->RFChangeInProgress) {
1492                        spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1493                        goto END;
1494                }
1495                priv->RFChangeInProgress = true;
1496                priv->bResetInProgress = true;
1497                spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1498
1499RESET_START:
1500
1501                down(&priv->wx_sem);
1502
1503                if (priv->rtllib->state == RTLLIB_LINKED)
1504                        LeisurePSLeave(dev);
1505
1506                if (IS_NIC_DOWN(priv)) {
1507                        RT_TRACE(COMP_ERR, "%s():the driver is not up! "
1508                                 "return\n", __func__);
1509                        up(&priv->wx_sem);
1510                        return ;
1511                }
1512                priv->up = 0;
1513
1514                RT_TRACE(COMP_RESET, "%s():======>start to down the driver\n",
1515                          __func__);
1516                mdelay(1000);
1517                RT_TRACE(COMP_RESET, "%s():111111111111111111111111======>start"
1518                         " to down the driver\n", __func__);
1519
1520                if (!netif_queue_stopped(dev))
1521                        netif_stop_queue(dev);
1522
1523                rtl8192_irq_disable(dev);
1524                del_timer_sync(&priv->watch_dog_timer);
1525                rtl8192_cancel_deferred_work(priv);
1526                deinit_hal_dm(dev);
1527                rtllib_stop_scan_syncro(ieee);
1528
1529                if (ieee->state == RTLLIB_LINKED) {
1530                        SEM_DOWN_IEEE_WX(&ieee->wx_sem);
1531                        printk(KERN_INFO "ieee->state is RTLLIB_LINKED\n");
1532                        rtllib_stop_send_beacons(priv->rtllib);
1533                        del_timer_sync(&ieee->associate_timer);
1534                        cancel_delayed_work(&ieee->associate_retry_wq);
1535                        rtllib_stop_scan(ieee);
1536                        netif_carrier_off(dev);
1537                        SEM_UP_IEEE_WX(&ieee->wx_sem);
1538                } else {
1539                        printk(KERN_INFO "ieee->state is NOT LINKED\n");
1540                        rtllib_softmac_stop_protocol(priv->rtllib, 0 , true);
1541                }
1542
1543                dm_backup_dynamic_mechanism_state(dev);
1544
1545                up(&priv->wx_sem);
1546                RT_TRACE(COMP_RESET, "%s():<==========down process is "
1547                         "finished\n", __func__);
1548
1549                RT_TRACE(COMP_RESET, "%s():<===========up process start\n",
1550                         __func__);
1551                reset_status = _rtl8192_up(dev, true);
1552
1553                RT_TRACE(COMP_RESET, "%s():<===========up process is "
1554                         "finished\n", __func__);
1555                if (reset_status == -1) {
1556                        if (reset_times < 3) {
1557                                reset_times++;
1558                                goto RESET_START;
1559                        } else {
1560                                RT_TRACE(COMP_ERR, " ERR!!! %s():  Reset "
1561                                         "Failed!!\n", __func__);
1562                        }
1563                }
1564
1565                ieee->is_silent_reset = 1;
1566
1567                spin_lock_irqsave(&priv->rf_ps_lock, flag);
1568                priv->RFChangeInProgress = false;
1569                spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1570
1571                EnableHWSecurityConfig8192(dev);
1572
1573                if (ieee->state == RTLLIB_LINKED && ieee->iw_mode ==
1574                    IW_MODE_INFRA) {
1575                        ieee->set_chan(ieee->dev,
1576                                       ieee->current_network.channel);
1577
1578                        queue_work_rsl(ieee->wq, &ieee->associate_complete_wq);
1579
1580                } else if (ieee->state == RTLLIB_LINKED && ieee->iw_mode ==
1581                           IW_MODE_ADHOC) {
1582                        ieee->set_chan(ieee->dev,
1583                                       ieee->current_network.channel);
1584                        ieee->link_change(ieee->dev);
1585
1586                        notify_wx_assoc_event(ieee);
1587
1588                        rtllib_start_send_beacons(ieee);
1589
1590                        if (ieee->data_hard_resume)
1591                                ieee->data_hard_resume(ieee->dev);
1592                        netif_carrier_on(ieee->dev);
1593                } else if (ieee->iw_mode == IW_MODE_MESH) {
1594                        rtl819x_silentreset_mesh_bk(dev, IsPortal);
1595                }
1596
1597                CamRestoreAllEntry(dev);
1598                dm_restore_dynamic_mechanism_state(dev);
1599END:
1600                priv->ResetProgress = RESET_TYPE_NORESET;
1601                priv->reset_count++;
1602
1603                priv->bForcedSilentReset = false;
1604                priv->bResetInProgress = false;
1605
1606                write_nic_byte(dev, UFWP, 1);
1607                RT_TRACE(COMP_RESET, "Reset finished!! ====>[%d]\n",
1608                         priv->reset_count);
1609        }
1610}
1611
1612static void rtl819x_update_rxcounts(struct r8192_priv *priv, u32 *TotalRxBcnNum,
1613                                    u32 *TotalRxDataNum)
1614{
1615        u16     SlotIndex;
1616        u8      i;
1617
1618        *TotalRxBcnNum = 0;
1619        *TotalRxDataNum = 0;
1620
1621        SlotIndex = (priv->rtllib->LinkDetectInfo.SlotIndex++) %
1622                        (priv->rtllib->LinkDetectInfo.SlotNum);
1623        priv->rtllib->LinkDetectInfo.RxBcnNum[SlotIndex] =
1624                        priv->rtllib->LinkDetectInfo.NumRecvBcnInPeriod;
1625        priv->rtllib->LinkDetectInfo.RxDataNum[SlotIndex] =
1626                        priv->rtllib->LinkDetectInfo.NumRecvDataInPeriod;
1627        for (i = 0; i < priv->rtllib->LinkDetectInfo.SlotNum; i++) {
1628                *TotalRxBcnNum += priv->rtllib->LinkDetectInfo.RxBcnNum[i];
1629                *TotalRxDataNum += priv->rtllib->LinkDetectInfo.RxDataNum[i];
1630        }
1631}
1632
1633
1634void    rtl819x_watchdog_wqcallback(void *data)
1635{
1636        struct r8192_priv *priv = container_of_dwork_rsl(data,
1637                                  struct r8192_priv, watch_dog_wq);
1638        struct net_device *dev = priv->rtllib->dev;
1639        struct rtllib_device *ieee = priv->rtllib;
1640        enum reset_type ResetType = RESET_TYPE_NORESET;
1641        static u8 check_reset_cnt;
1642        unsigned long flags;
1643        struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
1644                                        (&(priv->rtllib->PowerSaveControl));
1645        bool bBusyTraffic = false;
1646        bool    bHigherBusyTraffic = false;
1647        bool    bHigherBusyRxTraffic = false;
1648        bool bEnterPS = false;
1649
1650        if (IS_NIC_DOWN(priv) || (priv->bHwRadioOff == true))
1651                return;
1652
1653        if (priv->rtllib->state >= RTLLIB_LINKED) {
1654                if (priv->rtllib->CntAfterLink < 2)
1655                        priv->rtllib->CntAfterLink++;
1656        } else {
1657                priv->rtllib->CntAfterLink = 0;
1658        }
1659
1660        hal_dm_watchdog(dev);
1661
1662        if (rtllib_act_scanning(priv->rtllib, false) == false) {
1663                if ((ieee->iw_mode == IW_MODE_INFRA) && (ieee->state ==
1664                     RTLLIB_NOLINK) &&
1665                     (ieee->eRFPowerState == eRfOn) && !ieee->is_set_key &&
1666                     (!ieee->proto_stoppping) && !ieee->wx_set_enc) {
1667                        if ((ieee->PowerSaveControl.ReturnPoint ==
1668                             IPS_CALLBACK_NONE) &&
1669                             (!ieee->bNetPromiscuousMode)) {
1670                                RT_TRACE(COMP_PS, "====================>haha: "
1671                                         "IPSEnter()\n");
1672                                IPSEnter(dev);
1673                        }
1674                }
1675        }
1676        if ((ieee->state == RTLLIB_LINKED) && (ieee->iw_mode ==
1677             IW_MODE_INFRA) && (!ieee->bNetPromiscuousMode)) {
1678                if (ieee->LinkDetectInfo.NumRxOkInPeriod > 100 ||
1679                ieee->LinkDetectInfo.NumTxOkInPeriod > 100)
1680                        bBusyTraffic = true;
1681
1682
1683                if (ieee->LinkDetectInfo.NumRxOkInPeriod > 4000 ||
1684                    ieee->LinkDetectInfo.NumTxOkInPeriod > 4000) {
1685                        bHigherBusyTraffic = true;
1686                        if (ieee->LinkDetectInfo.NumRxOkInPeriod > 5000)
1687                                bHigherBusyRxTraffic = true;
1688                        else
1689                                bHigherBusyRxTraffic = false;
1690                }
1691
1692                if (((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod +
1693                    ieee->LinkDetectInfo.NumTxOkInPeriod) > 8) ||
1694                    (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2))
1695                        bEnterPS = false;
1696                else
1697                        bEnterPS = true;
1698
1699                if (ieee->current_network.beacon_interval < 95)
1700                        bEnterPS = false;
1701
1702                if (bEnterPS)
1703                        LeisurePSEnter(dev);
1704                else
1705                        LeisurePSLeave(dev);
1706
1707        } else {
1708                RT_TRACE(COMP_LPS, "====>no link LPS leave\n");
1709                LeisurePSLeave(dev);
1710        }
1711
1712        ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
1713        ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
1714        ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
1715        ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
1716
1717        ieee->LinkDetectInfo.bHigherBusyTraffic = bHigherBusyTraffic;
1718        ieee->LinkDetectInfo.bHigherBusyRxTraffic = bHigherBusyRxTraffic;
1719
1720        if (ieee->state == RTLLIB_LINKED && ieee->iw_mode == IW_MODE_INFRA) {
1721                u32     TotalRxBcnNum = 0;
1722                u32     TotalRxDataNum = 0;
1723
1724                rtl819x_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
1725
1726                if ((TotalRxBcnNum+TotalRxDataNum) == 0)
1727                        priv->check_roaming_cnt++;
1728                else
1729                        priv->check_roaming_cnt = 0;
1730
1731
1732                if (priv->check_roaming_cnt > 0) {
1733                        if (ieee->eRFPowerState == eRfOff)
1734                                RT_TRACE(COMP_ERR, "========>%s()\n", __func__);
1735
1736                        printk(KERN_INFO "===>%s(): AP is power off, chan:%d,"
1737                               " connect another one\n", __func__, priv->chan);
1738
1739                        ieee->state = RTLLIB_ASSOCIATING;
1740
1741                        RemovePeerTS(priv->rtllib,
1742                                     priv->rtllib->current_network.bssid);
1743                        ieee->is_roaming = true;
1744                        ieee->is_set_key = false;
1745                        ieee->link_change(dev);
1746                        if (ieee->LedControlHandler)
1747                                ieee->LedControlHandler(ieee->dev,
1748                                                        LED_CTL_START_TO_LINK);
1749
1750                        notify_wx_assoc_event(ieee);
1751
1752                        if (!(ieee->rtllib_ap_sec_type(ieee) &
1753                             (SEC_ALG_CCMP|SEC_ALG_TKIP)))
1754                                queue_delayed_work_rsl(ieee->wq,
1755                                        &ieee->associate_procedure_wq, 0);
1756
1757                        priv->check_roaming_cnt = 0;
1758                }
1759                ieee->LinkDetectInfo.NumRecvBcnInPeriod = 0;
1760                ieee->LinkDetectInfo.NumRecvDataInPeriod = 0;
1761
1762        }
1763
1764        spin_lock_irqsave(&priv->tx_lock, flags);
1765        if ((check_reset_cnt++ >= 3) && (!ieee->is_roaming) &&
1766            (!priv->RFChangeInProgress) && (!pPSC->bSwRfProcessing)) {
1767                ResetType = rtl819x_ifcheck_resetornot(dev);
1768                check_reset_cnt = 3;
1769        }
1770        spin_unlock_irqrestore(&priv->tx_lock, flags);
1771
1772        if (!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_NORMAL) {
1773                priv->ResetProgress = RESET_TYPE_NORMAL;
1774                RT_TRACE(COMP_RESET, "%s(): NOMAL RESET\n", __func__);
1775                return;
1776        }
1777
1778        if (((priv->force_reset) || (!priv->bDisableNormalResetCheck &&
1779              ResetType == RESET_TYPE_SILENT)))
1780                rtl819x_ifsilentreset(dev);
1781        priv->force_reset = false;
1782        priv->bForcedSilentReset = false;
1783        priv->bResetInProgress = false;
1784        RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
1785}
1786
1787void watch_dog_timer_callback(unsigned long data)
1788{
1789        struct r8192_priv *priv = rtllib_priv((struct net_device *)data);
1790        queue_delayed_work_rsl(priv->priv_wq, &priv->watch_dog_wq, 0);
1791        mod_timer(&priv->watch_dog_timer, jiffies +
1792                  MSECS(RTLLIB_WATCH_DOG_TIME));
1793}
1794
1795/****************************************************************************
1796 ---------------------------- NIC TX/RX STUFF---------------------------
1797*****************************************************************************/
1798void rtl8192_rx_enable(struct net_device *dev)
1799{
1800        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
1801        priv->ops->rx_enable(dev);
1802}
1803
1804void rtl8192_tx_enable(struct net_device *dev)
1805{
1806        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
1807
1808        priv->ops->tx_enable(dev);
1809
1810        rtllib_reset_queue(priv->rtllib);
1811}
1812
1813
1814static void rtl8192_free_rx_ring(struct net_device *dev)
1815{
1816        struct r8192_priv *priv = rtllib_priv(dev);
1817        int i, rx_queue_idx;
1818
1819        for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE;
1820             rx_queue_idx++) {
1821                for (i = 0; i < priv->rxringcount; i++) {
1822                        struct sk_buff *skb = priv->rx_buf[rx_queue_idx][i];
1823                        if (!skb)
1824                                continue;
1825
1826                        pci_unmap_single(priv->pdev,
1827                                *((dma_addr_t *)skb->cb),
1828                                priv->rxbuffersize, PCI_DMA_FROMDEVICE);
1829                                kfree_skb(skb);
1830                }
1831
1832                pci_free_consistent(priv->pdev,
1833                        sizeof(*priv->rx_ring[rx_queue_idx]) *
1834                        priv->rxringcount,
1835                        priv->rx_ring[rx_queue_idx],
1836                        priv->rx_ring_dma[rx_queue_idx]);
1837                priv->rx_ring[rx_queue_idx] = NULL;
1838        }
1839}
1840
1841static void rtl8192_free_tx_ring(struct net_device *dev, unsigned int prio)
1842{
1843        struct r8192_priv *priv = rtllib_priv(dev);
1844        struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1845
1846        while (skb_queue_len(&ring->queue)) {
1847                struct tx_desc *entry = &ring->desc[ring->idx];
1848                struct sk_buff *skb = __skb_dequeue(&ring->queue);
1849
1850                pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
1851                        skb->len, PCI_DMA_TODEVICE);
1852                kfree_skb(skb);
1853                ring->idx = (ring->idx + 1) % ring->entries;
1854        }
1855
1856        pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
1857        ring->desc, ring->dma);
1858        ring->desc = NULL;
1859}
1860
1861void rtl8192_data_hard_stop(struct net_device *dev)
1862{
1863}
1864
1865
1866void rtl8192_data_hard_resume(struct net_device *dev)
1867{
1868}
1869
1870void rtl8192_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
1871                            int rate)
1872{
1873        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
1874        int ret;
1875        struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1876                                    MAX_DEV_ADDR_SIZE);
1877        u8 queue_index = tcb_desc->queue_index;
1878
1879        if ((priv->rtllib->eRFPowerState == eRfOff) || IS_NIC_DOWN(priv) ||
1880             priv->bResetInProgress) {
1881                kfree_skb(skb);
1882                return;
1883        }
1884
1885        assert(queue_index != TXCMD_QUEUE);
1886
1887
1888        memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1889        skb_push(skb, priv->rtllib->tx_headroom);
1890        ret = rtl8192_tx(dev, skb);
1891        if (ret != 0) {
1892                kfree_skb(skb);
1893        };
1894
1895        if (queue_index != MGNT_QUEUE) {
1896                priv->rtllib->stats.tx_bytes += (skb->len -
1897                                                 priv->rtllib->tx_headroom);
1898                priv->rtllib->stats.tx_packets++;
1899        }
1900
1901
1902        return;
1903}
1904
1905int rtl8192_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1906{
1907        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
1908        int ret;
1909        struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1910                                    MAX_DEV_ADDR_SIZE);
1911        u8 queue_index = tcb_desc->queue_index;
1912
1913        if (queue_index != TXCMD_QUEUE) {
1914                if ((priv->rtllib->eRFPowerState == eRfOff) ||
1915                     IS_NIC_DOWN(priv) || priv->bResetInProgress) {
1916                        kfree_skb(skb);
1917                        return 0;
1918                }
1919        }
1920
1921        memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1922        if (queue_index == TXCMD_QUEUE) {
1923                rtl8192_tx_cmd(dev, skb);
1924                ret = 0;
1925                return ret;
1926        } else {
1927                tcb_desc->RATRIndex = 7;
1928                tcb_desc->bTxDisableRateFallBack = 1;
1929                tcb_desc->bTxUseDriverAssingedRate = 1;
1930                tcb_desc->bTxEnableFwCalcDur = 1;
1931                skb_push(skb, priv->rtllib->tx_headroom);
1932                ret = rtl8192_tx(dev, skb);
1933                if (ret != 0) {
1934                        kfree_skb(skb);
1935                };
1936        }
1937
1938
1939
1940        return ret;
1941
1942}
1943
1944static void rtl8192_tx_isr(struct net_device *dev, int prio)
1945{
1946        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
1947
1948        struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1949
1950        while (skb_queue_len(&ring->queue)) {
1951                struct tx_desc *entry = &ring->desc[ring->idx];
1952                struct sk_buff *skb;
1953
1954                if (prio != BEACON_QUEUE) {
1955                        if (entry->OWN)
1956                                return;
1957                        ring->idx = (ring->idx + 1) % ring->entries;
1958                }
1959
1960                skb = __skb_dequeue(&ring->queue);
1961                pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
1962                skb->len, PCI_DMA_TODEVICE);
1963
1964                kfree_skb(skb);
1965        }
1966        if (prio != BEACON_QUEUE)
1967                tasklet_schedule(&priv->irq_tx_tasklet);
1968}
1969
1970void rtl8192_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1971{
1972        struct r8192_priv *priv = rtllib_priv(dev);
1973        struct rtl8192_tx_ring *ring;
1974        struct tx_desc_cmd *entry;
1975        unsigned int idx;
1976        struct cb_desc *tcb_desc;
1977        unsigned long flags;
1978
1979        spin_lock_irqsave(&priv->irq_th_lock, flags);
1980        ring = &priv->tx_ring[TXCMD_QUEUE];
1981
1982        idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1983        entry = (struct tx_desc_cmd *) &ring->desc[idx];
1984
1985        tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1986
1987        priv->ops->tx_fill_cmd_descriptor(dev, entry, tcb_desc, skb);
1988
1989        __skb_queue_tail(&ring->queue, skb);
1990        spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1991
1992        return;
1993}
1994
1995short rtl8192_tx(struct net_device *dev, struct sk_buff *skb)
1996{
1997        struct r8192_priv *priv = rtllib_priv(dev);
1998        struct rtl8192_tx_ring  *ring;
1999        unsigned long flags;
2000        struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
2001                                    MAX_DEV_ADDR_SIZE);
2002        struct tx_desc *pdesc = NULL;
2003        struct rtllib_hdr_1addr *header = NULL;
2004        u16 fc = 0, type = 0, stype = 0;
2005        bool  multi_addr = false, broad_addr = false, uni_addr = false;
2006        u8 *pda_addr = NULL;
2007        int   idx;
2008        u32 fwinfo_size = 0;
2009
2010        if (priv->bdisable_nic) {
2011                RT_TRACE(COMP_ERR, "%s: ERR!! Nic is disabled! Can't tx packet"
2012                         " len=%d qidx=%d!!!\n", __func__, skb->len,
2013                         tcb_desc->queue_index);
2014                return skb->len;
2015        }
2016
2017        priv->rtllib->bAwakePktSent = true;
2018
2019        fwinfo_size = sizeof(struct tx_fwinfo_8190pci);
2020
2021        header = (struct rtllib_hdr_1addr *)(((u8 *)skb->data) + fwinfo_size);
2022        fc = header->frame_ctl;
2023        type = WLAN_FC_GET_TYPE(fc);
2024        stype = WLAN_FC_GET_STYPE(fc);
2025        pda_addr = header->addr1;
2026
2027        if (is_broadcast_ether_addr(pda_addr))
2028                broad_addr = true;
2029        else if (is_multicast_ether_addr(pda_addr))
2030                multi_addr = true;
2031        else
2032                uni_addr = true;
2033
2034        if (uni_addr)
2035                priv->stats.txbytesunicast += skb->len - fwinfo_size;
2036        else if (multi_addr)
2037                priv->stats.txbytesmulticast += skb->len - fwinfo_size;
2038        else
2039                priv->stats.txbytesbroadcast += skb->len - fwinfo_size;
2040
2041        spin_lock_irqsave(&priv->irq_th_lock, flags);
2042        ring = &priv->tx_ring[tcb_desc->queue_index];
2043        if (tcb_desc->queue_index != BEACON_QUEUE)
2044                idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
2045        else
2046                idx = 0;
2047
2048        pdesc = &ring->desc[idx];
2049        if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
2050                RT_TRACE(COMP_ERR, "No more TX desc@%d, ring->idx = %d, idx = "
2051                         "%d, skblen = 0x%x queuelen=%d",
2052                         tcb_desc->queue_index, ring->idx, idx, skb->len,
2053                         skb_queue_len(&ring->queue));
2054                spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2055                return skb->len;
2056        }
2057
2058        if (type == RTLLIB_FTYPE_DATA) {
2059                if (priv->rtllib->LedControlHandler)
2060                        priv->rtllib->LedControlHandler(dev, LED_CTL_TX);
2061        }
2062        priv->ops->tx_fill_descriptor(dev, pdesc, tcb_desc, skb);
2063        __skb_queue_tail(&ring->queue, skb);
2064        pdesc->OWN = 1;
2065        spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2066        dev->trans_start = jiffies;
2067
2068        write_nic_word(dev, TPPoll, 0x01 << tcb_desc->queue_index);
2069        return 0;
2070}
2071
2072static short rtl8192_alloc_rx_desc_ring(struct net_device *dev)
2073{
2074        struct r8192_priv *priv = rtllib_priv(dev);
2075        struct rx_desc *entry = NULL;
2076        int i, rx_queue_idx;
2077
2078        for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE; rx_queue_idx++) {
2079                priv->rx_ring[rx_queue_idx] = pci_alloc_consistent(priv->pdev,
2080                                        sizeof(*priv->rx_ring[rx_queue_idx]) *
2081                                        priv->rxringcount,
2082                                        &priv->rx_ring_dma[rx_queue_idx]);
2083
2084                if (!priv->rx_ring[rx_queue_idx] ||
2085                    (unsigned long)priv->rx_ring[rx_queue_idx] & 0xFF) {
2086                        RT_TRACE(COMP_ERR, "Cannot allocate RX ring\n");
2087                        return -ENOMEM;
2088                }
2089
2090                memset(priv->rx_ring[rx_queue_idx], 0,
2091                       sizeof(*priv->rx_ring[rx_queue_idx]) *
2092                       priv->rxringcount);
2093                priv->rx_idx[rx_queue_idx] = 0;
2094
2095                for (i = 0; i < priv->rxringcount; i++) {
2096                        struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
2097                        dma_addr_t *mapping;
2098                        entry = &priv->rx_ring[rx_queue_idx][i];
2099                        if (!skb)
2100                                return 0;
2101                        skb->dev = dev;
2102                        priv->rx_buf[rx_queue_idx][i] = skb;
2103                        mapping = (dma_addr_t *)skb->cb;
2104                        *mapping = pci_map_single(priv->pdev,
2105                                                  skb_tail_pointer_rsl(skb),
2106                                                  priv->rxbuffersize,
2107                                                  PCI_DMA_FROMDEVICE);
2108                        if (pci_dma_mapping_error(priv->pdev, *mapping)) {
2109                                dev_kfree_skb_any(skb);
2110                                return -1;
2111                        }
2112                        entry->BufferAddress = cpu_to_le32(*mapping);
2113
2114                        entry->Length = priv->rxbuffersize;
2115                        entry->OWN = 1;
2116                }
2117
2118                if(entry)
2119                        entry->EOR = 1;
2120        }
2121        return 0;
2122}
2123
2124static int rtl8192_alloc_tx_desc_ring(struct net_device *dev,
2125        unsigned int prio, unsigned int entries)
2126{
2127        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2128        struct tx_desc *ring;
2129        dma_addr_t dma;
2130        int i;
2131
2132        ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
2133        if (!ring || (unsigned long)ring & 0xFF) {
2134                RT_TRACE(COMP_ERR, "Cannot allocate TX ring (prio = %d)\n",
2135                         prio);
2136                return -ENOMEM;
2137        }
2138
2139        memset(ring, 0, sizeof(*ring)*entries);
2140        priv->tx_ring[prio].desc = ring;
2141        priv->tx_ring[prio].dma = dma;
2142        priv->tx_ring[prio].idx = 0;
2143        priv->tx_ring[prio].entries = entries;
2144        skb_queue_head_init(&priv->tx_ring[prio].queue);
2145
2146        for (i = 0; i < entries; i++)
2147                ring[i].NextDescAddress =
2148                        cpu_to_le32((u32)dma + ((i + 1) % entries) *
2149                        sizeof(*ring));
2150
2151        return 0;
2152}
2153
2154
2155short rtl8192_pci_initdescring(struct net_device *dev)
2156{
2157        u32 ret;
2158        int i;
2159        struct r8192_priv *priv = rtllib_priv(dev);
2160
2161        ret = rtl8192_alloc_rx_desc_ring(dev);
2162        if (ret)
2163                return ret;
2164
2165        for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
2166                ret = rtl8192_alloc_tx_desc_ring(dev, i, priv->txringcount);
2167                if (ret)
2168                        goto err_free_rings;
2169        }
2170
2171        return 0;
2172
2173err_free_rings:
2174        rtl8192_free_rx_ring(dev);
2175        for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
2176                if (priv->tx_ring[i].desc)
2177                        rtl8192_free_tx_ring(dev, i);
2178        return 1;
2179}
2180
2181void rtl8192_pci_resetdescring(struct net_device *dev)
2182{
2183        struct r8192_priv *priv = rtllib_priv(dev);
2184        int i, rx_queue_idx;
2185        unsigned long flags = 0;
2186
2187        for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE; rx_queue_idx++) {
2188                if (priv->rx_ring[rx_queue_idx]) {
2189                        struct rx_desc *entry = NULL;
2190                        for (i = 0; i < priv->rxringcount; i++) {
2191                                entry = &priv->rx_ring[rx_queue_idx][i];
2192                                entry->OWN = 1;
2193                        }
2194                        priv->rx_idx[rx_queue_idx] = 0;
2195                }
2196        }
2197
2198        spin_lock_irqsave(&priv->irq_th_lock, flags);
2199        for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
2200                if (priv->tx_ring[i].desc) {
2201                        struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
2202
2203                        while (skb_queue_len(&ring->queue)) {
2204                                struct tx_desc *entry = &ring->desc[ring->idx];
2205                                struct sk_buff *skb =
2206                                                 __skb_dequeue(&ring->queue);
2207
2208                                pci_unmap_single(priv->pdev,
2209                                                 le32_to_cpu(entry->TxBuffAddr),
2210                                                 skb->len, PCI_DMA_TODEVICE);
2211                                kfree_skb(skb);
2212                                ring->idx = (ring->idx + 1) % ring->entries;
2213                        }
2214                        ring->idx = 0;
2215                }
2216        }
2217        spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2218}
2219
2220void rtl819x_UpdateRxPktTimeStamp(struct net_device *dev,
2221                                  struct rtllib_rx_stats *stats)
2222{
2223        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2224
2225        if (stats->bIsAMPDU && !stats->bFirstMPDU)
2226                stats->mac_time = priv->LastRxDescTSF;
2227        else
2228                priv->LastRxDescTSF = stats->mac_time;
2229}
2230
2231long rtl819x_translate_todbm(struct r8192_priv *priv, u8 signal_strength_index)
2232{
2233        long    signal_power;
2234
2235        signal_power = (long)((signal_strength_index + 1) >> 1);
2236        signal_power -= 95;
2237
2238        return signal_power;
2239}
2240
2241
2242void
2243rtl819x_update_rxsignalstatistics8190pci(
2244        struct r8192_priv *priv,
2245        struct rtllib_rx_stats *pprevious_stats
2246        )
2247{
2248        int weighting = 0;
2249
2250
2251        if (priv->stats.recv_signal_power == 0)
2252                priv->stats.recv_signal_power =
2253                                         pprevious_stats->RecvSignalPower;
2254
2255        if (pprevious_stats->RecvSignalPower > priv->stats.recv_signal_power)
2256                weighting = 5;
2257        else if (pprevious_stats->RecvSignalPower <
2258                 priv->stats.recv_signal_power)
2259                weighting = (-5);
2260        priv->stats.recv_signal_power = (priv->stats.recv_signal_power * 5 +
2261                                        pprevious_stats->RecvSignalPower +
2262                                        weighting) / 6;
2263}
2264
2265void rtl819x_process_cck_rxpathsel(struct r8192_priv *priv,
2266                                   struct rtllib_rx_stats *pprevious_stats)
2267{
2268}
2269
2270
2271u8 rtl819x_query_rxpwrpercentage(char antpower)
2272{
2273        if ((antpower <= -100) || (antpower >= 20))
2274                return  0;
2275        else if (antpower >= 0)
2276                return  100;
2277        else
2278                return  100 + antpower;
2279
2280}       /* QueryRxPwrPercentage */
2281
2282u8
2283rtl819x_evm_dbtopercentage(
2284        char value
2285        )
2286{
2287        char ret_val;
2288
2289        ret_val = value;
2290
2291        if (ret_val >= 0)
2292                ret_val = 0;
2293        if (ret_val <= -33)
2294                ret_val = -33;
2295        ret_val = 0 - ret_val;
2296        ret_val *= 3;
2297        if (ret_val == 99)
2298                ret_val = 100;
2299        return ret_val;
2300}
2301
2302void
2303rtl8192_record_rxdesc_forlateruse(
2304        struct rtllib_rx_stats *psrc_stats,
2305        struct rtllib_rx_stats *ptarget_stats
2306)
2307{
2308        ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
2309        ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
2310}
2311
2312
2313
2314static void rtl8192_rx_normal(struct net_device *dev)
2315{
2316        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2317        struct rtllib_hdr_1addr *rtllib_hdr = NULL;
2318        bool unicast_packet = false;
2319        bool bLedBlinking = true;
2320        u16 fc = 0, type = 0;
2321        u32 skb_len = 0;
2322        int rx_queue_idx = RX_MPDU_QUEUE;
2323
2324        struct rtllib_rx_stats stats = {
2325                .signal = 0,
2326                .noise = -98,
2327                .rate = 0,
2328                .freq = RTLLIB_24GHZ_BAND,
2329        };
2330        unsigned int count = priv->rxringcount;
2331
2332        stats.nic_type = NIC_8192E;
2333
2334        while (count--) {
2335                struct rx_desc *pdesc = &priv->rx_ring[rx_queue_idx]
2336                                        [priv->rx_idx[rx_queue_idx]];
2337                struct sk_buff *skb = priv->rx_buf[rx_queue_idx]
2338                                      [priv->rx_idx[rx_queue_idx]];
2339
2340                if (pdesc->OWN) {
2341                        return;
2342                } else {
2343                        struct sk_buff *new_skb;
2344
2345                        if (!priv->ops->rx_query_status_descriptor(dev, &stats,
2346                        pdesc, skb))
2347                                goto done;
2348                        new_skb = dev_alloc_skb(priv->rxbuffersize);
2349                        /* if allocation of new skb failed - drop current packet
2350                        * and reuse skb */
2351                        if (unlikely(!new_skb))
2352                                goto done;
2353
2354                        pci_unmap_single(priv->pdev,
2355                                        *((dma_addr_t *)skb->cb),
2356                                        priv->rxbuffersize,
2357                                        PCI_DMA_FROMDEVICE);
2358
2359                        skb_put(skb, pdesc->Length);
2360                        skb_reserve(skb, stats.RxDrvInfoSize +
2361                                stats.RxBufShift);
2362                        skb_trim(skb, skb->len - 4/*sCrcLng*/);
2363                        rtllib_hdr = (struct rtllib_hdr_1addr *)skb->data;
2364                        if (!is_multicast_ether_addr(rtllib_hdr->addr1)) {
2365                                /* unicast packet */
2366                                unicast_packet = true;
2367                        }
2368                        fc = le16_to_cpu(rtllib_hdr->frame_ctl);
2369                        type = WLAN_FC_GET_TYPE(fc);
2370                        if (type == RTLLIB_FTYPE_MGMT)
2371                                bLedBlinking = false;
2372
2373                        if (bLedBlinking)
2374                                if (priv->rtllib->LedControlHandler)
2375                                        priv->rtllib->LedControlHandler(dev,
2376                                                                LED_CTL_RX);
2377
2378                        if (stats.bCRC) {
2379                                if (type != RTLLIB_FTYPE_MGMT)
2380                                        priv->stats.rxdatacrcerr++;
2381                                else
2382                                        priv->stats.rxmgmtcrcerr++;
2383                        }
2384
2385                        skb_len = skb->len;
2386
2387                        if (!rtllib_rx(priv->rtllib, skb, &stats)) {
2388                                dev_kfree_skb_any(skb);
2389                        } else {
2390                                priv->stats.rxok++;
2391                                if (unicast_packet)
2392                                        priv->stats.rxbytesunicast += skb_len;
2393                        }
2394
2395                        skb = new_skb;
2396                        skb->dev = dev;
2397
2398                        priv->rx_buf[rx_queue_idx][priv->rx_idx[rx_queue_idx]] =
2399                                                                         skb;
2400                        *((dma_addr_t *) skb->cb) = pci_map_single(priv->pdev,
2401                                                    skb_tail_pointer_rsl(skb),
2402                                                    priv->rxbuffersize,
2403                                                    PCI_DMA_FROMDEVICE);
2404                        if (pci_dma_mapping_error(priv->pdev,
2405                                                  *((dma_addr_t *)skb->cb))) {
2406                                dev_kfree_skb_any(skb);
2407                                return;
2408                        }
2409                }
2410done:
2411                pdesc->BufferAddress = cpu_to_le32(*((dma_addr_t *)skb->cb));
2412                pdesc->OWN = 1;
2413                pdesc->Length = priv->rxbuffersize;
2414                if (priv->rx_idx[rx_queue_idx] == priv->rxringcount-1)
2415                        pdesc->EOR = 1;
2416                priv->rx_idx[rx_queue_idx] = (priv->rx_idx[rx_queue_idx] + 1) %
2417                                              priv->rxringcount;
2418        }
2419
2420}
2421
2422static void rtl8192_rx_cmd(struct net_device *dev)
2423{
2424}
2425
2426
2427static void rtl8192_tx_resume(struct net_device *dev)
2428{
2429        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2430        struct rtllib_device *ieee = priv->rtllib;
2431        struct sk_buff *skb;
2432        int queue_index;
2433
2434        for (queue_index = BK_QUEUE;
2435             queue_index < MAX_QUEUE_SIZE; queue_index++) {
2436                while ((!skb_queue_empty(&ieee->skb_waitQ[queue_index])) &&
2437                (priv->rtllib->check_nic_enough_desc(dev, queue_index) > 0)) {
2438                        skb = skb_dequeue(&ieee->skb_waitQ[queue_index]);
2439                        ieee->softmac_data_hard_start_xmit(skb, dev, 0);
2440                }
2441        }
2442}
2443
2444void rtl8192_irq_tx_tasklet(struct r8192_priv *priv)
2445{
2446        rtl8192_tx_resume(priv->rtllib->dev);
2447}
2448
2449void rtl8192_irq_rx_tasklet(struct r8192_priv *priv)
2450{
2451        rtl8192_rx_normal(priv->rtllib->dev);
2452
2453        if (MAX_RX_QUEUE > 1)
2454                rtl8192_rx_cmd(priv->rtllib->dev);
2455
2456        write_nic_dword(priv->rtllib->dev, INTA_MASK,
2457                        read_nic_dword(priv->rtllib->dev, INTA_MASK) | IMR_RDU);
2458}
2459
2460/****************************************************************************
2461 ---------------------------- NIC START/CLOSE STUFF---------------------------
2462*****************************************************************************/
2463void rtl8192_cancel_deferred_work(struct r8192_priv *priv)
2464{
2465        cancel_delayed_work(&priv->watch_dog_wq);
2466        cancel_delayed_work(&priv->update_beacon_wq);
2467        cancel_delayed_work(&priv->rtllib->hw_sleep_wq);
2468        cancel_work_sync(&priv->reset_wq);
2469        cancel_work_sync(&priv->qos_activate);
2470}
2471
2472int _rtl8192_up(struct net_device *dev, bool is_silent_reset)
2473{
2474        if (_rtl8192_sta_up(dev, is_silent_reset) == -1)
2475                return -1;
2476        return 0;
2477}
2478
2479
2480static int rtl8192_open(struct net_device *dev)
2481{
2482        struct r8192_priv *priv = rtllib_priv(dev);
2483        int ret;
2484
2485        down(&priv->wx_sem);
2486        ret = rtl8192_up(dev);
2487        up(&priv->wx_sem);
2488        return ret;
2489
2490}
2491
2492
2493int rtl8192_up(struct net_device *dev)
2494{
2495        struct r8192_priv *priv = rtllib_priv(dev);
2496
2497        if (priv->up == 1)
2498                return -1;
2499        return _rtl8192_up(dev, false);
2500}
2501
2502
2503static int rtl8192_close(struct net_device *dev)
2504{
2505        struct r8192_priv *priv = rtllib_priv(dev);
2506        int ret;
2507
2508        if ((rtllib_act_scanning(priv->rtllib, false)) &&
2509                !(priv->rtllib->softmac_features & IEEE_SOFTMAC_SCAN)) {
2510                rtllib_stop_scan(priv->rtllib);
2511        }
2512
2513        down(&priv->wx_sem);
2514
2515        ret = rtl8192_down(dev, true);
2516
2517        up(&priv->wx_sem);
2518
2519        return ret;
2520
2521}
2522
2523int rtl8192_down(struct net_device *dev, bool shutdownrf)
2524{
2525        if (rtl8192_sta_down(dev, shutdownrf) == -1)
2526                return -1;
2527
2528        return 0;
2529}
2530
2531void rtl8192_commit(struct net_device *dev)
2532{
2533        struct r8192_priv *priv = rtllib_priv(dev);
2534
2535        if (priv->up == 0)
2536                return;
2537        rtllib_softmac_stop_protocol(priv->rtllib, 0 , true);
2538        rtl8192_irq_disable(dev);
2539        priv->ops->stop_adapter(dev, true);
2540        _rtl8192_up(dev, false);
2541}
2542
2543void rtl8192_restart(void *data)
2544{
2545        struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
2546                                  reset_wq);
2547        struct net_device *dev = priv->rtllib->dev;
2548
2549        down(&priv->wx_sem);
2550
2551        rtl8192_commit(dev);
2552
2553        up(&priv->wx_sem);
2554}
2555
2556static void r8192_set_multicast(struct net_device *dev)
2557{
2558        struct r8192_priv *priv = rtllib_priv(dev);
2559        short promisc;
2560
2561        promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
2562        priv->promisc = promisc;
2563
2564}
2565
2566
2567static int r8192_set_mac_adr(struct net_device *dev, void *mac)
2568{
2569        struct r8192_priv *priv = rtllib_priv(dev);
2570        struct sockaddr *addr = mac;
2571
2572        down(&priv->wx_sem);
2573
2574        memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
2575
2576        schedule_work(&priv->reset_wq);
2577        up(&priv->wx_sem);
2578
2579        return 0;
2580}
2581
2582/* based on ipw2200 driver */
2583static int rtl8192_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2584{
2585        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2586        struct iwreq *wrq = (struct iwreq *)rq;
2587        int ret = -1;
2588        struct rtllib_device *ieee = priv->rtllib;
2589        u32 key[4];
2590        u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2591        u8 zero_addr[6] = {0};
2592        struct iw_point *p = &wrq->u.data;
2593        struct ieee_param *ipw = NULL;
2594
2595        down(&priv->wx_sem);
2596
2597        switch (cmd) {
2598        case RTL_IOCTL_WPA_SUPPLICANT:
2599                if (p->length < sizeof(struct ieee_param) || !p->pointer) {
2600                        ret = -EINVAL;
2601                        goto out;
2602                }
2603
2604                ipw = kmalloc(p->length, GFP_KERNEL);
2605                if (ipw == NULL) {
2606                        ret = -ENOMEM;
2607                        goto out;
2608                }
2609                if (copy_from_user(ipw, p->pointer, p->length)) {
2610                        kfree(ipw);
2611                        ret = -EFAULT;
2612                        goto out;
2613                }
2614
2615                if (ipw->cmd == IEEE_CMD_SET_ENCRYPTION) {
2616                        if (ipw->u.crypt.set_tx) {
2617                                if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
2618                                        ieee->pairwise_key_type = KEY_TYPE_CCMP;
2619                                else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
2620                                        ieee->pairwise_key_type = KEY_TYPE_TKIP;
2621                                else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
2622                                        if (ipw->u.crypt.key_len == 13)
2623                                                ieee->pairwise_key_type =
2624                                                         KEY_TYPE_WEP104;
2625                                        else if (ipw->u.crypt.key_len == 5)
2626                                                ieee->pairwise_key_type =
2627                                                         KEY_TYPE_WEP40;
2628                                } else {
2629                                        ieee->pairwise_key_type = KEY_TYPE_NA;
2630                                }
2631
2632                                if (ieee->pairwise_key_type) {
2633                                        if (memcmp(ieee->ap_mac_addr, zero_addr,
2634                                            6) == 0)
2635                                                ieee->iw_mode = IW_MODE_ADHOC;
2636                                        memcpy((u8 *)key, ipw->u.crypt.key, 16);
2637                                        EnableHWSecurityConfig8192(dev);
2638                                        set_swcam(dev, 4, ipw->u.crypt.idx,
2639                                                  ieee->pairwise_key_type,
2640                                                  (u8 *)ieee->ap_mac_addr,
2641                                                  0, key, 0);
2642                                        setKey(dev, 4, ipw->u.crypt.idx,
2643                                               ieee->pairwise_key_type,
2644                                               (u8 *)ieee->ap_mac_addr, 0, key);
2645                                        if (ieee->iw_mode == IW_MODE_ADHOC) {
2646                                                set_swcam(dev, ipw->u.crypt.idx,
2647                                                        ipw->u.crypt.idx,
2648                                                        ieee->pairwise_key_type,
2649                                                        (u8 *)ieee->ap_mac_addr,
2650                                                        0, key, 0);
2651                                                setKey(dev, ipw->u.crypt.idx,
2652                                                       ipw->u.crypt.idx,
2653                                                       ieee->pairwise_key_type,
2654                                                       (u8 *)ieee->ap_mac_addr,
2655                                                       0, key);
2656                                        }
2657                                }
2658                                if ((ieee->pairwise_key_type == KEY_TYPE_CCMP)
2659                                     && ieee->pHTInfo->bCurrentHTSupport) {
2660                                        write_nic_byte(dev, 0x173, 1);
2661                                }
2662
2663                        } else {
2664                                memcpy((u8 *)key, ipw->u.crypt.key, 16);
2665                                if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
2666                                        ieee->group_key_type = KEY_TYPE_CCMP;
2667                                else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
2668                                        ieee->group_key_type = KEY_TYPE_TKIP;
2669                                else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
2670                                        if (ipw->u.crypt.key_len == 13)
2671                                                ieee->group_key_type =
2672                                                         KEY_TYPE_WEP104;
2673                                        else if (ipw->u.crypt.key_len == 5)
2674                                                ieee->group_key_type =
2675                                                         KEY_TYPE_WEP40;
2676                                } else
2677                                        ieee->group_key_type = KEY_TYPE_NA;
2678
2679                                if (ieee->group_key_type) {
2680                                        set_swcam(dev, ipw->u.crypt.idx,
2681                                                  ipw->u.crypt.idx,
2682                                                  ieee->group_key_type,
2683                                                  broadcast_addr, 0, key, 0);
2684                                        setKey(dev, ipw->u.crypt.idx,
2685                                               ipw->u.crypt.idx,
2686                                               ieee->group_key_type,
2687                                               broadcast_addr, 0, key);
2688                                }
2689                        }
2690                }
2691
2692                ret = rtllib_wpa_supplicant_ioctl(priv->rtllib, &wrq->u.data,
2693                                                  0);
2694                kfree(ipw);
2695                break;
2696        default:
2697                ret = -EOPNOTSUPP;
2698                break;
2699        }
2700
2701out:
2702        up(&priv->wx_sem);
2703
2704        return ret;
2705}
2706
2707
2708irqreturn_t rtl8192_interrupt(int irq, void *netdev)
2709{
2710        struct net_device *dev = (struct net_device *) netdev;
2711        struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2712        unsigned long flags;
2713        u32 inta;
2714        u32 intb;
2715        intb = 0;
2716
2717        if (priv->irq_enabled == 0)
2718                goto done;
2719
2720        spin_lock_irqsave(&priv->irq_th_lock, flags);
2721
2722        priv->ops->interrupt_recognized(dev, &inta, &intb);
2723        priv->stats.shints++;
2724
2725        if (!inta) {
2726                spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2727                goto done;
2728        }
2729
2730        if (inta == 0xffff) {
2731                spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2732                goto done;
2733        }
2734
2735        priv->stats.ints++;
2736
2737        if (!netif_running(dev)) {
2738                spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2739                goto done;
2740        }
2741
2742        if (inta & IMR_TBDOK) {
2743                RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
2744                priv->stats.txbeaconokint++;
2745        }
2746
2747        if (inta & IMR_TBDER) {
2748                RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
2749                priv->stats.txbeaconerr++;
2750        }
2751
2752        if (inta & IMR_BDOK)
2753                RT_TRACE(COMP_INTR, "beacon interrupt!\n");
2754
2755        if (inta  & IMR_MGNTDOK) {
2756                RT_TRACE(COMP_INTR, "Manage ok interrupt!\n");
2757                priv->stats.txmanageokint++;
2758                rtl8192_tx_isr(dev, MGNT_QUEUE);
2759                spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2760                if (priv->rtllib->ack_tx_to_ieee) {
2761                        if (rtl8192_is_tx_queue_empty(dev)) {
2762                                priv->rtllib->ack_tx_to_ieee = 0;
2763                                rtllib_ps_tx_ack(priv->rtllib, 1);
2764                        }
2765                }
2766                spin_lock_irqsave(&priv->irq_th_lock, flags);
2767        }
2768
2769        if (inta & IMR_COMDOK) {
2770                priv->stats.txcmdpktokint++;
2771                rtl8192_tx_isr(dev, TXCMD_QUEUE);
2772        }
2773
2774        if (inta & IMR_HIGHDOK)
2775                rtl8192_tx_isr(dev, HIGH_QUEUE);
2776
2777        if (inta & IMR_ROK) {
2778                priv->stats.rxint++;
2779                priv->InterruptLog.nIMR_ROK++;
2780                tasklet_schedule(&priv->irq_rx_tasklet);
2781        }
2782
2783        if (inta & IMR_BcnInt) {
2784                RT_TRACE(COMP_INTR, "prepare beacon for interrupt!\n");
2785                tasklet_schedule(&priv->irq_prepare_beacon_tasklet);
2786        }
2787
2788        if (inta & IMR_RDU) {
2789                RT_TRACE(COMP_INTR, "rx descriptor unavailable!\n");
2790                priv->stats.rxrdu++;
2791                write_nic_dword(dev, INTA_MASK,
2792                                read_nic_dword(dev, INTA_MASK) & ~IMR_RDU);
2793                tasklet_schedule(&priv->irq_rx_tasklet);
2794        }
2795
2796        if (inta & IMR_RXFOVW) {
2797                RT_TRACE(COMP_INTR, "rx overflow !\n");
2798                priv->stats.rxoverflow++;
2799                tasklet_schedule(&priv->irq_rx_tasklet);
2800        }
2801
2802        if (inta & IMR_TXFOVW)
2803                priv->stats.txoverflow++;
2804
2805        if (inta & IMR_BKDOK) {
2806                RT_TRACE(COMP_INTR, "BK Tx OK interrupt!\n");
2807                priv->stats.txbkokint++;
2808                priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2809                rtl8192_tx_isr(dev, BK_QUEUE);
2810        }
2811
2812        if (inta & IMR_BEDOK) {
2813                RT_TRACE(COMP_INTR, "BE TX OK interrupt!\n");
2814                priv->stats.txbeokint++;
2815                priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2816                rtl8192_tx_isr(dev, BE_QUEUE);
2817        }
2818
2819        if (inta & IMR_VIDOK) {
2820                RT_TRACE(COMP_INTR, "VI TX OK interrupt!\n");
2821                priv->stats.txviokint++;
2822                priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2823                rtl8192_tx_isr(dev, VI_QUEUE);
2824        }
2825
2826        if (inta & IMR_VODOK) {
2827                priv->stats.txvookint++;
2828                RT_TRACE(COMP_INTR, "Vo TX OK interrupt!\n");
2829                priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2830                rtl8192_tx_isr(dev, VO_QUEUE);
2831        }
2832
2833        spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2834
2835done:
2836
2837        return IRQ_HANDLED;
2838}
2839
2840
2841
2842/****************************************************************************
2843        ---------------------------- PCI_STUFF---------------------------
2844*****************************************************************************/
2845static const struct net_device_ops rtl8192_netdev_ops = {
2846        .ndo_open = rtl8192_open,
2847        .ndo_stop = rtl8192_close,
2848        .ndo_tx_timeout = rtl8192_tx_timeout,
2849        .ndo_do_ioctl = rtl8192_ioctl,
2850        .ndo_set_rx_mode = r8192_set_multicast,
2851        .ndo_set_mac_address = r8192_set_mac_adr,
2852        .ndo_validate_addr = eth_validate_addr,
2853        .ndo_change_mtu = eth_change_mtu,
2854        .ndo_start_xmit = rtllib_xmit,
2855};
2856
2857static int rtl8192_pci_probe(struct pci_dev *pdev,
2858                        const struct pci_device_id *id)
2859{
2860        unsigned long ioaddr = 0;
2861        struct net_device *dev = NULL;
2862        struct r8192_priv *priv = NULL;
2863        struct rtl819x_ops *ops = (struct rtl819x_ops *)(id->driver_data);
2864        unsigned long pmem_start, pmem_len, pmem_flags;
2865        int err = -ENOMEM;
2866        bool bdma64 = false;
2867        u8 revision_id;
2868
2869        RT_TRACE(COMP_INIT, "Configuring chip resources");
2870
2871        if (pci_enable_device(pdev)) {
2872                RT_TRACE(COMP_ERR, "Failed to enable PCI device");
2873                return -EIO;
2874        }
2875
2876        pci_set_master(pdev);
2877
2878        if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
2879                if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2880                        printk(KERN_INFO "Unable to obtain 32bit DMA for consistent allocations\n");
2881                        goto err_pci_disable;
2882                }
2883        }
2884        dev = alloc_rtllib(sizeof(struct r8192_priv));
2885        if (!dev)
2886                goto err_pci_disable;
2887
2888        err = -ENODEV;
2889        if (bdma64)
2890                dev->features |= NETIF_F_HIGHDMA;
2891
2892        pci_set_drvdata(pdev, dev);
2893        SET_NETDEV_DEV(dev, &pdev->dev);
2894        priv = rtllib_priv(dev);
2895        priv->rtllib = (struct rtllib_device *)netdev_priv_rsl(dev);
2896        priv->pdev = pdev;
2897        priv->rtllib->pdev = pdev;
2898        if ((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK) &&
2899            (pdev->subsystem_device == 0x3304))
2900                priv->rtllib->bSupportRemoteWakeUp = 1;
2901        else
2902                priv->rtllib->bSupportRemoteWakeUp = 0;
2903
2904        pmem_start = pci_resource_start(pdev, 1);
2905        pmem_len = pci_resource_len(pdev, 1);
2906        pmem_flags = pci_resource_flags(pdev, 1);
2907
2908        if (!(pmem_flags & IORESOURCE_MEM)) {
2909                RT_TRACE(COMP_ERR, "region #1 not a MMIO resource, aborting");
2910                goto err_rel_rtllib;
2911        }
2912
2913        printk(KERN_INFO "Memory mapped space start: 0x%08lx\n", pmem_start);
2914        if (!request_mem_region(pmem_start, pmem_len, DRV_NAME)) {
2915                RT_TRACE(COMP_ERR, "request_mem_region failed!");
2916                goto err_rel_rtllib;
2917        }
2918
2919
2920        ioaddr = (unsigned long)ioremap_nocache(pmem_start, pmem_len);
2921        if (ioaddr == (unsigned long)NULL) {
2922                RT_TRACE(COMP_ERR, "ioremap failed!");
2923                goto err_rel_mem;
2924        }
2925
2926        dev->mem_start = ioaddr;
2927        dev->mem_end = ioaddr + pci_resource_len(pdev, 0);
2928
2929        pci_read_config_byte(pdev, 0x08, &revision_id);
2930        /* If the revisionid is 0x10, the device uses rtl8192se. */
2931        if (pdev->device == 0x8192 && revision_id == 0x10)
2932                goto err_rel_mem;
2933
2934        priv->ops = ops;
2935
2936        if (rtl8192_pci_findadapter(pdev, dev) == false)
2937                goto err_rel_mem;
2938
2939        dev->irq = pdev->irq;
2940        priv->irq = 0;
2941
2942        dev->netdev_ops = &rtl8192_netdev_ops;
2943
2944        dev->wireless_handlers = (struct iw_handler_def *)
2945                                 &r8192_wx_handlers_def;
2946        dev->ethtool_ops = &rtl819x_ethtool_ops;
2947
2948        dev->type = ARPHRD_ETHER;
2949        dev->watchdog_timeo = HZ * 3;
2950
2951        if (dev_alloc_name(dev, ifname) < 0) {
2952                RT_TRACE(COMP_INIT, "Oops: devname already taken! Trying "
2953                         "wlan%%d...\n");
2954                        dev_alloc_name(dev, ifname);
2955        }
2956
2957        RT_TRACE(COMP_INIT, "Driver probe completed1\n");
2958        if (rtl8192_init(dev) != 0) {
2959                RT_TRACE(COMP_ERR, "Initialization failed");
2960                goto err_free_irq;
2961        }
2962
2963        netif_carrier_off(dev);
2964        netif_stop_queue(dev);
2965
2966        if (register_netdev(dev))
2967                goto err_free_irq;
2968        RT_TRACE(COMP_INIT, "dev name: %s\n", dev->name);
2969
2970        if (priv->polling_timer_on == 0)
2971                check_rfctrl_gpio_timer((unsigned long)dev);
2972
2973        RT_TRACE(COMP_INIT, "Driver probe completed\n");
2974        return 0;
2975
2976err_free_irq:
2977        free_irq(dev->irq, dev);
2978        priv->irq = 0;
2979err_rel_mem:
2980        release_mem_region(pmem_start, pmem_len);
2981err_rel_rtllib:
2982        free_rtllib(dev);
2983
2984        DMESG("wlan driver load failed\n");
2985        pci_set_drvdata(pdev, NULL);
2986err_pci_disable:
2987        pci_disable_device(pdev);
2988        return err;
2989}
2990
2991static void rtl8192_pci_disconnect(struct pci_dev *pdev)
2992{
2993        struct net_device *dev = pci_get_drvdata(pdev);
2994        struct r8192_priv *priv ;
2995        u32 i;
2996
2997        if (dev) {
2998                unregister_netdev(dev);
2999
3000                priv = rtllib_priv(dev);
3001
3002                del_timer_sync(&priv->gpio_polling_timer);
3003                cancel_delayed_work(&priv->gpio_change_rf_wq);
3004                priv->polling_timer_on = 0;
3005                rtl8192_down(dev, true);
3006                deinit_hal_dm(dev);
3007                if (priv->pFirmware) {
3008                        vfree(priv->pFirmware);
3009                        priv->pFirmware = NULL;
3010                }
3011                destroy_workqueue(priv->priv_wq);
3012                rtl8192_free_rx_ring(dev);
3013                for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
3014                        rtl8192_free_tx_ring(dev, i);
3015
3016                if (priv->irq) {
3017                        printk(KERN_INFO "Freeing irq %d\n", dev->irq);
3018                        free_irq(dev->irq, dev);
3019                        priv->irq = 0;
3020                }
3021                free_rtllib(dev);
3022
3023                kfree(priv->scan_cmd);
3024
3025                if (dev->mem_start != 0) {
3026                        iounmap((void __iomem *)dev->mem_start);
3027                        release_mem_region(pci_resource_start(pdev, 1),
3028                                        pci_resource_len(pdev, 1));
3029                }
3030        } else {
3031                priv = rtllib_priv(dev);
3032        }
3033
3034        pci_disable_device(pdev);
3035        RT_TRACE(COMP_DOWN, "wlan driver removed\n");
3036}
3037
3038bool NicIFEnableNIC(struct net_device *dev)
3039{
3040        bool init_status = true;
3041        struct r8192_priv *priv = rtllib_priv(dev);
3042        struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
3043                                        (&(priv->rtllib->PowerSaveControl));
3044
3045        if (IS_NIC_DOWN(priv)) {
3046                RT_TRACE(COMP_ERR, "ERR!!! %s(): Driver is already down!\n",
3047                         __func__);
3048                priv->bdisable_nic = false;
3049                return RT_STATUS_FAILURE;
3050        }
3051
3052        RT_TRACE(COMP_PS, "===========>%s()\n", __func__);
3053        priv->bfirst_init = true;
3054        init_status = priv->ops->initialize_adapter(dev);
3055        if (init_status != true) {
3056                RT_TRACE(COMP_ERR, "ERR!!! %s(): initialization is failed!\n",
3057                         __func__);
3058                priv->bdisable_nic = false;
3059                return -1;
3060        }
3061        RT_TRACE(COMP_INIT, "start adapter finished\n");
3062        RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
3063        priv->bfirst_init = false;
3064
3065        rtl8192_irq_enable(dev);
3066        priv->bdisable_nic = false;
3067        RT_TRACE(COMP_PS, "<===========%s()\n", __func__);
3068        return init_status;
3069}
3070bool NicIFDisableNIC(struct net_device *dev)
3071{
3072        bool    status = true;
3073        struct r8192_priv *priv = rtllib_priv(dev);
3074        u8 tmp_state = 0;
3075        RT_TRACE(COMP_PS, "=========>%s()\n", __func__);
3076        priv->bdisable_nic = true;
3077        tmp_state = priv->rtllib->state;
3078        rtllib_softmac_stop_protocol(priv->rtllib, 0, false);
3079        priv->rtllib->state = tmp_state;
3080        rtl8192_cancel_deferred_work(priv);
3081        rtl8192_irq_disable(dev);
3082
3083        priv->ops->stop_adapter(dev, false);
3084        RT_TRACE(COMP_PS, "<=========%s()\n", __func__);
3085
3086        return status;
3087}
3088
3089static int __init rtl8192_pci_module_init(void)
3090{
3091        printk(KERN_INFO "\nLinux kernel driver for RTL8192E WLAN cards\n");
3092        printk(KERN_INFO "Copyright (c) 2007-2008, Realsil Wlan Driver\n");
3093
3094        if (0 != pci_register_driver(&rtl8192_pci_driver)) {
3095                DMESG("No device found");
3096                /*pci_unregister_driver (&rtl8192_pci_driver);*/
3097                return -ENODEV;
3098        }
3099        return 0;
3100}
3101
3102static void __exit rtl8192_pci_module_exit(void)
3103{
3104        pci_unregister_driver(&rtl8192_pci_driver);
3105
3106        RT_TRACE(COMP_DOWN, "Exiting");
3107}
3108
3109void check_rfctrl_gpio_timer(unsigned long data)
3110{
3111        struct r8192_priv *priv = rtllib_priv((struct net_device *)data);
3112
3113        priv->polling_timer_on = 1;
3114
3115        queue_delayed_work_rsl(priv->priv_wq, &priv->gpio_change_rf_wq, 0);
3116
3117        mod_timer(&priv->gpio_polling_timer, jiffies +
3118                  MSECS(RTLLIB_WATCH_DOG_TIME));
3119}
3120
3121/***************************************************************************
3122        ------------------- module init / exit stubs ----------------
3123****************************************************************************/
3124module_init(rtl8192_pci_module_init);
3125module_exit(rtl8192_pci_module_exit);
3126
3127MODULE_DESCRIPTION("Linux driver for Realtek RTL819x WiFi cards");
3128MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
3129MODULE_VERSION(DRV_VERSION);
3130MODULE_LICENSE("GPL");
3131MODULE_FIRMWARE(RTL8192E_BOOT_IMG_FW);
3132MODULE_FIRMWARE(RTL8192E_MAIN_IMG_FW);
3133MODULE_FIRMWARE(RTL8192E_DATA_IMG_FW);
3134
3135module_param(ifname, charp, S_IRUGO|S_IWUSR);
3136module_param(hwwep, int, S_IRUGO|S_IWUSR);
3137module_param(channels, int, S_IRUGO|S_IWUSR);
3138
3139MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
3140MODULE_PARM_DESC(hwwep, " Try to use hardware WEP support(default use hw. set 0 to use software security)");
3141MODULE_PARM_DESC(channels, " Channel bitmask for specific locales. NYI");
3142