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