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