Version:  2.0.40 2.2.26 2.4.37 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18

Linux/drivers/staging/rtl8192e/rtl8192e/rtl_core.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us