Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/drivers/staging/rtl8192e/rtl8192e/rtl_core.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us