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