root/drivers/staging/rtl8192e/rtl8192e/rtl_core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rtl92e_readb
  2. rtl92e_readl
  3. rtl92e_readw
  4. rtl92e_writeb
  5. rtl92e_writel
  6. rtl92e_writew
  7. rtl92e_set_rf_state
  8. _rtl92e_check_nic_enough_desc
  9. _rtl92e_tx_timeout
  10. rtl92e_irq_enable
  11. rtl92e_irq_disable
  12. _rtl92e_set_chan
  13. _rtl92e_update_cap
  14. _rtl92e_update_beacon
  15. _rtl92e_qos_activate
  16. _rtl92e_qos_handle_probe_response
  17. _rtl92e_handle_beacon
  18. _rtl92e_qos_assoc_resp
  19. _rtl92e_handle_assoc_response
  20. _rtl92e_prepare_beacon
  21. _rtl92e_stop_beacon
  22. rtl92e_config_rate
  23. _rtl92e_refresh_support_rate
  24. _rtl92e_get_supported_wireless_mode
  25. rtl92e_set_wireless_mode
  26. _rtl92e_sta_up
  27. _rtl92e_sta_down
  28. _rtl92e_init_priv_handler
  29. _rtl92e_init_priv_constant
  30. _rtl92e_init_priv_variable
  31. _rtl92e_init_priv_lock
  32. _rtl92e_init_priv_task
  33. _rtl92e_get_channel_map
  34. _rtl92e_init
  35. _rtl92e_is_tx_queue_empty
  36. _rtl92e_tx_check_stuck
  37. _rtl92e_rx_check_stuck
  38. _rtl92e_if_check_reset
  39. _rtl92e_if_silent_reset
  40. _rtl92e_update_rxcounts
  41. _rtl92e_watchdog_wq_cb
  42. _rtl92e_watchdog_timer_cb
  43. rtl92e_rx_enable
  44. rtl92e_tx_enable
  45. _rtl92e_free_rx_ring
  46. _rtl92e_free_tx_ring
  47. _rtl92e_hard_data_xmit
  48. _rtl92e_hard_start_xmit
  49. _rtl92e_tx_isr
  50. _rtl92e_tx_cmd
  51. _rtl92e_tx
  52. _rtl92e_alloc_rx_ring
  53. _rtl92e_alloc_tx_ring
  54. _rtl92e_pci_initdescring
  55. rtl92e_reset_desc_ring
  56. rtl92e_update_rx_pkt_timestamp
  57. rtl92e_translate_to_dbm
  58. rtl92e_update_rx_statistics
  59. rtl92e_rx_db_to_percent
  60. rtl92e_evm_db_to_percent
  61. rtl92e_copy_mpdu_stats
  62. _rtl92e_rx_normal
  63. _rtl92e_tx_resume
  64. _rtl92e_irq_tx_tasklet
  65. _rtl92e_irq_rx_tasklet
  66. _rtl92e_cancel_deferred_work
  67. _rtl92e_up
  68. _rtl92e_open
  69. _rtl92e_try_up
  70. _rtl92e_close
  71. _rtl92e_down
  72. rtl92e_commit
  73. _rtl92e_restart
  74. _rtl92e_set_multicast
  75. _rtl92e_set_mac_adr
  76. _rtl92e_irq
  77. _rtl92e_pci_probe
  78. _rtl92e_pci_disconnect
  79. rtl92e_enable_nic
  80. rtl92e_disable_nic
  81. rtl92e_check_rfctrl_gpio_timer

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
   4  *
   5  * Based on the r8180 driver, which is:
   6  * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
   7  *
   8  * Contact Information: wlanfae <wlanfae@realtek.com>
   9  */
  10 #include <linux/uaccess.h>
  11 #include <linux/pci.h>
  12 #include <linux/vmalloc.h>
  13 #include <linux/ieee80211.h>
  14 #include "rtl_core.h"
  15 #include "r8192E_phy.h"
  16 #include "r8192E_phyreg.h"
  17 #include "r8190P_rtl8256.h"
  18 #include "r8192E_cmdpkt.h"
  19 
  20 #include "rtl_wx.h"
  21 #include "rtl_dm.h"
  22 
  23 #include "rtl_pm.h"
  24 
  25 int hwwep = 1;
  26 static int channels = 0x3fff;
  27 static char *ifname = "wlan%d";
  28 
  29 
  30 static const struct rtl819x_ops rtl819xp_ops = {
  31         .nic_type                       = NIC_8192E,
  32         .get_eeprom_size                = rtl92e_get_eeprom_size,
  33         .init_adapter_variable          = rtl92e_init_variables,
  34         .initialize_adapter             = rtl92e_start_adapter,
  35         .link_change                    = rtl92e_link_change,
  36         .tx_fill_descriptor             = rtl92e_fill_tx_desc,
  37         .tx_fill_cmd_descriptor         = rtl92e_fill_tx_cmd_desc,
  38         .rx_query_status_descriptor     = rtl92e_get_rx_stats,
  39         .rx_command_packet_handler = NULL,
  40         .stop_adapter                   = rtl92e_stop_adapter,
  41         .update_ratr_table              = rtl92e_update_ratr_table,
  42         .irq_enable                     = rtl92e_enable_irq,
  43         .irq_disable                    = rtl92e_disable_irq,
  44         .irq_clear                      = rtl92e_clear_irq,
  45         .rx_enable                      = rtl92e_enable_rx,
  46         .tx_enable                      = rtl92e_enable_tx,
  47         .interrupt_recognized           = rtl92e_ack_irq,
  48         .TxCheckStuckHandler            = rtl92e_is_tx_stuck,
  49         .RxCheckStuckHandler            = rtl92e_is_rx_stuck,
  50 };
  51 
  52 static struct pci_device_id rtl8192_pci_id_tbl[] = {
  53         {RTL_PCI_DEVICE(0x10ec, 0x8192, rtl819xp_ops)},
  54         {RTL_PCI_DEVICE(0x07aa, 0x0044, rtl819xp_ops)},
  55         {RTL_PCI_DEVICE(0x07aa, 0x0047, rtl819xp_ops)},
  56         {}
  57 };
  58 
  59 MODULE_DEVICE_TABLE(pci, rtl8192_pci_id_tbl);
  60 
  61 static int _rtl92e_pci_probe(struct pci_dev *pdev,
  62                              const struct pci_device_id *id);
  63 static void _rtl92e_pci_disconnect(struct pci_dev *pdev);
  64 static irqreturn_t _rtl92e_irq(int irq, void *netdev);
  65 
  66 static struct pci_driver rtl8192_pci_driver = {
  67         .name = DRV_NAME,       /* Driver name   */
  68         .id_table = rtl8192_pci_id_tbl, /* PCI_ID table  */
  69         .probe  = _rtl92e_pci_probe,    /* probe fn      */
  70         .remove  = _rtl92e_pci_disconnect,      /* remove fn */
  71         .suspend = rtl92e_suspend,      /* PM suspend fn */
  72         .resume = rtl92e_resume,                 /* PM resume fn  */
  73 };
  74 
  75 static short _rtl92e_is_tx_queue_empty(struct net_device *dev);
  76 static void _rtl92e_watchdog_wq_cb(void *data);
  77 static void _rtl92e_watchdog_timer_cb(struct timer_list *t);
  78 static void _rtl92e_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
  79                                    int rate);
  80 static int _rtl92e_hard_start_xmit(struct sk_buff *skb, struct net_device *dev);
  81 static void _rtl92e_tx_cmd(struct net_device *dev, struct sk_buff *skb);
  82 static short _rtl92e_tx(struct net_device *dev, struct sk_buff *skb);
  83 static short _rtl92e_pci_initdescring(struct net_device *dev);
  84 static void _rtl92e_irq_tx_tasklet(struct r8192_priv *priv);
  85 static void _rtl92e_irq_rx_tasklet(struct r8192_priv *priv);
  86 static void _rtl92e_cancel_deferred_work(struct r8192_priv *priv);
  87 static int _rtl92e_up(struct net_device *dev, bool is_silent_reset);
  88 static int _rtl92e_try_up(struct net_device *dev);
  89 static int _rtl92e_down(struct net_device *dev, bool shutdownrf);
  90 static void _rtl92e_restart(void *data);
  91 
  92 /****************************************************************************
  93  *  -----------------------------IO STUFF-------------------------
  94  ****************************************************************************/
  95 
  96 u8 rtl92e_readb(struct net_device *dev, int x)
  97 {
  98         return 0xff & readb((u8 __iomem *)dev->mem_start + x);
  99 }
 100 
 101 u32 rtl92e_readl(struct net_device *dev, int x)
 102 {
 103         return readl((u8 __iomem *)dev->mem_start + x);
 104 }
 105 
 106 u16 rtl92e_readw(struct net_device *dev, int x)
 107 {
 108         return readw((u8 __iomem *)dev->mem_start + x);
 109 }
 110 
 111 void rtl92e_writeb(struct net_device *dev, int x, u8 y)
 112 {
 113         writeb(y, (u8 __iomem *)dev->mem_start + x);
 114 
 115         udelay(20);
 116 }
 117 
 118 void rtl92e_writel(struct net_device *dev, int x, u32 y)
 119 {
 120         writel(y, (u8 __iomem *)dev->mem_start + x);
 121 
 122         udelay(20);
 123 }
 124 
 125 void rtl92e_writew(struct net_device *dev, int x, u16 y)
 126 {
 127         writew(y, (u8 __iomem *)dev->mem_start + x);
 128 
 129         udelay(20);
 130 }
 131 
 132 /****************************************************************************
 133  *  -----------------------------GENERAL FUNCTION-------------------------
 134  ****************************************************************************/
 135 bool rtl92e_set_rf_state(struct net_device *dev,
 136                          enum rt_rf_power_state StateToSet,
 137                          RT_RF_CHANGE_SOURCE ChangeSource)
 138 {
 139         struct r8192_priv *priv = rtllib_priv(dev);
 140         struct rtllib_device *ieee = priv->rtllib;
 141         bool                    bActionAllowed = false;
 142         bool                    bConnectBySSID = false;
 143         enum rt_rf_power_state rtState;
 144         u16                     RFWaitCounter = 0;
 145         unsigned long flag;
 146 
 147         RT_TRACE((COMP_PS | COMP_RF),
 148                  "===>rtl92e_set_rf_state(): StateToSet(%d)\n", StateToSet);
 149 
 150         while (true) {
 151                 spin_lock_irqsave(&priv->rf_ps_lock, flag);
 152                 if (priv->RFChangeInProgress) {
 153                         spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
 154                         RT_TRACE((COMP_PS | COMP_RF),
 155                                  "rtl92e_set_rf_state(): RF Change in progress! Wait to set..StateToSet(%d).\n",
 156                                  StateToSet);
 157 
 158                         while (priv->RFChangeInProgress) {
 159                                 RFWaitCounter++;
 160                                 RT_TRACE((COMP_PS | COMP_RF),
 161                                          "rtl92e_set_rf_state(): Wait 1 ms (%d times)...\n",
 162                                          RFWaitCounter);
 163                                 mdelay(1);
 164 
 165                                 if (RFWaitCounter > 100) {
 166                                         netdev_warn(dev,
 167                                                     "%s(): Timeout waiting for RF change.\n",
 168                                                     __func__);
 169                                         return false;
 170                                 }
 171                         }
 172                 } else {
 173                         priv->RFChangeInProgress = true;
 174                         spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
 175                         break;
 176                 }
 177         }
 178 
 179         rtState = priv->rtllib->eRFPowerState;
 180 
 181         switch (StateToSet) {
 182         case eRfOn:
 183                 priv->rtllib->RfOffReason &= (~ChangeSource);
 184 
 185                 if ((ChangeSource == RF_CHANGE_BY_HW) && priv->bHwRadioOff)
 186                         priv->bHwRadioOff = false;
 187 
 188                 if (!priv->rtllib->RfOffReason) {
 189                         priv->rtllib->RfOffReason = 0;
 190                         bActionAllowed = true;
 191 
 192 
 193                         if (rtState == eRfOff &&
 194                             ChangeSource >= RF_CHANGE_BY_HW)
 195                                 bConnectBySSID = true;
 196                 } else {
 197                         RT_TRACE((COMP_PS | COMP_RF),
 198                                  "rtl92e_set_rf_state - eRfon reject pMgntInfo->RfOffReason= 0x%x, ChangeSource=0x%X\n",
 199                                   priv->rtllib->RfOffReason, ChangeSource);
 200         }
 201 
 202                 break;
 203 
 204         case eRfOff:
 205 
 206                 if ((priv->rtllib->iw_mode == IW_MODE_INFRA) ||
 207                     (priv->rtllib->iw_mode == IW_MODE_ADHOC)) {
 208                         if ((priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) ||
 209                             (ChangeSource > RF_CHANGE_BY_IPS)) {
 210                                 if (ieee->state == RTLLIB_LINKED)
 211                                         priv->blinked_ingpio = true;
 212                                 else
 213                                         priv->blinked_ingpio = false;
 214                                 rtllib_MgntDisconnect(priv->rtllib,
 215                                                       WLAN_REASON_DISASSOC_STA_HAS_LEFT);
 216                         }
 217                 }
 218                 if ((ChangeSource == RF_CHANGE_BY_HW) && !priv->bHwRadioOff)
 219                         priv->bHwRadioOff = true;
 220                 priv->rtllib->RfOffReason |= ChangeSource;
 221                 bActionAllowed = true;
 222                 break;
 223 
 224         case eRfSleep:
 225                 priv->rtllib->RfOffReason |= ChangeSource;
 226                 bActionAllowed = true;
 227                 break;
 228 
 229         default:
 230                 break;
 231         }
 232 
 233         if (bActionAllowed) {
 234                 RT_TRACE((COMP_PS | COMP_RF),
 235                          "rtl92e_set_rf_state(): Action is allowed.... StateToSet(%d), RfOffReason(%#X)\n",
 236                          StateToSet, priv->rtllib->RfOffReason);
 237                 PHY_SetRFPowerState(dev, StateToSet);
 238                 if (StateToSet == eRfOn) {
 239 
 240                         if (bConnectBySSID && priv->blinked_ingpio) {
 241                                 schedule_delayed_work(
 242                                          &ieee->associate_procedure_wq, 0);
 243                                 priv->blinked_ingpio = false;
 244                         }
 245                 }
 246         } else {
 247                 RT_TRACE((COMP_PS | COMP_RF),
 248                          "rtl92e_set_rf_state(): Action is rejected.... StateToSet(%d), ChangeSource(%#X), RfOffReason(%#X)\n",
 249                          StateToSet, ChangeSource, priv->rtllib->RfOffReason);
 250         }
 251 
 252         spin_lock_irqsave(&priv->rf_ps_lock, flag);
 253         priv->RFChangeInProgress = false;
 254         spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
 255 
 256         RT_TRACE((COMP_PS | COMP_RF), "<===rtl92e_set_rf_state()\n");
 257         return bActionAllowed;
 258 }
 259 
 260 static short _rtl92e_check_nic_enough_desc(struct net_device *dev, int prio)
 261 {
 262         struct r8192_priv *priv = rtllib_priv(dev);
 263         struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
 264 
 265         if (ring->entries - skb_queue_len(&ring->queue) >= 2)
 266                 return 1;
 267         return 0;
 268 }
 269 
 270 static void _rtl92e_tx_timeout(struct net_device *dev)
 271 {
 272         struct r8192_priv *priv = rtllib_priv(dev);
 273 
 274         schedule_work(&priv->reset_wq);
 275         netdev_info(dev, "TXTIMEOUT");
 276 }
 277 
 278 void rtl92e_irq_enable(struct net_device *dev)
 279 {
 280         struct r8192_priv *priv = rtllib_priv(dev);
 281 
 282         priv->irq_enabled = 1;
 283 
 284         priv->ops->irq_enable(dev);
 285 }
 286 
 287 void rtl92e_irq_disable(struct net_device *dev)
 288 {
 289         struct r8192_priv *priv = rtllib_priv(dev);
 290 
 291         priv->ops->irq_disable(dev);
 292 
 293         priv->irq_enabled = 0;
 294 }
 295 
 296 static void _rtl92e_set_chan(struct net_device *dev, short ch)
 297 {
 298         struct r8192_priv *priv = rtllib_priv(dev);
 299 
 300         RT_TRACE(COMP_CH, "=====>%s()====ch:%d\n", __func__, ch);
 301         if (priv->chan_forced)
 302                 return;
 303 
 304         priv->chan = ch;
 305 
 306         if (priv->rf_set_chan)
 307                 priv->rf_set_chan(dev, priv->chan);
 308 }
 309 
 310 static void _rtl92e_update_cap(struct net_device *dev, u16 cap)
 311 {
 312         struct r8192_priv *priv = rtllib_priv(dev);
 313         struct rtllib_network *net = &priv->rtllib->current_network;
 314         bool            ShortPreamble;
 315 
 316         if (cap & WLAN_CAPABILITY_SHORT_PREAMBLE) {
 317                 if (priv->dot11CurrentPreambleMode != PREAMBLE_SHORT) {
 318                         ShortPreamble = true;
 319                         priv->dot11CurrentPreambleMode = PREAMBLE_SHORT;
 320                         RT_TRACE(COMP_DBG,
 321                                  "%s(): WLAN_CAPABILITY_SHORT_PREAMBLE\n",
 322                                  __func__);
 323                         priv->rtllib->SetHwRegHandler(dev, HW_VAR_ACK_PREAMBLE,
 324                                         (unsigned char *)&ShortPreamble);
 325                 }
 326         } else {
 327                 if (priv->dot11CurrentPreambleMode != PREAMBLE_LONG) {
 328                         ShortPreamble = false;
 329                         priv->dot11CurrentPreambleMode = PREAMBLE_LONG;
 330                         RT_TRACE(COMP_DBG,
 331                                  "%s(): WLAN_CAPABILITY_LONG_PREAMBLE\n",
 332                                  __func__);
 333                         priv->rtllib->SetHwRegHandler(dev, HW_VAR_ACK_PREAMBLE,
 334                                               (unsigned char *)&ShortPreamble);
 335                 }
 336         }
 337 
 338         if (net->mode & (IEEE_G | IEEE_N_24G)) {
 339                 u8      slot_time_val;
 340                 u8      CurSlotTime = priv->slot_time;
 341 
 342                 if ((cap & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
 343                    (!priv->rtllib->pHTInfo->bCurrentRT2RTLongSlotTime)) {
 344                         if (CurSlotTime != SHORT_SLOT_TIME) {
 345                                 slot_time_val = SHORT_SLOT_TIME;
 346                                 priv->rtllib->SetHwRegHandler(dev,
 347                                          HW_VAR_SLOT_TIME, &slot_time_val);
 348                         }
 349                 } else {
 350                         if (CurSlotTime != NON_SHORT_SLOT_TIME) {
 351                                 slot_time_val = NON_SHORT_SLOT_TIME;
 352                                 priv->rtllib->SetHwRegHandler(dev,
 353                                          HW_VAR_SLOT_TIME, &slot_time_val);
 354                         }
 355                 }
 356         }
 357 }
 358 
 359 static const struct rtllib_qos_parameters def_qos_parameters = {
 360         {cpu_to_le16(3), cpu_to_le16(3), cpu_to_le16(3), cpu_to_le16(3)},
 361         {cpu_to_le16(7), cpu_to_le16(7), cpu_to_le16(7), cpu_to_le16(7)},
 362         {2, 2, 2, 2},
 363         {0, 0, 0, 0},
 364         {0, 0, 0, 0}
 365 };
 366 
 367 static void _rtl92e_update_beacon(void *data)
 368 {
 369         struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
 370                                   update_beacon_wq.work);
 371         struct net_device *dev = priv->rtllib->dev;
 372         struct rtllib_device *ieee = priv->rtllib;
 373         struct rtllib_network *net = &ieee->current_network;
 374 
 375         if (ieee->pHTInfo->bCurrentHTSupport)
 376                 HT_update_self_and_peer_setting(ieee, net);
 377         ieee->pHTInfo->bCurrentRT2RTLongSlotTime =
 378                  net->bssht.bdRT2RTLongSlotTime;
 379         ieee->pHTInfo->RT2RT_HT_Mode = net->bssht.RT2RT_HT_Mode;
 380         _rtl92e_update_cap(dev, net->capability);
 381 }
 382 
 383 static void _rtl92e_qos_activate(void *data)
 384 {
 385         struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
 386                                   qos_activate);
 387         struct net_device *dev = priv->rtllib->dev;
 388         int i;
 389 
 390         mutex_lock(&priv->mutex);
 391         if (priv->rtllib->state != RTLLIB_LINKED)
 392                 goto success;
 393         RT_TRACE(COMP_QOS,
 394                  "qos active process with associate response received\n");
 395 
 396         for (i = 0; i <  QOS_QUEUE_NUM; i++)
 397                 priv->rtllib->SetHwRegHandler(dev, HW_VAR_AC_PARAM, (u8 *)(&i));
 398 
 399 
 400 success:
 401         mutex_unlock(&priv->mutex);
 402 }
 403 
 404 static int _rtl92e_qos_handle_probe_response(struct r8192_priv *priv,
 405                                              int active_network,
 406                                              struct rtllib_network *network)
 407 {
 408         int ret = 0;
 409         u32 size = sizeof(struct rtllib_qos_parameters);
 410 
 411         if (priv->rtllib->state != RTLLIB_LINKED)
 412                 return ret;
 413 
 414         if (priv->rtllib->iw_mode != IW_MODE_INFRA)
 415                 return ret;
 416 
 417         if (network->flags & NETWORK_HAS_QOS_MASK) {
 418                 if (active_network &&
 419                                 (network->flags & NETWORK_HAS_QOS_PARAMETERS))
 420                         network->qos_data.active = network->qos_data.supported;
 421 
 422                 if ((network->qos_data.active == 1) && (active_network == 1) &&
 423                                 (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
 424                                 (network->qos_data.old_param_count !=
 425                                 network->qos_data.param_count)) {
 426                         network->qos_data.old_param_count =
 427                                 network->qos_data.param_count;
 428                         priv->rtllib->wmm_acm = network->qos_data.wmm_acm;
 429                         schedule_work(&priv->qos_activate);
 430                         RT_TRACE(COMP_QOS,
 431                                  "QoS parameters change call qos_activate\n");
 432                 }
 433         } else {
 434                 memcpy(&priv->rtllib->current_network.qos_data.parameters,
 435                        &def_qos_parameters, size);
 436 
 437                 if ((network->qos_data.active == 1) && (active_network == 1)) {
 438                         schedule_work(&priv->qos_activate);
 439                         RT_TRACE(COMP_QOS,
 440                                  "QoS was disabled call qos_activate\n");
 441                 }
 442                 network->qos_data.active = 0;
 443                 network->qos_data.supported = 0;
 444         }
 445 
 446         return 0;
 447 }
 448 
 449 static int _rtl92e_handle_beacon(struct net_device *dev,
 450                                  struct rtllib_beacon *beacon,
 451                                  struct rtllib_network *network)
 452 {
 453         struct r8192_priv *priv = rtllib_priv(dev);
 454 
 455         _rtl92e_qos_handle_probe_response(priv, 1, network);
 456 
 457         schedule_delayed_work(&priv->update_beacon_wq, 0);
 458         return 0;
 459 
 460 }
 461 
 462 static int _rtl92e_qos_assoc_resp(struct r8192_priv *priv,
 463                                   struct rtllib_network *network)
 464 {
 465         unsigned long flags;
 466         u32 size = sizeof(struct rtllib_qos_parameters);
 467         int set_qos_param = 0;
 468 
 469         if (!priv || !network)
 470                 return 0;
 471 
 472         if (priv->rtllib->state != RTLLIB_LINKED)
 473                 return 0;
 474 
 475         if (priv->rtllib->iw_mode != IW_MODE_INFRA)
 476                 return 0;
 477 
 478         spin_lock_irqsave(&priv->rtllib->lock, flags);
 479         if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
 480                 memcpy(&priv->rtllib->current_network.qos_data.parameters,
 481                        &network->qos_data.parameters,
 482                        sizeof(struct rtllib_qos_parameters));
 483                 priv->rtllib->current_network.qos_data.active = 1;
 484                 priv->rtllib->wmm_acm = network->qos_data.wmm_acm;
 485                 set_qos_param = 1;
 486                 priv->rtllib->current_network.qos_data.old_param_count =
 487                         priv->rtllib->current_network.qos_data.param_count;
 488                 priv->rtllib->current_network.qos_data.param_count =
 489                         network->qos_data.param_count;
 490         } else {
 491                 memcpy(&priv->rtllib->current_network.qos_data.parameters,
 492                 &def_qos_parameters, size);
 493                 priv->rtllib->current_network.qos_data.active = 0;
 494                 priv->rtllib->current_network.qos_data.supported = 0;
 495                 set_qos_param = 1;
 496         }
 497 
 498         spin_unlock_irqrestore(&priv->rtllib->lock, flags);
 499 
 500         RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n", __func__,
 501                  network->flags, priv->rtllib->current_network.qos_data.active);
 502         if (set_qos_param == 1) {
 503                 rtl92e_dm_init_edca_turbo(priv->rtllib->dev);
 504                 schedule_work(&priv->qos_activate);
 505         }
 506         return 0;
 507 }
 508 
 509 static int _rtl92e_handle_assoc_response(struct net_device *dev,
 510                                  struct rtllib_assoc_response_frame *resp,
 511                                  struct rtllib_network *network)
 512 {
 513         struct r8192_priv *priv = rtllib_priv(dev);
 514 
 515         _rtl92e_qos_assoc_resp(priv, network);
 516         return 0;
 517 }
 518 
 519 static void _rtl92e_prepare_beacon(struct r8192_priv *priv)
 520 {
 521         struct net_device *dev = priv->rtllib->dev;
 522         struct sk_buff *pskb = NULL, *pnewskb = NULL;
 523         struct cb_desc *tcb_desc = NULL;
 524         struct rtl8192_tx_ring *ring = NULL;
 525         struct tx_desc *pdesc = NULL;
 526 
 527         ring = &priv->tx_ring[BEACON_QUEUE];
 528         pskb = __skb_dequeue(&ring->queue);
 529         kfree_skb(pskb);
 530 
 531         pnewskb = rtllib_get_beacon(priv->rtllib);
 532         if (!pnewskb)
 533                 return;
 534 
 535         tcb_desc = (struct cb_desc *)(pnewskb->cb + 8);
 536         tcb_desc->queue_index = BEACON_QUEUE;
 537         tcb_desc->data_rate = 2;
 538         tcb_desc->RATRIndex = 7;
 539         tcb_desc->bTxDisableRateFallBack = 1;
 540         tcb_desc->bTxUseDriverAssingedRate = 1;
 541         skb_push(pnewskb, priv->rtllib->tx_headroom);
 542 
 543         pdesc = &ring->desc[0];
 544         priv->ops->tx_fill_descriptor(dev, pdesc, tcb_desc, pnewskb);
 545         __skb_queue_tail(&ring->queue, pnewskb);
 546         pdesc->OWN = 1;
 547 }
 548 
 549 static void _rtl92e_stop_beacon(struct net_device *dev)
 550 {
 551 }
 552 
 553 void rtl92e_config_rate(struct net_device *dev, u16 *rate_config)
 554 {
 555         struct r8192_priv *priv = rtllib_priv(dev);
 556         struct rtllib_network *net;
 557         u8 i = 0, basic_rate = 0;
 558 
 559         net = &priv->rtllib->current_network;
 560 
 561         for (i = 0; i < net->rates_len; i++) {
 562                 basic_rate = net->rates[i] & 0x7f;
 563                 switch (basic_rate) {
 564                 case MGN_1M:
 565                         *rate_config |= RRSR_1M;
 566                         break;
 567                 case MGN_2M:
 568                         *rate_config |= RRSR_2M;
 569                         break;
 570                 case MGN_5_5M:
 571                         *rate_config |= RRSR_5_5M;
 572                         break;
 573                 case MGN_11M:
 574                         *rate_config |= RRSR_11M;
 575                         break;
 576                 case MGN_6M:
 577                         *rate_config |= RRSR_6M;
 578                         break;
 579                 case MGN_9M:
 580                         *rate_config |= RRSR_9M;
 581                         break;
 582                 case MGN_12M:
 583                         *rate_config |= RRSR_12M;
 584                         break;
 585                 case MGN_18M:
 586                         *rate_config |= RRSR_18M;
 587                         break;
 588                 case MGN_24M:
 589                         *rate_config |= RRSR_24M;
 590                         break;
 591                 case MGN_36M:
 592                         *rate_config |= RRSR_36M;
 593                         break;
 594                 case MGN_48M:
 595                         *rate_config |= RRSR_48M;
 596                         break;
 597                 case MGN_54M:
 598                         *rate_config |= RRSR_54M;
 599                         break;
 600                 }
 601         }
 602 
 603         for (i = 0; i < net->rates_ex_len; i++) {
 604                 basic_rate = net->rates_ex[i] & 0x7f;
 605                 switch (basic_rate) {
 606                 case MGN_1M:
 607                         *rate_config |= RRSR_1M;
 608                         break;
 609                 case MGN_2M:
 610                         *rate_config |= RRSR_2M;
 611                         break;
 612                 case MGN_5_5M:
 613                         *rate_config |= RRSR_5_5M;
 614                         break;
 615                 case MGN_11M:
 616                         *rate_config |= RRSR_11M;
 617                         break;
 618                 case MGN_6M:
 619                         *rate_config |= RRSR_6M;
 620                         break;
 621                 case MGN_9M:
 622                         *rate_config |= RRSR_9M;
 623                         break;
 624                 case MGN_12M:
 625                         *rate_config |= RRSR_12M;
 626                         break;
 627                 case MGN_18M:
 628                         *rate_config |= RRSR_18M;
 629                         break;
 630                 case MGN_24M:
 631                         *rate_config |= RRSR_24M;
 632                         break;
 633                 case MGN_36M:
 634                         *rate_config |= RRSR_36M;
 635                         break;
 636                 case MGN_48M:
 637                         *rate_config |= RRSR_48M;
 638                         break;
 639                 case MGN_54M:
 640                         *rate_config |= RRSR_54M;
 641                         break;
 642                 }
 643         }
 644 }
 645 
 646 static void _rtl92e_refresh_support_rate(struct r8192_priv *priv)
 647 {
 648         struct rtllib_device *ieee = priv->rtllib;
 649 
 650         if (ieee->mode == WIRELESS_MODE_N_24G ||
 651             ieee->mode == WIRELESS_MODE_N_5G) {
 652                 memcpy(ieee->Regdot11HTOperationalRateSet,
 653                        ieee->RegHTSuppRateSet, 16);
 654                 memcpy(ieee->Regdot11TxHTOperationalRateSet,
 655                        ieee->RegHTSuppRateSet, 16);
 656 
 657         } else {
 658                 memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
 659         }
 660 }
 661 
 662 static u8 _rtl92e_get_supported_wireless_mode(struct net_device *dev)
 663 {
 664         struct r8192_priv *priv = rtllib_priv(dev);
 665         u8 ret = 0;
 666 
 667         switch (priv->rf_chip) {
 668         case RF_8225:
 669         case RF_8256:
 670         case RF_6052:
 671         case RF_PSEUDO_11N:
 672                 ret = (WIRELESS_MODE_N_24G | WIRELESS_MODE_G | WIRELESS_MODE_B);
 673                 break;
 674         case RF_8258:
 675                 ret = (WIRELESS_MODE_A | WIRELESS_MODE_N_5G);
 676                 break;
 677         default:
 678                 ret = WIRELESS_MODE_B;
 679                 break;
 680         }
 681         return ret;
 682 }
 683 
 684 void rtl92e_set_wireless_mode(struct net_device *dev, u8 wireless_mode)
 685 {
 686         struct r8192_priv *priv = rtllib_priv(dev);
 687         u8 bSupportMode = _rtl92e_get_supported_wireless_mode(dev);
 688 
 689         if ((wireless_mode == WIRELESS_MODE_AUTO) ||
 690             ((wireless_mode & bSupportMode) == 0)) {
 691                 if (bSupportMode & WIRELESS_MODE_N_24G) {
 692                         wireless_mode = WIRELESS_MODE_N_24G;
 693                 } else if (bSupportMode & WIRELESS_MODE_N_5G) {
 694                         wireless_mode = WIRELESS_MODE_N_5G;
 695                 } else if ((bSupportMode & WIRELESS_MODE_A)) {
 696                         wireless_mode = WIRELESS_MODE_A;
 697                 } else if ((bSupportMode & WIRELESS_MODE_G)) {
 698                         wireless_mode = WIRELESS_MODE_G;
 699                 } else if ((bSupportMode & WIRELESS_MODE_B)) {
 700                         wireless_mode = WIRELESS_MODE_B;
 701                 } else {
 702                         netdev_info(dev,
 703                                     "%s(): Unsupported mode requested. Fallback to 802.11b\n",
 704                                     __func__);
 705                         wireless_mode = WIRELESS_MODE_B;
 706                 }
 707         }
 708 
 709         if ((wireless_mode & (WIRELESS_MODE_B | WIRELESS_MODE_G)) ==
 710             (WIRELESS_MODE_G | WIRELESS_MODE_B))
 711                 wireless_mode = WIRELESS_MODE_G;
 712 
 713         priv->rtllib->mode = wireless_mode;
 714 
 715         if ((wireless_mode == WIRELESS_MODE_N_24G) ||
 716             (wireless_mode == WIRELESS_MODE_N_5G)) {
 717                 priv->rtllib->pHTInfo->bEnableHT = 1;
 718         RT_TRACE(COMP_DBG, "%s(), wireless_mode:%x, bEnableHT = 1\n",
 719                  __func__, wireless_mode);
 720         } else {
 721                 priv->rtllib->pHTInfo->bEnableHT = 0;
 722                 RT_TRACE(COMP_DBG, "%s(), wireless_mode:%x, bEnableHT = 0\n",
 723                          __func__, wireless_mode);
 724         }
 725 
 726         RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
 727         _rtl92e_refresh_support_rate(priv);
 728 }
 729 
 730 static int _rtl92e_sta_up(struct net_device *dev, bool is_silent_reset)
 731 {
 732         struct r8192_priv *priv = rtllib_priv(dev);
 733         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
 734                                         (&priv->rtllib->PowerSaveControl);
 735         bool init_status = true;
 736 
 737         priv->bDriverIsGoingToUnload = false;
 738         priv->bdisable_nic = false;
 739 
 740         priv->up = 1;
 741         priv->rtllib->ieee_up = 1;
 742 
 743         priv->up_first_time = 0;
 744         RT_TRACE(COMP_INIT, "Bringing up iface");
 745         priv->bfirst_init = true;
 746         init_status = priv->ops->initialize_adapter(dev);
 747         if (!init_status) {
 748                 netdev_err(dev, "%s(): Initialization failed!\n", __func__);
 749                 priv->bfirst_init = false;
 750                 return -1;
 751         }
 752 
 753         RT_TRACE(COMP_INIT, "start adapter finished\n");
 754         RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
 755         priv->bfirst_init = false;
 756 
 757         if (priv->polling_timer_on == 0)
 758                 rtl92e_check_rfctrl_gpio_timer(&priv->gpio_polling_timer);
 759 
 760         if (priv->rtllib->state != RTLLIB_LINKED)
 761                 rtllib_softmac_start_protocol(priv->rtllib, 0);
 762         rtllib_reset_queue(priv->rtllib);
 763         _rtl92e_watchdog_timer_cb(&priv->watch_dog_timer);
 764 
 765         if (!netif_queue_stopped(dev))
 766                 netif_start_queue(dev);
 767         else
 768                 netif_wake_queue(dev);
 769 
 770         return 0;
 771 }
 772 
 773 static int _rtl92e_sta_down(struct net_device *dev, bool shutdownrf)
 774 {
 775         struct r8192_priv *priv = rtllib_priv(dev);
 776         unsigned long flags = 0;
 777         u8 RFInProgressTimeOut = 0;
 778 
 779         if (priv->up == 0)
 780                 return -1;
 781 
 782         if (priv->rtllib->rtllib_ips_leave)
 783                 priv->rtllib->rtllib_ips_leave(dev);
 784 
 785         if (priv->rtllib->state == RTLLIB_LINKED)
 786                 rtl92e_leisure_ps_leave(dev);
 787 
 788         priv->bDriverIsGoingToUnload = true;
 789         priv->up = 0;
 790         priv->rtllib->ieee_up = 0;
 791         priv->bfirst_after_down = true;
 792         RT_TRACE(COMP_DOWN, "==========>%s()\n", __func__);
 793         if (!netif_queue_stopped(dev))
 794                 netif_stop_queue(dev);
 795 
 796         priv->rtllib->wpa_ie_len = 0;
 797         kfree(priv->rtllib->wpa_ie);
 798         priv->rtllib->wpa_ie = NULL;
 799         rtl92e_cam_reset(dev);
 800         memset(priv->rtllib->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
 801         rtl92e_irq_disable(dev);
 802 
 803         del_timer_sync(&priv->watch_dog_timer);
 804         _rtl92e_cancel_deferred_work(priv);
 805         cancel_delayed_work(&priv->rtllib->hw_wakeup_wq);
 806 
 807         rtllib_softmac_stop_protocol(priv->rtllib, 0, true);
 808         spin_lock_irqsave(&priv->rf_ps_lock, flags);
 809         while (priv->RFChangeInProgress) {
 810                 spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
 811                 if (RFInProgressTimeOut > 100) {
 812                         spin_lock_irqsave(&priv->rf_ps_lock, flags);
 813                         break;
 814                 }
 815                 RT_TRACE(COMP_DBG,
 816                          "===>%s():RF is in progress, need to wait until rf change is done.\n",
 817                          __func__);
 818                 mdelay(1);
 819                 RFInProgressTimeOut++;
 820                 spin_lock_irqsave(&priv->rf_ps_lock, flags);
 821         }
 822         priv->RFChangeInProgress = true;
 823         spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
 824         priv->ops->stop_adapter(dev, false);
 825         spin_lock_irqsave(&priv->rf_ps_lock, flags);
 826         priv->RFChangeInProgress = false;
 827         spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
 828         udelay(100);
 829         memset(&priv->rtllib->current_network, 0,
 830                offsetof(struct rtllib_network, list));
 831         RT_TRACE(COMP_DOWN, "<==========%s()\n", __func__);
 832 
 833         return 0;
 834 }
 835 
 836 static void _rtl92e_init_priv_handler(struct net_device *dev)
 837 {
 838         struct r8192_priv *priv = rtllib_priv(dev);
 839 
 840         priv->rtllib->softmac_hard_start_xmit   = _rtl92e_hard_start_xmit;
 841         priv->rtllib->set_chan                  = _rtl92e_set_chan;
 842         priv->rtllib->link_change               = priv->ops->link_change;
 843         priv->rtllib->softmac_data_hard_start_xmit = _rtl92e_hard_data_xmit;
 844         priv->rtllib->check_nic_enough_desc     = _rtl92e_check_nic_enough_desc;
 845         priv->rtllib->handle_assoc_response     = _rtl92e_handle_assoc_response;
 846         priv->rtllib->handle_beacon             = _rtl92e_handle_beacon;
 847         priv->rtllib->SetWirelessMode           = rtl92e_set_wireless_mode;
 848         priv->rtllib->LeisurePSLeave            = rtl92e_leisure_ps_leave;
 849         priv->rtllib->SetBWModeHandler          = rtl92e_set_bw_mode;
 850         priv->rf_set_chan                       = rtl92e_set_channel;
 851 
 852         priv->rtllib->start_send_beacons = rtl92e_start_beacon;
 853         priv->rtllib->stop_send_beacons = _rtl92e_stop_beacon;
 854 
 855         priv->rtllib->sta_wake_up = rtl92e_hw_wakeup;
 856         priv->rtllib->enter_sleep_state = rtl92e_enter_sleep;
 857         priv->rtllib->ps_is_queue_empty = _rtl92e_is_tx_queue_empty;
 858 
 859         priv->rtllib->GetNmodeSupportBySecCfg = rtl92e_get_nmode_support_by_sec;
 860         priv->rtllib->GetHalfNmodeSupportByAPsHandler =
 861                                                 rtl92e_is_halfn_supported_by_ap;
 862 
 863         priv->rtllib->SetHwRegHandler = rtl92e_set_reg;
 864         priv->rtllib->AllowAllDestAddrHandler = rtl92e_set_monitor_mode;
 865         priv->rtllib->SetFwCmdHandler = NULL;
 866         priv->rtllib->InitialGainHandler = rtl92e_init_gain;
 867         priv->rtllib->rtllib_ips_leave_wq = rtl92e_rtllib_ips_leave_wq;
 868         priv->rtllib->rtllib_ips_leave = rtl92e_rtllib_ips_leave;
 869 
 870         priv->rtllib->LedControlHandler = NULL;
 871         priv->rtllib->UpdateBeaconInterruptHandler = NULL;
 872 
 873         priv->rtllib->ScanOperationBackupHandler = rtl92e_scan_op_backup;
 874 }
 875 
 876 static void _rtl92e_init_priv_constant(struct net_device *dev)
 877 {
 878         struct r8192_priv *priv = rtllib_priv(dev);
 879         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
 880                                         &priv->rtllib->PowerSaveControl;
 881 
 882         pPSC->RegMaxLPSAwakeIntvl = 5;
 883 }
 884 
 885 
 886 static void _rtl92e_init_priv_variable(struct net_device *dev)
 887 {
 888         struct r8192_priv *priv = rtllib_priv(dev);
 889         u8 i;
 890 
 891         priv->AcmMethod = eAcmWay2_SW;
 892         priv->dot11CurrentPreambleMode = PREAMBLE_AUTO;
 893         priv->rtllib->status = 0;
 894         priv->polling_timer_on = 0;
 895         priv->up_first_time = 1;
 896         priv->blinked_ingpio = false;
 897         priv->bDriverIsGoingToUnload = false;
 898         priv->being_init_adapter = false;
 899         priv->initialized_at_probe = false;
 900         priv->bdisable_nic = false;
 901         priv->bfirst_init = false;
 902         priv->txringcount = 64;
 903         priv->rxbuffersize = 9100;
 904         priv->rxringcount = MAX_RX_COUNT;
 905         priv->irq_enabled = 0;
 906         priv->chan = 1;
 907         priv->RegChannelPlan = 0xf;
 908         priv->rtllib->mode = WIRELESS_MODE_AUTO;
 909         priv->rtllib->iw_mode = IW_MODE_INFRA;
 910         priv->rtllib->bNetPromiscuousMode = false;
 911         priv->rtllib->IntelPromiscuousModeInfo.bPromiscuousOn = false;
 912         priv->rtllib->IntelPromiscuousModeInfo.bFilterSourceStationFrame =
 913                                                                  false;
 914         priv->rtllib->ieee_up = 0;
 915         priv->retry_rts = DEFAULT_RETRY_RTS;
 916         priv->retry_data = DEFAULT_RETRY_DATA;
 917         priv->rtllib->rts = DEFAULT_RTS_THRESHOLD;
 918         priv->rtllib->rate = 110;
 919         priv->rtllib->short_slot = 1;
 920         priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
 921         priv->bcck_in_ch14 = false;
 922         priv->bfsync_processing  = false;
 923         priv->CCKPresentAttentuation = 0;
 924         priv->rfa_txpowertrackingindex = 0;
 925         priv->rfc_txpowertrackingindex = 0;
 926         priv->CckPwEnl = 6;
 927         priv->ScanDelay = 50;
 928         priv->ResetProgress = RESET_TYPE_NORESET;
 929         priv->bForcedSilentReset = false;
 930         priv->bDisableNormalResetCheck = false;
 931         priv->force_reset = false;
 932         memset(priv->rtllib->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
 933 
 934         memset(&priv->InterruptLog, 0, sizeof(struct log_int_8190));
 935         priv->RxCounter = 0;
 936         priv->rtllib->wx_set_enc = 0;
 937         priv->bHwRadioOff = false;
 938         priv->RegRfOff = false;
 939         priv->isRFOff = false;
 940         priv->bInPowerSaveMode = false;
 941         priv->rtllib->RfOffReason = 0;
 942         priv->RFChangeInProgress = false;
 943         priv->bHwRfOffAction = 0;
 944         priv->SetRFPowerStateInProgress = false;
 945         priv->rtllib->PowerSaveControl.bInactivePs = true;
 946         priv->rtllib->PowerSaveControl.bIPSModeBackup = false;
 947         priv->rtllib->PowerSaveControl.bLeisurePs = true;
 948         priv->rtllib->PowerSaveControl.bFwCtrlLPS = false;
 949         priv->rtllib->LPSDelayCnt = 0;
 950         priv->rtllib->sta_sleep = LPS_IS_WAKE;
 951         priv->rtllib->eRFPowerState = eRfOn;
 952 
 953         priv->rtllib->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
 954         priv->rtllib->iw_mode = IW_MODE_INFRA;
 955         priv->rtllib->active_scan = 1;
 956         priv->rtllib->be_scan_inprogress = false;
 957         priv->rtllib->modulation = RTLLIB_CCK_MODULATION |
 958                                    RTLLIB_OFDM_MODULATION;
 959         priv->rtllib->host_encrypt = 1;
 960         priv->rtllib->host_decrypt = 1;
 961 
 962         priv->rtllib->fts = DEFAULT_FRAG_THRESHOLD;
 963 
 964         priv->card_type = PCI;
 965 
 966         priv->pFirmware = vzalloc(sizeof(struct rt_firmware));
 967         if (!priv->pFirmware)
 968                 netdev_err(dev,
 969                            "rtl8192e: Unable to allocate space for firmware\n");
 970 
 971         skb_queue_head_init(&priv->skb_queue);
 972 
 973         for (i = 0; i < MAX_QUEUE_SIZE; i++)
 974                 skb_queue_head_init(&priv->rtllib->skb_waitQ[i]);
 975         for (i = 0; i < MAX_QUEUE_SIZE; i++)
 976                 skb_queue_head_init(&priv->rtllib->skb_aggQ[i]);
 977 }
 978 
 979 static void _rtl92e_init_priv_lock(struct r8192_priv *priv)
 980 {
 981         spin_lock_init(&priv->tx_lock);
 982         spin_lock_init(&priv->irq_th_lock);
 983         spin_lock_init(&priv->rf_ps_lock);
 984         spin_lock_init(&priv->ps_lock);
 985         mutex_init(&priv->wx_mutex);
 986         mutex_init(&priv->rf_mutex);
 987         mutex_init(&priv->mutex);
 988 }
 989 
 990 static void _rtl92e_init_priv_task(struct net_device *dev)
 991 {
 992         struct r8192_priv *priv = rtllib_priv(dev);
 993 
 994         INIT_WORK_RSL(&priv->reset_wq, (void *)_rtl92e_restart, dev);
 995         INIT_WORK_RSL(&priv->rtllib->ips_leave_wq, (void *)rtl92e_ips_leave_wq,
 996                       dev);
 997         INIT_DELAYED_WORK_RSL(&priv->watch_dog_wq,
 998                               (void *)_rtl92e_watchdog_wq_cb, dev);
 999         INIT_DELAYED_WORK_RSL(&priv->txpower_tracking_wq,
1000                               (void *)rtl92e_dm_txpower_tracking_wq, dev);
1001         INIT_DELAYED_WORK_RSL(&priv->rfpath_check_wq,
1002                               (void *)rtl92e_dm_rf_pathcheck_wq, dev);
1003         INIT_DELAYED_WORK_RSL(&priv->update_beacon_wq,
1004                               (void *)_rtl92e_update_beacon, dev);
1005         INIT_WORK_RSL(&priv->qos_activate, (void *)_rtl92e_qos_activate, dev);
1006         INIT_DELAYED_WORK_RSL(&priv->rtllib->hw_wakeup_wq,
1007                               (void *)rtl92e_hw_wakeup_wq, dev);
1008         INIT_DELAYED_WORK_RSL(&priv->rtllib->hw_sleep_wq,
1009                               (void *)rtl92e_hw_sleep_wq, dev);
1010         tasklet_init(&priv->irq_rx_tasklet,
1011                      (void(*)(unsigned long))_rtl92e_irq_rx_tasklet,
1012                      (unsigned long)priv);
1013         tasklet_init(&priv->irq_tx_tasklet,
1014                      (void(*)(unsigned long))_rtl92e_irq_tx_tasklet,
1015                      (unsigned long)priv);
1016         tasklet_init(&priv->irq_prepare_beacon_tasklet,
1017                      (void(*)(unsigned long))_rtl92e_prepare_beacon,
1018                      (unsigned long)priv);
1019 }
1020 
1021 static short _rtl92e_get_channel_map(struct net_device *dev)
1022 {
1023         int i;
1024 
1025         struct r8192_priv *priv = rtllib_priv(dev);
1026 
1027         if ((priv->rf_chip != RF_8225) && (priv->rf_chip != RF_8256) &&
1028                                                 (priv->rf_chip != RF_6052)) {
1029                 netdev_err(dev, "%s: unknown rf chip, can't set channel map\n",
1030                            __func__);
1031                 return -1;
1032         }
1033 
1034         if (priv->ChannelPlan >= COUNTRY_CODE_MAX) {
1035                 netdev_info(dev,
1036                             "rtl819x_init:Error channel plan! Set to default.\n");
1037                 priv->ChannelPlan = COUNTRY_CODE_FCC;
1038         }
1039         RT_TRACE(COMP_INIT, "Channel plan is %d\n", priv->ChannelPlan);
1040         dot11d_init(priv->rtllib);
1041         dot11d_channel_map(priv->ChannelPlan, priv->rtllib);
1042         for (i = 1; i <= 11; i++)
1043                 (priv->rtllib->active_channel_map)[i] = 1;
1044         (priv->rtllib->active_channel_map)[12] = 2;
1045         (priv->rtllib->active_channel_map)[13] = 2;
1046 
1047         return 0;
1048 }
1049 
1050 static short _rtl92e_init(struct net_device *dev)
1051 {
1052         struct r8192_priv *priv = rtllib_priv(dev);
1053 
1054         memset(&priv->stats, 0, sizeof(struct rt_stats));
1055 
1056         _rtl92e_init_priv_handler(dev);
1057         _rtl92e_init_priv_constant(dev);
1058         _rtl92e_init_priv_variable(dev);
1059         _rtl92e_init_priv_lock(priv);
1060         _rtl92e_init_priv_task(dev);
1061         priv->ops->get_eeprom_size(dev);
1062         priv->ops->init_adapter_variable(dev);
1063         _rtl92e_get_channel_map(dev);
1064 
1065         rtl92e_dm_init(dev);
1066 
1067         timer_setup(&priv->watch_dog_timer, _rtl92e_watchdog_timer_cb, 0);
1068 
1069         timer_setup(&priv->gpio_polling_timer, rtl92e_check_rfctrl_gpio_timer,
1070                     0);
1071 
1072         rtl92e_irq_disable(dev);
1073         if (request_irq(dev->irq, _rtl92e_irq, IRQF_SHARED, dev->name, dev)) {
1074                 netdev_err(dev, "Error allocating IRQ %d", dev->irq);
1075                 return -1;
1076         }
1077 
1078         priv->irq = dev->irq;
1079         RT_TRACE(COMP_INIT, "IRQ %d\n", dev->irq);
1080 
1081         if (_rtl92e_pci_initdescring(dev) != 0) {
1082                 netdev_err(dev, "Endopoints initialization failed");
1083                 free_irq(dev->irq, dev);
1084                 return -1;
1085         }
1086 
1087         return 0;
1088 }
1089 
1090 /***************************************************************************
1091  * -------------------------------WATCHDOG STUFF---------------------------
1092  **************************************************************************/
1093 static short _rtl92e_is_tx_queue_empty(struct net_device *dev)
1094 {
1095         int i = 0;
1096         struct r8192_priv *priv = rtllib_priv(dev);
1097 
1098         for (i = 0; i <= MGNT_QUEUE; i++) {
1099                 if ((i == TXCMD_QUEUE) || (i == HCCA_QUEUE))
1100                         continue;
1101                 if (skb_queue_len(&(&priv->tx_ring[i])->queue) > 0) {
1102                         netdev_info(dev, "===>tx queue is not empty:%d, %d\n",
1103                                i, skb_queue_len(&(&priv->tx_ring[i])->queue));
1104                         return 0;
1105                 }
1106         }
1107         return 1;
1108 }
1109 
1110 static enum reset_type _rtl92e_tx_check_stuck(struct net_device *dev)
1111 {
1112         struct r8192_priv *priv = rtllib_priv(dev);
1113         u8      QueueID;
1114         bool    bCheckFwTxCnt = false;
1115         struct rtl8192_tx_ring  *ring = NULL;
1116         struct sk_buff *skb = NULL;
1117         struct cb_desc *tcb_desc = NULL;
1118         unsigned long flags = 0;
1119 
1120         switch (priv->rtllib->ps) {
1121         case RTLLIB_PS_DISABLED:
1122                 break;
1123         case (RTLLIB_PS_MBCAST | RTLLIB_PS_UNICAST):
1124                 break;
1125         default:
1126                 break;
1127         }
1128         spin_lock_irqsave(&priv->irq_th_lock, flags);
1129         for (QueueID = 0; QueueID < MAX_TX_QUEUE; QueueID++) {
1130                 if (QueueID == TXCMD_QUEUE)
1131                         continue;
1132 
1133                 if (QueueID == BEACON_QUEUE)
1134                         continue;
1135 
1136                 ring = &priv->tx_ring[QueueID];
1137 
1138                 if (skb_queue_len(&ring->queue) == 0) {
1139                         continue;
1140                 } else {
1141                         skb = __skb_peek(&ring->queue);
1142                         tcb_desc = (struct cb_desc *)(skb->cb +
1143                                     MAX_DEV_ADDR_SIZE);
1144                         tcb_desc->nStuckCount++;
1145                         bCheckFwTxCnt = true;
1146                         if (tcb_desc->nStuckCount > 1)
1147                                 netdev_info(dev,
1148                                             "%s: QueueID=%d tcb_desc->nStuckCount=%d\n",
1149                                             __func__, QueueID,
1150                                             tcb_desc->nStuckCount);
1151                 }
1152         }
1153         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1154 
1155         if (bCheckFwTxCnt) {
1156                 if (priv->ops->TxCheckStuckHandler(dev)) {
1157                         RT_TRACE(COMP_RESET,
1158                                  "TxCheckStuck(): Fw indicates no Tx condition!\n");
1159                         return RESET_TYPE_SILENT;
1160                 }
1161         }
1162 
1163         return RESET_TYPE_NORESET;
1164 }
1165 
1166 static enum reset_type _rtl92e_rx_check_stuck(struct net_device *dev)
1167 {
1168         struct r8192_priv *priv = rtllib_priv(dev);
1169 
1170         if (priv->ops->RxCheckStuckHandler(dev)) {
1171                 RT_TRACE(COMP_RESET, "RxStuck Condition\n");
1172                 return RESET_TYPE_SILENT;
1173         }
1174 
1175         return RESET_TYPE_NORESET;
1176 }
1177 
1178 static enum reset_type _rtl92e_if_check_reset(struct net_device *dev)
1179 {
1180         struct r8192_priv *priv = rtllib_priv(dev);
1181         enum reset_type TxResetType = RESET_TYPE_NORESET;
1182         enum reset_type RxResetType = RESET_TYPE_NORESET;
1183         enum rt_rf_power_state rfState;
1184 
1185         rfState = priv->rtllib->eRFPowerState;
1186 
1187         if (rfState == eRfOn)
1188                 TxResetType = _rtl92e_tx_check_stuck(dev);
1189 
1190         if (rfState == eRfOn &&
1191             (priv->rtllib->iw_mode == IW_MODE_INFRA) &&
1192             (priv->rtllib->state == RTLLIB_LINKED))
1193                 RxResetType = _rtl92e_rx_check_stuck(dev);
1194 
1195         if (TxResetType == RESET_TYPE_NORMAL ||
1196             RxResetType == RESET_TYPE_NORMAL) {
1197                 netdev_info(dev, "%s(): TxResetType is %d, RxResetType is %d\n",
1198                             __func__, TxResetType, RxResetType);
1199                 return RESET_TYPE_NORMAL;
1200         } else if (TxResetType == RESET_TYPE_SILENT ||
1201                    RxResetType == RESET_TYPE_SILENT) {
1202                 netdev_info(dev, "%s(): TxResetType is %d, RxResetType is %d\n",
1203                             __func__, TxResetType, RxResetType);
1204                 return RESET_TYPE_SILENT;
1205         } else {
1206                 return RESET_TYPE_NORESET;
1207         }
1208 
1209 }
1210 
1211 static void _rtl92e_if_silent_reset(struct net_device *dev)
1212 {
1213         struct r8192_priv *priv = rtllib_priv(dev);
1214         u8      reset_times = 0;
1215         int reset_status = 0;
1216         struct rtllib_device *ieee = priv->rtllib;
1217         unsigned long flag;
1218 
1219         if (priv->ResetProgress == RESET_TYPE_NORESET) {
1220 
1221                 RT_TRACE(COMP_RESET, "=========>Reset progress!!\n");
1222 
1223                 priv->ResetProgress = RESET_TYPE_SILENT;
1224 
1225                 spin_lock_irqsave(&priv->rf_ps_lock, flag);
1226                 if (priv->RFChangeInProgress) {
1227                         spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1228                         goto END;
1229                 }
1230                 priv->RFChangeInProgress = true;
1231                 priv->bResetInProgress = true;
1232                 spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1233 
1234 RESET_START:
1235 
1236                 mutex_lock(&priv->wx_mutex);
1237 
1238                 if (priv->rtllib->state == RTLLIB_LINKED)
1239                         rtl92e_leisure_ps_leave(dev);
1240 
1241                 if (priv->up) {
1242                         netdev_info(dev, "%s():the driver is not up.\n",
1243                                     __func__);
1244                         mutex_unlock(&priv->wx_mutex);
1245                         return;
1246                 }
1247                 priv->up = 0;
1248 
1249                 RT_TRACE(COMP_RESET, "%s():======>start to down the driver\n",
1250                           __func__);
1251                 mdelay(1000);
1252                 RT_TRACE(COMP_RESET,
1253                          "%s():111111111111111111111111======>start to down the driver\n",
1254                          __func__);
1255 
1256                 if (!netif_queue_stopped(dev))
1257                         netif_stop_queue(dev);
1258 
1259                 rtl92e_irq_disable(dev);
1260                 del_timer_sync(&priv->watch_dog_timer);
1261                 _rtl92e_cancel_deferred_work(priv);
1262                 rtl92e_dm_deinit(dev);
1263                 rtllib_stop_scan_syncro(ieee);
1264 
1265                 if (ieee->state == RTLLIB_LINKED) {
1266                         mutex_lock(&ieee->wx_mutex);
1267                         netdev_info(dev, "ieee->state is RTLLIB_LINKED\n");
1268                         rtllib_stop_send_beacons(priv->rtllib);
1269                         del_timer_sync(&ieee->associate_timer);
1270                         cancel_delayed_work(&ieee->associate_retry_wq);
1271                         rtllib_stop_scan(ieee);
1272                         netif_carrier_off(dev);
1273                         mutex_unlock(&ieee->wx_mutex);
1274                 } else {
1275                         netdev_info(dev, "ieee->state is NOT LINKED\n");
1276                         rtllib_softmac_stop_protocol(priv->rtllib, 0, true);
1277                 }
1278 
1279                 rtl92e_dm_backup_state(dev);
1280 
1281                 mutex_unlock(&priv->wx_mutex);
1282                 RT_TRACE(COMP_RESET,
1283                          "%s():<==========down process is finished\n",
1284                          __func__);
1285 
1286                 RT_TRACE(COMP_RESET, "%s():<===========up process start\n",
1287                          __func__);
1288                 reset_status = _rtl92e_up(dev, true);
1289 
1290                 RT_TRACE(COMP_RESET,
1291                          "%s():<===========up process is finished\n", __func__);
1292                 if (reset_status == -1) {
1293                         if (reset_times < 3) {
1294                                 reset_times++;
1295                                 goto RESET_START;
1296                         } else {
1297                                 netdev_warn(dev, "%s(): Reset Failed\n",
1298                                             __func__);
1299                         }
1300                 }
1301 
1302                 ieee->is_silent_reset = 1;
1303 
1304                 spin_lock_irqsave(&priv->rf_ps_lock, flag);
1305                 priv->RFChangeInProgress = false;
1306                 spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1307 
1308                 rtl92e_enable_hw_security_config(dev);
1309 
1310                 if (ieee->state == RTLLIB_LINKED && ieee->iw_mode ==
1311                     IW_MODE_INFRA) {
1312                         ieee->set_chan(ieee->dev,
1313                                        ieee->current_network.channel);
1314 
1315                         schedule_work(&ieee->associate_complete_wq);
1316 
1317                 } else if (ieee->state == RTLLIB_LINKED && ieee->iw_mode ==
1318                            IW_MODE_ADHOC) {
1319                         ieee->set_chan(ieee->dev,
1320                                        ieee->current_network.channel);
1321                         ieee->link_change(ieee->dev);
1322 
1323                         notify_wx_assoc_event(ieee);
1324 
1325                         rtllib_start_send_beacons(ieee);
1326 
1327                         netif_carrier_on(ieee->dev);
1328                 }
1329 
1330                 rtl92e_cam_restore(dev);
1331                 rtl92e_dm_restore_state(dev);
1332 END:
1333                 priv->ResetProgress = RESET_TYPE_NORESET;
1334                 priv->reset_count++;
1335 
1336                 priv->bForcedSilentReset = false;
1337                 priv->bResetInProgress = false;
1338 
1339                 rtl92e_writeb(dev, UFWP, 1);
1340                 RT_TRACE(COMP_RESET, "Reset finished!! ====>[%d]\n",
1341                          priv->reset_count);
1342         }
1343 }
1344 
1345 static void _rtl92e_update_rxcounts(struct r8192_priv *priv, u32 *TotalRxBcnNum,
1346                                     u32 *TotalRxDataNum)
1347 {
1348         u16     SlotIndex;
1349         u8      i;
1350 
1351         *TotalRxBcnNum = 0;
1352         *TotalRxDataNum = 0;
1353 
1354         SlotIndex = (priv->rtllib->LinkDetectInfo.SlotIndex++) %
1355                         (priv->rtllib->LinkDetectInfo.SlotNum);
1356         priv->rtllib->LinkDetectInfo.RxBcnNum[SlotIndex] =
1357                         priv->rtllib->LinkDetectInfo.NumRecvBcnInPeriod;
1358         priv->rtllib->LinkDetectInfo.RxDataNum[SlotIndex] =
1359                         priv->rtllib->LinkDetectInfo.NumRecvDataInPeriod;
1360         for (i = 0; i < priv->rtllib->LinkDetectInfo.SlotNum; i++) {
1361                 *TotalRxBcnNum += priv->rtllib->LinkDetectInfo.RxBcnNum[i];
1362                 *TotalRxDataNum += priv->rtllib->LinkDetectInfo.RxDataNum[i];
1363         }
1364 }
1365 
1366 static void _rtl92e_watchdog_wq_cb(void *data)
1367 {
1368         struct r8192_priv *priv = container_of_dwork_rsl(data,
1369                                   struct r8192_priv, watch_dog_wq);
1370         struct net_device *dev = priv->rtllib->dev;
1371         struct rtllib_device *ieee = priv->rtllib;
1372         enum reset_type ResetType = RESET_TYPE_NORESET;
1373         static u8 check_reset_cnt;
1374         unsigned long flags;
1375         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
1376                                         (&priv->rtllib->PowerSaveControl);
1377         bool bBusyTraffic = false;
1378         bool    bHigherBusyTraffic = false;
1379         bool    bHigherBusyRxTraffic = false;
1380         bool bEnterPS = false;
1381 
1382         if (!priv->up || priv->bHwRadioOff)
1383                 return;
1384 
1385         if (priv->rtllib->state >= RTLLIB_LINKED) {
1386                 if (priv->rtllib->CntAfterLink < 2)
1387                         priv->rtllib->CntAfterLink++;
1388         } else {
1389                 priv->rtllib->CntAfterLink = 0;
1390         }
1391 
1392         rtl92e_dm_watchdog(dev);
1393 
1394         if (rtllib_act_scanning(priv->rtllib, false) == false) {
1395                 if ((ieee->iw_mode == IW_MODE_INFRA) && (ieee->state ==
1396                      RTLLIB_NOLINK) &&
1397                      (ieee->eRFPowerState == eRfOn) && !ieee->is_set_key &&
1398                      (!ieee->proto_stoppping) && !ieee->wx_set_enc) {
1399                         if ((ieee->PowerSaveControl.ReturnPoint ==
1400                              IPS_CALLBACK_NONE) &&
1401                              (!ieee->bNetPromiscuousMode)) {
1402                                 RT_TRACE(COMP_PS,
1403                                          "====================>haha: rtl92e_ips_enter()\n");
1404                                 rtl92e_ips_enter(dev);
1405                         }
1406                 }
1407         }
1408         if ((ieee->state == RTLLIB_LINKED) && (ieee->iw_mode ==
1409              IW_MODE_INFRA) && (!ieee->bNetPromiscuousMode)) {
1410                 if (ieee->LinkDetectInfo.NumRxOkInPeriod > 100 ||
1411                 ieee->LinkDetectInfo.NumTxOkInPeriod > 100)
1412                         bBusyTraffic = true;
1413 
1414 
1415                 if (ieee->LinkDetectInfo.NumRxOkInPeriod > 4000 ||
1416                     ieee->LinkDetectInfo.NumTxOkInPeriod > 4000) {
1417                         bHigherBusyTraffic = true;
1418                         if (ieee->LinkDetectInfo.NumRxOkInPeriod > 5000)
1419                                 bHigherBusyRxTraffic = true;
1420                         else
1421                                 bHigherBusyRxTraffic = false;
1422                 }
1423 
1424                 if (((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod +
1425                     ieee->LinkDetectInfo.NumTxOkInPeriod) > 8) ||
1426                     (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2))
1427                         bEnterPS = false;
1428                 else
1429                         bEnterPS = true;
1430 
1431                 if (ieee->current_network.beacon_interval < 95)
1432                         bEnterPS = false;
1433 
1434                 if (bEnterPS)
1435                         rtl92e_leisure_ps_enter(dev);
1436                 else
1437                         rtl92e_leisure_ps_leave(dev);
1438 
1439         } else {
1440                 RT_TRACE(COMP_LPS, "====>no link LPS leave\n");
1441                 rtl92e_leisure_ps_leave(dev);
1442         }
1443 
1444         ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
1445         ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
1446         ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
1447         ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
1448 
1449         ieee->LinkDetectInfo.bHigherBusyTraffic = bHigherBusyTraffic;
1450         ieee->LinkDetectInfo.bHigherBusyRxTraffic = bHigherBusyRxTraffic;
1451 
1452         if (ieee->state == RTLLIB_LINKED && ieee->iw_mode == IW_MODE_INFRA) {
1453                 u32     TotalRxBcnNum = 0;
1454                 u32     TotalRxDataNum = 0;
1455 
1456                 _rtl92e_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
1457 
1458                 if ((TotalRxBcnNum + TotalRxDataNum) == 0)
1459                         priv->check_roaming_cnt++;
1460                 else
1461                         priv->check_roaming_cnt = 0;
1462 
1463 
1464                 if (priv->check_roaming_cnt > 0) {
1465                         if (ieee->eRFPowerState == eRfOff)
1466                                 netdev_info(dev, "%s(): RF is off\n", __func__);
1467 
1468                         netdev_info(dev,
1469                                     "===>%s(): AP is power off, chan:%d, connect another one\n",
1470                                     __func__, priv->chan);
1471 
1472                         ieee->state = RTLLIB_ASSOCIATING;
1473 
1474                         RemovePeerTS(priv->rtllib,
1475                                      priv->rtllib->current_network.bssid);
1476                         ieee->is_roaming = true;
1477                         ieee->is_set_key = false;
1478                         ieee->link_change(dev);
1479                         if (ieee->LedControlHandler)
1480                                 ieee->LedControlHandler(ieee->dev,
1481                                                         LED_CTL_START_TO_LINK);
1482 
1483                         notify_wx_assoc_event(ieee);
1484 
1485                         if (!(ieee->rtllib_ap_sec_type(ieee) &
1486                              (SEC_ALG_CCMP | SEC_ALG_TKIP)))
1487                                 schedule_delayed_work(
1488                                         &ieee->associate_procedure_wq, 0);
1489 
1490                         priv->check_roaming_cnt = 0;
1491                 }
1492                 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 0;
1493                 ieee->LinkDetectInfo.NumRecvDataInPeriod = 0;
1494 
1495         }
1496 
1497         spin_lock_irqsave(&priv->tx_lock, flags);
1498         if ((check_reset_cnt++ >= 3) && (!ieee->is_roaming) &&
1499             (!priv->RFChangeInProgress) && (!pPSC->bSwRfProcessing)) {
1500                 ResetType = _rtl92e_if_check_reset(dev);
1501                 check_reset_cnt = 3;
1502         }
1503         spin_unlock_irqrestore(&priv->tx_lock, flags);
1504 
1505         if (!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_NORMAL) {
1506                 priv->ResetProgress = RESET_TYPE_NORMAL;
1507                 RT_TRACE(COMP_RESET, "%s(): NOMAL RESET\n", __func__);
1508                 return;
1509         }
1510 
1511         if (((priv->force_reset) || (!priv->bDisableNormalResetCheck &&
1512               ResetType == RESET_TYPE_SILENT)))
1513                 _rtl92e_if_silent_reset(dev);
1514         priv->force_reset = false;
1515         priv->bForcedSilentReset = false;
1516         priv->bResetInProgress = false;
1517         RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
1518 }
1519 
1520 static void _rtl92e_watchdog_timer_cb(struct timer_list *t)
1521 {
1522         struct r8192_priv *priv = from_timer(priv, t, watch_dog_timer);
1523 
1524         schedule_delayed_work(&priv->watch_dog_wq, 0);
1525         mod_timer(&priv->watch_dog_timer, jiffies +
1526                   msecs_to_jiffies(RTLLIB_WATCH_DOG_TIME));
1527 }
1528 
1529 /****************************************************************************
1530  * ---------------------------- NIC TX/RX STUFF---------------------------
1531  ****************************************************************************/
1532 void rtl92e_rx_enable(struct net_device *dev)
1533 {
1534         struct r8192_priv *priv = rtllib_priv(dev);
1535 
1536         priv->ops->rx_enable(dev);
1537 }
1538 
1539 void rtl92e_tx_enable(struct net_device *dev)
1540 {
1541         struct r8192_priv *priv = rtllib_priv(dev);
1542 
1543         priv->ops->tx_enable(dev);
1544 
1545         rtllib_reset_queue(priv->rtllib);
1546 }
1547 
1548 
1549 static void _rtl92e_free_rx_ring(struct net_device *dev)
1550 {
1551         struct r8192_priv *priv = rtllib_priv(dev);
1552         int i, rx_queue_idx;
1553 
1554         for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE;
1555              rx_queue_idx++) {
1556                 for (i = 0; i < priv->rxringcount; i++) {
1557                         struct sk_buff *skb = priv->rx_buf[rx_queue_idx][i];
1558 
1559                         if (!skb)
1560                                 continue;
1561 
1562                         pci_unmap_single(priv->pdev,
1563                                 *((dma_addr_t *)skb->cb),
1564                                 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
1565                         kfree_skb(skb);
1566                 }
1567 
1568                 pci_free_consistent(priv->pdev,
1569                         sizeof(*priv->rx_ring[rx_queue_idx]) *
1570                         priv->rxringcount,
1571                         priv->rx_ring[rx_queue_idx],
1572                         priv->rx_ring_dma[rx_queue_idx]);
1573                 priv->rx_ring[rx_queue_idx] = NULL;
1574         }
1575 }
1576 
1577 static void _rtl92e_free_tx_ring(struct net_device *dev, unsigned int prio)
1578 {
1579         struct r8192_priv *priv = rtllib_priv(dev);
1580         struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1581 
1582         while (skb_queue_len(&ring->queue)) {
1583                 struct tx_desc *entry = &ring->desc[ring->idx];
1584                 struct sk_buff *skb = __skb_dequeue(&ring->queue);
1585 
1586                 pci_unmap_single(priv->pdev, entry->TxBuffAddr,
1587                         skb->len, PCI_DMA_TODEVICE);
1588                 kfree_skb(skb);
1589                 ring->idx = (ring->idx + 1) % ring->entries;
1590         }
1591 
1592         pci_free_consistent(priv->pdev, sizeof(*ring->desc) * ring->entries,
1593         ring->desc, ring->dma);
1594         ring->desc = NULL;
1595 }
1596 
1597 static void _rtl92e_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
1598                                    int rate)
1599 {
1600         struct r8192_priv *priv = rtllib_priv(dev);
1601         int ret;
1602         struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1603                                     MAX_DEV_ADDR_SIZE);
1604         u8 queue_index = tcb_desc->queue_index;
1605 
1606         if ((priv->rtllib->eRFPowerState == eRfOff) || !priv->up ||
1607              priv->bResetInProgress) {
1608                 kfree_skb(skb);
1609                 return;
1610         }
1611 
1612         if (queue_index == TXCMD_QUEUE)
1613                 netdev_warn(dev, "%s(): queue index == TXCMD_QUEUE\n",
1614                             __func__);
1615 
1616         memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1617         skb_push(skb, priv->rtllib->tx_headroom);
1618         ret = _rtl92e_tx(dev, skb);
1619 
1620         if (queue_index != MGNT_QUEUE) {
1621                 priv->rtllib->stats.tx_bytes += (skb->len -
1622                                                  priv->rtllib->tx_headroom);
1623                 priv->rtllib->stats.tx_packets++;
1624         }
1625 
1626         if (ret != 0)
1627                 kfree_skb(skb);
1628 }
1629 
1630 static int _rtl92e_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1631 {
1632         struct r8192_priv *priv = rtllib_priv(dev);
1633         int ret;
1634         struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1635                                     MAX_DEV_ADDR_SIZE);
1636         u8 queue_index = tcb_desc->queue_index;
1637 
1638         if (queue_index != TXCMD_QUEUE) {
1639                 if ((priv->rtllib->eRFPowerState == eRfOff) ||
1640                      !priv->up || priv->bResetInProgress) {
1641                         kfree_skb(skb);
1642                         return 0;
1643                 }
1644         }
1645 
1646         memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1647         if (queue_index == TXCMD_QUEUE) {
1648                 _rtl92e_tx_cmd(dev, skb);
1649                 return 0;
1650         }
1651 
1652         tcb_desc->RATRIndex = 7;
1653         tcb_desc->bTxDisableRateFallBack = 1;
1654         tcb_desc->bTxUseDriverAssingedRate = 1;
1655         tcb_desc->bTxEnableFwCalcDur = 1;
1656         skb_push(skb, priv->rtllib->tx_headroom);
1657         ret = _rtl92e_tx(dev, skb);
1658         if (ret != 0)
1659                 kfree_skb(skb);
1660         return ret;
1661 }
1662 
1663 static void _rtl92e_tx_isr(struct net_device *dev, int prio)
1664 {
1665         struct r8192_priv *priv = rtllib_priv(dev);
1666 
1667         struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1668 
1669         while (skb_queue_len(&ring->queue)) {
1670                 struct tx_desc *entry = &ring->desc[ring->idx];
1671                 struct sk_buff *skb;
1672 
1673                 if (prio != BEACON_QUEUE) {
1674                         if (entry->OWN)
1675                                 return;
1676                         ring->idx = (ring->idx + 1) % ring->entries;
1677                 }
1678 
1679                 skb = __skb_dequeue(&ring->queue);
1680                 pci_unmap_single(priv->pdev, entry->TxBuffAddr,
1681                 skb->len, PCI_DMA_TODEVICE);
1682 
1683                 kfree_skb(skb);
1684         }
1685         if (prio != BEACON_QUEUE)
1686                 tasklet_schedule(&priv->irq_tx_tasklet);
1687 }
1688 
1689 static void _rtl92e_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1690 {
1691         struct r8192_priv *priv = rtllib_priv(dev);
1692         struct rtl8192_tx_ring *ring;
1693         struct tx_desc_cmd *entry;
1694         unsigned int idx;
1695         struct cb_desc *tcb_desc;
1696         unsigned long flags;
1697 
1698         spin_lock_irqsave(&priv->irq_th_lock, flags);
1699         ring = &priv->tx_ring[TXCMD_QUEUE];
1700 
1701         idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1702         entry = (struct tx_desc_cmd *)&ring->desc[idx];
1703 
1704         tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1705 
1706         priv->ops->tx_fill_cmd_descriptor(dev, entry, tcb_desc, skb);
1707 
1708         __skb_queue_tail(&ring->queue, skb);
1709         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1710 }
1711 
1712 static short _rtl92e_tx(struct net_device *dev, struct sk_buff *skb)
1713 {
1714         struct r8192_priv *priv = rtllib_priv(dev);
1715         struct rtl8192_tx_ring  *ring;
1716         unsigned long flags;
1717         struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1718                                     MAX_DEV_ADDR_SIZE);
1719         struct tx_desc *pdesc = NULL;
1720         struct rtllib_hdr_1addr *header = NULL;
1721         u16 fc = 0, type = 0;
1722         u8 *pda_addr = NULL;
1723         int   idx;
1724         u32 fwinfo_size = 0;
1725 
1726         if (priv->bdisable_nic) {
1727                 netdev_warn(dev, "%s: Nic is disabled! Can't tx packet.\n",
1728                             __func__);
1729                 return skb->len;
1730         }
1731 
1732         priv->rtllib->bAwakePktSent = true;
1733 
1734         fwinfo_size = sizeof(struct tx_fwinfo_8190pci);
1735 
1736         header = (struct rtllib_hdr_1addr *)(((u8 *)skb->data) + fwinfo_size);
1737         fc = le16_to_cpu(header->frame_ctl);
1738         type = WLAN_FC_GET_TYPE(fc);
1739         pda_addr = header->addr1;
1740 
1741         if (is_broadcast_ether_addr(pda_addr))
1742                 priv->stats.txbytesbroadcast += skb->len - fwinfo_size;
1743         else if (is_multicast_ether_addr(pda_addr))
1744                 priv->stats.txbytesmulticast += skb->len - fwinfo_size;
1745         else
1746                 priv->stats.txbytesunicast += skb->len - fwinfo_size;
1747 
1748         spin_lock_irqsave(&priv->irq_th_lock, flags);
1749         ring = &priv->tx_ring[tcb_desc->queue_index];
1750         if (tcb_desc->queue_index != BEACON_QUEUE)
1751                 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1752         else
1753                 idx = 0;
1754 
1755         pdesc = &ring->desc[idx];
1756         if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
1757                 netdev_warn(dev,
1758                             "No more TX desc@%d, ring->idx = %d, idx = %d, skblen = 0x%x queuelen=%d",
1759                             tcb_desc->queue_index, ring->idx, idx, skb->len,
1760                             skb_queue_len(&ring->queue));
1761                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1762                 return skb->len;
1763         }
1764 
1765         if (type == RTLLIB_FTYPE_DATA) {
1766                 if (priv->rtllib->LedControlHandler)
1767                         priv->rtllib->LedControlHandler(dev, LED_CTL_TX);
1768         }
1769         priv->ops->tx_fill_descriptor(dev, pdesc, tcb_desc, skb);
1770         __skb_queue_tail(&ring->queue, skb);
1771         pdesc->OWN = 1;
1772         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1773         netif_trans_update(dev);
1774 
1775         rtl92e_writew(dev, TPPoll, 0x01 << tcb_desc->queue_index);
1776         return 0;
1777 }
1778 
1779 static short _rtl92e_alloc_rx_ring(struct net_device *dev)
1780 {
1781         struct r8192_priv *priv = rtllib_priv(dev);
1782         struct rx_desc *entry = NULL;
1783         int i, rx_queue_idx;
1784 
1785         for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE; rx_queue_idx++) {
1786                 priv->rx_ring[rx_queue_idx] = pci_zalloc_consistent(priv->pdev,
1787                                               sizeof(*priv->rx_ring[rx_queue_idx]) * priv->rxringcount,
1788                                               &priv->rx_ring_dma[rx_queue_idx]);
1789                 if (!priv->rx_ring[rx_queue_idx] ||
1790                     (unsigned long)priv->rx_ring[rx_queue_idx] & 0xFF) {
1791                         netdev_warn(dev, "Cannot allocate RX ring\n");
1792                         return -ENOMEM;
1793                 }
1794 
1795                 priv->rx_idx[rx_queue_idx] = 0;
1796 
1797                 for (i = 0; i < priv->rxringcount; i++) {
1798                         struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
1799                         dma_addr_t *mapping;
1800 
1801                         entry = &priv->rx_ring[rx_queue_idx][i];
1802                         if (!skb)
1803                                 return 0;
1804                         skb->dev = dev;
1805                         priv->rx_buf[rx_queue_idx][i] = skb;
1806                         mapping = (dma_addr_t *)skb->cb;
1807                         *mapping = pci_map_single(priv->pdev,
1808                                                   skb_tail_pointer_rsl(skb),
1809                                                   priv->rxbuffersize,
1810                                                   PCI_DMA_FROMDEVICE);
1811                         if (pci_dma_mapping_error(priv->pdev, *mapping)) {
1812                                 dev_kfree_skb_any(skb);
1813                                 return -1;
1814                         }
1815                         entry->BufferAddress = *mapping;
1816 
1817                         entry->Length = priv->rxbuffersize;
1818                         entry->OWN = 1;
1819                 }
1820 
1821                 if (entry)
1822                         entry->EOR = 1;
1823         }
1824         return 0;
1825 }
1826 
1827 static int _rtl92e_alloc_tx_ring(struct net_device *dev, unsigned int prio,
1828                                  unsigned int entries)
1829 {
1830         struct r8192_priv *priv = rtllib_priv(dev);
1831         struct tx_desc *ring;
1832         dma_addr_t dma;
1833         int i;
1834 
1835         ring = pci_zalloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1836         if (!ring || (unsigned long)ring & 0xFF) {
1837                 netdev_warn(dev, "Cannot allocate TX ring (prio = %d)\n", prio);
1838                 return -ENOMEM;
1839         }
1840 
1841         priv->tx_ring[prio].desc = ring;
1842         priv->tx_ring[prio].dma = dma;
1843         priv->tx_ring[prio].idx = 0;
1844         priv->tx_ring[prio].entries = entries;
1845         skb_queue_head_init(&priv->tx_ring[prio].queue);
1846 
1847         for (i = 0; i < entries; i++)
1848                 ring[i].NextDescAddress =
1849                         (u32)dma + ((i + 1) % entries) *
1850                         sizeof(*ring);
1851 
1852         return 0;
1853 }
1854 
1855 static short _rtl92e_pci_initdescring(struct net_device *dev)
1856 {
1857         u32 ret;
1858         int i;
1859         struct r8192_priv *priv = rtllib_priv(dev);
1860 
1861         ret = _rtl92e_alloc_rx_ring(dev);
1862         if (ret)
1863                 return ret;
1864 
1865         for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1866                 ret = _rtl92e_alloc_tx_ring(dev, i, priv->txringcount);
1867                 if (ret)
1868                         goto err_free_rings;
1869         }
1870 
1871         return 0;
1872 
1873 err_free_rings:
1874         _rtl92e_free_rx_ring(dev);
1875         for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
1876                 if (priv->tx_ring[i].desc)
1877                         _rtl92e_free_tx_ring(dev, i);
1878         return 1;
1879 }
1880 
1881 void rtl92e_reset_desc_ring(struct net_device *dev)
1882 {
1883         struct r8192_priv *priv = rtllib_priv(dev);
1884         int i, rx_queue_idx;
1885         unsigned long flags = 0;
1886 
1887         for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE; rx_queue_idx++) {
1888                 if (priv->rx_ring[rx_queue_idx]) {
1889                         struct rx_desc *entry = NULL;
1890 
1891                         for (i = 0; i < priv->rxringcount; i++) {
1892                                 entry = &priv->rx_ring[rx_queue_idx][i];
1893                                 entry->OWN = 1;
1894                         }
1895                         priv->rx_idx[rx_queue_idx] = 0;
1896                 }
1897         }
1898 
1899         spin_lock_irqsave(&priv->irq_th_lock, flags);
1900         for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1901                 if (priv->tx_ring[i].desc) {
1902                         struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
1903 
1904                         while (skb_queue_len(&ring->queue)) {
1905                                 struct tx_desc *entry = &ring->desc[ring->idx];
1906                                 struct sk_buff *skb =
1907                                                  __skb_dequeue(&ring->queue);
1908 
1909                                 pci_unmap_single(priv->pdev,
1910                                                  entry->TxBuffAddr,
1911                                                  skb->len, PCI_DMA_TODEVICE);
1912                                 kfree_skb(skb);
1913                                 ring->idx = (ring->idx + 1) % ring->entries;
1914                         }
1915                         ring->idx = 0;
1916                 }
1917         }
1918         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1919 }
1920 
1921 void rtl92e_update_rx_pkt_timestamp(struct net_device *dev,
1922                                     struct rtllib_rx_stats *stats)
1923 {
1924         struct r8192_priv *priv = rtllib_priv(dev);
1925 
1926         if (stats->bIsAMPDU && !stats->bFirstMPDU)
1927                 stats->mac_time = priv->LastRxDescTSF;
1928         else
1929                 priv->LastRxDescTSF = stats->mac_time;
1930 }
1931 
1932 long rtl92e_translate_to_dbm(struct r8192_priv *priv, u8 signal_strength_index)
1933 {
1934         long    signal_power;
1935 
1936         signal_power = (long)((signal_strength_index + 1) >> 1);
1937         signal_power -= 95;
1938 
1939         return signal_power;
1940 }
1941 
1942 
1943 void rtl92e_update_rx_statistics(struct r8192_priv *priv,
1944                                  struct rtllib_rx_stats *pprevious_stats)
1945 {
1946         int weighting = 0;
1947 
1948 
1949         if (priv->stats.recv_signal_power == 0)
1950                 priv->stats.recv_signal_power =
1951                                          pprevious_stats->RecvSignalPower;
1952 
1953         if (pprevious_stats->RecvSignalPower > priv->stats.recv_signal_power)
1954                 weighting = 5;
1955         else if (pprevious_stats->RecvSignalPower <
1956                  priv->stats.recv_signal_power)
1957                 weighting = (-5);
1958         priv->stats.recv_signal_power = (priv->stats.recv_signal_power * 5 +
1959                                         pprevious_stats->RecvSignalPower +
1960                                         weighting) / 6;
1961 }
1962 
1963 u8 rtl92e_rx_db_to_percent(s8 antpower)
1964 {
1965         if ((antpower <= -100) || (antpower >= 20))
1966                 return  0;
1967         else if (antpower >= 0)
1968                 return  100;
1969         else
1970                 return  100 + antpower;
1971 
1972 }       /* QueryRxPwrPercentage */
1973 
1974 u8 rtl92e_evm_db_to_percent(s8 value)
1975 {
1976         s8 ret_val;
1977 
1978         ret_val = value;
1979 
1980         if (ret_val >= 0)
1981                 ret_val = 0;
1982         if (ret_val <= -33)
1983                 ret_val = -33;
1984         ret_val = 0 - ret_val;
1985         ret_val *= 3;
1986         if (ret_val == 99)
1987                 ret_val = 100;
1988         return ret_val;
1989 }
1990 
1991 void rtl92e_copy_mpdu_stats(struct rtllib_rx_stats *psrc_stats,
1992                             struct rtllib_rx_stats *ptarget_stats)
1993 {
1994         ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
1995         ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
1996 }
1997 
1998 
1999 
2000 static void _rtl92e_rx_normal(struct net_device *dev)
2001 {
2002         struct r8192_priv *priv = rtllib_priv(dev);
2003         struct rtllib_hdr_1addr *rtllib_hdr = NULL;
2004         bool unicast_packet = false;
2005         bool bLedBlinking = true;
2006         u16 fc = 0, type = 0;
2007         u32 skb_len = 0;
2008         int rx_queue_idx = RX_MPDU_QUEUE;
2009 
2010         struct rtllib_rx_stats stats = {
2011                 .signal = 0,
2012                 .noise = (u8)-98,
2013                 .rate = 0,
2014                 .freq = RTLLIB_24GHZ_BAND,
2015         };
2016         unsigned int count = priv->rxringcount;
2017 
2018         stats.nic_type = NIC_8192E;
2019 
2020         while (count--) {
2021                 struct rx_desc *pdesc = &priv->rx_ring[rx_queue_idx]
2022                                         [priv->rx_idx[rx_queue_idx]];
2023                 struct sk_buff *skb = priv->rx_buf[rx_queue_idx]
2024                                       [priv->rx_idx[rx_queue_idx]];
2025                 struct sk_buff *new_skb;
2026 
2027                 if (pdesc->OWN)
2028                         return;
2029                 if (!priv->ops->rx_query_status_descriptor(dev, &stats,
2030                 pdesc, skb))
2031                         goto done;
2032                 new_skb = dev_alloc_skb(priv->rxbuffersize);
2033                 /* if allocation of new skb failed - drop current packet
2034                  * and reuse skb
2035                  */
2036                 if (unlikely(!new_skb))
2037                         goto done;
2038 
2039                 pci_unmap_single(priv->pdev,
2040                                 *((dma_addr_t *)skb->cb),
2041                                 priv->rxbuffersize,
2042                                 PCI_DMA_FROMDEVICE);
2043 
2044                 skb_put(skb, pdesc->Length);
2045                 skb_reserve(skb, stats.RxDrvInfoSize +
2046                         stats.RxBufShift);
2047                 skb_trim(skb, skb->len - 4/*sCrcLng*/);
2048                 rtllib_hdr = (struct rtllib_hdr_1addr *)skb->data;
2049                 if (!is_multicast_ether_addr(rtllib_hdr->addr1)) {
2050                         /* unicast packet */
2051                         unicast_packet = true;
2052                 }
2053                 fc = le16_to_cpu(rtllib_hdr->frame_ctl);
2054                 type = WLAN_FC_GET_TYPE(fc);
2055                 if (type == RTLLIB_FTYPE_MGMT)
2056                         bLedBlinking = false;
2057 
2058                 if (bLedBlinking)
2059                         if (priv->rtllib->LedControlHandler)
2060                                 priv->rtllib->LedControlHandler(dev,
2061                                                         LED_CTL_RX);
2062 
2063                 if (stats.bCRC) {
2064                         if (type != RTLLIB_FTYPE_MGMT)
2065                                 priv->stats.rxdatacrcerr++;
2066                         else
2067                                 priv->stats.rxmgmtcrcerr++;
2068                 }
2069 
2070                 skb_len = skb->len;
2071 
2072                 if (!rtllib_rx(priv->rtllib, skb, &stats)) {
2073                         dev_kfree_skb_any(skb);
2074                 } else {
2075                         priv->stats.rxok++;
2076                         if (unicast_packet)
2077                                 priv->stats.rxbytesunicast += skb_len;
2078                 }
2079 
2080                 skb = new_skb;
2081                 skb->dev = dev;
2082 
2083                 priv->rx_buf[rx_queue_idx][priv->rx_idx[rx_queue_idx]] =
2084                                                                  skb;
2085                 *((dma_addr_t *)skb->cb) = pci_map_single(priv->pdev,
2086                                             skb_tail_pointer_rsl(skb),
2087                                             priv->rxbuffersize,
2088                                             PCI_DMA_FROMDEVICE);
2089                 if (pci_dma_mapping_error(priv->pdev,
2090                                           *((dma_addr_t *)skb->cb))) {
2091                         dev_kfree_skb_any(skb);
2092                         return;
2093                 }
2094 done:
2095                 pdesc->BufferAddress = *((dma_addr_t *)skb->cb);
2096                 pdesc->OWN = 1;
2097                 pdesc->Length = priv->rxbuffersize;
2098                 if (priv->rx_idx[rx_queue_idx] == priv->rxringcount - 1)
2099                         pdesc->EOR = 1;
2100                 priv->rx_idx[rx_queue_idx] = (priv->rx_idx[rx_queue_idx] + 1) %
2101                                               priv->rxringcount;
2102         }
2103 
2104 }
2105 
2106 static void _rtl92e_tx_resume(struct net_device *dev)
2107 {
2108         struct r8192_priv *priv = rtllib_priv(dev);
2109         struct rtllib_device *ieee = priv->rtllib;
2110         struct sk_buff *skb;
2111         int queue_index;
2112 
2113         for (queue_index = BK_QUEUE;
2114              queue_index < MAX_QUEUE_SIZE; queue_index++) {
2115                 while ((!skb_queue_empty(&ieee->skb_waitQ[queue_index])) &&
2116                 (priv->rtllib->check_nic_enough_desc(dev, queue_index) > 0)) {
2117                         skb = skb_dequeue(&ieee->skb_waitQ[queue_index]);
2118                         ieee->softmac_data_hard_start_xmit(skb, dev, 0);
2119                 }
2120         }
2121 }
2122 
2123 static void _rtl92e_irq_tx_tasklet(struct r8192_priv *priv)
2124 {
2125         _rtl92e_tx_resume(priv->rtllib->dev);
2126 }
2127 
2128 static void _rtl92e_irq_rx_tasklet(struct r8192_priv *priv)
2129 {
2130         _rtl92e_rx_normal(priv->rtllib->dev);
2131 
2132         rtl92e_writel(priv->rtllib->dev, INTA_MASK,
2133                       rtl92e_readl(priv->rtllib->dev, INTA_MASK) | IMR_RDU);
2134 }
2135 
2136 /****************************************************************************
2137  * ---------------------------- NIC START/CLOSE STUFF---------------------------
2138  ****************************************************************************/
2139 static void _rtl92e_cancel_deferred_work(struct r8192_priv *priv)
2140 {
2141         cancel_delayed_work_sync(&priv->watch_dog_wq);
2142         cancel_delayed_work_sync(&priv->update_beacon_wq);
2143         cancel_delayed_work(&priv->rtllib->hw_sleep_wq);
2144         cancel_work_sync(&priv->reset_wq);
2145         cancel_work_sync(&priv->qos_activate);
2146 }
2147 
2148 static int _rtl92e_up(struct net_device *dev, bool is_silent_reset)
2149 {
2150         if (_rtl92e_sta_up(dev, is_silent_reset) == -1)
2151                 return -1;
2152         return 0;
2153 }
2154 
2155 static int _rtl92e_open(struct net_device *dev)
2156 {
2157         struct r8192_priv *priv = rtllib_priv(dev);
2158         int ret;
2159 
2160         mutex_lock(&priv->wx_mutex);
2161         ret = _rtl92e_try_up(dev);
2162         mutex_unlock(&priv->wx_mutex);
2163         return ret;
2164 
2165 }
2166 
2167 static int _rtl92e_try_up(struct net_device *dev)
2168 {
2169         struct r8192_priv *priv = rtllib_priv(dev);
2170 
2171         if (priv->up == 1)
2172                 return -1;
2173         return _rtl92e_up(dev, false);
2174 }
2175 
2176 
2177 static int _rtl92e_close(struct net_device *dev)
2178 {
2179         struct r8192_priv *priv = rtllib_priv(dev);
2180         int ret;
2181 
2182         if ((rtllib_act_scanning(priv->rtllib, false)) &&
2183                 !(priv->rtllib->softmac_features & IEEE_SOFTMAC_SCAN)) {
2184                 rtllib_stop_scan(priv->rtllib);
2185         }
2186 
2187         mutex_lock(&priv->wx_mutex);
2188 
2189         ret = _rtl92e_down(dev, true);
2190 
2191         mutex_unlock(&priv->wx_mutex);
2192 
2193         return ret;
2194 
2195 }
2196 
2197 static int _rtl92e_down(struct net_device *dev, bool shutdownrf)
2198 {
2199         if (_rtl92e_sta_down(dev, shutdownrf) == -1)
2200                 return -1;
2201 
2202         return 0;
2203 }
2204 
2205 void rtl92e_commit(struct net_device *dev)
2206 {
2207         struct r8192_priv *priv = rtllib_priv(dev);
2208 
2209         if (priv->up == 0)
2210                 return;
2211         rtllib_softmac_stop_protocol(priv->rtllib, 0, true);
2212         rtl92e_irq_disable(dev);
2213         priv->ops->stop_adapter(dev, true);
2214         _rtl92e_up(dev, false);
2215 }
2216 
2217 static void _rtl92e_restart(void *data)
2218 {
2219         struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
2220                                   reset_wq);
2221         struct net_device *dev = priv->rtllib->dev;
2222 
2223         mutex_lock(&priv->wx_mutex);
2224 
2225         rtl92e_commit(dev);
2226 
2227         mutex_unlock(&priv->wx_mutex);
2228 }
2229 
2230 static void _rtl92e_set_multicast(struct net_device *dev)
2231 {
2232         struct r8192_priv *priv = rtllib_priv(dev);
2233         short promisc;
2234 
2235         promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
2236         priv->promisc = promisc;
2237 
2238 }
2239 
2240 
2241 static int _rtl92e_set_mac_adr(struct net_device *dev, void *mac)
2242 {
2243         struct r8192_priv *priv = rtllib_priv(dev);
2244         struct sockaddr *addr = mac;
2245 
2246         mutex_lock(&priv->wx_mutex);
2247 
2248         ether_addr_copy(dev->dev_addr, addr->sa_data);
2249 
2250         schedule_work(&priv->reset_wq);
2251         mutex_unlock(&priv->wx_mutex);
2252 
2253         return 0;
2254 }
2255 
2256 static irqreturn_t _rtl92e_irq(int irq, void *netdev)
2257 {
2258         struct net_device *dev = netdev;
2259         struct r8192_priv *priv = rtllib_priv(dev);
2260         unsigned long flags;
2261         u32 inta;
2262         u32 intb;
2263 
2264         intb = 0;
2265 
2266         if (priv->irq_enabled == 0)
2267                 goto done;
2268 
2269         spin_lock_irqsave(&priv->irq_th_lock, flags);
2270 
2271         priv->ops->interrupt_recognized(dev, &inta, &intb);
2272         priv->stats.shints++;
2273 
2274         if (!inta) {
2275                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2276                 goto done;
2277         }
2278 
2279         if (inta == 0xffff) {
2280                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2281                 goto done;
2282         }
2283 
2284         priv->stats.ints++;
2285 
2286         if (!netif_running(dev)) {
2287                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2288                 goto done;
2289         }
2290 
2291         if (inta & IMR_TBDOK) {
2292                 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
2293                 priv->stats.txbeaconokint++;
2294         }
2295 
2296         if (inta & IMR_TBDER) {
2297                 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
2298                 priv->stats.txbeaconerr++;
2299         }
2300 
2301         if (inta & IMR_BDOK)
2302                 RT_TRACE(COMP_INTR, "beacon interrupt!\n");
2303 
2304         if (inta  & IMR_MGNTDOK) {
2305                 RT_TRACE(COMP_INTR, "Manage ok interrupt!\n");
2306                 priv->stats.txmanageokint++;
2307                 _rtl92e_tx_isr(dev, MGNT_QUEUE);
2308                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2309                 if (priv->rtllib->ack_tx_to_ieee) {
2310                         if (_rtl92e_is_tx_queue_empty(dev)) {
2311                                 priv->rtllib->ack_tx_to_ieee = 0;
2312                                 rtllib_ps_tx_ack(priv->rtllib, 1);
2313                         }
2314                 }
2315                 spin_lock_irqsave(&priv->irq_th_lock, flags);
2316         }
2317 
2318         if (inta & IMR_COMDOK) {
2319                 priv->stats.txcmdpktokint++;
2320                 _rtl92e_tx_isr(dev, TXCMD_QUEUE);
2321         }
2322 
2323         if (inta & IMR_HIGHDOK)
2324                 _rtl92e_tx_isr(dev, HIGH_QUEUE);
2325 
2326         if (inta & IMR_ROK) {
2327                 priv->stats.rxint++;
2328                 priv->InterruptLog.nIMR_ROK++;
2329                 tasklet_schedule(&priv->irq_rx_tasklet);
2330         }
2331 
2332         if (inta & IMR_BcnInt) {
2333                 RT_TRACE(COMP_INTR, "prepare beacon for interrupt!\n");
2334                 tasklet_schedule(&priv->irq_prepare_beacon_tasklet);
2335         }
2336 
2337         if (inta & IMR_RDU) {
2338                 RT_TRACE(COMP_INTR, "rx descriptor unavailable!\n");
2339                 priv->stats.rxrdu++;
2340                 rtl92e_writel(dev, INTA_MASK,
2341                               rtl92e_readl(dev, INTA_MASK) & ~IMR_RDU);
2342                 tasklet_schedule(&priv->irq_rx_tasklet);
2343         }
2344 
2345         if (inta & IMR_RXFOVW) {
2346                 RT_TRACE(COMP_INTR, "rx overflow !\n");
2347                 priv->stats.rxoverflow++;
2348                 tasklet_schedule(&priv->irq_rx_tasklet);
2349         }
2350 
2351         if (inta & IMR_TXFOVW)
2352                 priv->stats.txoverflow++;
2353 
2354         if (inta & IMR_BKDOK) {
2355                 RT_TRACE(COMP_INTR, "BK Tx OK interrupt!\n");
2356                 priv->stats.txbkokint++;
2357                 priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2358                 _rtl92e_tx_isr(dev, BK_QUEUE);
2359         }
2360 
2361         if (inta & IMR_BEDOK) {
2362                 RT_TRACE(COMP_INTR, "BE TX OK interrupt!\n");
2363                 priv->stats.txbeokint++;
2364                 priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2365                 _rtl92e_tx_isr(dev, BE_QUEUE);
2366         }
2367 
2368         if (inta & IMR_VIDOK) {
2369                 RT_TRACE(COMP_INTR, "VI TX OK interrupt!\n");
2370                 priv->stats.txviokint++;
2371                 priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2372                 _rtl92e_tx_isr(dev, VI_QUEUE);
2373         }
2374 
2375         if (inta & IMR_VODOK) {
2376                 priv->stats.txvookint++;
2377                 RT_TRACE(COMP_INTR, "Vo TX OK interrupt!\n");
2378                 priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2379                 _rtl92e_tx_isr(dev, VO_QUEUE);
2380         }
2381 
2382         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2383 
2384 done:
2385 
2386         return IRQ_HANDLED;
2387 }
2388 
2389 
2390 
2391 /****************************************************************************
2392  * ---------------------------- PCI_STUFF---------------------------
2393  ****************************************************************************/
2394 static const struct net_device_ops rtl8192_netdev_ops = {
2395         .ndo_open = _rtl92e_open,
2396         .ndo_stop = _rtl92e_close,
2397         .ndo_tx_timeout = _rtl92e_tx_timeout,
2398         .ndo_set_rx_mode = _rtl92e_set_multicast,
2399         .ndo_set_mac_address = _rtl92e_set_mac_adr,
2400         .ndo_validate_addr = eth_validate_addr,
2401         .ndo_start_xmit = rtllib_xmit,
2402 };
2403 
2404 static int _rtl92e_pci_probe(struct pci_dev *pdev,
2405                              const struct pci_device_id *id)
2406 {
2407         unsigned long ioaddr = 0;
2408         struct net_device *dev = NULL;
2409         struct r8192_priv *priv = NULL;
2410         struct rtl819x_ops *ops = (struct rtl819x_ops *)(id->driver_data);
2411         unsigned long pmem_start, pmem_len, pmem_flags;
2412         int err = -ENOMEM;
2413         u8 revision_id;
2414 
2415         RT_TRACE(COMP_INIT, "Configuring chip resources");
2416 
2417         if (pci_enable_device(pdev)) {
2418                 dev_err(&pdev->dev, "Failed to enable PCI device");
2419                 return -EIO;
2420         }
2421 
2422         pci_set_master(pdev);
2423 
2424         if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
2425                 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2426                         dev_info(&pdev->dev,
2427                                  "Unable to obtain 32bit DMA for consistent allocations\n");
2428                         goto err_pci_disable;
2429                 }
2430         }
2431         dev = alloc_rtllib(sizeof(struct r8192_priv));
2432         if (!dev)
2433                 goto err_pci_disable;
2434 
2435         err = -ENODEV;
2436 
2437         pci_set_drvdata(pdev, dev);
2438         SET_NETDEV_DEV(dev, &pdev->dev);
2439         priv = rtllib_priv(dev);
2440         priv->rtllib = (struct rtllib_device *)netdev_priv_rsl(dev);
2441         priv->pdev = pdev;
2442         priv->rtllib->pdev = pdev;
2443         if ((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK) &&
2444             (pdev->subsystem_device == 0x3304))
2445                 priv->rtllib->bSupportRemoteWakeUp = 1;
2446         else
2447                 priv->rtllib->bSupportRemoteWakeUp = 0;
2448 
2449         pmem_start = pci_resource_start(pdev, 1);
2450         pmem_len = pci_resource_len(pdev, 1);
2451         pmem_flags = pci_resource_flags(pdev, 1);
2452 
2453         if (!(pmem_flags & IORESOURCE_MEM)) {
2454                 netdev_err(dev, "region #1 not a MMIO resource, aborting");
2455                 goto err_rel_rtllib;
2456         }
2457 
2458         dev_info(&pdev->dev, "Memory mapped space start: 0x%08lx\n",
2459                  pmem_start);
2460         if (!request_mem_region(pmem_start, pmem_len, DRV_NAME)) {
2461                 netdev_err(dev, "request_mem_region failed!");
2462                 goto err_rel_rtllib;
2463         }
2464 
2465 
2466         ioaddr = (unsigned long)ioremap_nocache(pmem_start, pmem_len);
2467         if (ioaddr == (unsigned long)NULL) {
2468                 netdev_err(dev, "ioremap failed!");
2469                 goto err_rel_mem;
2470         }
2471 
2472         dev->mem_start = ioaddr;
2473         dev->mem_end = ioaddr + pci_resource_len(pdev, 0);
2474 
2475         pci_read_config_byte(pdev, 0x08, &revision_id);
2476         /* If the revisionid is 0x10, the device uses rtl8192se. */
2477         if (pdev->device == 0x8192 && revision_id == 0x10)
2478                 goto err_unmap;
2479 
2480         priv->ops = ops;
2481 
2482         if (rtl92e_check_adapter(pdev, dev) == false)
2483                 goto err_unmap;
2484 
2485         dev->irq = pdev->irq;
2486         priv->irq = 0;
2487 
2488         dev->netdev_ops = &rtl8192_netdev_ops;
2489 
2490         dev->wireless_handlers = &r8192_wx_handlers_def;
2491         dev->ethtool_ops = &rtl819x_ethtool_ops;
2492 
2493         dev->type = ARPHRD_ETHER;
2494         dev->watchdog_timeo = HZ * 3;
2495 
2496         if (dev_alloc_name(dev, ifname) < 0) {
2497                 RT_TRACE(COMP_INIT,
2498                          "Oops: devname already taken! Trying wlan%%d...\n");
2499                 dev_alloc_name(dev, ifname);
2500         }
2501 
2502         RT_TRACE(COMP_INIT, "Driver probe completed1\n");
2503         if (_rtl92e_init(dev) != 0) {
2504                 netdev_warn(dev, "Initialization failed");
2505                 goto err_free_irq;
2506         }
2507 
2508         netif_carrier_off(dev);
2509         netif_stop_queue(dev);
2510 
2511         if (register_netdev(dev))
2512                 goto err_free_irq;
2513         RT_TRACE(COMP_INIT, "dev name: %s\n", dev->name);
2514 
2515         if (priv->polling_timer_on == 0)
2516                 rtl92e_check_rfctrl_gpio_timer(&priv->gpio_polling_timer);
2517 
2518         RT_TRACE(COMP_INIT, "Driver probe completed\n");
2519         return 0;
2520 
2521 err_free_irq:
2522         free_irq(dev->irq, dev);
2523         priv->irq = 0;
2524 err_unmap:
2525         iounmap((void __iomem *)ioaddr);
2526 err_rel_mem:
2527         release_mem_region(pmem_start, pmem_len);
2528 err_rel_rtllib:
2529         free_rtllib(dev);
2530 err_pci_disable:
2531         pci_disable_device(pdev);
2532         return err;
2533 }
2534 
2535 static void _rtl92e_pci_disconnect(struct pci_dev *pdev)
2536 {
2537         struct net_device *dev = pci_get_drvdata(pdev);
2538         struct r8192_priv *priv;
2539         u32 i;
2540 
2541         if (dev) {
2542                 unregister_netdev(dev);
2543 
2544                 priv = rtllib_priv(dev);
2545 
2546                 del_timer_sync(&priv->gpio_polling_timer);
2547                 cancel_delayed_work_sync(&priv->gpio_change_rf_wq);
2548                 priv->polling_timer_on = 0;
2549                 _rtl92e_down(dev, true);
2550                 rtl92e_dm_deinit(dev);
2551                 vfree(priv->pFirmware);
2552                 priv->pFirmware = NULL;
2553                 _rtl92e_free_rx_ring(dev);
2554                 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
2555                         _rtl92e_free_tx_ring(dev, i);
2556 
2557                 if (priv->irq) {
2558                         dev_info(&pdev->dev, "Freeing irq %d\n", dev->irq);
2559                         free_irq(dev->irq, dev);
2560                         priv->irq = 0;
2561                 }
2562                 free_rtllib(dev);
2563 
2564                 if (dev->mem_start != 0) {
2565                         iounmap((void __iomem *)dev->mem_start);
2566                         release_mem_region(pci_resource_start(pdev, 1),
2567                                         pci_resource_len(pdev, 1));
2568                 }
2569         } else {
2570                 priv = rtllib_priv(dev);
2571         }
2572 
2573         pci_disable_device(pdev);
2574         RT_TRACE(COMP_DOWN, "wlan driver removed\n");
2575 }
2576 
2577 bool rtl92e_enable_nic(struct net_device *dev)
2578 {
2579         bool init_status = true;
2580         struct r8192_priv *priv = rtllib_priv(dev);
2581         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
2582                                         (&priv->rtllib->PowerSaveControl);
2583 
2584         if (!priv->up) {
2585                 netdev_warn(dev, "%s(): Driver is already down!\n", __func__);
2586                 priv->bdisable_nic = false;
2587                 return false;
2588         }
2589 
2590         RT_TRACE(COMP_PS, "===========>%s()\n", __func__);
2591         priv->bfirst_init = true;
2592         init_status = priv->ops->initialize_adapter(dev);
2593         if (!init_status) {
2594                 netdev_warn(dev, "%s(): Initialization failed!\n", __func__);
2595                 priv->bdisable_nic = false;
2596                 return false;
2597         }
2598         RT_TRACE(COMP_INIT, "start adapter finished\n");
2599         RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
2600         priv->bfirst_init = false;
2601 
2602         rtl92e_irq_enable(dev);
2603         priv->bdisable_nic = false;
2604         RT_TRACE(COMP_PS, "<===========%s()\n", __func__);
2605         return init_status;
2606 }
2607 
2608 bool rtl92e_disable_nic(struct net_device *dev)
2609 {
2610         struct r8192_priv *priv = rtllib_priv(dev);
2611         u8 tmp_state = 0;
2612 
2613         RT_TRACE(COMP_PS, "=========>%s()\n", __func__);
2614         priv->bdisable_nic = true;
2615         tmp_state = priv->rtllib->state;
2616         rtllib_softmac_stop_protocol(priv->rtllib, 0, false);
2617         priv->rtllib->state = tmp_state;
2618         _rtl92e_cancel_deferred_work(priv);
2619         rtl92e_irq_disable(dev);
2620 
2621         priv->ops->stop_adapter(dev, false);
2622         RT_TRACE(COMP_PS, "<=========%s()\n", __func__);
2623 
2624         return true;
2625 }
2626 
2627 module_pci_driver(rtl8192_pci_driver);
2628 
2629 void rtl92e_check_rfctrl_gpio_timer(struct timer_list *t)
2630 {
2631         struct r8192_priv *priv = from_timer(priv, t, gpio_polling_timer);
2632 
2633         priv->polling_timer_on = 1;
2634 
2635         schedule_delayed_work(&priv->gpio_change_rf_wq, 0);
2636 
2637         mod_timer(&priv->gpio_polling_timer, jiffies +
2638                   msecs_to_jiffies(RTLLIB_WATCH_DOG_TIME));
2639 }
2640 
2641 /***************************************************************************
2642  * ------------------- module init / exit stubs ----------------
2643  ***************************************************************************/
2644 MODULE_DESCRIPTION("Linux driver for Realtek RTL819x WiFi cards");
2645 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
2646 MODULE_VERSION(DRV_VERSION);
2647 MODULE_LICENSE("GPL");
2648 MODULE_FIRMWARE(RTL8192E_BOOT_IMG_FW);
2649 MODULE_FIRMWARE(RTL8192E_MAIN_IMG_FW);
2650 MODULE_FIRMWARE(RTL8192E_DATA_IMG_FW);
2651 
2652 module_param(ifname, charp, 0644);
2653 module_param(hwwep, int, 0644);
2654 module_param(channels, int, 0644);
2655 
2656 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
2657 MODULE_PARM_DESC(hwwep, " Try to use hardware WEP support(default use hw. set 0 to use software security)");
2658 MODULE_PARM_DESC(channels, " Channel bitmask for specific locales. NYI");

/* [<][>][^][v][top][bottom][index][help] */