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