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