Version:  2.0.40 2.2.26 2.4.37 2.6.39 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

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                 ret = 0;
1919                 return ret;
1920         } else {
1921                 tcb_desc->RATRIndex = 7;
1922                 tcb_desc->bTxDisableRateFallBack = 1;
1923                 tcb_desc->bTxUseDriverAssingedRate = 1;
1924                 tcb_desc->bTxEnableFwCalcDur = 1;
1925                 skb_push(skb, priv->rtllib->tx_headroom);
1926                 ret = rtl8192_tx(dev, skb);
1927                 if (ret != 0)
1928                         kfree_skb(skb);
1929         }
1930 
1931         return ret;
1932 }
1933 
1934 static void rtl8192_tx_isr(struct net_device *dev, int prio)
1935 {
1936         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
1937 
1938         struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1939 
1940         while (skb_queue_len(&ring->queue)) {
1941                 struct tx_desc *entry = &ring->desc[ring->idx];
1942                 struct sk_buff *skb;
1943 
1944                 if (prio != BEACON_QUEUE) {
1945                         if (entry->OWN)
1946                                 return;
1947                         ring->idx = (ring->idx + 1) % ring->entries;
1948                 }
1949 
1950                 skb = __skb_dequeue(&ring->queue);
1951                 pci_unmap_single(priv->pdev, entry->TxBuffAddr,
1952                 skb->len, PCI_DMA_TODEVICE);
1953 
1954                 kfree_skb(skb);
1955         }
1956         if (prio != BEACON_QUEUE)
1957                 tasklet_schedule(&priv->irq_tx_tasklet);
1958 }
1959 
1960 void rtl8192_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1961 {
1962         struct r8192_priv *priv = rtllib_priv(dev);
1963         struct rtl8192_tx_ring *ring;
1964         struct tx_desc_cmd *entry;
1965         unsigned int idx;
1966         struct cb_desc *tcb_desc;
1967         unsigned long flags;
1968 
1969         spin_lock_irqsave(&priv->irq_th_lock, flags);
1970         ring = &priv->tx_ring[TXCMD_QUEUE];
1971 
1972         idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1973         entry = (struct tx_desc_cmd *) &ring->desc[idx];
1974 
1975         tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1976 
1977         priv->ops->tx_fill_cmd_descriptor(dev, entry, tcb_desc, skb);
1978 
1979         __skb_queue_tail(&ring->queue, skb);
1980         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1981 
1982         return;
1983 }
1984 
1985 short rtl8192_tx(struct net_device *dev, struct sk_buff *skb)
1986 {
1987         struct r8192_priv *priv = rtllib_priv(dev);
1988         struct rtl8192_tx_ring  *ring;
1989         unsigned long flags;
1990         struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1991                                     MAX_DEV_ADDR_SIZE);
1992         struct tx_desc *pdesc = NULL;
1993         struct rtllib_hdr_1addr *header = NULL;
1994         u16 fc = 0, type = 0, stype = 0;
1995         bool  multi_addr = false, broad_addr = false, uni_addr = false;
1996         u8 *pda_addr = NULL;
1997         int   idx;
1998         u32 fwinfo_size = 0;
1999 
2000         if (priv->bdisable_nic) {
2001                 RT_TRACE(COMP_ERR, "%s: ERR!! Nic is disabled! Can't tx packet"
2002                          " len=%d qidx=%d!!!\n", __func__, skb->len,
2003                          tcb_desc->queue_index);
2004                 return skb->len;
2005         }
2006 
2007         priv->rtllib->bAwakePktSent = true;
2008 
2009         fwinfo_size = sizeof(struct tx_fwinfo_8190pci);
2010 
2011         header = (struct rtllib_hdr_1addr *)(((u8 *)skb->data) + fwinfo_size);
2012         fc = le16_to_cpu(header->frame_ctl);
2013         type = WLAN_FC_GET_TYPE(fc);
2014         stype = WLAN_FC_GET_STYPE(fc);
2015         pda_addr = header->addr1;
2016 
2017         if (is_broadcast_ether_addr(pda_addr))
2018                 broad_addr = true;
2019         else if (is_multicast_ether_addr(pda_addr))
2020                 multi_addr = true;
2021         else
2022                 uni_addr = true;
2023 
2024         if (uni_addr)
2025                 priv->stats.txbytesunicast += skb->len - fwinfo_size;
2026         else if (multi_addr)
2027                 priv->stats.txbytesmulticast += skb->len - fwinfo_size;
2028         else
2029                 priv->stats.txbytesbroadcast += skb->len - fwinfo_size;
2030 
2031         spin_lock_irqsave(&priv->irq_th_lock, flags);
2032         ring = &priv->tx_ring[tcb_desc->queue_index];
2033         if (tcb_desc->queue_index != BEACON_QUEUE)
2034                 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
2035         else
2036                 idx = 0;
2037 
2038         pdesc = &ring->desc[idx];
2039         if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
2040                 RT_TRACE(COMP_ERR, "No more TX desc@%d, ring->idx = %d, idx = "
2041                          "%d, skblen = 0x%x queuelen=%d",
2042                          tcb_desc->queue_index, ring->idx, idx, skb->len,
2043                          skb_queue_len(&ring->queue));
2044                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2045                 return skb->len;
2046         }
2047 
2048         if (type == RTLLIB_FTYPE_DATA) {
2049                 if (priv->rtllib->LedControlHandler)
2050                         priv->rtllib->LedControlHandler(dev, LED_CTL_TX);
2051         }
2052         priv->ops->tx_fill_descriptor(dev, pdesc, tcb_desc, skb);
2053         __skb_queue_tail(&ring->queue, skb);
2054         pdesc->OWN = 1;
2055         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2056         dev->trans_start = jiffies;
2057 
2058         write_nic_word(dev, TPPoll, 0x01 << tcb_desc->queue_index);
2059         return 0;
2060 }
2061 
2062 static short rtl8192_alloc_rx_desc_ring(struct net_device *dev)
2063 {
2064         struct r8192_priv *priv = rtllib_priv(dev);
2065         struct rx_desc *entry = NULL;
2066         int i, rx_queue_idx;
2067 
2068         for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE; rx_queue_idx++) {
2069                 priv->rx_ring[rx_queue_idx] = pci_alloc_consistent(priv->pdev,
2070                                         sizeof(*priv->rx_ring[rx_queue_idx]) *
2071                                         priv->rxringcount,
2072                                         &priv->rx_ring_dma[rx_queue_idx]);
2073 
2074                 if (!priv->rx_ring[rx_queue_idx] ||
2075                     (unsigned long)priv->rx_ring[rx_queue_idx] & 0xFF) {
2076                         RT_TRACE(COMP_ERR, "Cannot allocate RX ring\n");
2077                         return -ENOMEM;
2078                 }
2079 
2080                 memset(priv->rx_ring[rx_queue_idx], 0,
2081                        sizeof(*priv->rx_ring[rx_queue_idx]) *
2082                        priv->rxringcount);
2083                 priv->rx_idx[rx_queue_idx] = 0;
2084 
2085                 for (i = 0; i < priv->rxringcount; i++) {
2086                         struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
2087                         dma_addr_t *mapping;
2088                         entry = &priv->rx_ring[rx_queue_idx][i];
2089                         if (!skb)
2090                                 return 0;
2091                         skb->dev = dev;
2092                         priv->rx_buf[rx_queue_idx][i] = skb;
2093                         mapping = (dma_addr_t *)skb->cb;
2094                         *mapping = pci_map_single(priv->pdev,
2095                                                   skb_tail_pointer_rsl(skb),
2096                                                   priv->rxbuffersize,
2097                                                   PCI_DMA_FROMDEVICE);
2098                         if (pci_dma_mapping_error(priv->pdev, *mapping)) {
2099                                 dev_kfree_skb_any(skb);
2100                                 return -1;
2101                         }
2102                         entry->BufferAddress = *mapping;
2103 
2104                         entry->Length = priv->rxbuffersize;
2105                         entry->OWN = 1;
2106                 }
2107 
2108                 if(entry)
2109                         entry->EOR = 1;
2110         }
2111         return 0;
2112 }
2113 
2114 static int rtl8192_alloc_tx_desc_ring(struct net_device *dev,
2115         unsigned int prio, unsigned int entries)
2116 {
2117         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2118         struct tx_desc *ring;
2119         dma_addr_t dma;
2120         int i;
2121 
2122         ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
2123         if (!ring || (unsigned long)ring & 0xFF) {
2124                 RT_TRACE(COMP_ERR, "Cannot allocate TX ring (prio = %d)\n",
2125                          prio);
2126                 return -ENOMEM;
2127         }
2128 
2129         memset(ring, 0, sizeof(*ring)*entries);
2130         priv->tx_ring[prio].desc = ring;
2131         priv->tx_ring[prio].dma = dma;
2132         priv->tx_ring[prio].idx = 0;
2133         priv->tx_ring[prio].entries = entries;
2134         skb_queue_head_init(&priv->tx_ring[prio].queue);
2135 
2136         for (i = 0; i < entries; i++)
2137                 ring[i].NextDescAddress =
2138                         (u32)dma + ((i + 1) % entries) *
2139                         sizeof(*ring);
2140 
2141         return 0;
2142 }
2143 
2144 
2145 short rtl8192_pci_initdescring(struct net_device *dev)
2146 {
2147         u32 ret;
2148         int i;
2149         struct r8192_priv *priv = rtllib_priv(dev);
2150 
2151         ret = rtl8192_alloc_rx_desc_ring(dev);
2152         if (ret)
2153                 return ret;
2154 
2155         for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
2156                 ret = rtl8192_alloc_tx_desc_ring(dev, i, priv->txringcount);
2157                 if (ret)
2158                         goto err_free_rings;
2159         }
2160 
2161         return 0;
2162 
2163 err_free_rings:
2164         rtl8192_free_rx_ring(dev);
2165         for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
2166                 if (priv->tx_ring[i].desc)
2167                         rtl8192_free_tx_ring(dev, i);
2168         return 1;
2169 }
2170 
2171 void rtl8192_pci_resetdescring(struct net_device *dev)
2172 {
2173         struct r8192_priv *priv = rtllib_priv(dev);
2174         int i, rx_queue_idx;
2175         unsigned long flags = 0;
2176 
2177         for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE; rx_queue_idx++) {
2178                 if (priv->rx_ring[rx_queue_idx]) {
2179                         struct rx_desc *entry = NULL;
2180                         for (i = 0; i < priv->rxringcount; i++) {
2181                                 entry = &priv->rx_ring[rx_queue_idx][i];
2182                                 entry->OWN = 1;
2183                         }
2184                         priv->rx_idx[rx_queue_idx] = 0;
2185                 }
2186         }
2187 
2188         spin_lock_irqsave(&priv->irq_th_lock, flags);
2189         for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
2190                 if (priv->tx_ring[i].desc) {
2191                         struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
2192 
2193                         while (skb_queue_len(&ring->queue)) {
2194                                 struct tx_desc *entry = &ring->desc[ring->idx];
2195                                 struct sk_buff *skb =
2196                                                  __skb_dequeue(&ring->queue);
2197 
2198                                 pci_unmap_single(priv->pdev,
2199                                                  entry->TxBuffAddr,
2200                                                  skb->len, PCI_DMA_TODEVICE);
2201                                 kfree_skb(skb);
2202                                 ring->idx = (ring->idx + 1) % ring->entries;
2203                         }
2204                         ring->idx = 0;
2205                 }
2206         }
2207         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2208 }
2209 
2210 void rtl819x_UpdateRxPktTimeStamp(struct net_device *dev,
2211                                   struct rtllib_rx_stats *stats)
2212 {
2213         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2214 
2215         if (stats->bIsAMPDU && !stats->bFirstMPDU)
2216                 stats->mac_time = priv->LastRxDescTSF;
2217         else
2218                 priv->LastRxDescTSF = stats->mac_time;
2219 }
2220 
2221 long rtl819x_translate_todbm(struct r8192_priv *priv, u8 signal_strength_index)
2222 {
2223         long    signal_power;
2224 
2225         signal_power = (long)((signal_strength_index + 1) >> 1);
2226         signal_power -= 95;
2227 
2228         return signal_power;
2229 }
2230 
2231 
2232 void
2233 rtl819x_update_rxsignalstatistics8190pci(
2234         struct r8192_priv *priv,
2235         struct rtllib_rx_stats *pprevious_stats
2236         )
2237 {
2238         int weighting = 0;
2239 
2240 
2241         if (priv->stats.recv_signal_power == 0)
2242                 priv->stats.recv_signal_power =
2243                                          pprevious_stats->RecvSignalPower;
2244 
2245         if (pprevious_stats->RecvSignalPower > priv->stats.recv_signal_power)
2246                 weighting = 5;
2247         else if (pprevious_stats->RecvSignalPower <
2248                  priv->stats.recv_signal_power)
2249                 weighting = (-5);
2250         priv->stats.recv_signal_power = (priv->stats.recv_signal_power * 5 +
2251                                         pprevious_stats->RecvSignalPower +
2252                                         weighting) / 6;
2253 }
2254 
2255 void rtl819x_process_cck_rxpathsel(struct r8192_priv *priv,
2256                                    struct rtllib_rx_stats *pprevious_stats)
2257 {
2258 }
2259 
2260 
2261 u8 rtl819x_query_rxpwrpercentage(char antpower)
2262 {
2263         if ((antpower <= -100) || (antpower >= 20))
2264                 return  0;
2265         else if (antpower >= 0)
2266                 return  100;
2267         else
2268                 return  100 + antpower;
2269 
2270 }       /* QueryRxPwrPercentage */
2271 
2272 u8
2273 rtl819x_evm_dbtopercentage(
2274         char value
2275         )
2276 {
2277         char ret_val;
2278 
2279         ret_val = value;
2280 
2281         if (ret_val >= 0)
2282                 ret_val = 0;
2283         if (ret_val <= -33)
2284                 ret_val = -33;
2285         ret_val = 0 - ret_val;
2286         ret_val *= 3;
2287         if (ret_val == 99)
2288                 ret_val = 100;
2289         return ret_val;
2290 }
2291 
2292 void
2293 rtl8192_record_rxdesc_forlateruse(
2294         struct rtllib_rx_stats *psrc_stats,
2295         struct rtllib_rx_stats *ptarget_stats
2296 )
2297 {
2298         ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
2299         ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
2300 }
2301 
2302 
2303 
2304 static void rtl8192_rx_normal(struct net_device *dev)
2305 {
2306         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2307         struct rtllib_hdr_1addr *rtllib_hdr = NULL;
2308         bool unicast_packet = false;
2309         bool bLedBlinking = true;
2310         u16 fc = 0, type = 0;
2311         u32 skb_len = 0;
2312         int rx_queue_idx = RX_MPDU_QUEUE;
2313 
2314         struct rtllib_rx_stats stats = {
2315                 .signal = 0,
2316                 .noise = -98,
2317                 .rate = 0,
2318                 .freq = RTLLIB_24GHZ_BAND,
2319         };
2320         unsigned int count = priv->rxringcount;
2321 
2322         stats.nic_type = NIC_8192E;
2323 
2324         while (count--) {
2325                 struct rx_desc *pdesc = &priv->rx_ring[rx_queue_idx]
2326                                         [priv->rx_idx[rx_queue_idx]];
2327                 struct sk_buff *skb = priv->rx_buf[rx_queue_idx]
2328                                       [priv->rx_idx[rx_queue_idx]];
2329 
2330                 if (pdesc->OWN) {
2331                         return;
2332                 } else {
2333                         struct sk_buff *new_skb;
2334 
2335                         if (!priv->ops->rx_query_status_descriptor(dev, &stats,
2336                         pdesc, skb))
2337                                 goto done;
2338                         new_skb = dev_alloc_skb(priv->rxbuffersize);
2339                         /* if allocation of new skb failed - drop current packet
2340                         * and reuse skb */
2341                         if (unlikely(!new_skb))
2342                                 goto done;
2343 
2344                         pci_unmap_single(priv->pdev,
2345                                         *((dma_addr_t *)skb->cb),
2346                                         priv->rxbuffersize,
2347                                         PCI_DMA_FROMDEVICE);
2348 
2349                         skb_put(skb, pdesc->Length);
2350                         skb_reserve(skb, stats.RxDrvInfoSize +
2351                                 stats.RxBufShift);
2352                         skb_trim(skb, skb->len - 4/*sCrcLng*/);
2353                         rtllib_hdr = (struct rtllib_hdr_1addr *)skb->data;
2354                         if (!is_multicast_ether_addr(rtllib_hdr->addr1)) {
2355                                 /* unicast packet */
2356                                 unicast_packet = true;
2357                         }
2358                         fc = le16_to_cpu(rtllib_hdr->frame_ctl);
2359                         type = WLAN_FC_GET_TYPE(fc);
2360                         if (type == RTLLIB_FTYPE_MGMT)
2361                                 bLedBlinking = false;
2362 
2363                         if (bLedBlinking)
2364                                 if (priv->rtllib->LedControlHandler)
2365                                         priv->rtllib->LedControlHandler(dev,
2366                                                                 LED_CTL_RX);
2367 
2368                         if (stats.bCRC) {
2369                                 if (type != RTLLIB_FTYPE_MGMT)
2370                                         priv->stats.rxdatacrcerr++;
2371                                 else
2372                                         priv->stats.rxmgmtcrcerr++;
2373                         }
2374 
2375                         skb_len = skb->len;
2376 
2377                         if (!rtllib_rx(priv->rtllib, skb, &stats)) {
2378                                 dev_kfree_skb_any(skb);
2379                         } else {
2380                                 priv->stats.rxok++;
2381                                 if (unicast_packet)
2382                                         priv->stats.rxbytesunicast += skb_len;
2383                         }
2384 
2385                         skb = new_skb;
2386                         skb->dev = dev;
2387 
2388                         priv->rx_buf[rx_queue_idx][priv->rx_idx[rx_queue_idx]] =
2389                                                                          skb;
2390                         *((dma_addr_t *) skb->cb) = pci_map_single(priv->pdev,
2391                                                     skb_tail_pointer_rsl(skb),
2392                                                     priv->rxbuffersize,
2393                                                     PCI_DMA_FROMDEVICE);
2394                         if (pci_dma_mapping_error(priv->pdev,
2395                                                   *((dma_addr_t *)skb->cb))) {
2396                                 dev_kfree_skb_any(skb);
2397                                 return;
2398                         }
2399                 }
2400 done:
2401                 pdesc->BufferAddress = *((dma_addr_t *)skb->cb);
2402                 pdesc->OWN = 1;
2403                 pdesc->Length = priv->rxbuffersize;
2404                 if (priv->rx_idx[rx_queue_idx] == priv->rxringcount-1)
2405                         pdesc->EOR = 1;
2406                 priv->rx_idx[rx_queue_idx] = (priv->rx_idx[rx_queue_idx] + 1) %
2407                                               priv->rxringcount;
2408         }
2409 
2410 }
2411 
2412 static void rtl8192_rx_cmd(struct net_device *dev)
2413 {
2414 }
2415 
2416 
2417 static void rtl8192_tx_resume(struct net_device *dev)
2418 {
2419         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2420         struct rtllib_device *ieee = priv->rtllib;
2421         struct sk_buff *skb;
2422         int queue_index;
2423 
2424         for (queue_index = BK_QUEUE;
2425              queue_index < MAX_QUEUE_SIZE; queue_index++) {
2426                 while ((!skb_queue_empty(&ieee->skb_waitQ[queue_index])) &&
2427                 (priv->rtllib->check_nic_enough_desc(dev, queue_index) > 0)) {
2428                         skb = skb_dequeue(&ieee->skb_waitQ[queue_index]);
2429                         ieee->softmac_data_hard_start_xmit(skb, dev, 0);
2430                 }
2431         }
2432 }
2433 
2434 void rtl8192_irq_tx_tasklet(struct r8192_priv *priv)
2435 {
2436         rtl8192_tx_resume(priv->rtllib->dev);
2437 }
2438 
2439 void rtl8192_irq_rx_tasklet(struct r8192_priv *priv)
2440 {
2441         rtl8192_rx_normal(priv->rtllib->dev);
2442 
2443         if (MAX_RX_QUEUE > 1)
2444                 rtl8192_rx_cmd(priv->rtllib->dev);
2445 
2446         write_nic_dword(priv->rtllib->dev, INTA_MASK,
2447                         read_nic_dword(priv->rtllib->dev, INTA_MASK) | IMR_RDU);
2448 }
2449 
2450 /****************************************************************************
2451  ---------------------------- NIC START/CLOSE STUFF---------------------------
2452 *****************************************************************************/
2453 void rtl8192_cancel_deferred_work(struct r8192_priv *priv)
2454 {
2455         cancel_delayed_work(&priv->watch_dog_wq);
2456         cancel_delayed_work(&priv->update_beacon_wq);
2457         cancel_delayed_work(&priv->rtllib->hw_sleep_wq);
2458         cancel_work_sync(&priv->reset_wq);
2459         cancel_work_sync(&priv->qos_activate);
2460 }
2461 
2462 int _rtl8192_up(struct net_device *dev, bool is_silent_reset)
2463 {
2464         if (_rtl8192_sta_up(dev, is_silent_reset) == -1)
2465                 return -1;
2466         return 0;
2467 }
2468 
2469 
2470 static int rtl8192_open(struct net_device *dev)
2471 {
2472         struct r8192_priv *priv = rtllib_priv(dev);
2473         int ret;
2474 
2475         down(&priv->wx_sem);
2476         ret = rtl8192_up(dev);
2477         up(&priv->wx_sem);
2478         return ret;
2479 
2480 }
2481 
2482 
2483 int rtl8192_up(struct net_device *dev)
2484 {
2485         struct r8192_priv *priv = rtllib_priv(dev);
2486 
2487         if (priv->up == 1)
2488                 return -1;
2489         return _rtl8192_up(dev, false);
2490 }
2491 
2492 
2493 static int rtl8192_close(struct net_device *dev)
2494 {
2495         struct r8192_priv *priv = rtllib_priv(dev);
2496         int ret;
2497 
2498         if ((rtllib_act_scanning(priv->rtllib, false)) &&
2499                 !(priv->rtllib->softmac_features & IEEE_SOFTMAC_SCAN)) {
2500                 rtllib_stop_scan(priv->rtllib);
2501         }
2502 
2503         down(&priv->wx_sem);
2504 
2505         ret = rtl8192_down(dev, true);
2506 
2507         up(&priv->wx_sem);
2508 
2509         return ret;
2510 
2511 }
2512 
2513 int rtl8192_down(struct net_device *dev, bool shutdownrf)
2514 {
2515         if (rtl8192_sta_down(dev, shutdownrf) == -1)
2516                 return -1;
2517 
2518         return 0;
2519 }
2520 
2521 void rtl8192_commit(struct net_device *dev)
2522 {
2523         struct r8192_priv *priv = rtllib_priv(dev);
2524 
2525         if (priv->up == 0)
2526                 return;
2527         rtllib_softmac_stop_protocol(priv->rtllib, 0 , true);
2528         rtl8192_irq_disable(dev);
2529         priv->ops->stop_adapter(dev, true);
2530         _rtl8192_up(dev, false);
2531 }
2532 
2533 void rtl8192_restart(void *data)
2534 {
2535         struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
2536                                   reset_wq);
2537         struct net_device *dev = priv->rtllib->dev;
2538 
2539         down(&priv->wx_sem);
2540 
2541         rtl8192_commit(dev);
2542 
2543         up(&priv->wx_sem);
2544 }
2545 
2546 static void r8192_set_multicast(struct net_device *dev)
2547 {
2548         struct r8192_priv *priv = rtllib_priv(dev);
2549         short promisc;
2550 
2551         promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
2552         priv->promisc = promisc;
2553 
2554 }
2555 
2556 
2557 static int r8192_set_mac_adr(struct net_device *dev, void *mac)
2558 {
2559         struct r8192_priv *priv = rtllib_priv(dev);
2560         struct sockaddr *addr = mac;
2561 
2562         down(&priv->wx_sem);
2563 
2564         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
2565 
2566         schedule_work(&priv->reset_wq);
2567         up(&priv->wx_sem);
2568 
2569         return 0;
2570 }
2571 
2572 /* based on ipw2200 driver */
2573 static int rtl8192_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2574 {
2575         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2576         struct iwreq *wrq = (struct iwreq *)rq;
2577         int ret = -1;
2578         struct rtllib_device *ieee = priv->rtllib;
2579         u32 key[4];
2580         u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2581         u8 zero_addr[6] = {0};
2582         struct iw_point *p = &wrq->u.data;
2583         struct ieee_param *ipw = NULL;
2584 
2585         down(&priv->wx_sem);
2586 
2587         switch (cmd) {
2588         case RTL_IOCTL_WPA_SUPPLICANT:
2589                 if (p->length < sizeof(struct ieee_param) || !p->pointer) {
2590                         ret = -EINVAL;
2591                         goto out;
2592                 }
2593 
2594                 ipw = memdup_user(p->pointer, p->length);
2595                 if (IS_ERR(ipw)) {
2596                         ret = PTR_ERR(ipw);
2597                         goto out;
2598                 }
2599 
2600                 if (ipw->cmd == IEEE_CMD_SET_ENCRYPTION) {
2601                         if (ipw->u.crypt.set_tx) {
2602                                 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
2603                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
2604                                 else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
2605                                         ieee->pairwise_key_type = KEY_TYPE_TKIP;
2606                                 else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
2607                                         if (ipw->u.crypt.key_len == 13)
2608                                                 ieee->pairwise_key_type =
2609                                                          KEY_TYPE_WEP104;
2610                                         else if (ipw->u.crypt.key_len == 5)
2611                                                 ieee->pairwise_key_type =
2612                                                          KEY_TYPE_WEP40;
2613                                 } else {
2614                                         ieee->pairwise_key_type = KEY_TYPE_NA;
2615                                 }
2616 
2617                                 if (ieee->pairwise_key_type) {
2618                                         if (memcmp(ieee->ap_mac_addr, zero_addr,
2619                                             6) == 0)
2620                                                 ieee->iw_mode = IW_MODE_ADHOC;
2621                                         memcpy((u8 *)key, ipw->u.crypt.key, 16);
2622                                         EnableHWSecurityConfig8192(dev);
2623                                         set_swcam(dev, 4, ipw->u.crypt.idx,
2624                                                   ieee->pairwise_key_type,
2625                                                   (u8 *)ieee->ap_mac_addr,
2626                                                   0, key, 0);
2627                                         setKey(dev, 4, ipw->u.crypt.idx,
2628                                                ieee->pairwise_key_type,
2629                                                (u8 *)ieee->ap_mac_addr, 0, key);
2630                                         if (ieee->iw_mode == IW_MODE_ADHOC) {
2631                                                 set_swcam(dev, ipw->u.crypt.idx,
2632                                                         ipw->u.crypt.idx,
2633                                                         ieee->pairwise_key_type,
2634                                                         (u8 *)ieee->ap_mac_addr,
2635                                                         0, key, 0);
2636                                                 setKey(dev, ipw->u.crypt.idx,
2637                                                        ipw->u.crypt.idx,
2638                                                        ieee->pairwise_key_type,
2639                                                        (u8 *)ieee->ap_mac_addr,
2640                                                        0, key);
2641                                         }
2642                                 }
2643                                 if ((ieee->pairwise_key_type == KEY_TYPE_CCMP)
2644                                      && ieee->pHTInfo->bCurrentHTSupport) {
2645                                         write_nic_byte(dev, 0x173, 1);
2646                                 }
2647 
2648                         } else {
2649                                 memcpy((u8 *)key, ipw->u.crypt.key, 16);
2650                                 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
2651                                         ieee->group_key_type = KEY_TYPE_CCMP;
2652                                 else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
2653                                         ieee->group_key_type = KEY_TYPE_TKIP;
2654                                 else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
2655                                         if (ipw->u.crypt.key_len == 13)
2656                                                 ieee->group_key_type =
2657                                                          KEY_TYPE_WEP104;
2658                                         else if (ipw->u.crypt.key_len == 5)
2659                                                 ieee->group_key_type =
2660                                                          KEY_TYPE_WEP40;
2661                                 } else
2662                                         ieee->group_key_type = KEY_TYPE_NA;
2663 
2664                                 if (ieee->group_key_type) {
2665                                         set_swcam(dev, ipw->u.crypt.idx,
2666                                                   ipw->u.crypt.idx,
2667                                                   ieee->group_key_type,
2668                                                   broadcast_addr, 0, key, 0);
2669                                         setKey(dev, ipw->u.crypt.idx,
2670                                                ipw->u.crypt.idx,
2671                                                ieee->group_key_type,
2672                                                broadcast_addr, 0, key);
2673                                 }
2674                         }
2675                 }
2676 
2677                 ret = rtllib_wpa_supplicant_ioctl(priv->rtllib, &wrq->u.data,
2678                                                   0);
2679                 kfree(ipw);
2680                 break;
2681         default:
2682                 ret = -EOPNOTSUPP;
2683                 break;
2684         }
2685 
2686 out:
2687         up(&priv->wx_sem);
2688 
2689         return ret;
2690 }
2691 
2692 
2693 static irqreturn_t rtl8192_interrupt(int irq, void *netdev)
2694 {
2695         struct net_device *dev = (struct net_device *) netdev;
2696         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
2697         unsigned long flags;
2698         u32 inta;
2699         u32 intb;
2700         intb = 0;
2701 
2702         if (priv->irq_enabled == 0)
2703                 goto done;
2704 
2705         spin_lock_irqsave(&priv->irq_th_lock, flags);
2706 
2707         priv->ops->interrupt_recognized(dev, &inta, &intb);
2708         priv->stats.shints++;
2709 
2710         if (!inta) {
2711                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2712                 goto done;
2713         }
2714 
2715         if (inta == 0xffff) {
2716                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2717                 goto done;
2718         }
2719 
2720         priv->stats.ints++;
2721 
2722         if (!netif_running(dev)) {
2723                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2724                 goto done;
2725         }
2726 
2727         if (inta & IMR_TBDOK) {
2728                 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
2729                 priv->stats.txbeaconokint++;
2730         }
2731 
2732         if (inta & IMR_TBDER) {
2733                 RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
2734                 priv->stats.txbeaconerr++;
2735         }
2736 
2737         if (inta & IMR_BDOK)
2738                 RT_TRACE(COMP_INTR, "beacon interrupt!\n");
2739 
2740         if (inta  & IMR_MGNTDOK) {
2741                 RT_TRACE(COMP_INTR, "Manage ok interrupt!\n");
2742                 priv->stats.txmanageokint++;
2743                 rtl8192_tx_isr(dev, MGNT_QUEUE);
2744                 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2745                 if (priv->rtllib->ack_tx_to_ieee) {
2746                         if (rtl8192_is_tx_queue_empty(dev)) {
2747                                 priv->rtllib->ack_tx_to_ieee = 0;
2748                                 rtllib_ps_tx_ack(priv->rtllib, 1);
2749                         }
2750                 }
2751                 spin_lock_irqsave(&priv->irq_th_lock, flags);
2752         }
2753 
2754         if (inta & IMR_COMDOK) {
2755                 priv->stats.txcmdpktokint++;
2756                 rtl8192_tx_isr(dev, TXCMD_QUEUE);
2757         }
2758 
2759         if (inta & IMR_HIGHDOK)
2760                 rtl8192_tx_isr(dev, HIGH_QUEUE);
2761 
2762         if (inta & IMR_ROK) {
2763                 priv->stats.rxint++;
2764                 priv->InterruptLog.nIMR_ROK++;
2765                 tasklet_schedule(&priv->irq_rx_tasklet);
2766         }
2767 
2768         if (inta & IMR_BcnInt) {
2769                 RT_TRACE(COMP_INTR, "prepare beacon for interrupt!\n");
2770                 tasklet_schedule(&priv->irq_prepare_beacon_tasklet);
2771         }
2772 
2773         if (inta & IMR_RDU) {
2774                 RT_TRACE(COMP_INTR, "rx descriptor unavailable!\n");
2775                 priv->stats.rxrdu++;
2776                 write_nic_dword(dev, INTA_MASK,
2777                                 read_nic_dword(dev, INTA_MASK) & ~IMR_RDU);
2778                 tasklet_schedule(&priv->irq_rx_tasklet);
2779         }
2780 
2781         if (inta & IMR_RXFOVW) {
2782                 RT_TRACE(COMP_INTR, "rx overflow !\n");
2783                 priv->stats.rxoverflow++;
2784                 tasklet_schedule(&priv->irq_rx_tasklet);
2785         }
2786 
2787         if (inta & IMR_TXFOVW)
2788                 priv->stats.txoverflow++;
2789 
2790         if (inta & IMR_BKDOK) {
2791                 RT_TRACE(COMP_INTR, "BK Tx OK interrupt!\n");
2792                 priv->stats.txbkokint++;
2793                 priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2794                 rtl8192_tx_isr(dev, BK_QUEUE);
2795         }
2796 
2797         if (inta & IMR_BEDOK) {
2798                 RT_TRACE(COMP_INTR, "BE TX OK interrupt!\n");
2799                 priv->stats.txbeokint++;
2800                 priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2801                 rtl8192_tx_isr(dev, BE_QUEUE);
2802         }
2803 
2804         if (inta & IMR_VIDOK) {
2805                 RT_TRACE(COMP_INTR, "VI TX OK interrupt!\n");
2806                 priv->stats.txviokint++;
2807                 priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2808                 rtl8192_tx_isr(dev, VI_QUEUE);
2809         }
2810 
2811         if (inta & IMR_VODOK) {
2812                 priv->stats.txvookint++;
2813                 RT_TRACE(COMP_INTR, "Vo TX OK interrupt!\n");
2814                 priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2815                 rtl8192_tx_isr(dev, VO_QUEUE);
2816         }
2817 
2818         spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2819 
2820 done:
2821 
2822         return IRQ_HANDLED;
2823 }
2824 
2825 
2826 
2827 /****************************************************************************
2828         ---------------------------- PCI_STUFF---------------------------
2829 *****************************************************************************/
2830 static const struct net_device_ops rtl8192_netdev_ops = {
2831         .ndo_open = rtl8192_open,
2832         .ndo_stop = rtl8192_close,
2833         .ndo_tx_timeout = rtl8192_tx_timeout,
2834         .ndo_do_ioctl = rtl8192_ioctl,
2835         .ndo_set_rx_mode = r8192_set_multicast,
2836         .ndo_set_mac_address = r8192_set_mac_adr,
2837         .ndo_validate_addr = eth_validate_addr,
2838         .ndo_change_mtu = eth_change_mtu,
2839         .ndo_start_xmit = rtllib_xmit,
2840 };
2841 
2842 static int rtl8192_pci_probe(struct pci_dev *pdev,
2843                         const struct pci_device_id *id)
2844 {
2845         unsigned long ioaddr = 0;
2846         struct net_device *dev = NULL;
2847         struct r8192_priv *priv = NULL;
2848         struct rtl819x_ops *ops = (struct rtl819x_ops *)(id->driver_data);
2849         unsigned long pmem_start, pmem_len, pmem_flags;
2850         int err = -ENOMEM;
2851         bool bdma64 = false;
2852         u8 revision_id;
2853 
2854         RT_TRACE(COMP_INIT, "Configuring chip resources");
2855 
2856         if (pci_enable_device(pdev)) {
2857                 RT_TRACE(COMP_ERR, "Failed to enable PCI device");
2858                 return -EIO;
2859         }
2860 
2861         pci_set_master(pdev);
2862 
2863         if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
2864                 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2865                         printk(KERN_INFO "Unable to obtain 32bit DMA for consistent allocations\n");
2866                         goto err_pci_disable;
2867                 }
2868         }
2869         dev = alloc_rtllib(sizeof(struct r8192_priv));
2870         if (!dev)
2871                 goto err_pci_disable;
2872 
2873         err = -ENODEV;
2874         if (bdma64)
2875                 dev->features |= NETIF_F_HIGHDMA;
2876 
2877         pci_set_drvdata(pdev, dev);
2878         SET_NETDEV_DEV(dev, &pdev->dev);
2879         priv = rtllib_priv(dev);
2880         priv->rtllib = (struct rtllib_device *)netdev_priv_rsl(dev);
2881         priv->pdev = pdev;
2882         priv->rtllib->pdev = pdev;
2883         if ((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK) &&
2884             (pdev->subsystem_device == 0x3304))
2885                 priv->rtllib->bSupportRemoteWakeUp = 1;
2886         else
2887                 priv->rtllib->bSupportRemoteWakeUp = 0;
2888 
2889         pmem_start = pci_resource_start(pdev, 1);
2890         pmem_len = pci_resource_len(pdev, 1);
2891         pmem_flags = pci_resource_flags(pdev, 1);
2892 
2893         if (!(pmem_flags & IORESOURCE_MEM)) {
2894                 RT_TRACE(COMP_ERR, "region #1 not a MMIO resource, aborting");
2895                 goto err_rel_rtllib;
2896         }
2897 
2898         printk(KERN_INFO "Memory mapped space start: 0x%08lx\n", pmem_start);
2899         if (!request_mem_region(pmem_start, pmem_len, DRV_NAME)) {
2900                 RT_TRACE(COMP_ERR, "request_mem_region failed!");
2901                 goto err_rel_rtllib;
2902         }
2903 
2904 
2905         ioaddr = (unsigned long)ioremap_nocache(pmem_start, pmem_len);
2906         if (ioaddr == (unsigned long)NULL) {
2907                 RT_TRACE(COMP_ERR, "ioremap failed!");
2908                 goto err_rel_mem;
2909         }
2910 
2911         dev->mem_start = ioaddr;
2912         dev->mem_end = ioaddr + pci_resource_len(pdev, 0);
2913 
2914         pci_read_config_byte(pdev, 0x08, &revision_id);
2915         /* If the revisionid is 0x10, the device uses rtl8192se. */
2916         if (pdev->device == 0x8192 && revision_id == 0x10)
2917                 goto err_rel_mem;
2918 
2919         priv->ops = ops;
2920 
2921         if (rtl8192_pci_findadapter(pdev, dev) == false)
2922                 goto err_rel_mem;
2923 
2924         dev->irq = pdev->irq;
2925         priv->irq = 0;
2926 
2927         dev->netdev_ops = &rtl8192_netdev_ops;
2928 
2929         dev->wireless_handlers = (struct iw_handler_def *)
2930                                  &r8192_wx_handlers_def;
2931         dev->ethtool_ops = &rtl819x_ethtool_ops;
2932 
2933         dev->type = ARPHRD_ETHER;
2934         dev->watchdog_timeo = HZ * 3;
2935 
2936         if (dev_alloc_name(dev, ifname) < 0) {
2937                 RT_TRACE(COMP_INIT, "Oops: devname already taken! Trying "
2938                          "wlan%%d...\n");
2939                         dev_alloc_name(dev, ifname);
2940         }
2941 
2942         RT_TRACE(COMP_INIT, "Driver probe completed1\n");
2943         if (rtl8192_init(dev) != 0) {
2944                 RT_TRACE(COMP_ERR, "Initialization failed");
2945                 goto err_free_irq;
2946         }
2947 
2948         netif_carrier_off(dev);
2949         netif_stop_queue(dev);
2950 
2951         if (register_netdev(dev))
2952                 goto err_free_irq;
2953         RT_TRACE(COMP_INIT, "dev name: %s\n", dev->name);
2954 
2955         if (priv->polling_timer_on == 0)
2956                 check_rfctrl_gpio_timer((unsigned long)dev);
2957 
2958         RT_TRACE(COMP_INIT, "Driver probe completed\n");
2959         return 0;
2960 
2961 err_free_irq:
2962         free_irq(dev->irq, dev);
2963         priv->irq = 0;
2964 err_rel_mem:
2965         release_mem_region(pmem_start, pmem_len);
2966 err_rel_rtllib:
2967         free_rtllib(dev);
2968 
2969         DMESG("wlan driver load failed\n");
2970 err_pci_disable:
2971         pci_disable_device(pdev);
2972         return err;
2973 }
2974 
2975 static void rtl8192_pci_disconnect(struct pci_dev *pdev)
2976 {
2977         struct net_device *dev = pci_get_drvdata(pdev);
2978         struct r8192_priv *priv ;
2979         u32 i;
2980 
2981         if (dev) {
2982                 unregister_netdev(dev);
2983 
2984                 priv = rtllib_priv(dev);
2985 
2986                 del_timer_sync(&priv->gpio_polling_timer);
2987                 cancel_delayed_work(&priv->gpio_change_rf_wq);
2988                 priv->polling_timer_on = 0;
2989                 rtl8192_down(dev, true);
2990                 deinit_hal_dm(dev);
2991                 if (priv->pFirmware) {
2992                         vfree(priv->pFirmware);
2993                         priv->pFirmware = NULL;
2994                 }
2995                 destroy_workqueue(priv->priv_wq);
2996                 rtl8192_free_rx_ring(dev);
2997                 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
2998                         rtl8192_free_tx_ring(dev, i);
2999 
3000                 if (priv->irq) {
3001                         printk(KERN_INFO "Freeing irq %d\n", dev->irq);
3002                         free_irq(dev->irq, dev);
3003                         priv->irq = 0;
3004                 }
3005                 free_rtllib(dev);
3006 
3007                 kfree(priv->scan_cmd);
3008 
3009                 if (dev->mem_start != 0) {
3010                         iounmap((void __iomem *)dev->mem_start);
3011                         release_mem_region(pci_resource_start(pdev, 1),
3012                                         pci_resource_len(pdev, 1));
3013                 }
3014         } else {
3015                 priv = rtllib_priv(dev);
3016         }
3017 
3018         pci_disable_device(pdev);
3019         RT_TRACE(COMP_DOWN, "wlan driver removed\n");
3020 }
3021 
3022 bool NicIFEnableNIC(struct net_device *dev)
3023 {
3024         bool init_status = true;
3025         struct r8192_priv *priv = rtllib_priv(dev);
3026         struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
3027                                         (&(priv->rtllib->PowerSaveControl));
3028 
3029         if (IS_NIC_DOWN(priv)) {
3030                 RT_TRACE(COMP_ERR, "ERR!!! %s(): Driver is already down!\n",
3031                          __func__);
3032                 priv->bdisable_nic = false;
3033                 return RT_STATUS_FAILURE;
3034         }
3035 
3036         RT_TRACE(COMP_PS, "===========>%s()\n", __func__);
3037         priv->bfirst_init = true;
3038         init_status = priv->ops->initialize_adapter(dev);
3039         if (!init_status) {
3040                 RT_TRACE(COMP_ERR, "ERR!!! %s(): initialization is failed!\n",
3041                          __func__);
3042                 priv->bdisable_nic = false;
3043                 return -1;
3044         }
3045         RT_TRACE(COMP_INIT, "start adapter finished\n");
3046         RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
3047         priv->bfirst_init = false;
3048 
3049         rtl8192_irq_enable(dev);
3050         priv->bdisable_nic = false;
3051         RT_TRACE(COMP_PS, "<===========%s()\n", __func__);
3052         return init_status;
3053 }
3054 bool NicIFDisableNIC(struct net_device *dev)
3055 {
3056         bool    status = true;
3057         struct r8192_priv *priv = rtllib_priv(dev);
3058         u8 tmp_state = 0;
3059         RT_TRACE(COMP_PS, "=========>%s()\n", __func__);
3060         priv->bdisable_nic = true;
3061         tmp_state = priv->rtllib->state;
3062         rtllib_softmac_stop_protocol(priv->rtllib, 0, false);
3063         priv->rtllib->state = tmp_state;
3064         rtl8192_cancel_deferred_work(priv);
3065         rtl8192_irq_disable(dev);
3066 
3067         priv->ops->stop_adapter(dev, false);
3068         RT_TRACE(COMP_PS, "<=========%s()\n", __func__);
3069 
3070         return status;
3071 }
3072 
3073 static int __init rtl8192_pci_module_init(void)
3074 {
3075         printk(KERN_INFO "\nLinux kernel driver for RTL8192E WLAN cards\n");
3076         printk(KERN_INFO "Copyright (c) 2007-2008, Realsil Wlan Driver\n");
3077 
3078         if (0 != pci_register_driver(&rtl8192_pci_driver)) {
3079                 DMESG("No device found");
3080                 /*pci_unregister_driver (&rtl8192_pci_driver);*/
3081                 return -ENODEV;
3082         }
3083         return 0;
3084 }
3085 
3086 static void __exit rtl8192_pci_module_exit(void)
3087 {
3088         pci_unregister_driver(&rtl8192_pci_driver);
3089 
3090         RT_TRACE(COMP_DOWN, "Exiting");
3091 }
3092 
3093 void check_rfctrl_gpio_timer(unsigned long data)
3094 {
3095         struct r8192_priv *priv = rtllib_priv((struct net_device *)data);
3096 
3097         priv->polling_timer_on = 1;
3098 
3099         queue_delayed_work_rsl(priv->priv_wq, &priv->gpio_change_rf_wq, 0);
3100 
3101         mod_timer(&priv->gpio_polling_timer, jiffies +
3102                   MSECS(RTLLIB_WATCH_DOG_TIME));
3103 }
3104 
3105 /***************************************************************************
3106         ------------------- module init / exit stubs ----------------
3107 ****************************************************************************/
3108 module_init(rtl8192_pci_module_init);
3109 module_exit(rtl8192_pci_module_exit);
3110 
3111 MODULE_DESCRIPTION("Linux driver for Realtek RTL819x WiFi cards");
3112 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
3113 MODULE_VERSION(DRV_VERSION);
3114 MODULE_LICENSE("GPL");
3115 MODULE_FIRMWARE(RTL8192E_BOOT_IMG_FW);
3116 MODULE_FIRMWARE(RTL8192E_MAIN_IMG_FW);
3117 MODULE_FIRMWARE(RTL8192E_DATA_IMG_FW);
3118 
3119 module_param(ifname, charp, S_IRUGO|S_IWUSR);
3120 module_param(hwwep, int, S_IRUGO|S_IWUSR);
3121 module_param(channels, int, S_IRUGO|S_IWUSR);
3122 
3123 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
3124 MODULE_PARM_DESC(hwwep, " Try to use hardware WEP support(default use hw. set 0 to use software security)");
3125 MODULE_PARM_DESC(channels, " Channel bitmask for specific locales. NYI");
3126 

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