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