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