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