35797bf9931e47a03c1fa5b114bb4e5fca628a36
[safe/jmp/linux-2.6] / drivers / staging / rtl8192e / r8192E_core.c
1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  * Linux device driver for RTL8190P / RTL8192E
4  *
5  * Based on the r8180 driver, which is:
6  * Copyright 2004-2005 Andrea Merello <andreamrl@tiscali.it>, et al.
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * The full GNU General Public License is included in this distribution in the
21  * file called LICENSE.
22  *
23  * Contact Information:
24  * Jerry chuang <wlanfae@realtek.com>
25  */
26
27 #ifndef CONFIG_FORCE_HARD_FLOAT
28 double __floatsidf (int i) { return i; }
29 unsigned int __fixunsdfsi (double d) { return d; }
30 double __adddf3(double a, double b) { return a+b; }
31 double __addsf3(float a, float b) { return a+b; }
32 double __subdf3(double a, double b) { return a-b; }
33 double __extendsfdf2(float a) {return a;}
34 #endif
35
36 #undef LOOP_TEST
37 #undef RX_DONT_PASS_UL
38 #undef DEBUG_EPROM
39 #undef DEBUG_RX_VERBOSE
40 #undef DUMMY_RX
41 #undef DEBUG_ZERO_RX
42 #undef DEBUG_RX_SKB
43 #undef DEBUG_TX_FRAG
44 #undef DEBUG_RX_FRAG
45 #undef DEBUG_TX_FILLDESC
46 #undef DEBUG_TX
47 #undef DEBUG_IRQ
48 #undef DEBUG_RX
49 #undef DEBUG_RXALLOC
50 #undef DEBUG_REGISTERS
51 #undef DEBUG_RING
52 #undef DEBUG_IRQ_TASKLET
53 #undef DEBUG_TX_ALLOC
54 #undef DEBUG_TX_DESC
55
56 //#define CONFIG_RTL8192_IO_MAP
57 #include <asm/uaccess.h>
58 #include "r8192E_hw.h"
59 #include "r8192E.h"
60 #include "r8190_rtl8256.h" /* RTL8225 Radio frontend */
61 #include "r8180_93cx6.h"   /* Card EEPROM */
62 #include "r8192E_wx.h"
63 #include "r819xE_phy.h" //added by WB 4.30.2008
64 #include "r819xE_phyreg.h"
65 #include "r819xE_cmdpkt.h"
66 #include "r8192E_dm.h"
67 //#include "r8192xU_phyreg.h"
68 //#include <linux/usb.h>
69 // FIXME: check if 2.6.7 is ok
70
71 #ifdef CONFIG_PM_RTL
72 #include "r8192_pm.h"
73 #endif
74
75 #ifdef ENABLE_DOT11D
76 #include "dot11d.h"
77 #endif
78
79 //set here to open your trace code. //WB
80 u32 rt_global_debug_component = \
81                 //              COMP_INIT       |
82                         //      COMP_EPROM      |
83                 //              COMP_PHY        |
84                 //              COMP_RF         |
85                                 COMP_FIRMWARE   |
86                         //      COMP_TRACE      |
87                 //              COMP_DOWN       |
88                 //              COMP_SWBW       |
89                 //              COMP_SEC        |
90 //                              COMP_QOS        |
91 //                              COMP_RATE       |
92                 //              COMP_RECV       |
93                 //              COMP_SEND       |
94                 //              COMP_POWER      |
95                         //      COMP_EVENTS     |
96                         //      COMP_RESET      |
97                         //      COMP_CMDPKT     |
98                         //      COMP_POWER_TRACKING     |
99                         //      COMP_INTR       |
100                                 COMP_ERR ; //always open err flags on
101 #ifndef PCI_DEVICE
102 #define PCI_DEVICE(vend,dev)\
103         .vendor=(vend),.device=(dev),\
104         .subvendor=PCI_ANY_ID,.subdevice=PCI_ANY_ID
105 #endif
106 static struct pci_device_id rtl8192_pci_id_tbl[] __devinitdata = {
107 #ifdef RTL8190P
108         /* Realtek */
109         /* Dlink */
110         { PCI_DEVICE(0x10ec, 0x8190) },
111         /* Corega */
112         { PCI_DEVICE(0x07aa, 0x0045) },
113         { PCI_DEVICE(0x07aa, 0x0046) },
114 #else
115         /* Realtek */
116         { PCI_DEVICE(0x10ec, 0x8192) },
117
118         /* Corega */
119         { PCI_DEVICE(0x07aa, 0x0044) },
120         { PCI_DEVICE(0x07aa, 0x0047) },
121 #endif
122         {}
123 };
124
125 static char* ifname = "wlan%d";
126 #if 0
127 static int hwseqnum = 0;
128 static int hwwep = 0;
129 #endif
130 static int hwwep = 1; //default use hw. set 0 to use software security
131 static int channels = 0x3fff;
132
133 MODULE_LICENSE("GPL");
134 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
135 MODULE_VERSION("V 1.1");
136 #endif
137 MODULE_DEVICE_TABLE(pci, rtl8192_pci_id_tbl);
138 //MODULE_AUTHOR("Andrea Merello <andreamrl@tiscali.it>");
139 MODULE_DESCRIPTION("Linux driver for Realtek RTL819x WiFi cards");
140
141 #if 0
142 MODULE_PARM(ifname,"s");
143 MODULE_PARM_DESC(devname," Net interface name, wlan%d=default");
144
145 MODULE_PARM(hwseqnum,"i");
146 MODULE_PARM_DESC(hwseqnum," Try to use hardware 802.11 header sequence numbers. Zero=default");
147
148 MODULE_PARM(hwwep,"i");
149 MODULE_PARM_DESC(hwwep," Try to use hardware WEP support. Still broken and not available on all cards");
150
151 MODULE_PARM(channels,"i");
152 MODULE_PARM_DESC(channels," Channel bitmask for specific locales. NYI");
153 #endif
154
155 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 9)
156 module_param(ifname, charp, S_IRUGO|S_IWUSR );
157 //module_param(hwseqnum,int, S_IRUGO|S_IWUSR);
158 module_param(hwwep,int, S_IRUGO|S_IWUSR);
159 module_param(channels,int, S_IRUGO|S_IWUSR);
160 #else
161 MODULE_PARM(ifname, "s");
162 //MODULE_PARM(hwseqnum,"i");
163 MODULE_PARM(hwwep,"i");
164 MODULE_PARM(channels,"i");
165 #endif
166
167 MODULE_PARM_DESC(ifname," Net interface name, wlan%d=default");
168 //MODULE_PARM_DESC(hwseqnum," Try to use hardware 802.11 header sequence numbers. Zero=default");
169 MODULE_PARM_DESC(hwwep," Try to use hardware WEP support. Still broken and not available on all cards");
170 MODULE_PARM_DESC(channels," Channel bitmask for specific locales. NYI");
171
172 static int __devinit rtl8192_pci_probe(struct pci_dev *pdev,
173                          const struct pci_device_id *id);
174 static void __devexit rtl8192_pci_disconnect(struct pci_dev *pdev);
175
176 static struct pci_driver rtl8192_pci_driver = {
177         .name           = RTL819xE_MODULE_NAME,           /* Driver name   */
178         .id_table       = rtl8192_pci_id_tbl,             /* PCI_ID table  */
179         .probe          = rtl8192_pci_probe,              /* probe fn      */
180         .remove         = __devexit_p(rtl8192_pci_disconnect),    /* remove fn     */
181 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)
182 #ifdef CONFIG_PM_RTL
183         .suspend        = rtl8192E_suspend,               /* PM suspend fn */
184         .resume         = rtl8192E_resume,                 /* PM resume fn  */
185 #else
186         .suspend        = NULL,                           /* PM suspend fn */
187         .resume         = NULL,                           /* PM resume fn  */
188 #endif
189 #endif
190 };
191
192 #ifdef ENABLE_DOT11D
193
194 typedef struct _CHANNEL_LIST
195 {
196         u8      Channel[32];
197         u8      Len;
198 }CHANNEL_LIST, *PCHANNEL_LIST;
199
200 static CHANNEL_LIST ChannelPlan[] = {
201         {{1,2,3,4,5,6,7,8,9,10,11,36,40,44,48,52,56,60,64,149,153,157,161,165},24},             //FCC
202         {{1,2,3,4,5,6,7,8,9,10,11},11},                                                 //IC
203         {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21},   //ETSI
204         {{1,2,3,4,5,6,7,8,9,10,11,12,13},13},    //Spain. Change to ETSI.
205         {{1,2,3,4,5,6,7,8,9,10,11,12,13},13},   //France. Change to ETSI.
206         {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22},        //MKK                                   //MKK
207         {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22},//MKK1
208         {{1,2,3,4,5,6,7,8,9,10,11,12,13},13},   //Israel.
209         {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22},                        // For 11a , TELEC
210         {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64}, 22},    //MIC
211         {{1,2,3,4,5,6,7,8,9,10,11,12,13,14},14}                                 //For Global Domain. 1-11:active scan, 12-14 passive scan. //+YJ, 080626
212 };
213
214 static void rtl819x_set_channel_map(u8 channel_plan, struct r8192_priv* priv)
215 {
216         int i, max_chan=-1, min_chan=-1;
217         struct ieee80211_device* ieee = priv->ieee80211;
218         switch (channel_plan)
219         {
220                 case COUNTRY_CODE_FCC:
221                 case COUNTRY_CODE_IC:
222                 case COUNTRY_CODE_ETSI:
223                 case COUNTRY_CODE_SPAIN:
224                 case COUNTRY_CODE_FRANCE:
225                 case COUNTRY_CODE_MKK:
226                 case COUNTRY_CODE_MKK1:
227                 case COUNTRY_CODE_ISRAEL:
228                 case COUNTRY_CODE_TELEC:
229                 case COUNTRY_CODE_MIC:
230                 {
231                         Dot11d_Init(ieee);
232                         ieee->bGlobalDomain = false;
233                         //acturally 8225 & 8256 rf chip only support B,G,24N mode
234                         if ((priv->rf_chip == RF_8225) || (priv->rf_chip == RF_8256))
235                         {
236                                 min_chan = 1;
237                                 max_chan = 14;
238                         }
239                         else
240                         {
241                                 RT_TRACE(COMP_ERR, "unknown rf chip, can't set channel map in function:%s()\n", __FUNCTION__);
242                         }
243                         if (ChannelPlan[channel_plan].Len != 0){
244                                 // Clear old channel map
245                                 memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
246                                 // Set new channel map
247                                 for (i=0;i<ChannelPlan[channel_plan].Len;i++)
248                                 {
249                                         if (ChannelPlan[channel_plan].Channel[i] < min_chan || ChannelPlan[channel_plan].Channel[i] > max_chan)
250                                             break;
251                                         GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
252                                 }
253                         }
254                         break;
255                 }
256                 case COUNTRY_CODE_GLOBAL_DOMAIN:
257                 {
258                         GET_DOT11D_INFO(ieee)->bEnabled = 0; //this flag enabled to follow 11d country IE setting, otherwise, it shall follow global domain setting
259                         Dot11d_Reset(ieee);
260                         ieee->bGlobalDomain = true;
261                         break;
262                 }
263                 default:
264                         break;
265         }
266 }
267 #endif
268
269
270 #define eqMacAddr(a,b) ( ((a)[0]==(b)[0] && (a)[1]==(b)[1] && (a)[2]==(b)[2] && (a)[3]==(b)[3] && (a)[4]==(b)[4] && (a)[5]==(b)[5]) ? 1:0 )
271 /* 2007/07/25 MH Defien temp tx fw info. */
272 static TX_FWINFO_T Tmp_TxFwInfo;
273
274
275 #define         rx_hal_is_cck_rate(_pdrvinfo)\
276                         (_pdrvinfo->RxRate == DESC90_RATE1M ||\
277                         _pdrvinfo->RxRate == DESC90_RATE2M ||\
278                         _pdrvinfo->RxRate == DESC90_RATE5_5M ||\
279                         _pdrvinfo->RxRate == DESC90_RATE11M) &&\
280                         !_pdrvinfo->RxHT\
281
282
283 void CamResetAllEntry(struct net_device *dev)
284 {
285         //u8 ucIndex;
286         u32 ulcommand = 0;
287
288 #if 1
289         ulcommand |= BIT31|BIT30;
290         write_nic_dword(dev, RWCAM, ulcommand);
291 #else
292         for(ucIndex=0;ucIndex<TOTAL_CAM_ENTRY;ucIndex++)
293                 CAM_mark_invalid(dev, ucIndex);
294         for(ucIndex=0;ucIndex<TOTAL_CAM_ENTRY;ucIndex++)
295                 CAM_empty_entry(dev, ucIndex);
296 #endif
297 }
298
299
300 void write_cam(struct net_device *dev, u8 addr, u32 data)
301 {
302         write_nic_dword(dev, WCAMI, data);
303         write_nic_dword(dev, RWCAM, BIT31|BIT16|(addr&0xff) );
304 }
305 u32 read_cam(struct net_device *dev, u8 addr)
306 {
307         write_nic_dword(dev, RWCAM, 0x80000000|(addr&0xff) );
308         return read_nic_dword(dev, 0xa8);
309 }
310
311 ////////////////////////////////////////////////////////////
312 #ifdef CONFIG_RTL8180_IO_MAP
313
314 u8 read_nic_byte(struct net_device *dev, int x)
315 {
316         return 0xff&inb(dev->base_addr +x);
317 }
318
319 u32 read_nic_dword(struct net_device *dev, int x)
320 {
321         return inl(dev->base_addr +x);
322 }
323
324 u16 read_nic_word(struct net_device *dev, int x)
325 {
326         return inw(dev->base_addr +x);
327 }
328
329 void write_nic_byte(struct net_device *dev, int x,u8 y)
330 {
331         outb(y&0xff,dev->base_addr +x);
332 }
333
334 void write_nic_word(struct net_device *dev, int x,u16 y)
335 {
336         outw(y,dev->base_addr +x);
337 }
338
339 void write_nic_dword(struct net_device *dev, int x,u32 y)
340 {
341         outl(y,dev->base_addr +x);
342 }
343
344 #else /* RTL_IO_MAP */
345
346 u8 read_nic_byte(struct net_device *dev, int x)
347 {
348         return 0xff&readb((u8*)dev->mem_start +x);
349 }
350
351 u32 read_nic_dword(struct net_device *dev, int x)
352 {
353         return readl((u8*)dev->mem_start +x);
354 }
355
356 u16 read_nic_word(struct net_device *dev, int x)
357 {
358         return readw((u8*)dev->mem_start +x);
359 }
360
361 void write_nic_byte(struct net_device *dev, int x,u8 y)
362 {
363         writeb(y,(u8*)dev->mem_start +x);
364         udelay(20);
365 }
366
367 void write_nic_dword(struct net_device *dev, int x,u32 y)
368 {
369         writel(y,(u8*)dev->mem_start +x);
370         udelay(20);
371 }
372
373 void write_nic_word(struct net_device *dev, int x,u16 y)
374 {
375         writew(y,(u8*)dev->mem_start +x);
376         udelay(20);
377 }
378
379 #endif /* RTL_IO_MAP */
380
381
382 ///////////////////////////////////////////////////////////
383
384 //u8 read_phy_cck(struct net_device *dev, u8 adr);
385 //u8 read_phy_ofdm(struct net_device *dev, u8 adr);
386 /* this might still called in what was the PHY rtl8185/rtl8192 common code
387  * plans are to possibilty turn it again in one common code...
388  */
389 inline void force_pci_posting(struct net_device *dev)
390 {
391 }
392
393
394 //warning message WB
395 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
396 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
397 void rtl8192_interrupt(int irq, void *netdev, struct pt_regs *regs);
398 #else
399 irqreturn_t rtl8192_interrupt(int irq, void *netdev, struct pt_regs *regs);
400 #endif
401 #else
402 irqreturn_t rtl8192_interrupt(int irq, void *netdev);
403 #endif
404 //static struct net_device_stats *rtl8192_stats(struct net_device *dev);
405 void rtl8192_commit(struct net_device *dev);
406 //void rtl8192_restart(struct net_device *dev);
407 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
408 void rtl8192_restart(struct work_struct *work);
409 //void rtl8192_rq_tx_ack(struct work_struct *work);
410 #else
411  void rtl8192_restart(struct net_device *dev);
412 // //void rtl8192_rq_tx_ack(struct net_device *dev);
413  #endif
414
415 void watch_dog_timer_callback(unsigned long data);
416 #ifdef ENABLE_IPS
417 void IPSEnter(struct net_device *dev);
418 void IPSLeave(struct net_device *dev);
419 void InactivePsWorkItemCallback(struct net_device *dev);
420 #endif
421 /****************************************************************************
422    -----------------------------PROCFS STUFF-------------------------
423 *****************************************************************************/
424
425 static struct proc_dir_entry *rtl8192_proc = NULL;
426
427
428
429 static int proc_get_stats_ap(char *page, char **start,
430                           off_t offset, int count,
431                           int *eof, void *data)
432 {
433         struct net_device *dev = data;
434         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
435         struct ieee80211_device *ieee = priv->ieee80211;
436         struct ieee80211_network *target;
437
438         int len = 0;
439
440         list_for_each_entry(target, &ieee->network_list, list) {
441
442                 len += snprintf(page + len, count - len,
443                 "%s ", target->ssid);
444
445                 if(target->wpa_ie_len>0 || target->rsn_ie_len>0){
446                         len += snprintf(page + len, count - len,
447                         "WPA\n");
448                 }
449                 else{
450                         len += snprintf(page + len, count - len,
451                         "non_WPA\n");
452                 }
453
454         }
455
456         *eof = 1;
457         return len;
458 }
459
460 static int proc_get_registers(char *page, char **start,
461                           off_t offset, int count,
462                           int *eof, void *data)
463 {
464         struct net_device *dev = data;
465 //      struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
466
467         int len = 0;
468         int i,n;
469
470         int max=0xff;
471
472         /* This dump the current register page */
473         len += snprintf(page + len, count - len,
474                         "\n####################page 0##################\n ");
475
476         for(n=0;n<=max;)
477         {
478                 //printk( "\nD: %2x> ", n);
479                 len += snprintf(page + len, count - len,
480                         "\nD:  %2x > ",n);
481
482                 for(i=0;i<16 && n<=max;i++,n++)
483                 len += snprintf(page + len, count - len,
484                         "%2x ",read_nic_byte(dev,n));
485
486                 //      printk("%2x ",read_nic_byte(dev,n));
487         }
488         len += snprintf(page + len, count - len,"\n");
489         len += snprintf(page + len, count - len,
490                         "\n####################page 1##################\n ");
491         for(n=0;n<=max;)
492         {
493                 //printk( "\nD: %2x> ", n);
494                 len += snprintf(page + len, count - len,
495                         "\nD:  %2x > ",n);
496
497                 for(i=0;i<16 && n<=max;i++,n++)
498                 len += snprintf(page + len, count - len,
499                         "%2x ",read_nic_byte(dev,0x100|n));
500
501                 //      printk("%2x ",read_nic_byte(dev,n));
502         }
503
504         len += snprintf(page + len, count - len,
505                         "\n####################page 3##################\n ");
506         for(n=0;n<=max;)
507         {
508                 //printk( "\nD: %2x> ", n);
509                 len += snprintf(page + len, count - len,
510                         "\nD:  %2x > ",n);
511
512                 for(i=0;i<16 && n<=max;i++,n++)
513                 len += snprintf(page + len, count - len,
514                         "%2x ",read_nic_byte(dev,0x300|n));
515
516                 //      printk("%2x ",read_nic_byte(dev,n));
517         }
518
519
520         *eof = 1;
521         return len;
522
523 }
524
525
526 #if 0
527 static int proc_get_cck_reg(char *page, char **start,
528                           off_t offset, int count,
529                           int *eof, void *data)
530 {
531         struct net_device *dev = data;
532 //      struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
533
534         int len = 0;
535         int i,n;
536
537         int max = 0x5F;
538
539         /* This dump the current register page */
540         for(n=0;n<=max;)
541         {
542                 //printk( "\nD: %2x> ", n);
543                 len += snprintf(page + len, count - len,
544                         "\nD:  %2x > ",n);
545
546                 for(i=0;i<16 && n<=max;i++,n++)
547                 len += snprintf(page + len, count - len,
548                         "%2x ",read_phy_cck(dev,n));
549
550                 //      printk("%2x ",read_nic_byte(dev,n));
551         }
552         len += snprintf(page + len, count - len,"\n");
553
554
555         *eof = 1;
556         return len;
557 }
558
559 #endif
560
561 #if 0
562 static int proc_get_ofdm_reg(char *page, char **start,
563                           off_t offset, int count,
564                           int *eof, void *data)
565 {
566
567         struct net_device *dev = data;
568 //      struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
569
570         int len = 0;
571         int i,n;
572
573         //int max=0xff;
574         int max = 0x40;
575
576         /* This dump the current register page */
577         for(n=0;n<=max;)
578         {
579                 //printk( "\nD: %2x> ", n);
580                 len += snprintf(page + len, count - len,
581                         "\nD:  %2x > ",n);
582
583                 for(i=0;i<16 && n<=max;i++,n++)
584                 len += snprintf(page + len, count - len,
585                         "%2x ",read_phy_ofdm(dev,n));
586
587                 //      printk("%2x ",read_nic_byte(dev,n));
588         }
589         len += snprintf(page + len, count - len,"\n");
590
591
592
593         *eof = 1;
594         return len;
595 }
596
597 #endif
598
599 #if 0
600 static int proc_get_stats_hw(char *page, char **start,
601                           off_t offset, int count,
602                           int *eof, void *data)
603 {
604         struct net_device *dev = data;
605         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
606
607         int len = 0;
608
609         len += snprintf(page + len, count - len,
610                 "NIC int: %lu\n"
611                 "Total int: %lu\n",
612                 priv->stats.ints,
613                 priv->stats.shints);
614
615         *eof = 1;
616         return len;
617 }
618 #endif
619
620 static int proc_get_stats_tx(char *page, char **start,
621                           off_t offset, int count,
622                           int *eof, void *data)
623 {
624         struct net_device *dev = data;
625         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
626
627         int len = 0;
628
629         len += snprintf(page + len, count - len,
630                 "TX VI priority ok int: %lu\n"
631 //              "TX VI priority error int: %lu\n"
632                 "TX VO priority ok int: %lu\n"
633 //              "TX VO priority error int: %lu\n"
634                 "TX BE priority ok int: %lu\n"
635 //              "TX BE priority error int: %lu\n"
636                 "TX BK priority ok int: %lu\n"
637 //              "TX BK priority error int: %lu\n"
638                 "TX MANAGE priority ok int: %lu\n"
639 //              "TX MANAGE priority error int: %lu\n"
640                 "TX BEACON priority ok int: %lu\n"
641                 "TX BEACON priority error int: %lu\n"
642                 "TX CMDPKT priority ok int: %lu\n"
643 //              "TX high priority ok int: %lu\n"
644 //              "TX high priority failed error int: %lu\n"
645 //              "TX queue resume: %lu\n"
646                 "TX queue stopped?: %d\n"
647                 "TX fifo overflow: %lu\n"
648 //              "TX beacon: %lu\n"
649 //              "TX VI queue: %d\n"
650 //              "TX VO queue: %d\n"
651 //              "TX BE queue: %d\n"
652 //              "TX BK queue: %d\n"
653 //              "TX HW queue: %d\n"
654 //              "TX VI dropped: %lu\n"
655 //              "TX VO dropped: %lu\n"
656 //              "TX BE dropped: %lu\n"
657 //              "TX BK dropped: %lu\n"
658                 "TX total data packets %lu\n"
659                 "TX total data bytes :%lu\n",
660 //              "TX beacon aborted: %lu\n",
661                 priv->stats.txviokint,
662 //              priv->stats.txvierr,
663                 priv->stats.txvookint,
664 //              priv->stats.txvoerr,
665                 priv->stats.txbeokint,
666 //              priv->stats.txbeerr,
667                 priv->stats.txbkokint,
668 //              priv->stats.txbkerr,
669                 priv->stats.txmanageokint,
670 //              priv->stats.txmanageerr,
671                 priv->stats.txbeaconokint,
672                 priv->stats.txbeaconerr,
673                 priv->stats.txcmdpktokint,
674 //              priv->stats.txhpokint,
675 //              priv->stats.txhperr,
676 //              priv->stats.txresumed,
677                 netif_queue_stopped(dev),
678                 priv->stats.txoverflow,
679 //              priv->stats.txbeacon,
680 //              atomic_read(&(priv->tx_pending[VI_QUEUE])),
681 //              atomic_read(&(priv->tx_pending[VO_QUEUE])),
682 //              atomic_read(&(priv->tx_pending[BE_QUEUE])),
683 //              atomic_read(&(priv->tx_pending[BK_QUEUE])),
684 //              read_nic_byte(dev, TXFIFOCOUNT),
685 //              priv->stats.txvidrop,
686 //              priv->stats.txvodrop,
687                 priv->ieee80211->stats.tx_packets,
688                 priv->ieee80211->stats.tx_bytes
689
690
691 //              priv->stats.txbedrop,
692 //              priv->stats.txbkdrop
693                         //      priv->stats.txdatapkt
694 //              priv->stats.txbeaconerr
695                 );
696
697         *eof = 1;
698         return len;
699 }
700
701
702
703 static int proc_get_stats_rx(char *page, char **start,
704                           off_t offset, int count,
705                           int *eof, void *data)
706 {
707         struct net_device *dev = data;
708         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
709
710         int len = 0;
711
712         len += snprintf(page + len, count - len,
713                 "RX packets: %lu\n"
714                 "RX desc err: %lu\n"
715                 "RX rx overflow error: %lu\n"
716                 "RX invalid urb error: %lu\n",
717                 priv->stats.rxint,
718                 priv->stats.rxrdu,
719                 priv->stats.rxoverflow,
720                 priv->stats.rxurberr);
721
722         *eof = 1;
723         return len;
724 }
725
726 static void rtl8192_proc_module_init(void)
727 {
728         RT_TRACE(COMP_INIT, "Initializing proc filesystem");
729 #if(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
730         rtl8192_proc=create_proc_entry(RTL819xE_MODULE_NAME, S_IFDIR, proc_net);
731 #else
732         rtl8192_proc=create_proc_entry(RTL819xE_MODULE_NAME, S_IFDIR, init_net.proc_net);
733 #endif
734 }
735
736
737 static void rtl8192_proc_module_remove(void)
738 {
739 #if(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
740         remove_proc_entry(RTL819xE_MODULE_NAME, proc_net);
741 #else
742         remove_proc_entry(RTL819xE_MODULE_NAME, init_net.proc_net);
743 #endif
744 }
745
746
747 static void rtl8192_proc_remove_one(struct net_device *dev)
748 {
749         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
750
751         printk("dev name=======> %s\n",dev->name);
752
753         if (priv->dir_dev) {
754         //      remove_proc_entry("stats-hw", priv->dir_dev);
755                 remove_proc_entry("stats-tx", priv->dir_dev);
756                 remove_proc_entry("stats-rx", priv->dir_dev);
757         //      remove_proc_entry("stats-ieee", priv->dir_dev);
758                 remove_proc_entry("stats-ap", priv->dir_dev);
759                 remove_proc_entry("registers", priv->dir_dev);
760         //      remove_proc_entry("cck-registers",priv->dir_dev);
761         //      remove_proc_entry("ofdm-registers",priv->dir_dev);
762                 //remove_proc_entry(dev->name, rtl8192_proc);
763                 remove_proc_entry("wlan0", rtl8192_proc);
764                 priv->dir_dev = NULL;
765         }
766 }
767
768
769 static void rtl8192_proc_init_one(struct net_device *dev)
770 {
771         struct proc_dir_entry *e;
772         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
773         priv->dir_dev = create_proc_entry(dev->name,
774                                           S_IFDIR | S_IRUGO | S_IXUGO,
775                                           rtl8192_proc);
776         if (!priv->dir_dev) {
777                 RT_TRACE(COMP_ERR, "Unable to initialize /proc/net/rtl8192/%s\n",
778                       dev->name);
779                 return;
780         }
781         #if 0
782         e = create_proc_read_entry("stats-hw", S_IFREG | S_IRUGO,
783                                    priv->dir_dev, proc_get_stats_hw, dev);
784
785         if (!e) {
786                 DMESGE("Unable to initialize "
787                       "/proc/net/rtl8192/%s/stats-hw\n",
788                       dev->name);
789         }
790         #endif
791         e = create_proc_read_entry("stats-rx", S_IFREG | S_IRUGO,
792                                    priv->dir_dev, proc_get_stats_rx, dev);
793
794         if (!e) {
795                 RT_TRACE(COMP_ERR,"Unable to initialize "
796                       "/proc/net/rtl8192/%s/stats-rx\n",
797                       dev->name);
798         }
799
800
801         e = create_proc_read_entry("stats-tx", S_IFREG | S_IRUGO,
802                                    priv->dir_dev, proc_get_stats_tx, dev);
803
804         if (!e) {
805                 RT_TRACE(COMP_ERR, "Unable to initialize "
806                       "/proc/net/rtl8192/%s/stats-tx\n",
807                       dev->name);
808         }
809         #if 0
810         e = create_proc_read_entry("stats-ieee", S_IFREG | S_IRUGO,
811                                    priv->dir_dev, proc_get_stats_ieee, dev);
812
813         if (!e) {
814                 DMESGE("Unable to initialize "
815                       "/proc/net/rtl8192/%s/stats-ieee\n",
816                       dev->name);
817         }
818
819         #endif
820
821         e = create_proc_read_entry("stats-ap", S_IFREG | S_IRUGO,
822                                    priv->dir_dev, proc_get_stats_ap, dev);
823
824         if (!e) {
825                 RT_TRACE(COMP_ERR, "Unable to initialize "
826                       "/proc/net/rtl8192/%s/stats-ap\n",
827                       dev->name);
828         }
829
830         e = create_proc_read_entry("registers", S_IFREG | S_IRUGO,
831                                    priv->dir_dev, proc_get_registers, dev);
832         if (!e) {
833                 RT_TRACE(COMP_ERR, "Unable to initialize "
834                       "/proc/net/rtl8192/%s/registers\n",
835                       dev->name);
836         }
837 #if 0
838         e = create_proc_read_entry("cck-registers", S_IFREG | S_IRUGO,
839                                    priv->dir_dev, proc_get_cck_reg, dev);
840         if (!e) {
841                 RT_TRACE(COMP_ERR, "Unable to initialize "
842                       "/proc/net/rtl8192/%s/cck-registers\n",
843                       dev->name);
844         }
845
846         e = create_proc_read_entry("ofdm-registers", S_IFREG | S_IRUGO,
847                                    priv->dir_dev, proc_get_ofdm_reg, dev);
848         if (!e) {
849                 RT_TRACE(COMP_ERR, "Unable to initialize "
850                       "/proc/net/rtl8192/%s/ofdm-registers\n",
851                       dev->name);
852         }
853 #endif
854 }
855 /****************************************************************************
856    -----------------------------MISC STUFF-------------------------
857 *****************************************************************************/
858
859 /* this is only for debugging */
860 static void print_buffer(u32 *buffer, int len)
861 {
862         int i;
863         u8 *buf =(u8*)buffer;
864
865         printk("ASCII BUFFER DUMP (len: %x):\n",len);
866
867         for(i=0;i<len;i++)
868                 printk("%c",buf[i]);
869
870         printk("\nBINARY BUFFER DUMP (len: %x):\n",len);
871
872         for(i=0;i<len;i++)
873                 printk("%x",buf[i]);
874
875         printk("\n");
876 }
877
878 short check_nic_enough_desc(struct net_device *dev, int prio)
879 {
880     struct r8192_priv *priv = ieee80211_priv(dev);
881     struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
882
883     /* for now we reserve two free descriptor as a safety boundary
884      * between the tail and the head
885      */
886     if (ring->entries - skb_queue_len(&ring->queue) >= 2) {
887         return 1;
888     } else {
889         return 0;
890     }
891 }
892
893 static void tx_timeout(struct net_device *dev)
894 {
895         struct r8192_priv *priv = ieee80211_priv(dev);
896         //rtl8192_commit(dev);
897
898 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
899         schedule_work(&priv->reset_wq);
900 #else
901         schedule_task(&priv->reset_wq);
902 #endif
903         printk("TXTIMEOUT");
904 }
905
906
907 /* this is only for debug */
908 static void dump_eprom(struct net_device *dev)
909 {
910         int i;
911         for(i=0; i<0xff; i++)
912                 RT_TRACE(COMP_INIT, "EEPROM addr %x : %x", i, eprom_read(dev,i));
913 }
914
915 /* this is only for debug */
916 static void rtl8192_dump_reg(struct net_device *dev)
917 {
918         int i;
919         int n;
920         int max=0x5ff;
921
922         RT_TRACE(COMP_INIT, "Dumping NIC register map");
923
924         for(n=0;n<=max;)
925         {
926                 printk( "\nD: %2x> ", n);
927                 for(i=0;i<16 && n<=max;i++,n++)
928                         printk("%2x ",read_nic_byte(dev,n));
929         }
930         printk("\n");
931 }
932
933 /****************************************************************************
934       ------------------------------HW STUFF---------------------------
935 *****************************************************************************/
936
937
938 static void rtl8192_irq_enable(struct net_device *dev)
939 {
940         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
941         priv->irq_enabled = 1;
942         write_nic_dword(dev,INTA_MASK, priv->irq_mask);
943 }
944
945
946 static void rtl8192_irq_disable(struct net_device *dev)
947 {
948         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
949
950         write_nic_dword(dev,INTA_MASK,0);
951         force_pci_posting(dev);
952         priv->irq_enabled = 0;
953 }
954
955
956 static void rtl8192_set_mode(struct net_device *dev,int mode)
957 {
958         u8 ecmd;
959         ecmd=read_nic_byte(dev, EPROM_CMD);
960         ecmd=ecmd &~ EPROM_CMD_OPERATING_MODE_MASK;
961         ecmd=ecmd | (mode<<EPROM_CMD_OPERATING_MODE_SHIFT);
962         ecmd=ecmd &~ (1<<EPROM_CS_SHIFT);
963         ecmd=ecmd &~ (1<<EPROM_CK_SHIFT);
964         write_nic_byte(dev, EPROM_CMD, ecmd);
965 }
966
967
968 void rtl8192_update_msr(struct net_device *dev)
969 {
970         struct r8192_priv *priv = ieee80211_priv(dev);
971         u8 msr;
972
973         msr  = read_nic_byte(dev, MSR);
974         msr &= ~ MSR_LINK_MASK;
975
976         /* do not change in link_state != WLAN_LINK_ASSOCIATED.
977          * msr must be updated if the state is ASSOCIATING.
978          * this is intentional and make sense for ad-hoc and
979          * master (see the create BSS/IBSS func)
980          */
981         if (priv->ieee80211->state == IEEE80211_LINKED){
982
983                 if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
984                         msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
985                 else if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
986                         msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
987                 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
988                         msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
989
990         }else
991                 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
992
993         write_nic_byte(dev, MSR, msr);
994 }
995
996 void rtl8192_set_chan(struct net_device *dev,short ch)
997 {
998     struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
999     RT_TRACE(COMP_RF, "=====>%s()====ch:%d\n", __FUNCTION__, ch);
1000     priv->chan=ch;
1001 #if 0
1002     if(priv->ieee80211->iw_mode == IW_MODE_ADHOC ||
1003             priv->ieee80211->iw_mode == IW_MODE_MASTER){
1004
1005         priv->ieee80211->link_state = WLAN_LINK_ASSOCIATED;
1006         priv->ieee80211->master_chan = ch;
1007         rtl8192_update_beacon_ch(dev);
1008     }
1009 #endif
1010
1011     /* this hack should avoid frame TX during channel setting*/
1012
1013
1014     //  tx = read_nic_dword(dev,TX_CONF);
1015     //  tx &= ~TX_LOOPBACK_MASK;
1016
1017 #ifndef LOOP_TEST
1018     //TODO
1019     //  write_nic_dword(dev,TX_CONF, tx |( TX_LOOPBACK_MAC<<TX_LOOPBACK_SHIFT));
1020
1021     //need to implement rf set channel here WB
1022
1023     if (priv->rf_set_chan)
1024         priv->rf_set_chan(dev,priv->chan);
1025     //  mdelay(10);
1026     //  write_nic_dword(dev,TX_CONF,tx | (TX_LOOPBACK_NONE<<TX_LOOPBACK_SHIFT));
1027 #endif
1028 }
1029
1030 void rtl8192_rx_enable(struct net_device *dev)
1031 {
1032     struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1033     write_nic_dword(dev, RDQDA,priv->rx_ring_dma);
1034 }
1035
1036 /* the TX_DESC_BASE setting is according to the following queue index
1037  *  BK_QUEUE       ===>                        0
1038  *  BE_QUEUE       ===>                        1
1039  *  VI_QUEUE       ===>                        2
1040  *  VO_QUEUE       ===>                        3
1041  *  HCCA_QUEUE     ===>                        4
1042  *  TXCMD_QUEUE    ===>                        5
1043  *  MGNT_QUEUE     ===>                        6
1044  *  HIGH_QUEUE     ===>                        7
1045  *  BEACON_QUEUE   ===>                        8
1046  *  */
1047 static u32 TX_DESC_BASE[] = {BKQDA, BEQDA, VIQDA, VOQDA, HCCAQDA, CQDA, MQDA, HQDA, BQDA};
1048 void rtl8192_tx_enable(struct net_device *dev)
1049 {
1050     struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1051     u32 i;
1052     for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
1053         write_nic_dword(dev, TX_DESC_BASE[i], priv->tx_ring[i].dma);
1054
1055     ieee80211_reset_queue(priv->ieee80211);
1056 }
1057
1058 #if 0
1059 void rtl8192_beacon_tx_enable(struct net_device *dev)
1060 {
1061         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1062         u32 reg;
1063
1064         reg = read_nic_dword(priv->ieee80211->dev,INTA_MASK);
1065
1066         /* enable Beacon realted interrupt signal */
1067         reg |= (IMR_BcnInt | IMR_BcnInt | IMR_TBDOK | IMR_TBDER);
1068         write_nic_byte(dev,reg);
1069 }
1070 #endif
1071
1072 static void rtl8192_free_rx_ring(struct net_device *dev)
1073 {
1074     struct r8192_priv *priv = ieee80211_priv(dev);
1075     int i;
1076
1077     for (i = 0; i < priv->rxringcount; i++) {
1078         struct sk_buff *skb = priv->rx_buf[i];
1079         if (!skb)
1080             continue;
1081
1082         pci_unmap_single(priv->pdev,
1083                 *((dma_addr_t *)skb->cb),
1084                 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
1085         kfree_skb(skb);
1086     }
1087
1088     pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * priv->rxringcount,
1089             priv->rx_ring, priv->rx_ring_dma);
1090     priv->rx_ring = NULL;
1091 }
1092
1093 static void rtl8192_free_tx_ring(struct net_device *dev, unsigned int prio)
1094 {
1095     struct r8192_priv *priv = ieee80211_priv(dev);
1096     struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1097
1098     while (skb_queue_len(&ring->queue)) {
1099         tx_desc_819x_pci *entry = &ring->desc[ring->idx];
1100         struct sk_buff *skb = __skb_dequeue(&ring->queue);
1101
1102         pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
1103                 skb->len, PCI_DMA_TODEVICE);
1104         kfree_skb(skb);
1105         ring->idx = (ring->idx + 1) % ring->entries;
1106     }
1107
1108     pci_free_consistent(priv->pdev, sizeof(*ring->desc)*ring->entries,
1109             ring->desc, ring->dma);
1110     ring->desc = NULL;
1111 }
1112
1113
1114 static void rtl8192_beacon_disable(struct net_device *dev)
1115 {
1116         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1117         u32 reg;
1118
1119         reg = read_nic_dword(priv->ieee80211->dev,INTA_MASK);
1120
1121         /* disable Beacon realted interrupt signal */
1122         reg &= ~(IMR_BcnInt | IMR_BcnInt | IMR_TBDOK | IMR_TBDER);
1123         write_nic_dword(priv->ieee80211->dev, INTA_MASK, reg);
1124 }
1125
1126 void rtl8192_rtx_disable(struct net_device *dev)
1127 {
1128         u8 cmd;
1129         struct r8192_priv *priv = ieee80211_priv(dev);
1130         int i;
1131
1132         cmd=read_nic_byte(dev,CMDR);
1133 //      if(!priv->ieee80211->bSupportRemoteWakeUp) {
1134                 write_nic_byte(dev, CMDR, cmd &~ \
1135                                 (CR_TE|CR_RE));
1136 //      }
1137         force_pci_posting(dev);
1138         mdelay(30);
1139
1140         for(i = 0; i < MAX_QUEUE_SIZE; i++) {
1141                 skb_queue_purge(&priv->ieee80211->skb_waitQ [i]);
1142         }
1143         for(i = 0; i < MAX_QUEUE_SIZE; i++) {
1144                 skb_queue_purge(&priv->ieee80211->skb_aggQ [i]);
1145         }
1146
1147
1148         skb_queue_purge(&priv->skb_queue);
1149         return;
1150 }
1151
1152 static void rtl8192_reset(struct net_device *dev)
1153 {
1154     rtl8192_irq_disable(dev);
1155     printk("This is RTL819xP Reset procedure\n");
1156 }
1157
1158 static u16 rtl_rate[] = {10,20,55,110,60,90,120,180,240,360,480,540};
1159 inline u16 rtl8192_rate2rate(short rate)
1160 {
1161         if (rate >11) return 0;
1162         return rtl_rate[rate];
1163 }
1164
1165
1166
1167 static u32 rtl819xusb_rx_command_packet(struct net_device *dev, struct ieee80211_rx_stats *pstats)
1168 {
1169         u32     status;
1170
1171         //RT_TRACE(COMP_RECV, DBG_TRACE, ("---> RxCommandPacketHandle819xUsb()\n"));
1172
1173         RT_TRACE(COMP_EVENTS, "---->rtl819xusb_rx_command_packet()\n");
1174         status = cmpk_message_handle_rx(dev, pstats);
1175         if (status)
1176         {
1177                 DMESG("rxcommandpackethandle819xusb: It is a command packet\n");
1178         }
1179         else
1180         {
1181                 //RT_TRACE(COMP_RECV, DBG_TRACE, ("RxCommandPacketHandle819xUsb: It is not a command packet\n"));
1182         }
1183
1184         //RT_TRACE(COMP_RECV, DBG_TRACE, ("<--- RxCommandPacketHandle819xUsb()\n"));
1185         return status;
1186 }
1187
1188 #if 0
1189 void rtl8192_tx_queues_stop(struct net_device *dev)
1190 {
1191         //struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1192         u8 dma_poll_mask = (1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
1193         dma_poll_mask |= (1<<TX_DMA_STOP_HIPRIORITY_SHIFT);
1194         dma_poll_mask |= (1<<TX_DMA_STOP_NORMPRIORITY_SHIFT);
1195         dma_poll_mask |= (1<<TX_DMA_STOP_BEACON_SHIFT);
1196
1197         rtl8192_set_mode(dev,EPROM_CMD_CONFIG);
1198         write_nic_byte(dev,TX_DMA_POLLING,dma_poll_mask);
1199         rtl8192_set_mode(dev,EPROM_CMD_NORMAL);
1200 }
1201 #endif
1202
1203 static void rtl8192_data_hard_stop(struct net_device *dev)
1204 {
1205         //FIXME !!
1206         #if 0
1207         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1208         priv->dma_poll_mask |= (1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
1209         rtl8192_set_mode(dev,EPROM_CMD_CONFIG);
1210         write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
1211         rtl8192_set_mode(dev,EPROM_CMD_NORMAL);
1212         #endif
1213 }
1214
1215
1216 static void rtl8192_data_hard_resume(struct net_device *dev)
1217 {
1218         // FIXME !!
1219         #if 0
1220         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1221         priv->dma_poll_mask &= ~(1<<TX_DMA_STOP_LOWPRIORITY_SHIFT);
1222         rtl8192_set_mode(dev,EPROM_CMD_CONFIG);
1223         write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
1224         rtl8192_set_mode(dev,EPROM_CMD_NORMAL);
1225         #endif
1226 }
1227
1228 /* this function TX data frames when the ieee80211 stack requires this.
1229  * It checks also if we need to stop the ieee tx queue, eventually do it
1230  */
1231 static void rtl8192_hard_data_xmit(struct sk_buff *skb, struct net_device *dev, int rate)
1232 {
1233         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1234         int ret;
1235         //unsigned long flags;
1236         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1237         u8 queue_index = tcb_desc->queue_index;
1238         /* shall not be referred by command packet */
1239         assert(queue_index != TXCMD_QUEUE);
1240
1241         //spin_lock_irqsave(&priv->tx_lock,flags);
1242
1243         memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev));
1244 #if 0
1245         tcb_desc->RATRIndex = 7;
1246         tcb_desc->bTxDisableRateFallBack = 1;
1247         tcb_desc->bTxUseDriverAssingedRate = 1;
1248         tcb_desc->bTxEnableFwCalcDur = 1;
1249 #endif
1250         skb_push(skb, priv->ieee80211->tx_headroom);
1251         ret = rtl8192_tx(dev, skb);
1252         if(ret != 0) {
1253                 kfree_skb(skb);
1254         };
1255
1256 //
1257         if(queue_index!=MGNT_QUEUE) {
1258         priv->ieee80211->stats.tx_bytes+=(skb->len - priv->ieee80211->tx_headroom);
1259         priv->ieee80211->stats.tx_packets++;
1260         }
1261
1262         //spin_unlock_irqrestore(&priv->tx_lock,flags);
1263
1264 //      return ret;
1265         return;
1266 }
1267
1268 /* This is a rough attempt to TX a frame
1269  * This is called by the ieee 80211 stack to TX management frames.
1270  * If the ring is full packet are dropped (for data frame the queue
1271  * is stopped before this can happen).
1272  */
1273 static int rtl8192_hard_start_xmit(struct sk_buff *skb,struct net_device *dev)
1274 {
1275         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1276
1277
1278         int ret;
1279         //unsigned long flags;
1280         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1281         u8 queue_index = tcb_desc->queue_index;
1282
1283
1284         //spin_lock_irqsave(&priv->tx_lock,flags);
1285
1286         memcpy((unsigned char *)(skb->cb),&dev,sizeof(dev));
1287         if(queue_index == TXCMD_QUEUE) {
1288         //      skb_push(skb, USB_HWDESC_HEADER_LEN);
1289                 rtl819xE_tx_cmd(dev, skb);
1290                 ret = 0;
1291                 //spin_unlock_irqrestore(&priv->tx_lock,flags);
1292                 return ret;
1293         } else {
1294         //      RT_TRACE(COMP_SEND, "To send management packet\n");
1295                 tcb_desc->RATRIndex = 7;
1296                 tcb_desc->bTxDisableRateFallBack = 1;
1297                 tcb_desc->bTxUseDriverAssingedRate = 1;
1298                 tcb_desc->bTxEnableFwCalcDur = 1;
1299                 skb_push(skb, priv->ieee80211->tx_headroom);
1300                 ret = rtl8192_tx(dev, skb);
1301                 if(ret != 0) {
1302                         kfree_skb(skb);
1303                 };
1304         }
1305
1306 //      priv->ieee80211->stats.tx_bytes+=skb->len;
1307 //      priv->ieee80211->stats.tx_packets++;
1308
1309         //spin_unlock_irqrestore(&priv->tx_lock,flags);
1310
1311         return ret;
1312
1313 }
1314
1315
1316 void rtl8192_try_wake_queue(struct net_device *dev, int pri);
1317
1318 static void rtl8192_tx_isr(struct net_device *dev, int prio)
1319 {
1320     struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1321
1322     struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1323
1324     while (skb_queue_len(&ring->queue)) {
1325         tx_desc_819x_pci *entry = &ring->desc[ring->idx];
1326         struct sk_buff *skb;
1327
1328         /* beacon packet will only use the first descriptor defautly,
1329          * and the OWN may not be cleared by the hardware
1330          * */
1331         if(prio != BEACON_QUEUE) {
1332             if(entry->OWN)
1333                 return;
1334             ring->idx = (ring->idx + 1) % ring->entries;
1335         }
1336
1337         skb = __skb_dequeue(&ring->queue);
1338         pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
1339                 skb->len, PCI_DMA_TODEVICE);
1340
1341         kfree_skb(skb);
1342     }
1343     if (prio == MGNT_QUEUE){
1344         if (priv->ieee80211->ack_tx_to_ieee){
1345             if (rtl8192_is_tx_queue_empty(dev)){
1346                 priv->ieee80211->ack_tx_to_ieee = 0;
1347                 ieee80211_ps_tx_ack(priv->ieee80211, 1);
1348             }
1349         }
1350     }
1351
1352     if(prio != BEACON_QUEUE) {
1353         /* try to deal with the pending packets  */
1354         tasklet_schedule(&priv->irq_tx_tasklet);
1355     }
1356
1357 }
1358
1359 static void rtl8192_stop_beacon(struct net_device *dev)
1360 {
1361         //rtl8192_beacon_disable(dev);
1362 }
1363
1364 static void rtl8192_config_rate(struct net_device* dev, u16* rate_config)
1365 {
1366          struct r8192_priv *priv = ieee80211_priv(dev);
1367          struct ieee80211_network *net;
1368          u8 i=0, basic_rate = 0;
1369          net = & priv->ieee80211->current_network;
1370
1371          for (i=0; i<net->rates_len; i++)
1372          {
1373                  basic_rate = net->rates[i]&0x7f;
1374                  switch(basic_rate)
1375                  {
1376                          case MGN_1M:   *rate_config |= RRSR_1M;        break;
1377                          case MGN_2M:   *rate_config |= RRSR_2M;        break;
1378                          case MGN_5_5M: *rate_config |= RRSR_5_5M;      break;
1379                          case MGN_11M:  *rate_config |= RRSR_11M;       break;
1380                          case MGN_6M:   *rate_config |= RRSR_6M;        break;
1381                          case MGN_9M:   *rate_config |= RRSR_9M;        break;
1382                          case MGN_12M:  *rate_config |= RRSR_12M;       break;
1383                          case MGN_18M:  *rate_config |= RRSR_18M;       break;
1384                          case MGN_24M:  *rate_config |= RRSR_24M;       break;
1385                          case MGN_36M:  *rate_config |= RRSR_36M;       break;
1386                          case MGN_48M:  *rate_config |= RRSR_48M;       break;
1387                          case MGN_54M:  *rate_config |= RRSR_54M;       break;
1388                  }
1389          }
1390          for (i=0; i<net->rates_ex_len; i++)
1391          {
1392                  basic_rate = net->rates_ex[i]&0x7f;
1393                  switch(basic_rate)
1394                  {
1395                          case MGN_1M:   *rate_config |= RRSR_1M;        break;
1396                          case MGN_2M:   *rate_config |= RRSR_2M;        break;
1397                          case MGN_5_5M: *rate_config |= RRSR_5_5M;      break;
1398                          case MGN_11M:  *rate_config |= RRSR_11M;       break;
1399                          case MGN_6M:   *rate_config |= RRSR_6M;        break;
1400                          case MGN_9M:   *rate_config |= RRSR_9M;        break;
1401                          case MGN_12M:  *rate_config |= RRSR_12M;       break;
1402                          case MGN_18M:  *rate_config |= RRSR_18M;       break;
1403                          case MGN_24M:  *rate_config |= RRSR_24M;       break;
1404                          case MGN_36M:  *rate_config |= RRSR_36M;       break;
1405                          case MGN_48M:  *rate_config |= RRSR_48M;       break;
1406                          case MGN_54M:  *rate_config |= RRSR_54M;       break;
1407                  }
1408          }
1409 }
1410
1411
1412 #define SHORT_SLOT_TIME 9
1413 #define NON_SHORT_SLOT_TIME 20
1414
1415 static void rtl8192_update_cap(struct net_device* dev, u16 cap)
1416 {
1417         u32 tmp = 0;
1418         struct r8192_priv *priv = ieee80211_priv(dev);
1419         struct ieee80211_network *net = &priv->ieee80211->current_network;
1420         priv->short_preamble = cap & WLAN_CAPABILITY_SHORT_PREAMBLE;
1421         tmp = priv->basic_rate;
1422         if (priv->short_preamble)
1423                 tmp |= BRSR_AckShortPmb;
1424         write_nic_dword(dev, RRSR, tmp);
1425
1426         if (net->mode & (IEEE_G|IEEE_N_24G))
1427         {
1428                 u8 slot_time = 0;
1429                 if ((cap & WLAN_CAPABILITY_SHORT_SLOT)&&(!priv->ieee80211->pHTInfo->bCurrentRT2RTLongSlotTime))
1430                 {//short slot time
1431                         slot_time = SHORT_SLOT_TIME;
1432                 }
1433                 else //long slot time
1434                         slot_time = NON_SHORT_SLOT_TIME;
1435                 priv->slot_time = slot_time;
1436                 write_nic_byte(dev, SLOT_TIME, slot_time);
1437         }
1438
1439 }
1440
1441 static void rtl8192_net_update(struct net_device *dev)
1442 {
1443
1444         struct r8192_priv *priv = ieee80211_priv(dev);
1445         struct ieee80211_network *net;
1446         u16 BcnTimeCfg = 0, BcnCW = 6, BcnIFS = 0xf;
1447         u16 rate_config = 0;
1448         net = &priv->ieee80211->current_network;
1449         //update Basic rate: RR, BRSR
1450         rtl8192_config_rate(dev, &rate_config);
1451         // 2007.01.16, by Emily
1452         // Select RRSR (in Legacy-OFDM and CCK)
1453         // For 8190, we select only 24M, 12M, 6M, 11M, 5.5M, 2M, and 1M from the Basic rate.
1454         // We do not use other rates.
1455          priv->basic_rate = rate_config &= 0x15f;
1456         //BSSID
1457         write_nic_dword(dev,BSSIDR,((u32*)net->bssid)[0]);
1458         write_nic_word(dev,BSSIDR+4,((u16*)net->bssid)[2]);
1459 #if 0
1460         //MSR
1461         rtl8192_update_msr(dev);
1462 #endif
1463
1464
1465 //      rtl8192_update_cap(dev, net->capability);
1466         if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
1467         {
1468                 write_nic_word(dev, ATIMWND, 2);
1469                 write_nic_word(dev, BCN_DMATIME, 256);
1470                 write_nic_word(dev, BCN_INTERVAL, net->beacon_interval);
1471         //      write_nic_word(dev, BcnIntTime, 100);
1472         //BIT15 of BCN_DRV_EARLY_INT will indicate whether software beacon or hw beacon is applied.
1473                 write_nic_word(dev, BCN_DRV_EARLY_INT, 10);
1474                 write_nic_byte(dev, BCN_ERR_THRESH, 100);
1475
1476                 BcnTimeCfg |= (BcnCW<<BCN_TCFG_CW_SHIFT);
1477         // TODO: BcnIFS may required to be changed on ASIC
1478                 BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
1479
1480                 write_nic_word(dev, BCN_TCFG, BcnTimeCfg);
1481         }
1482
1483
1484 }
1485
1486 inline u8 rtl8192_IsWirelessBMode(u16 rate)
1487 {
1488         if( ((rate <= 110) && (rate != 60) && (rate != 90)) || (rate == 220) )
1489                 return 1;
1490         else return 0;
1491 }
1492
1493 u16 N_DBPSOfRate(u16 DataRate);
1494
1495 static u16 ComputeTxTime(
1496         u16             FrameLength,
1497         u16             DataRate,
1498         u8              bManagementFrame,
1499         u8              bShortPreamble
1500 )
1501 {
1502         u16     FrameTime;
1503         u16     N_DBPS;
1504         u16     Ceiling;
1505
1506         if( rtl8192_IsWirelessBMode(DataRate) )
1507         {
1508                 if( bManagementFrame || !bShortPreamble || DataRate == 10 )
1509                 {       // long preamble
1510                         FrameTime = (u16)(144+48+(FrameLength*8/(DataRate/10)));
1511                 }
1512                 else
1513                 {       // Short preamble
1514                         FrameTime = (u16)(72+24+(FrameLength*8/(DataRate/10)));
1515                 }
1516                 if( ( FrameLength*8 % (DataRate/10) ) != 0 ) //Get the Ceilling
1517                                 FrameTime ++;
1518         } else {        //802.11g DSSS-OFDM PLCP length field calculation.
1519                 N_DBPS = N_DBPSOfRate(DataRate);
1520                 Ceiling = (16 + 8*FrameLength + 6) / N_DBPS
1521                                 + (((16 + 8*FrameLength + 6) % N_DBPS) ? 1 : 0);
1522                 FrameTime = (u16)(16 + 4 + 4*Ceiling + 6);
1523         }
1524         return FrameTime;
1525 }
1526
1527 u16 N_DBPSOfRate(u16 DataRate)
1528 {
1529          u16 N_DBPS = 24;
1530
1531          switch(DataRate)
1532          {
1533          case 60:
1534           N_DBPS = 24;
1535           break;
1536
1537          case 90:
1538           N_DBPS = 36;
1539           break;
1540
1541          case 120:
1542           N_DBPS = 48;
1543           break;
1544
1545          case 180:
1546           N_DBPS = 72;
1547           break;
1548
1549          case 240:
1550           N_DBPS = 96;
1551           break;
1552
1553          case 360:
1554           N_DBPS = 144;
1555           break;
1556
1557          case 480:
1558           N_DBPS = 192;
1559           break;
1560
1561          case 540:
1562           N_DBPS = 216;
1563           break;
1564
1565          default:
1566           break;
1567          }
1568
1569          return N_DBPS;
1570 }
1571
1572 static unsigned int txqueue2outpipe(unsigned int tx_queue)
1573 {
1574         unsigned int outpipe = 0x04;
1575
1576         switch (tx_queue) {
1577                 case VO_QUEUE://EP4
1578                    outpipe = 0x04;
1579                 break;
1580
1581                 case VI_QUEUE://EP5
1582                    outpipe = 0x05;
1583                 break;
1584
1585                 case BE_QUEUE://EP6
1586                    outpipe = 0x06;
1587                 break;
1588
1589                 case BK_QUEUE://EP7
1590                    outpipe = 0x07;
1591                 break;
1592
1593                 case HCCA_QUEUE://EP8
1594                    outpipe = 0x08;
1595                 break;
1596
1597                 case BEACON_QUEUE://EPA
1598                    outpipe = 0x0A;
1599                 break;
1600
1601                 case HIGH_QUEUE://EPB
1602                    outpipe = 0x0B;
1603                 break;
1604
1605                 case MGNT_QUEUE://EPC
1606                    outpipe = 0x0C;
1607                 break;
1608
1609                 case TXCMD_QUEUE://EPD
1610                    outpipe = 0x0D;
1611                 break;
1612
1613                 default:
1614                   printk("Unknow queue index!\n");
1615                 break;
1616         }
1617
1618         return outpipe;
1619 }
1620
1621 void rtl819xE_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1622 {
1623     struct r8192_priv *priv = ieee80211_priv(dev);
1624     struct rtl8192_tx_ring *ring;
1625     tx_desc_819x_pci *entry;
1626     unsigned int idx;
1627     dma_addr_t mapping;
1628     cb_desc *tcb_desc;
1629     unsigned long flags;
1630
1631     ring = &priv->tx_ring[TXCMD_QUEUE];
1632     mapping = pci_map_single(priv->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1633
1634     spin_lock_irqsave(&priv->irq_th_lock,flags);
1635     idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1636     entry = &ring->desc[idx];
1637
1638     tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1639     memset(entry,0,12);
1640     entry->LINIP = tcb_desc->bLastIniPkt;
1641     entry->FirstSeg = 1;//first segment
1642     entry->LastSeg = 1; //last segment
1643     if(tcb_desc->bCmdOrInit == DESC_PACKET_TYPE_INIT) {
1644         entry->CmdInit = DESC_PACKET_TYPE_INIT;
1645     } else {
1646         entry->CmdInit = DESC_PACKET_TYPE_NORMAL;
1647         entry->Offset = sizeof(TX_FWINFO_8190PCI) + 8;
1648         entry->PktSize = (u16)(tcb_desc->pkt_size + entry->Offset);
1649         entry->QueueSelect = QSLT_CMD;
1650         entry->TxFWInfoSize = 0x08;
1651         entry->RATid = (u8)DESC_PACKET_TYPE_INIT;
1652     }
1653     entry->TxBufferSize = skb->len;
1654     entry->TxBuffAddr = cpu_to_le32(mapping);
1655     entry->OWN = 1;
1656
1657 #ifdef JOHN_DUMP_TXDESC
1658     {       int i;
1659         tx_desc_819x_pci *entry1 =  &ring->desc[0];
1660         unsigned int *ptr= (unsigned int *)entry1;
1661         printk("<Tx descriptor>:\n");
1662         for (i = 0; i < 8; i++)
1663             printk("%8x ", ptr[i]);
1664         printk("\n");
1665     }
1666 #endif
1667     __skb_queue_tail(&ring->queue, skb);
1668     spin_unlock_irqrestore(&priv->irq_th_lock,flags);
1669
1670     write_nic_byte(dev, TPPoll, TPPoll_CQ);
1671
1672     return;
1673 }
1674
1675 /*
1676  * Mapping Software/Hardware descriptor queue id to "Queue Select Field"
1677  * in TxFwInfo data structure
1678  * 2006.10.30 by Emily
1679  *
1680  * \param QUEUEID       Software Queue
1681 */
1682 static u8 MapHwQueueToFirmwareQueue(u8 QueueID)
1683 {
1684         u8 QueueSelect = 0x0;       //defualt set to
1685
1686         switch(QueueID) {
1687                 case BE_QUEUE:
1688                         QueueSelect = QSLT_BE;  //or QSelect = pTcb->priority;
1689                         break;
1690
1691                 case BK_QUEUE:
1692                         QueueSelect = QSLT_BK;  //or QSelect = pTcb->priority;
1693                         break;
1694
1695                 case VO_QUEUE:
1696                         QueueSelect = QSLT_VO;  //or QSelect = pTcb->priority;
1697                         break;
1698
1699                 case VI_QUEUE:
1700                         QueueSelect = QSLT_VI;  //or QSelect = pTcb->priority;
1701                         break;
1702                 case MGNT_QUEUE:
1703                         QueueSelect = QSLT_MGNT;
1704                         break;
1705
1706                 case BEACON_QUEUE:
1707                         QueueSelect = QSLT_BEACON;
1708                         break;
1709
1710                         // TODO: 2006.10.30 mark other queue selection until we verify it is OK
1711                         // TODO: Remove Assertions
1712 //#if (RTL819X_FPGA_VER & RTL819X_FPGA_GUANGAN_070502)
1713                 case TXCMD_QUEUE:
1714                         QueueSelect = QSLT_CMD;
1715                         break;
1716 //#endif
1717                 case HIGH_QUEUE:
1718                         //QueueSelect = QSLT_HIGH;
1719                         //break;
1720
1721                 default:
1722                         RT_TRACE(COMP_ERR, "TransmitTCB(): Impossible Queue Selection: %d \n", QueueID);
1723                         break;
1724         }
1725         return QueueSelect;
1726 }
1727
1728 static u8 MRateToHwRate8190Pci(u8 rate)
1729 {
1730         u8  ret = DESC90_RATE1M;
1731
1732         switch(rate) {
1733                 case MGN_1M:    ret = DESC90_RATE1M;            break;
1734                 case MGN_2M:    ret = DESC90_RATE2M;            break;
1735                 case MGN_5_5M:  ret = DESC90_RATE5_5M;  break;
1736                 case MGN_11M:   ret = DESC90_RATE11M;   break;
1737                 case MGN_6M:    ret = DESC90_RATE6M;            break;
1738                 case MGN_9M:    ret = DESC90_RATE9M;            break;
1739                 case MGN_12M:   ret = DESC90_RATE12M;   break;
1740                 case MGN_18M:   ret = DESC90_RATE18M;   break;
1741                 case MGN_24M:   ret = DESC90_RATE24M;   break;
1742                 case MGN_36M:   ret = DESC90_RATE36M;   break;
1743                 case MGN_48M:   ret = DESC90_RATE48M;   break;
1744                 case MGN_54M:   ret = DESC90_RATE54M;   break;
1745
1746                 // HT rate since here
1747                 case MGN_MCS0:  ret = DESC90_RATEMCS0;  break;
1748                 case MGN_MCS1:  ret = DESC90_RATEMCS1;  break;
1749                 case MGN_MCS2:  ret = DESC90_RATEMCS2;  break;
1750                 case MGN_MCS3:  ret = DESC90_RATEMCS3;  break;
1751                 case MGN_MCS4:  ret = DESC90_RATEMCS4;  break;
1752                 case MGN_MCS5:  ret = DESC90_RATEMCS5;  break;
1753                 case MGN_MCS6:  ret = DESC90_RATEMCS6;  break;
1754                 case MGN_MCS7:  ret = DESC90_RATEMCS7;  break;
1755                 case MGN_MCS8:  ret = DESC90_RATEMCS8;  break;
1756                 case MGN_MCS9:  ret = DESC90_RATEMCS9;  break;
1757                 case MGN_MCS10: ret = DESC90_RATEMCS10; break;
1758                 case MGN_MCS11: ret = DESC90_RATEMCS11; break;
1759                 case MGN_MCS12: ret = DESC90_RATEMCS12; break;
1760                 case MGN_MCS13: ret = DESC90_RATEMCS13; break;
1761                 case MGN_MCS14: ret = DESC90_RATEMCS14; break;
1762                 case MGN_MCS15: ret = DESC90_RATEMCS15; break;
1763                 case (0x80|0x20): ret = DESC90_RATEMCS32; break;
1764
1765                 default:       break;
1766         }
1767         return ret;
1768 }
1769
1770
1771 static u8 QueryIsShort(u8 TxHT, u8 TxRate, cb_desc *tcb_desc)
1772 {
1773         u8   tmp_Short;
1774
1775         tmp_Short = (TxHT==1)?((tcb_desc->bUseShortGI)?1:0):((tcb_desc->bUseShortPreamble)?1:0);
1776
1777         if(TxHT==1 && TxRate != DESC90_RATEMCS15)
1778                 tmp_Short = 0;
1779
1780         return tmp_Short;
1781 }
1782
1783 /*
1784  * The tx procedure is just as following,
1785  * skb->cb will contain all the following information,
1786  * priority, morefrag, rate, &dev.
1787  * */
1788 short rtl8192_tx(struct net_device *dev, struct sk_buff* skb)
1789 {
1790     struct r8192_priv *priv = ieee80211_priv(dev);
1791     struct rtl8192_tx_ring  *ring;
1792     unsigned long flags;
1793     cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1794     tx_desc_819x_pci *pdesc = NULL;
1795     TX_FWINFO_8190PCI *pTxFwInfo = NULL;
1796     dma_addr_t mapping;
1797     bool  multi_addr=false,broad_addr=false,uni_addr=false;
1798     u8*   pda_addr = NULL;
1799     int   idx;
1800
1801     mapping = pci_map_single(priv->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1802     /* collect the tx packets statitcs */
1803     pda_addr = ((u8*)skb->data) + sizeof(TX_FWINFO_8190PCI);
1804     if(is_multicast_ether_addr(pda_addr))
1805         multi_addr = true;
1806     else if(is_broadcast_ether_addr(pda_addr))
1807         broad_addr = true;
1808     else
1809         uni_addr = true;
1810
1811     if(uni_addr)
1812         priv->stats.txbytesunicast += (u8)(skb->len) - sizeof(TX_FWINFO_8190PCI);
1813     else if(multi_addr)
1814         priv->stats.txbytesmulticast +=(u8)(skb->len) - sizeof(TX_FWINFO_8190PCI);
1815     else
1816         priv->stats.txbytesbroadcast += (u8)(skb->len) - sizeof(TX_FWINFO_8190PCI);
1817
1818     /* fill tx firmware */
1819     pTxFwInfo = (PTX_FWINFO_8190PCI)skb->data;
1820     memset(pTxFwInfo,0,sizeof(TX_FWINFO_8190PCI));
1821     pTxFwInfo->TxHT = (tcb_desc->data_rate&0x80)?1:0;
1822     pTxFwInfo->TxRate = MRateToHwRate8190Pci((u8)tcb_desc->data_rate);
1823     pTxFwInfo->EnableCPUDur = tcb_desc->bTxEnableFwCalcDur;
1824     pTxFwInfo->Short    = QueryIsShort(pTxFwInfo->TxHT, pTxFwInfo->TxRate, tcb_desc);
1825
1826     /* Aggregation related */
1827     if(tcb_desc->bAMPDUEnable) {
1828         pTxFwInfo->AllowAggregation = 1;
1829         pTxFwInfo->RxMF = tcb_desc->ampdu_factor;
1830         pTxFwInfo->RxAMD = tcb_desc->ampdu_density;
1831     } else {
1832         pTxFwInfo->AllowAggregation = 0;
1833         pTxFwInfo->RxMF = 0;
1834         pTxFwInfo->RxAMD = 0;
1835     }
1836
1837     //
1838     // Protection mode related
1839     //
1840     pTxFwInfo->RtsEnable =      (tcb_desc->bRTSEnable)?1:0;
1841     pTxFwInfo->CtsEnable =      (tcb_desc->bCTSEnable)?1:0;
1842     pTxFwInfo->RtsSTBC =        (tcb_desc->bRTSSTBC)?1:0;
1843     pTxFwInfo->RtsHT=           (tcb_desc->rts_rate&0x80)?1:0;
1844     pTxFwInfo->RtsRate =                MRateToHwRate8190Pci((u8)tcb_desc->rts_rate);
1845     pTxFwInfo->RtsBandwidth = 0;
1846     pTxFwInfo->RtsSubcarrier = tcb_desc->RTSSC;
1847     pTxFwInfo->RtsShort =       (pTxFwInfo->RtsHT==0)?(tcb_desc->bRTSUseShortPreamble?1:0):(tcb_desc->bRTSUseShortGI?1:0);
1848     //
1849     // Set Bandwidth and sub-channel settings.
1850     //
1851     if(priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20_40)
1852     {
1853         if(tcb_desc->bPacketBW)
1854         {
1855             pTxFwInfo->TxBandwidth = 1;
1856 #ifdef RTL8190P
1857             pTxFwInfo->TxSubCarrier = 3;
1858 #else
1859             pTxFwInfo->TxSubCarrier = 0;        //By SD3's Jerry suggestion, use duplicated mode, cosa 04012008
1860 #endif
1861         }
1862         else
1863         {
1864             pTxFwInfo->TxBandwidth = 0;
1865             pTxFwInfo->TxSubCarrier = priv->nCur40MhzPrimeSC;
1866         }
1867     } else {
1868         pTxFwInfo->TxBandwidth = 0;
1869         pTxFwInfo->TxSubCarrier = 0;
1870     }
1871
1872     if (0)
1873     {
1874             /* 2007/07/25 MH  Copy current TX FW info.*/
1875             memcpy((void*)(&Tmp_TxFwInfo), (void*)(pTxFwInfo), sizeof(TX_FWINFO_8190PCI));
1876             printk("&&&&&&&&&&&&&&&&&&&&&&====>print out fwinf\n");
1877             printk("===>enable fwcacl:%d\n", Tmp_TxFwInfo.EnableCPUDur);
1878             printk("===>RTS STBC:%d\n", Tmp_TxFwInfo.RtsSTBC);
1879             printk("===>RTS Subcarrier:%d\n", Tmp_TxFwInfo.RtsSubcarrier);
1880             printk("===>Allow Aggregation:%d\n", Tmp_TxFwInfo.AllowAggregation);
1881             printk("===>TX HT bit:%d\n", Tmp_TxFwInfo.TxHT);
1882             printk("===>Tx rate:%d\n", Tmp_TxFwInfo.TxRate);
1883             printk("===>Received AMPDU Density:%d\n", Tmp_TxFwInfo.RxAMD);
1884             printk("===>Received MPDU Factor:%d\n", Tmp_TxFwInfo.RxMF);
1885             printk("===>TxBandwidth:%d\n", Tmp_TxFwInfo.TxBandwidth);
1886             printk("===>TxSubCarrier:%d\n", Tmp_TxFwInfo.TxSubCarrier);
1887
1888         printk("<=====**********************out of print\n");
1889
1890     }
1891     spin_lock_irqsave(&priv->irq_th_lock,flags);
1892     ring = &priv->tx_ring[tcb_desc->queue_index];
1893     if (tcb_desc->queue_index != BEACON_QUEUE) {
1894         idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1895     } else {
1896         idx = 0;
1897     }
1898
1899     pdesc = &ring->desc[idx];
1900     if((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
1901             RT_TRACE(COMP_ERR,"No more TX desc@%d, ring->idx = %d,idx = %d,%x", \
1902                             tcb_desc->queue_index,ring->idx, idx,skb->len);
1903             return skb->len;
1904     }
1905
1906     /* fill tx descriptor */
1907     memset((u8*)pdesc,0,12);
1908     /*DWORD 0*/
1909     pdesc->LINIP = 0;
1910     pdesc->CmdInit = 1;
1911     pdesc->Offset = sizeof(TX_FWINFO_8190PCI) + 8; //We must add 8!! Emily
1912     pdesc->PktSize = (u16)skb->len-sizeof(TX_FWINFO_8190PCI);
1913
1914     /*DWORD 1*/
1915     pdesc->SecCAMID= 0;
1916     pdesc->RATid = tcb_desc->RATRIndex;
1917
1918
1919     pdesc->NoEnc = 1;
1920     pdesc->SecType = 0x0;
1921     if (tcb_desc->bHwSec) {
1922         static u8 tmp =0;
1923         if (!tmp) {
1924             printk("==>================hw sec\n");
1925             tmp = 1;
1926         }
1927         switch (priv->ieee80211->pairwise_key_type) {
1928             case KEY_TYPE_WEP40:
1929             case KEY_TYPE_WEP104:
1930                 pdesc->SecType = 0x1;
1931                 pdesc->NoEnc = 0;
1932                 break;
1933             case KEY_TYPE_TKIP:
1934                 pdesc->SecType = 0x2;
1935                 pdesc->NoEnc = 0;
1936                 break;
1937             case KEY_TYPE_CCMP:
1938                 pdesc->SecType = 0x3;
1939                 pdesc->NoEnc = 0;
1940                 break;
1941             case KEY_TYPE_NA:
1942                 pdesc->SecType = 0x0;
1943                 pdesc->NoEnc = 1;
1944                 break;
1945         }
1946     }
1947
1948     //
1949     // Set Packet ID
1950     //
1951     pdesc->PktId = 0x0;
1952
1953     pdesc->QueueSelect = MapHwQueueToFirmwareQueue(tcb_desc->queue_index);
1954     pdesc->TxFWInfoSize = sizeof(TX_FWINFO_8190PCI);
1955
1956     pdesc->DISFB = tcb_desc->bTxDisableRateFallBack;
1957     pdesc->USERATE = tcb_desc->bTxUseDriverAssingedRate;
1958
1959     pdesc->FirstSeg =1;
1960     pdesc->LastSeg = 1;
1961     pdesc->TxBufferSize = skb->len;
1962
1963     pdesc->TxBuffAddr = cpu_to_le32(mapping);
1964     __skb_queue_tail(&ring->queue, skb);
1965     pdesc->OWN = 1;
1966     spin_unlock_irqrestore(&priv->irq_th_lock,flags);
1967     dev->trans_start = jiffies;
1968     write_nic_word(dev,TPPoll,0x01<<tcb_desc->queue_index);
1969     return 0;
1970 }
1971
1972 static short rtl8192_alloc_rx_desc_ring(struct net_device *dev)
1973 {
1974     struct r8192_priv *priv = ieee80211_priv(dev);
1975     rx_desc_819x_pci *entry = NULL;
1976     int i;
1977
1978     priv->rx_ring = pci_alloc_consistent(priv->pdev,
1979             sizeof(*priv->rx_ring) * priv->rxringcount, &priv->rx_ring_dma);
1980
1981     if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
1982         RT_TRACE(COMP_ERR,"Cannot allocate RX ring\n");
1983         return -ENOMEM;
1984     }
1985
1986     memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * priv->rxringcount);
1987     priv->rx_idx = 0;
1988
1989     for (i = 0; i < priv->rxringcount; i++) {
1990         struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
1991         dma_addr_t *mapping;
1992         entry = &priv->rx_ring[i];
1993         if (!skb)
1994             return 0;
1995         priv->rx_buf[i] = skb;
1996         mapping = (dma_addr_t *)skb->cb;
1997         *mapping = pci_map_single(priv->pdev, skb->tail,//skb_tail_pointer(skb),
1998                 priv->rxbuffersize, PCI_DMA_FROMDEVICE);
1999
2000         entry->BufferAddress = cpu_to_le32(*mapping);
2001
2002         entry->Length = priv->rxbuffersize;
2003         entry->OWN = 1;
2004     }
2005
2006     entry->EOR = 1;
2007     return 0;
2008 }
2009
2010 static int rtl8192_alloc_tx_desc_ring(struct net_device *dev,
2011         unsigned int prio, unsigned int entries)
2012 {
2013     struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
2014     tx_desc_819x_pci *ring;
2015     dma_addr_t dma;
2016     int i;
2017
2018     ring = pci_alloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
2019     if (!ring || (unsigned long)ring & 0xFF) {
2020         RT_TRACE(COMP_ERR, "Cannot allocate TX ring (prio = %d)\n", prio);
2021         return -ENOMEM;
2022     }
2023
2024     memset(ring, 0, sizeof(*ring)*entries);
2025     priv->tx_ring[prio].desc = ring;
2026     priv->tx_ring[prio].dma = dma;
2027     priv->tx_ring[prio].idx = 0;
2028     priv->tx_ring[prio].entries = entries;
2029     skb_queue_head_init(&priv->tx_ring[prio].queue);
2030
2031     for (i = 0; i < entries; i++)
2032         ring[i].NextDescAddress =
2033             cpu_to_le32((u32)dma + ((i + 1) % entries) * sizeof(*ring));
2034
2035     return 0;
2036 }
2037
2038
2039 static short rtl8192_pci_initdescring(struct net_device *dev)
2040 {
2041     u32 ret;
2042     int i;
2043     struct r8192_priv *priv = ieee80211_priv(dev);
2044
2045     ret = rtl8192_alloc_rx_desc_ring(dev);
2046     if (ret) {
2047         return ret;
2048     }
2049
2050
2051     /* general process for other queue */
2052     for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
2053         if ((ret = rtl8192_alloc_tx_desc_ring(dev, i, priv->txringcount)))
2054             goto err_free_rings;
2055     }
2056
2057 #if 0
2058     /* specific process for hardware beacon process */
2059     if ((ret = rtl8192_alloc_tx_desc_ring(dev, MAX_TX_QUEUE_COUNT - 1, 2)))
2060         goto err_free_rings;
2061 #endif
2062
2063     return 0;
2064
2065 err_free_rings:
2066     rtl8192_free_rx_ring(dev);
2067     for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
2068         if (priv->tx_ring[i].desc)
2069             rtl8192_free_tx_ring(dev, i);
2070     return 1;
2071 }
2072
2073 static void rtl8192_pci_resetdescring(struct net_device *dev)
2074 {
2075     struct r8192_priv *priv = ieee80211_priv(dev);
2076     int i;
2077
2078     /* force the rx_idx to the first one */
2079     if(priv->rx_ring) {
2080         rx_desc_819x_pci *entry = NULL;
2081         for (i = 0; i < priv->rxringcount; i++) {
2082             entry = &priv->rx_ring[i];
2083             entry->OWN = 1;
2084         }
2085         priv->rx_idx = 0;
2086     }
2087
2088     /* after reset, release previous pending packet, and force the
2089      * tx idx to the first one */
2090     for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
2091         if (priv->tx_ring[i].desc) {
2092             struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
2093
2094             while (skb_queue_len(&ring->queue)) {
2095                 tx_desc_819x_pci *entry = &ring->desc[ring->idx];
2096                 struct sk_buff *skb = __skb_dequeue(&ring->queue);
2097
2098                 pci_unmap_single(priv->pdev, le32_to_cpu(entry->TxBuffAddr),
2099                         skb->len, PCI_DMA_TODEVICE);
2100                 kfree_skb(skb);
2101                 ring->idx = (ring->idx + 1) % ring->entries;
2102             }
2103             ring->idx = 0;
2104         }
2105     }
2106 }
2107
2108 #if 1
2109 extern void rtl8192_update_ratr_table(struct net_device* dev);
2110 static void rtl8192_link_change(struct net_device *dev)
2111 {
2112 //      int i;
2113
2114         struct r8192_priv *priv = ieee80211_priv(dev);
2115         struct ieee80211_device* ieee = priv->ieee80211;
2116         //write_nic_word(dev, BCN_INTR_ITV, net->beacon_interval);
2117         if (ieee->state == IEEE80211_LINKED)
2118         {
2119                 rtl8192_net_update(dev);
2120                 rtl8192_update_ratr_table(dev);
2121 #if 1
2122                 //add this as in pure N mode, wep encryption will use software way, but there is no chance to set this as wep will not set group key in wext. WB.2008.07.08
2123                 if ((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type))
2124                 EnableHWSecurityConfig8192(dev);
2125 #endif
2126         }
2127         else
2128         {
2129                 write_nic_byte(dev, 0x173, 0);
2130         }
2131         /*update timing params*/
2132         //rtl8192_set_chan(dev, priv->chan);
2133         //MSR
2134         rtl8192_update_msr(dev);
2135
2136         // 2007/10/16 MH MAC Will update TSF according to all received beacon, so we have
2137         //      // To set CBSSID bit when link with any AP or STA.
2138         if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC)
2139         {
2140                 u32 reg = 0;
2141                 reg = read_nic_dword(dev, RCR);
2142                 if (priv->ieee80211->state == IEEE80211_LINKED)
2143                         priv->ReceiveConfig = reg |= RCR_CBSSID;
2144                 else
2145                         priv->ReceiveConfig = reg &= ~RCR_CBSSID;
2146                 write_nic_dword(dev, RCR, reg);
2147         }
2148 }
2149 #endif
2150
2151
2152 static struct ieee80211_qos_parameters def_qos_parameters = {
2153         {3,3,3,3},/* cw_min */
2154         {7,7,7,7},/* cw_max */
2155         {2,2,2,2},/* aifs */
2156         {0,0,0,0},/* flags */
2157         {0,0,0,0} /* tx_op_limit */
2158 };
2159
2160 #if LINUX_VERSION_CODE >=KERNEL_VERSION(2,6,20)
2161 static void rtl8192_update_beacon(struct work_struct * work)
2162 {
2163         struct r8192_priv *priv = container_of(work, struct r8192_priv, update_beacon_wq.work);
2164         struct net_device *dev = priv->ieee80211->dev;
2165 #else
2166 void rtl8192_update_beacon(struct net_device *dev)
2167 {
2168         struct r8192_priv *priv = ieee80211_priv(dev);
2169 #endif
2170         struct ieee80211_device* ieee = priv->ieee80211;
2171         struct ieee80211_network* net = &ieee->current_network;
2172
2173         if (ieee->pHTInfo->bCurrentHTSupport)
2174                 HTUpdateSelfAndPeerSetting(ieee, net);
2175         ieee->pHTInfo->bCurrentRT2RTLongSlotTime = net->bssht.bdRT2RTLongSlotTime;
2176         rtl8192_update_cap(dev, net->capability);
2177 }
2178 /*
2179 * background support to run QoS activate functionality
2180 */
2181 static int WDCAPARA_ADD[] = {EDCAPARA_BE,EDCAPARA_BK,EDCAPARA_VI,EDCAPARA_VO};
2182 #if LINUX_VERSION_CODE >=KERNEL_VERSION(2,6,20)
2183 static void rtl8192_qos_activate(struct work_struct * work)
2184 {
2185         struct r8192_priv *priv = container_of(work, struct r8192_priv, qos_activate);
2186         struct net_device *dev = priv->ieee80211->dev;
2187 #else
2188 void rtl8192_qos_activate(struct net_device *dev)
2189 {
2190         struct r8192_priv *priv = ieee80211_priv(dev);
2191 #endif
2192         struct ieee80211_qos_parameters *qos_parameters = &priv->ieee80211->current_network.qos_data.parameters;
2193         u8 mode = priv->ieee80211->current_network.mode;
2194 //        u32 size = sizeof(struct ieee80211_qos_parameters);
2195         u8  u1bAIFS;
2196         u32 u4bAcParam;
2197         int i;
2198         if (priv == NULL)
2199                 return;
2200
2201 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16))
2202         down(&priv->mutex);
2203 #else
2204         mutex_lock(&priv->mutex);
2205 #endif
2206         if(priv->ieee80211->state != IEEE80211_LINKED)
2207                 goto success;
2208         RT_TRACE(COMP_QOS,"qos active process with associate response received\n");
2209         /* It better set slot time at first */
2210         /* For we just support b/g mode at present, let the slot time at 9/20 selection */
2211         /* update the ac parameter to related registers */
2212         for(i = 0; i <  QOS_QUEUE_NUM; i++) {
2213                 //Mode G/A: slotTimeTimer = 9; Mode B: 20
2214                 u1bAIFS = qos_parameters->aifs[i] * ((mode&(IEEE_G|IEEE_N_24G)) ?9:20) + aSifsTime;
2215                 u4bAcParam = ((((u32)(qos_parameters->tx_op_limit[i]))<< AC_PARAM_TXOP_LIMIT_OFFSET)|
2216                                 (((u32)(qos_parameters->cw_max[i]))<< AC_PARAM_ECW_MAX_OFFSET)|
2217                                 (((u32)(qos_parameters->cw_min[i]))<< AC_PARAM_ECW_MIN_OFFSET)|
2218                                 ((u32)u1bAIFS << AC_PARAM_AIFS_OFFSET));
2219                 printk("===>u4bAcParam:%x, ", u4bAcParam);
2220                 write_nic_dword(dev, WDCAPARA_ADD[i], u4bAcParam);
2221                 //write_nic_dword(dev, WDCAPARA_ADD[i], 0x005e4332);
2222         }
2223
2224 success:
2225 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16))
2226         up(&priv->mutex);
2227 #else
2228         mutex_unlock(&priv->mutex);
2229 #endif
2230 }
2231
2232 static int rtl8192_qos_handle_probe_response(struct r8192_priv *priv,
2233                 int active_network,
2234                 struct ieee80211_network *network)
2235 {
2236         int ret = 0;
2237         u32 size = sizeof(struct ieee80211_qos_parameters);
2238
2239         if(priv->ieee80211->state !=IEEE80211_LINKED)
2240                 return ret;
2241
2242         if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
2243                 return ret;
2244
2245         if (network->flags & NETWORK_HAS_QOS_MASK) {
2246                 if (active_network &&
2247                                 (network->flags & NETWORK_HAS_QOS_PARAMETERS))
2248                         network->qos_data.active = network->qos_data.supported;
2249
2250                 if ((network->qos_data.active == 1) && (active_network == 1) &&
2251                                 (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
2252                                 (network->qos_data.old_param_count !=
2253                                  network->qos_data.param_count)) {
2254                         network->qos_data.old_param_count =
2255                                 network->qos_data.param_count;
2256 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
2257                         queue_work(priv->priv_wq, &priv->qos_activate);
2258 #else
2259                         schedule_task(&priv->qos_activate);
2260 #endif
2261                         RT_TRACE (COMP_QOS, "QoS parameters change call "
2262                                         "qos_activate\n");
2263                 }
2264         } else {
2265                 memcpy(&priv->ieee80211->current_network.qos_data.parameters,\
2266                        &def_qos_parameters, size);
2267
2268                 if ((network->qos_data.active == 1) && (active_network == 1)) {
2269 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
2270                         queue_work(priv->priv_wq, &priv->qos_activate);
2271 #else
2272                         schedule_task(&priv->qos_activate);
2273 #endif
2274                         RT_TRACE(COMP_QOS, "QoS was disabled call qos_activate \n");
2275                 }
2276                 network->qos_data.active = 0;
2277                 network->qos_data.supported = 0;
2278         }
2279
2280         return 0;
2281 }
2282
2283 /* handle manage frame frame beacon and probe response */
2284 static int rtl8192_handle_beacon(struct net_device * dev,
2285                               struct ieee80211_beacon * beacon,
2286                               struct ieee80211_network * network)
2287 {
2288         struct r8192_priv *priv = ieee80211_priv(dev);
2289
2290         rtl8192_qos_handle_probe_response(priv,1,network);
2291
2292 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
2293         queue_delayed_work(priv->priv_wq, &priv->update_beacon_wq, 0);
2294 #else
2295 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2296         schedule_task(&priv->update_beacon_wq);
2297 #else
2298         queue_work(priv->priv_wq, &priv->update_beacon_wq);
2299 #endif
2300 #endif
2301         return 0;
2302
2303 }
2304
2305 /*
2306 * handling the beaconing responses. if we get different QoS setting
2307 * off the network from the associated setting, adjust the QoS
2308 * setting
2309 */
2310 static int rtl8192_qos_association_resp(struct r8192_priv *priv,
2311                                     struct ieee80211_network *network)
2312 {
2313         int ret = 0;
2314         unsigned long flags;
2315         u32 size = sizeof(struct ieee80211_qos_parameters);
2316         int set_qos_param = 0;
2317
2318         if ((priv == NULL) || (network == NULL))
2319                 return ret;
2320
2321         if(priv->ieee80211->state !=IEEE80211_LINKED)
2322                 return ret;
2323
2324         if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
2325                 return ret;
2326
2327         spin_lock_irqsave(&priv->ieee80211->lock, flags);
2328         if(network->flags & NETWORK_HAS_QOS_PARAMETERS) {
2329                 memcpy(&priv->ieee80211->current_network.qos_data.parameters,\
2330                          &network->qos_data.parameters,\
2331                         sizeof(struct ieee80211_qos_parameters));
2332                 priv->ieee80211->current_network.qos_data.active = 1;
2333 #if 0
2334                 if((priv->ieee80211->current_network.qos_data.param_count != \
2335                                         network->qos_data.param_count))
2336 #endif
2337                  {
2338                         set_qos_param = 1;
2339                         /* update qos parameter for current network */
2340                         priv->ieee80211->current_network.qos_data.old_param_count = \
2341                                  priv->ieee80211->current_network.qos_data.param_count;
2342                         priv->ieee80211->current_network.qos_data.param_count = \
2343                                  network->qos_data.param_count;
2344                 }
2345         } else {
2346                 memcpy(&priv->ieee80211->current_network.qos_data.parameters,\
2347                        &def_qos_parameters, size);
2348                 priv->ieee80211->current_network.qos_data.active = 0;
2349                 priv->ieee80211->current_network.qos_data.supported = 0;
2350                 set_qos_param = 1;
2351         }
2352
2353         spin_unlock_irqrestore(&priv->ieee80211->lock, flags);
2354
2355         RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n",__FUNCTION__,network->flags ,priv->ieee80211->current_network.qos_data.active);
2356         if (set_qos_param == 1)
2357 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
2358                 queue_work(priv->priv_wq, &priv->qos_activate);
2359 #else
2360                 schedule_task(&priv->qos_activate);
2361 #endif
2362
2363
2364         return ret;
2365 }
2366
2367
2368 static int rtl8192_handle_assoc_response(struct net_device *dev,
2369                                      struct ieee80211_assoc_response_frame *resp,
2370                                      struct ieee80211_network *network)
2371 {
2372         struct r8192_priv *priv = ieee80211_priv(dev);
2373         rtl8192_qos_association_resp(priv, network);
2374         return 0;
2375 }
2376
2377
2378 //updateRATRTabel for MCS only. Basic rate is not implement.
2379 void rtl8192_update_ratr_table(struct net_device* dev)
2380         //      POCTET_STRING   posLegacyRate,
2381         //      u8*                     pMcsRate)
2382         //      PRT_WLAN_STA    pEntry)
2383 {
2384         struct r8192_priv* priv = ieee80211_priv(dev);
2385         struct ieee80211_device* ieee = priv->ieee80211;
2386         u8* pMcsRate = ieee->dot11HTOperationalRateSet;
2387         //struct ieee80211_network *net = &ieee->current_network;
2388         u32 ratr_value = 0;
2389         u8 rate_index = 0;
2390
2391         rtl8192_config_rate(dev, (u16*)(&ratr_value));
2392         ratr_value |= (*(u16*)(pMcsRate)) << 12;
2393 //      switch (net->mode)
2394         switch (ieee->mode)
2395         {
2396                 case IEEE_A:
2397                         ratr_value &= 0x00000FF0;
2398                         break;
2399                 case IEEE_B:
2400                         ratr_value &= 0x0000000F;
2401                         break;
2402                 case IEEE_G:
2403                         ratr_value &= 0x00000FF7;
2404                         break;
2405                 case IEEE_N_24G:
2406                 case IEEE_N_5G:
2407                         if (ieee->pHTInfo->PeerMimoPs == 0) //MIMO_PS_STATIC
2408                                 ratr_value &= 0x0007F007;
2409                         else{
2410                                 if (priv->rf_type == RF_1T2R)
2411                                         ratr_value &= 0x000FF007;
2412                                 else
2413                                         ratr_value &= 0x0F81F007;
2414                         }
2415                         break;
2416                 default:
2417                         break;
2418         }
2419         ratr_value &= 0x0FFFFFFF;
2420         if(ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI40MHz){
2421                 ratr_value |= 0x80000000;
2422         }else if(!ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI20MHz){
2423                 ratr_value |= 0x80000000;
2424         }
2425         write_nic_dword(dev, RATR0+rate_index*4, ratr_value);
2426         write_nic_byte(dev, UFWP, 1);
2427 }
2428
2429 static u8 ccmp_ie[4] = {0x00,0x50,0xf2,0x04};
2430 static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
2431 static bool GetNmodeSupportBySecCfg8190Pci(struct net_device*dev)
2432 {
2433 #if 1
2434         struct r8192_priv* priv = ieee80211_priv(dev);
2435         struct ieee80211_device* ieee = priv->ieee80211;
2436         int wpa_ie_len= ieee->wpa_ie_len;
2437         struct ieee80211_crypt_data* crypt;
2438         int encrypt;
2439
2440         crypt = ieee->crypt[ieee->tx_keyidx];
2441         encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY) || (ieee->host_encrypt && crypt && crypt->ops && (0 == strcmp(crypt->ops->name,"WEP")));
2442
2443         /* simply judge  */
2444         if(encrypt && (wpa_ie_len == 0)) {
2445                 /* wep encryption, no N mode setting */
2446                 return false;
2447 //      } else if((wpa_ie_len != 0)&&(memcmp(&(ieee->wpa_ie[14]),ccmp_ie,4))) {
2448         } else if((wpa_ie_len != 0)) {
2449                 /* parse pairwise key type */
2450                 //if((pairwisekey = WEP40)||(pairwisekey = WEP104)||(pairwisekey = TKIP))
2451                 if (((ieee->wpa_ie[0] == 0xdd) && (!memcmp(&(ieee->wpa_ie[14]),ccmp_ie,4))) || ((ieee->wpa_ie[0] == 0x30) && (!memcmp(&ieee->wpa_ie[10],ccmp_rsn_ie, 4))))
2452                         return true;
2453                 else
2454                         return false;
2455         } else {
2456                 //RT_TRACE(COMP_ERR,"In %s The GroupEncAlgorithm is [4]\n",__FUNCTION__ );
2457                 return true;
2458         }
2459
2460 #if 0
2461         //In here we discuss with SD4 David. He think we still can send TKIP in broadcast group key in MCS rate.
2462         //We can't force in G mode if Pairwie key is AES and group key is TKIP
2463         if((pSecInfo->GroupEncAlgorithm == WEP104_Encryption) || (pSecInfo->GroupEncAlgorithm == WEP40_Encryption)  ||
2464            (pSecInfo->PairwiseEncAlgorithm == WEP104_Encryption) ||
2465            (pSecInfo->PairwiseEncAlgorithm == WEP40_Encryption) || (pSecInfo->PairwiseEncAlgorithm == TKIP_Encryption))
2466         {
2467                 return  false;
2468         }
2469         else
2470                 return true;
2471 #endif
2472         return true;
2473 #endif
2474 }
2475
2476 static void rtl8192_refresh_supportrate(struct r8192_priv* priv)
2477 {
2478         struct ieee80211_device* ieee = priv->ieee80211;
2479         //we donot consider set support rate for ABG mode, only HT MCS rate is set here.
2480         if (ieee->mode == WIRELESS_MODE_N_24G || ieee->mode == WIRELESS_MODE_N_5G)
2481         {
2482                 memcpy(ieee->Regdot11HTOperationalRateSet, ieee->RegHTSuppRateSet, 16);
2483                 //RT_DEBUG_DATA(COMP_INIT, ieee->RegHTSuppRateSet, 16);
2484                 //RT_DEBUG_DATA(COMP_INIT, ieee->Regdot11HTOperationalRateSet, 16);
2485         }
2486         else
2487                 memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
2488         return;
2489 }
2490
2491 static u8 rtl8192_getSupportedWireleeMode(struct net_device*dev)
2492 {
2493         struct r8192_priv *priv = ieee80211_priv(dev);
2494         u8 ret = 0;
2495         switch(priv->rf_chip)
2496         {
2497                 case RF_8225:
2498                 case RF_8256:
2499                 case RF_PSEUDO_11N:
2500                         ret = (WIRELESS_MODE_N_24G|WIRELESS_MODE_G|WIRELESS_MODE_B);
2501                         break;
2502                 case RF_8258:
2503                         ret = (WIRELESS_MODE_A|WIRELESS_MODE_N_5G);
2504                         break;
2505                 default:
2506                         ret = WIRELESS_MODE_B;
2507                         break;
2508         }
2509         return ret;
2510 }
2511
2512 static void rtl8192_SetWirelessMode(struct net_device* dev, u8 wireless_mode)
2513 {
2514         struct r8192_priv *priv = ieee80211_priv(dev);
2515         u8 bSupportMode = rtl8192_getSupportedWireleeMode(dev);
2516
2517 #if 1
2518         if ((wireless_mode == WIRELESS_MODE_AUTO) || ((wireless_mode&bSupportMode)==0))
2519         {
2520                 if(bSupportMode & WIRELESS_MODE_N_24G)
2521                 {
2522                         wireless_mode = WIRELESS_MODE_N_24G;
2523                 }
2524                 else if(bSupportMode & WIRELESS_MODE_N_5G)
2525                 {
2526                         wireless_mode = WIRELESS_MODE_N_5G;
2527                 }
2528                 else if((bSupportMode & WIRELESS_MODE_A))
2529                 {
2530                         wireless_mode = WIRELESS_MODE_A;
2531                 }
2532                 else if((bSupportMode & WIRELESS_MODE_G))
2533                 {
2534                         wireless_mode = WIRELESS_MODE_G;
2535                 }
2536                 else if((bSupportMode & WIRELESS_MODE_B))
2537                 {
2538                         wireless_mode = WIRELESS_MODE_B;
2539                 }
2540                 else{
2541                         RT_TRACE(COMP_ERR, "%s(), No valid wireless mode supported, SupportedWirelessMode(%x)!!!\n", __FUNCTION__,bSupportMode);
2542                         wireless_mode = WIRELESS_MODE_B;
2543                 }
2544         }
2545 #ifdef TO_DO_LIST //// TODO: this function doesn't work well at this time, we shoud wait for FPGA
2546         ActUpdateChannelAccessSetting( pAdapter, pHalData->CurrentWirelessMode, &pAdapter->MgntInfo.Info8185.ChannelAccessSetting );
2547 #endif
2548         priv->ieee80211->mode = wireless_mode;
2549
2550         if ((wireless_mode == WIRELESS_MODE_N_24G) ||  (wireless_mode == WIRELESS_MODE_N_5G))
2551                 priv->ieee80211->pHTInfo->bEnableHT = 1;
2552         else
2553                 priv->ieee80211->pHTInfo->bEnableHT = 0;
2554         RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
2555         rtl8192_refresh_supportrate(priv);
2556 #endif
2557
2558 }
2559 //init priv variables here
2560
2561 static bool GetHalfNmodeSupportByAPs819xPci(struct net_device* dev)
2562 {
2563         bool                    Reval;
2564         struct r8192_priv* priv = ieee80211_priv(dev);
2565         struct ieee80211_device* ieee = priv->ieee80211;
2566
2567         if(ieee->bHalfWirelessN24GMode == true)
2568                 Reval = true;
2569         else
2570                 Reval =  false;
2571
2572         return Reval;
2573 }
2574
2575 short rtl8192_is_tx_queue_empty(struct net_device *dev)
2576 {
2577         int i=0;
2578         struct r8192_priv *priv = ieee80211_priv(dev);
2579         for (i=0; i<=MGNT_QUEUE; i++)
2580         {
2581                 if ((i== TXCMD_QUEUE) || (i == HCCA_QUEUE) )
2582                         continue;
2583                 if (skb_queue_len(&(&priv->tx_ring[i])->queue) > 0){
2584                         printk("===>tx queue is not empty:%d, %d\n", i, skb_queue_len(&(&priv->tx_ring[i])->queue));
2585                         return 0;
2586                 }
2587         }
2588         return 1;
2589 }
2590 #if 0
2591 void rtl8192_rq_tx_ack(struct net_device *dev)
2592 {
2593         struct r8192_priv *priv = ieee80211_priv(dev);
2594         priv->ieee80211->ack_tx_to_ieee = 1;
2595 }
2596 #endif
2597 static void rtl8192_hw_sleep_down(struct net_device *dev)
2598 {
2599         RT_TRACE(COMP_POWER, "%s()============>come to sleep down\n", __FUNCTION__);
2600         MgntActSet_RF_State(dev, eRfSleep, RF_CHANGE_BY_PS);
2601 }
2602 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
2603 static void rtl8192_hw_sleep_wq (struct work_struct *work)
2604 {
2605 //      struct r8180_priv *priv = container_of(work, struct r8180_priv, watch_dog_wq);
2606 //      struct ieee80211_device * ieee = (struct ieee80211_device*)
2607 //                                             container_of(work, struct ieee80211_device, watch_dog_wq);
2608         struct delayed_work *dwork = container_of(work,struct delayed_work,work);
2609         struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_sleep_wq);
2610         struct net_device *dev = ieee->dev;
2611 #else
2612 void rtl8192_hw_sleep_wq(struct net_device* dev)
2613 {
2614 #endif
2615         //printk("=========>%s()\n", __FUNCTION__);
2616         rtl8192_hw_sleep_down(dev);
2617 }
2618 //      printk("dev is %d\n",dev);
2619 //      printk("&*&(^*(&(&=========>%s()\n", __FUNCTION__);
2620 static void rtl8192_hw_wakeup(struct net_device* dev)
2621 {
2622 //      u32 flags = 0;
2623
2624 //      spin_lock_irqsave(&priv->ps_lock,flags);
2625         RT_TRACE(COMP_POWER, "%s()============>come to wake up\n", __FUNCTION__);
2626         MgntActSet_RF_State(dev, eRfOn, RF_CHANGE_BY_PS);
2627         //FIXME: will we send package stored while nic is sleep?
2628 //      spin_unlock_irqrestore(&priv->ps_lock,flags);
2629 }
2630 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
2631 void rtl8192_hw_wakeup_wq (struct work_struct *work)
2632 {
2633 //      struct r8180_priv *priv = container_of(work, struct r8180_priv, watch_dog_wq);
2634 //      struct ieee80211_device * ieee = (struct ieee80211_device*)
2635 //                                             container_of(work, struct ieee80211_device, watch_dog_wq);
2636         struct delayed_work *dwork = container_of(work,struct delayed_work,work);
2637         struct ieee80211_device *ieee = container_of(dwork,struct ieee80211_device,hw_wakeup_wq);
2638         struct net_device *dev = ieee->dev;
2639 #else
2640 void rtl8192_hw_wakeup_wq(struct net_device* dev)
2641 {
2642 #endif
2643         rtl8192_hw_wakeup(dev);
2644
2645 }
2646
2647 #define MIN_SLEEP_TIME 50
2648 #define MAX_SLEEP_TIME 10000
2649 static void rtl8192_hw_to_sleep(struct net_device *dev, u32 th, u32 tl)
2650 {
2651
2652         struct r8192_priv *priv = ieee80211_priv(dev);
2653
2654         u32 rb = jiffies;
2655         unsigned long flags;
2656
2657         spin_lock_irqsave(&priv->ps_lock,flags);
2658
2659         /* Writing HW register with 0 equals to disable
2660          * the timer, that is not really what we want
2661          */
2662         tl -= MSECS(4+16+7);
2663
2664         //if(tl == 0) tl = 1;
2665
2666         /* FIXME HACK FIXME HACK */
2667 //      force_pci_posting(dev);
2668         //mdelay(1);
2669
2670 //      rb = read_nic_dword(dev, TSFTR);
2671
2672         /* If the interval in witch we are requested to sleep is too
2673          * short then give up and remain awake
2674          */
2675         if(((tl>=rb)&& (tl-rb) <= MSECS(MIN_SLEEP_TIME))
2676                 ||((rb>tl)&& (rb-tl) < MSECS(MIN_SLEEP_TIME))) {
2677                 spin_unlock_irqrestore(&priv->ps_lock,flags);
2678                 printk("too short to sleep\n");
2679                 return;
2680         }
2681
2682 //      write_nic_dword(dev, TimerInt, tl);
2683 //      rb = read_nic_dword(dev, TSFTR);
2684         {
2685                 u32 tmp = (tl>rb)?(tl-rb):(rb-tl);
2686         //      if (tl<rb)
2687                 queue_delayed_work(priv->ieee80211->wq, &priv->ieee80211->hw_wakeup_wq, tmp); //as tl may be less than rb
2688         }
2689         /* if we suspect the TimerInt is gone beyond tl
2690          * while setting it, then give up
2691          */
2692 #if 1
2693         if(((tl > rb) && ((tl-rb) > MSECS(MAX_SLEEP_TIME)))||
2694                 ((tl < rb) && ((rb-tl) > MSECS(MAX_SLEEP_TIME)))) {
2695                 printk("========>too long to sleep:%x, %x, %lx\n", tl, rb,  MSECS(MAX_SLEEP_TIME));
2696                 spin_unlock_irqrestore(&priv->ps_lock,flags);
2697                 return;
2698         }
2699 #endif
2700 //      if(priv->rf_sleep)
2701 //              priv->rf_sleep(dev);
2702
2703         //printk("<=========%s()\n", __FUNCTION__);
2704         queue_delayed_work(priv->ieee80211->wq, (void *)&priv->ieee80211->hw_sleep_wq,0);
2705         spin_unlock_irqrestore(&priv->ps_lock,flags);
2706 }
2707 static void rtl8192_init_priv_variable(struct net_device* dev)
2708 {
2709         struct r8192_priv *priv = ieee80211_priv(dev);
2710         u8 i;
2711         priv->being_init_adapter = false;
2712         priv->txbuffsize = 1600;//1024;
2713         priv->txfwbuffersize = 4096;
2714         priv->txringcount = 64;//32;
2715         //priv->txbeaconcount = priv->txringcount;
2716         priv->txbeaconcount = 2;
2717         priv->rxbuffersize = 9100;//2048;//1024;
2718         priv->rxringcount = MAX_RX_COUNT;//64;
2719         priv->irq_enabled=0;
2720         priv->card_8192 = NIC_8192E;
2721         priv->rx_skb_complete = 1;
2722         priv->chan = 1; //set to channel 1
2723         priv->RegWirelessMode = WIRELESS_MODE_AUTO;
2724         priv->RegChannelPlan = 0xf;
2725         priv->nrxAMPDU_size = 0;
2726         priv->nrxAMPDU_aggr_num = 0;
2727         priv->last_rxdesc_tsf_high = 0;
2728         priv->last_rxdesc_tsf_low = 0;
2729         priv->ieee80211->mode = WIRELESS_MODE_AUTO; //SET AUTO
2730         priv->ieee80211->iw_mode = IW_MODE_INFRA;
2731         priv->ieee80211->ieee_up=0;
2732         priv->retry_rts = DEFAULT_RETRY_RTS;
2733         priv->retry_data = DEFAULT_RETRY_DATA;
2734         priv->ieee80211->rts = DEFAULT_RTS_THRESHOLD;
2735         priv->ieee80211->rate = 110; //11 mbps
2736         priv->ieee80211->short_slot = 1;
2737         priv->promisc = (dev->flags & IFF_PROMISC) ? 1:0;
2738         priv->bcck_in_ch14 = false;
2739         priv->bfsync_processing  = false;
2740         priv->CCKPresentAttentuation = 0;
2741         priv->rfa_txpowertrackingindex = 0;
2742         priv->rfc_txpowertrackingindex = 0;
2743         priv->CckPwEnl = 6;
2744         priv->ScanDelay = 50;//for Scan TODO
2745         //added by amy for silent reset
2746         priv->ResetProgress = RESET_TYPE_NORESET;
2747         priv->bForcedSilentReset = 0;
2748         priv->bDisableNormalResetCheck = false;
2749         priv->force_reset = false;
2750         //added by amy for power save
2751         priv->RegRfOff = 0;
2752         priv->ieee80211->RfOffReason = 0;
2753         priv->RFChangeInProgress = false;
2754         priv->bHwRfOffAction = 0;
2755         priv->SetRFPowerStateInProgress = false;
2756         priv->ieee80211->PowerSaveControl.bInactivePs = true;
2757         priv->ieee80211->PowerSaveControl.bIPSModeBackup = false;
2758         //just for debug
2759         priv->txpower_checkcnt = 0;
2760         priv->thermal_readback_index =0;
2761         priv->txpower_tracking_callback_cnt = 0;
2762         priv->ccktxpower_adjustcnt_ch14 = 0;
2763         priv->ccktxpower_adjustcnt_not_ch14 = 0;
2764
2765         priv->ieee80211->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
2766         priv->ieee80211->iw_mode = IW_MODE_INFRA;
2767         priv->ieee80211->softmac_features  = IEEE_SOFTMAC_SCAN |
2768                 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
2769                 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE;/* |
2770                 IEEE_SOFTMAC_BEACONS;*///added by amy 080604 //|  //IEEE_SOFTMAC_SINGLE_QUEUE;
2771
2772         priv->ieee80211->active_scan = 1;
2773         priv->ieee80211->modulation = IEEE80211_CCK_MODULATION | IEEE80211_OFDM_MODULATION;
2774         priv->ieee80211->host_encrypt = 1;
2775         priv->ieee80211->host_decrypt = 1;
2776         //priv->ieee80211->start_send_beacons = NULL;//rtl819xusb_beacon_tx;//-by amy 080604
2777         //priv->ieee80211->stop_send_beacons = NULL;//rtl8192_beacon_stop;//-by amy 080604
2778         priv->ieee80211->start_send_beacons = rtl8192_start_beacon;//+by david 081107
2779         priv->ieee80211->stop_send_beacons = rtl8192_stop_beacon;//+by david 081107
2780         priv->ieee80211->softmac_hard_start_xmit = rtl8192_hard_start_xmit;
2781         priv->ieee80211->set_chan = rtl8192_set_chan;
2782         priv->ieee80211->link_change = rtl8192_link_change;
2783         priv->ieee80211->softmac_data_hard_start_xmit = rtl8192_hard_data_xmit;
2784         priv->ieee80211->data_hard_stop = rtl8192_data_hard_stop;
2785         priv->ieee80211->data_hard_resume = rtl8192_data_hard_resume;
2786         priv->ieee80211->init_wmmparam_flag = 0;
2787         priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
2788         priv->ieee80211->check_nic_enough_desc = check_nic_enough_desc;
2789         priv->ieee80211->tx_headroom = sizeof(TX_FWINFO_8190PCI);
2790         priv->ieee80211->qos_support = 1;
2791         priv->ieee80211->dot11PowerSaveMode = 0;
2792         //added by WB
2793 //      priv->ieee80211->SwChnlByTimerHandler = rtl8192_phy_SwChnl;
2794         priv->ieee80211->SetBWModeHandler = rtl8192_SetBWMode;
2795         priv->ieee80211->handle_assoc_response = rtl8192_handle_assoc_response;
2796         priv->ieee80211->handle_beacon = rtl8192_handle_beacon;
2797
2798         priv->ieee80211->sta_wake_up = rtl8192_hw_wakeup;
2799 //      priv->ieee80211->ps_request_tx_ack = rtl8192_rq_tx_ack;
2800         priv->ieee80211->enter_sleep_state = rtl8192_hw_to_sleep;
2801         priv->ieee80211->ps_is_queue_empty = rtl8192_is_tx_queue_empty;
2802         //added by david
2803         priv->ieee80211->GetNmodeSupportBySecCfg = GetNmodeSupportBySecCfg8190Pci;
2804         priv->ieee80211->SetWirelessMode = rtl8192_SetWirelessMode;
2805         priv->ieee80211->GetHalfNmodeSupportByAPsHandler = GetHalfNmodeSupportByAPs819xPci;
2806
2807         //added by amy
2808         priv->ieee80211->InitialGainHandler = InitialGain819xPci;
2809
2810         priv->card_type = USB;
2811         {
2812                 priv->ShortRetryLimit = 0x30;
2813                 priv->LongRetryLimit = 0x30;
2814         }
2815         priv->EarlyRxThreshold = 7;
2816         priv->enable_gpio0 = 0;
2817
2818         priv->TransmitConfig = 0;
2819
2820         priv->ReceiveConfig = RCR_ADD3  |
2821                 RCR_AMF | RCR_ADF |             //accept management/data
2822                 RCR_AICV |                      //accept control frame for SW AP needs PS-poll, 2005.07.07, by rcnjko.
2823                 RCR_AB | RCR_AM | RCR_APM |     //accept BC/MC/UC
2824                 RCR_AAP | ((u32)7<<RCR_MXDMA_OFFSET) |
2825                 ((u32)7 << RCR_FIFO_OFFSET) | RCR_ONLYERLPKT;
2826
2827         priv->irq_mask =        (u32)(IMR_ROK | IMR_VODOK | IMR_VIDOK | IMR_BEDOK | IMR_BKDOK |\
2828                                 IMR_HCCADOK | IMR_MGNTDOK | IMR_COMDOK | IMR_HIGHDOK |\
2829                                 IMR_BDOK | IMR_RXCMDOK | IMR_TIMEOUT0 | IMR_RDU | IMR_RXFOVW    |\
2830                                 IMR_TXFOVW | IMR_BcnInt | IMR_TBDOK | IMR_TBDER);
2831
2832         priv->AcmControl = 0;
2833         priv->pFirmware = (rt_firmware*)vmalloc(sizeof(rt_firmware));
2834         if (priv->pFirmware)
2835         memset(priv->pFirmware, 0, sizeof(rt_firmware));
2836
2837         /* rx related queue */
2838         skb_queue_head_init(&priv->rx_queue);
2839         skb_queue_head_init(&priv->skb_queue);
2840
2841         /* Tx related queue */
2842         for(i = 0; i < MAX_QUEUE_SIZE; i++) {
2843                 skb_queue_head_init(&priv->ieee80211->skb_waitQ [i]);
2844         }
2845         for(i = 0; i < MAX_QUEUE_SIZE; i++) {
2846                 skb_queue_head_init(&priv->ieee80211->skb_aggQ [i]);
2847         }
2848         priv->rf_set_chan = rtl8192_phy_SwChnl;
2849 }
2850
2851 //init lock here
2852 static void rtl8192_init_priv_lock(struct r8192_priv* priv)
2853 {
2854         spin_lock_init(&priv->tx_lock);
2855         spin_lock_init(&priv->irq_lock);//added by thomas
2856         spin_lock_init(&priv->irq_th_lock);
2857         spin_lock_init(&priv->rf_ps_lock);
2858         spin_lock_init(&priv->ps_lock);
2859         //spin_lock_init(&priv->rf_lock);
2860         sema_init(&priv->wx_sem,1);
2861         sema_init(&priv->rf_sem,1);
2862 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16))
2863         sema_init(&priv->mutex, 1);
2864 #else
2865         mutex_init(&priv->mutex);
2866 #endif
2867 }
2868
2869 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
2870 extern  void    rtl819x_watchdog_wqcallback(struct work_struct *work);
2871 #else
2872 extern  void    rtl819x_watchdog_wqcallback(struct net_device *dev);
2873 #endif
2874
2875 void rtl8192_irq_rx_tasklet(struct r8192_priv *priv);
2876 void rtl8192_irq_tx_tasklet(struct r8192_priv *priv);
2877 void rtl8192_prepare_beacon(struct r8192_priv *priv);
2878 //init tasklet and wait_queue here. only 2.6 above kernel is considered
2879 #define DRV_NAME "wlan0"
2880 static void rtl8192_init_priv_task(struct net_device* dev)
2881 {
2882         struct r8192_priv *priv = ieee80211_priv(dev);
2883
2884 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
2885 #ifdef PF_SYNCTHREAD
2886         priv->priv_wq = create_workqueue(DRV_NAME,0);
2887 #else
2888         priv->priv_wq = create_workqueue(DRV_NAME);
2889 #endif
2890 #endif
2891
2892 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
2893 //      INIT_WORK(&priv->reset_wq, (void(*)(void*)) rtl8192_restart);
2894         INIT_WORK(&priv->reset_wq,  rtl8192_restart);
2895 //      INIT_DELAYED_WORK(&priv->watch_dog_wq, hal_dm_watchdog);
2896         INIT_DELAYED_WORK(&priv->watch_dog_wq, rtl819x_watchdog_wqcallback);
2897         INIT_DELAYED_WORK(&priv->txpower_tracking_wq,  dm_txpower_trackingcallback);
2898         INIT_DELAYED_WORK(&priv->rfpath_check_wq,  dm_rf_pathcheck_workitemcallback);
2899         INIT_DELAYED_WORK(&priv->update_beacon_wq, rtl8192_update_beacon);
2900         //INIT_WORK(&priv->SwChnlWorkItem,  rtl8192_SwChnl_WorkItem);
2901         //INIT_WORK(&priv->SetBWModeWorkItem,  rtl8192_SetBWModeWorkItem);
2902         INIT_WORK(&priv->qos_activate, rtl8192_qos_activate);
2903         INIT_DELAYED_WORK(&priv->ieee80211->hw_wakeup_wq,(void*) rtl8192_hw_wakeup_wq);
2904         INIT_DELAYED_WORK(&priv->ieee80211->hw_sleep_wq,(void*) rtl8192_hw_sleep_wq);
2905
2906 #else
2907 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
2908         tq_init(&priv->reset_wq, (void*)rtl8192_restart, dev);
2909         tq_init(&priv->watch_dog_wq, (void*)rtl819x_watchdog_wqcallback, dev);
2910         tq_init(&priv->txpower_tracking_wq, (void*)dm_txpower_trackingcallback, dev);
2911         tq_init(&priv->rfpath_check_wq, (void*)dm_rf_pathcheck_workitemcallback, dev);
2912         tq_init(&priv->update_beacon_wq, (void*)rtl8192_update_beacon, dev);
2913         //tq_init(&priv->SwChnlWorkItem, (void*) rtl8192_SwChnl_WorkItem, dev);
2914         //tq_init(&priv->SetBWModeWorkItem, (void*)rtl8192_SetBWModeWorkItem, dev);
2915         tq_init(&priv->qos_activate, (void *)rtl8192_qos_activate, dev);
2916         tq_init(&priv->ieee80211->hw_wakeup_wq,(void*) rtl8192_hw_wakeup_wq, dev);
2917         tq_init(&priv->ieee80211->hw_sleep_wq,(void*) rtl8192_hw_sleep_wq, dev);
2918
2919 #else
2920         INIT_WORK(&priv->reset_wq,(void(*)(void*)) rtl8192_restart,dev);
2921 //      INIT_WORK(&priv->watch_dog_wq, (void(*)(void*)) hal_dm_watchdog,dev);
2922         INIT_WORK(&priv->watch_dog_wq, (void(*)(void*)) rtl819x_watchdog_wqcallback,dev);
2923         INIT_WORK(&priv->txpower_tracking_wq, (void(*)(void*)) dm_txpower_trackingcallback,dev);
2924         INIT_WORK(&priv->rfpath_check_wq, (void(*)(void*)) dm_rf_pathcheck_workitemcallback,dev);
2925         INIT_WORK(&priv->update_beacon_wq, (void(*)(void*))rtl8192_update_beacon,dev);
2926         //INIT_WORK(&priv->SwChnlWorkItem, (void(*)(void*)) rtl8192_SwChnl_WorkItem, dev);
2927         //INIT_WORK(&priv->SetBWModeWorkItem, (void(*)(void*)) rtl8192_SetBWModeWorkItem, dev);
2928         INIT_WORK(&priv->qos_activate, (void(*)(void *))rtl8192_qos_activate, dev);
2929         INIT_WORK(&priv->ieee80211->hw_wakeup_wq,(void*) rtl8192_hw_wakeup_wq, dev);
2930         INIT_WORK(&priv->ieee80211->hw_sleep_wq,(void*) rtl8192_hw_sleep_wq, dev);
2931 #endif
2932 #endif
2933
2934         tasklet_init(&priv->irq_rx_tasklet,
2935              (void(*)(unsigned long))rtl8192_irq_rx_tasklet,
2936              (unsigned long)priv);
2937         tasklet_init(&priv->irq_tx_tasklet,
2938              (void(*)(unsigned long))rtl8192_irq_tx_tasklet,
2939              (unsigned long)priv);
2940         tasklet_init(&priv->irq_prepare_beacon_tasklet,
2941                 (void(*)(unsigned long))rtl8192_prepare_beacon,
2942                 (unsigned long)priv);
2943 }
2944
2945 static void rtl8192_get_eeprom_size(struct net_device* dev)
2946 {
2947         u16 curCR = 0;
2948         struct r8192_priv *priv = ieee80211_priv(dev);
2949         RT_TRACE(COMP_INIT, "===========>%s()\n", __FUNCTION__);
2950         curCR = read_nic_dword(dev, EPROM_CMD);
2951         RT_TRACE(COMP_INIT, "read from Reg Cmd9346CR(%x):%x\n", EPROM_CMD, curCR);
2952         //whether need I consider BIT5?
2953         priv->epromtype = (curCR & EPROM_CMD_9356SEL) ? EPROM_93c56 : EPROM_93c46;
2954         RT_TRACE(COMP_INIT, "<===========%s(), epromtype:%d\n", __FUNCTION__, priv->epromtype);
2955 }
2956
2957 //used to swap endian. as ntohl & htonl are not neccessary to swap endian, so use this instead.
2958 static inline u16 endian_swap(u16* data)
2959 {
2960         u16 tmp = *data;
2961         *data = (tmp >> 8) | (tmp << 8);
2962         return *data;
2963 }
2964
2965 /*
2966  *      Note:   Adapter->EEPROMAddressSize should be set before this function call.
2967  *                      EEPROM address size can be got through GetEEPROMSize8185()
2968 */
2969 static void rtl8192_read_eeprom_info(struct net_device* dev)
2970 {
2971         struct r8192_priv *priv = ieee80211_priv(dev);
2972
2973         u8                      tempval;
2974 #ifdef RTL8192E
2975         u8                      ICVer8192, ICVer8256;
2976 #endif
2977         u16                     i,usValue, IC_Version;
2978         u16                     EEPROMId;
2979 #ifdef RTL8190P
2980         u8                      offset;//, tmpAFR;
2981         u8                      EepromTxPower[100];
2982 #endif
2983         u8 bMac_Tmp_Addr[6] = {0x00, 0xe0, 0x4c, 0x00, 0x00, 0x01};
2984         RT_TRACE(COMP_INIT, "====> rtl8192_read_eeprom_info\n");
2985
2986
2987         // TODO: I don't know if we need to apply EF function to EEPROM read function
2988
2989         //2 Read EEPROM ID to make sure autoload is success
2990         EEPROMId = eprom_read(dev, 0);
2991         if( EEPROMId != RTL8190_EEPROM_ID )
2992         {
2993                 RT_TRACE(COMP_ERR, "EEPROM ID is invalid:%x, %x\n", EEPROMId, RTL8190_EEPROM_ID);
2994                 priv->AutoloadFailFlag=true;
2995         }
2996         else
2997         {
2998                 priv->AutoloadFailFlag=false;
2999         }
3000
3001         //
3002         // Assign Chip Version ID
3003         //
3004         // Read IC Version && Channel Plan
3005         if(!priv->AutoloadFailFlag)
3006         {
3007                 // VID, PID
3008                 priv->eeprom_vid = eprom_read(dev, (EEPROM_VID >> 1));
3009                 priv->eeprom_did = eprom_read(dev, (EEPROM_DID >> 1));
3010
3011                 usValue = eprom_read(dev, (u16)(EEPROM_Customer_ID>>1)) >> 8 ;
3012                 priv->eeprom_CustomerID = (u8)( usValue & 0xff);
3013                 usValue = eprom_read(dev, (EEPROM_ICVersion_ChannelPlan>>1));
3014                 priv->eeprom_ChannelPlan = usValue&0xff;
3015                 IC_Version = ((usValue&0xff00)>>8);
3016
3017 #ifdef RTL8190P
3018                 priv->card_8192_version = (VERSION_8190)(IC_Version);
3019 #else
3020         #ifdef RTL8192E
3021                 ICVer8192 = (IC_Version&0xf);           //bit0~3; 1:A cut, 2:B cut, 3:C cut...
3022                 ICVer8256 = ((IC_Version&0xf0)>>4);//bit4~6, bit7 reserved for other RF chip; 1:A cut, 2:B cut, 3:C cut...
3023                 RT_TRACE(COMP_INIT, "\nICVer8192 = 0x%x\n", ICVer8192);
3024                 RT_TRACE(COMP_INIT, "\nICVer8256 = 0x%x\n", ICVer8256);
3025                 if(ICVer8192 == 0x2)    //B-cut
3026                 {
3027                         if(ICVer8256 == 0x5) //E-cut
3028                                 priv->card_8192_version= VERSION_8190_BE;
3029                 }
3030         #endif
3031 #endif
3032                 switch(priv->card_8192_version)
3033                 {
3034                         case VERSION_8190_BD:
3035                         case VERSION_8190_BE:
3036                                 break;
3037                         default:
3038                                 priv->card_8192_version = VERSION_8190_BD;
3039                                 break;
3040                 }
3041                 RT_TRACE(COMP_INIT, "\nIC Version = 0x%x\n", priv->card_8192_version);
3042         }
3043         else
3044         {
3045                 priv->card_8192_version = VERSION_8190_BD;
3046                 priv->eeprom_vid = 0;
3047                 priv->eeprom_did = 0;
3048                 priv->eeprom_CustomerID = 0;
3049                 priv->eeprom_ChannelPlan = 0;
3050                 RT_TRACE(COMP_INIT, "\nIC Version = 0x%x\n", 0xff);
3051         }
3052
3053         RT_TRACE(COMP_INIT, "EEPROM VID = 0x%4x\n", priv->eeprom_vid);
3054         RT_TRACE(COMP_INIT, "EEPROM DID = 0x%4x\n", priv->eeprom_did);
3055         RT_TRACE(COMP_INIT,"EEPROM Customer ID: 0x%2x\n", priv->eeprom_CustomerID);
3056
3057         //2 Read Permanent MAC address
3058         if(!priv->AutoloadFailFlag)
3059         {
3060                 for(i = 0; i < 6; i += 2)
3061                 {
3062                         usValue = eprom_read(dev, (u16) ((EEPROM_NODE_ADDRESS_BYTE_0+i)>>1));
3063                         *(u16*)(&dev->dev_addr[i]) = usValue;
3064                 }
3065         } else {
3066                 // when auto load failed,  the last address byte set to be a random one.
3067                 // added by david woo.2007/11/7
3068                 memcpy(dev->dev_addr, bMac_Tmp_Addr, 6);
3069                 #if 0
3070                 for(i = 0; i < 6; i++)
3071                 {
3072                         Adapter->PermanentAddress[i] = sMacAddr[i];
3073                         PlatformEFIOWrite1Byte(Adapter, IDR0+i, sMacAddr[i]);
3074                 }
3075                 #endif
3076         }
3077
3078         RT_TRACE(COMP_INIT, "Permanent Address = %02x-%02x-%02x-%02x-%02x-%02x\n",
3079                         dev->dev_addr[0], dev->dev_addr[1],
3080                         dev->dev_addr[2], dev->dev_addr[3],
3081                         dev->dev_addr[4], dev->dev_addr[5]);
3082
3083                 //2 TX Power Check EEPROM Fail or not
3084         if(priv->card_8192_version > VERSION_8190_BD) {
3085                 priv->bTXPowerDataReadFromEEPORM = true;
3086         } else {
3087                 priv->bTXPowerDataReadFromEEPORM = false;
3088         }
3089
3090         // 2007/11/15 MH 8190PCI Default=2T4R, 8192PCIE dafault=1T2R
3091         priv->rf_type = RTL819X_DEFAULT_RF_TYPE;
3092
3093         if(priv->card_8192_version > VERSION_8190_BD)
3094         {
3095                 // Read RF-indication and Tx Power gain index diff of legacy to HT OFDM rate.
3096                 if(!priv->AutoloadFailFlag)
3097                 {
3098                         tempval = (eprom_read(dev, (EEPROM_RFInd_PowerDiff>>1))) & 0xff;
3099                         priv->EEPROMLegacyHTTxPowerDiff = tempval & 0xf;        // bit[3:0]
3100
3101                         if (tempval&0x80)       //RF-indication, bit[7]
3102                                 priv->rf_type = RF_1T2R;
3103                         else
3104                                 priv->rf_type = RF_2T4R;
3105                 }
3106                 else
3107                 {
3108                         priv->EEPROMLegacyHTTxPowerDiff = EEPROM_Default_LegacyHTTxPowerDiff;
3109                 }
3110                 RT_TRACE(COMP_INIT, "EEPROMLegacyHTTxPowerDiff = %d\n",
3111                         priv->EEPROMLegacyHTTxPowerDiff);
3112
3113                 // Read ThermalMeter from EEPROM
3114                 if(!priv->AutoloadFailFlag)
3115                 {
3116                         priv->EEPROMThermalMeter = (u8)(((eprom_read(dev, (EEPROM_ThermalMeter>>1))) & 0xff00)>>8);
3117                 }
3118                 else
3119                 {
3120                         priv->EEPROMThermalMeter = EEPROM_Default_ThermalMeter;
3121                 }
3122                 RT_TRACE(COMP_INIT, "ThermalMeter = %d\n", priv->EEPROMThermalMeter);
3123                 //vivi, for tx power track
3124                 priv->TSSI_13dBm = priv->EEPROMThermalMeter *100;
3125
3126                 if(priv->epromtype == EPROM_93c46)
3127                 {
3128                 // Read antenna tx power offset of B/C/D to A and CrystalCap from EEPROM
3129                 if(!priv->AutoloadFailFlag)
3130                 {
3131                                 usValue = eprom_read(dev, (EEPROM_TxPwDiff_CrystalCap>>1));
3132                                 priv->EEPROMAntPwDiff = (usValue&0x0fff);
3133                                 priv->EEPROMCrystalCap = (u8)((usValue&0xf000)>>12);
3134                 }
3135                 else
3136                 {
3137                                 priv->EEPROMAntPwDiff = EEPROM_Default_AntTxPowerDiff;
3138                                 priv->EEPROMCrystalCap = EEPROM_Default_TxPwDiff_CrystalCap;
3139                 }
3140                         RT_TRACE(COMP_INIT, "EEPROMAntPwDiff = %d\n", priv->EEPROMAntPwDiff);
3141                         RT_TRACE(COMP_INIT, "EEPROMCrystalCap = %d\n", priv->EEPROMCrystalCap);
3142
3143                 //
3144                 // Get per-channel Tx Power Level
3145                 //
3146                 for(i=0; i<14; i+=2)
3147                 {
3148                         if(!priv->AutoloadFailFlag)
3149                         {
3150                                 usValue = eprom_read(dev, (u16) ((EEPROM_TxPwIndex_CCK+i)>>1) );
3151                         }
3152                         else
3153                         {
3154                                 usValue = EEPROM_Default_TxPower;
3155                         }
3156                         *((u16*)(&priv->EEPROMTxPowerLevelCCK[i])) = usValue;
3157                         RT_TRACE(COMP_INIT,"CCK Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelCCK[i]);
3158                         RT_TRACE(COMP_INIT, "CCK Tx Power Level, Index %d = 0x%02x\n", i+1, priv->EEPROMTxPowerLevelCCK[i+1]);
3159                 }
3160                 for(i=0; i<14; i+=2)
3161                 {
3162                         if(!priv->AutoloadFailFlag)
3163                         {
3164                                 usValue = eprom_read(dev, (u16) ((EEPROM_TxPwIndex_OFDM_24G+i)>>1) );
3165                         }
3166                         else
3167                         {
3168                                 usValue = EEPROM_Default_TxPower;
3169                         }
3170                         *((u16*)(&priv->EEPROMTxPowerLevelOFDM24G[i])) = usValue;
3171                         RT_TRACE(COMP_INIT, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelOFDM24G[i]);
3172                         RT_TRACE(COMP_INIT, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i+1, priv->EEPROMTxPowerLevelOFDM24G[i+1]);
3173                 }
3174                 }
3175                 else if(priv->epromtype== EPROM_93c56)
3176                 {
3177                 #ifdef RTL8190P
3178                         // Read CrystalCap from EEPROM
3179                         if(!priv->AutoloadFailFlag)
3180                         {
3181                                 priv->EEPROMAntPwDiff = EEPROM_Default_AntTxPowerDiff;
3182                                 priv->EEPROMCrystalCap = (u8)(((eprom_read(dev, (EEPROM_C56_CrystalCap>>1))) & 0xf000)>>12);
3183                         }
3184                         else
3185                         {
3186                                 priv->EEPROMAntPwDiff = EEPROM_Default_AntTxPowerDiff;
3187                                 priv->EEPROMCrystalCap = EEPROM_Default_TxPwDiff_CrystalCap;
3188                         }
3189                         RT_TRACE(COMP_INIT,"EEPROMAntPwDiff = %d\n", priv->EEPROMAntPwDiff);
3190                         RT_TRACE(COMP_INIT, "EEPROMCrystalCap = %d\n", priv->EEPROMCrystalCap);
3191
3192                         // Get Tx Power Level by Channel
3193                         if(!priv->AutoloadFailFlag)
3194                         {
3195                                     // Read Tx power of Channel 1 ~ 14 from EEPROM.
3196                                for(i = 0; i < 12; i+=2)
3197                                 {
3198                                         if (i <6)
3199                                                 offset = EEPROM_C56_RfA_CCK_Chnl1_TxPwIndex + i;
3200                                         else
3201                                                 offset = EEPROM_C56_RfC_CCK_Chnl1_TxPwIndex + i - 6;
3202                                         usValue = eprom_read(dev, (offset>>1));
3203                                        *((u16*)(&EepromTxPower[i])) = usValue;
3204                                 }
3205
3206                                for(i = 0; i < 12; i++)
3207                                 {
3208                                         if (i <= 2)
3209                                                 priv->EEPROMRfACCKChnl1TxPwLevel[i] = EepromTxPower[i];
3210                                         else if ((i >=3 )&&(i <= 5))
3211                                                 priv->EEPROMRfAOfdmChnlTxPwLevel[i-3] = EepromTxPower[i];
3212                                         else if ((i >=6 )&&(i <= 8))
3213                                                 priv->EEPROMRfCCCKChnl1TxPwLevel[i-6] = EepromTxPower[i];
3214                                         else
3215                                                 priv->EEPROMRfCOfdmChnlTxPwLevel[i-9] = EepromTxPower[i];
3216                                 }
3217                         }
3218                         else
3219                         {
3220                                 priv->EEPROMRfACCKChnl1TxPwLevel[0] = EEPROM_Default_TxPowerLevel;
3221                                 priv->EEPROMRfACCKChnl1TxPwLevel[1] = EEPROM_Default_TxPowerLevel;
3222                                 priv->EEPROMRfACCKChnl1TxPwLevel[2] = EEPROM_Default_TxPowerLevel;
3223
3224                                 priv->EEPROMRfAOfdmChnlTxPwLevel[0] = EEPROM_Default_TxPowerLevel;
3225                                 priv->EEPROMRfAOfdmChnlTxPwLevel[1] = EEPROM_Default_TxPowerLevel;
3226                                 priv->EEPROMRfAOfdmChnlTxPwLevel[2] = EEPROM_Default_TxPowerLevel;
3227
3228                                 priv->EEPROMRfCCCKChnl1TxPwLevel[0] = EEPROM_Default_TxPowerLevel;
3229                                 priv->EEPROMRfCCCKChnl1TxPwLevel[1] = EEPROM_Default_TxPowerLevel;
3230                                 priv->EEPROMRfCCCKChnl1TxPwLevel[2] = EEPROM_Default_TxPowerLevel;
3231
3232                                 priv->EEPROMRfCOfdmChnlTxPwLevel[0] = EEPROM_Default_TxPowerLevel;
3233                                 priv->EEPROMRfCOfdmChnlTxPwLevel[1] = EEPROM_Default_TxPowerLevel;
3234                                 priv->EEPROMRfCOfdmChnlTxPwLevel[2] = EEPROM_Default_TxPowerLevel;
3235                         }
3236                         RT_TRACE(COMP_INIT, "priv->EEPROMRfACCKChnl1TxPwLevel[0] = 0x%x\n", priv->EEPROMRfACCKChnl1TxPwLevel[0]);
3237                         RT_TRACE(COMP_INIT, "priv->EEPROMRfACCKChnl1TxPwLevel[1] = 0x%x\n", priv->EEPROMRfACCKChnl1TxPwLevel[1]);
3238                         RT_TRACE(COMP_INIT, "priv->EEPROMRfACCKChnl1TxPwLevel[2] = 0x%x\n", priv->EEPROMRfACCKChnl1TxPwLevel[2]);
3239                         RT_TRACE(COMP_INIT, "priv->EEPROMRfAOfdmChnlTxPwLevel[0] = 0x%x\n", priv->EEPROMRfAOfdmChnlTxPwLevel[0]);
3240                         RT_TRACE(COMP_INIT, "priv->EEPROMRfAOfdmChnlTxPwLevel[1] = 0x%x\n", priv->EEPROMRfAOfdmChnlTxPwLevel[1]);
3241                         RT_TRACE(COMP_INIT, "priv->EEPROMRfAOfdmChnlTxPwLevel[2] = 0x%x\n", priv->EEPROMRfAOfdmChnlTxPwLevel[2]);
3242                         RT_TRACE(COMP_INIT, "priv->EEPROMRfCCCKChnl1TxPwLevel[0] = 0x%x\n", priv->EEPROMRfCCCKChnl1TxPwLevel[0]);
3243                         RT_TRACE(COMP_INIT, "priv->EEPROMRfCCCKChnl1TxPwLevel[1] = 0x%x\n", priv->EEPROMRfCCCKChnl1TxPwLevel[1]);
3244                         RT_TRACE(COMP_INIT, "priv->EEPROMRfCCCKChnl1TxPwLevel[2] = 0x%x\n", priv->EEPROMRfCCCKChnl1TxPwLevel[2]);
3245                         RT_TRACE(COMP_INIT, "priv->EEPROMRfCOfdmChnlTxPwLevel[0] = 0x%x\n", priv->EEPROMRfCOfdmChnlTxPwLevel[0]);
3246                         RT_TRACE(COMP_INIT, "priv->EEPROMRfCOfdmChnlTxPwLevel[1] = 0x%x\n", priv->EEPROMRfCOfdmChnlTxPwLevel[1]);
3247                         RT_TRACE(COMP_INIT, "priv->EEPROMRfCOfdmChnlTxPwLevel[2] = 0x%x\n", priv->EEPROMRfCOfdmChnlTxPwLevel[2]);
3248 #endif
3249
3250                 }
3251                 //
3252                 // Update HAL variables.
3253                 //
3254                 if(priv->epromtype == EPROM_93c46)
3255                 {
3256                         for(i=0; i<14; i++)
3257                         {
3258                                 priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK[i];
3259                                 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[i];
3260                         }
3261                         priv->LegacyHTTxPowerDiff = priv->EEPROMLegacyHTTxPowerDiff;
3262                 // Antenna B gain offset to antenna A, bit0~3
3263                         priv->AntennaTxPwDiff[0] = (priv->EEPROMAntPwDiff & 0xf);
3264                 // Antenna C gain offset to antenna A, bit4~7
3265                         priv->AntennaTxPwDiff[1] = ((priv->EEPROMAntPwDiff & 0xf0)>>4);
3266                 // Antenna D gain offset to antenna A, bit8~11
3267                         priv->AntennaTxPwDiff[2] = ((priv->EEPROMAntPwDiff & 0xf00)>>8);
3268                 // CrystalCap, bit12~15
3269                         priv->CrystalCap = priv->EEPROMCrystalCap;
3270                 // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
3271                         priv->ThermalMeter[0] = (priv->EEPROMThermalMeter & 0xf);
3272                         priv->ThermalMeter[1] = ((priv->EEPROMThermalMeter & 0xf0)>>4);
3273                 }
3274                 else if(priv->epromtype == EPROM_93c56)
3275                 {
3276                         //char  cck_pwr_diff_a=0, cck_pwr_diff_c=0;
3277
3278                         //cck_pwr_diff_a = pHalData->EEPROMRfACCKChnl7TxPwLevel - pHalData->EEPROMRfAOfdmChnlTxPwLevel[1];
3279                         //cck_pwr_diff_c = pHalData->EEPROMRfCCCKChnl7TxPwLevel - pHalData->EEPROMRfCOfdmChnlTxPwLevel[1];
3280                         for(i=0; i<3; i++)      // channel 1~3 use the same Tx Power Level.
3281                         {
3282                                 priv->TxPowerLevelCCK_A[i]  = priv->EEPROMRfACCKChnl1TxPwLevel[0];
3283                                 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[0];
3284                                 priv->TxPowerLevelCCK_C[i] =  priv->EEPROMRfCCCKChnl1TxPwLevel[0];
3285                                 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[0];
3286                         }
3287                         for(i=3; i<9; i++)      // channel 4~9 use the same Tx Power Level
3288                         {
3289                                 priv->TxPowerLevelCCK_A[i]  = priv->EEPROMRfACCKChnl1TxPwLevel[1];
3290                                 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[1];
3291                                 priv->TxPowerLevelCCK_C[i] =  priv->EEPROMRfCCCKChnl1TxPwLevel[1];
3292                                 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[1];
3293                         }
3294                         for(i=9; i<14; i++)     // channel 10~14 use the same Tx Power Level
3295                         {
3296                                 priv->TxPowerLevelCCK_A[i]  = priv->EEPROMRfACCKChnl1TxPwLevel[2];
3297                                 priv->TxPowerLevelOFDM24G_A[i] = priv->EEPROMRfAOfdmChnlTxPwLevel[2];
3298                                 priv->TxPowerLevelCCK_C[i] =  priv->EEPROMRfCCCKChnl1TxPwLevel[2];
3299                                 priv->TxPowerLevelOFDM24G_C[i] = priv->EEPROMRfCOfdmChnlTxPwLevel[2];
3300                         }
3301                         for(i=0; i<14; i++)
3302                                 RT_TRACE(COMP_INIT, "priv->TxPowerLevelCCK_A[%d] = 0x%x\n", i, priv->TxPowerLevelCCK_A[i]);
3303                         for(i=0; i<14; i++)
3304                                 RT_TRACE(COMP_INIT,"priv->TxPowerLevelOFDM24G_A[%d] = 0x%x\n", i, priv->TxPowerLevelOFDM24G_A[i]);
3305                         for(i=0; i<14; i++)
3306                                 RT_TRACE(COMP_INIT, "priv->TxPowerLevelCCK_C[%d] = 0x%x\n", i, priv->TxPowerLevelCCK_C[i]);
3307                         for(i=0; i<14; i++)
3308                                 RT_TRACE(COMP_INIT, "priv->TxPowerLevelOFDM24G_C[%d] = 0x%x\n", i, priv->TxPowerLevelOFDM24G_C[i]);
3309                         priv->LegacyHTTxPowerDiff = priv->EEPROMLegacyHTTxPowerDiff;
3310                         priv->AntennaTxPwDiff[0] = 0;
3311                         priv->AntennaTxPwDiff[1] = 0;
3312                         priv->AntennaTxPwDiff[2] = 0;
3313                         priv->CrystalCap = priv->EEPROMCrystalCap;
3314                         // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
3315                         priv->ThermalMeter[0] = (priv->EEPROMThermalMeter & 0xf);
3316                         priv->ThermalMeter[1] = ((priv->EEPROMThermalMeter & 0xf0)>>4);
3317                 }
3318         }
3319
3320         if(priv->rf_type == RF_1T2R)
3321         {
3322                 RT_TRACE(COMP_INIT, "\n1T2R config\n");
3323         }
3324         else if (priv->rf_type == RF_2T4R)
3325         {
3326                 RT_TRACE(COMP_INIT, "\n2T4R config\n");
3327         }
3328
3329         // 2008/01/16 MH We can only know RF type in the function. So we have to init
3330         // DIG RATR table again.
3331         init_rate_adaptive(dev);
3332
3333         //1 Make a copy for following variables and we can change them if we want
3334
3335         priv->rf_chip= RF_8256;
3336
3337         if(priv->RegChannelPlan == 0xf)
3338         {
3339                 priv->ChannelPlan = priv->eeprom_ChannelPlan;
3340         }
3341         else
3342         {
3343                 priv->ChannelPlan = priv->RegChannelPlan;
3344         }
3345
3346         //
3347         //  Used PID and DID to Set CustomerID
3348         //
3349         if( priv->eeprom_vid == 0x1186 &&  priv->eeprom_did == 0x3304 )
3350         {
3351                 priv->CustomerID =  RT_CID_DLINK;
3352         }
3353
3354         switch(priv->eeprom_CustomerID)
3355         {
3356                 case EEPROM_CID_DEFAULT:
3357                         priv->CustomerID = RT_CID_DEFAULT;
3358                         break;
3359                 case EEPROM_CID_CAMEO:
3360                         priv->CustomerID = RT_CID_819x_CAMEO;
3361                         break;
3362                 case  EEPROM_CID_RUNTOP:
3363                         priv->CustomerID = RT_CID_819x_RUNTOP;
3364                         break;
3365                 case EEPROM_CID_NetCore:
3366                         priv->CustomerID = RT_CID_819x_Netcore;
3367                         break;
3368                 case EEPROM_CID_TOSHIBA:        // Merge by Jacken, 2008/01/31
3369                         priv->CustomerID = RT_CID_TOSHIBA;
3370                         if(priv->eeprom_ChannelPlan&0x80)
3371                                 priv->ChannelPlan = priv->eeprom_ChannelPlan&0x7f;
3372                         else
3373                                 priv->ChannelPlan = 0x0;
3374                         RT_TRACE(COMP_INIT, "Toshiba ChannelPlan = 0x%x\n",
3375                                 priv->ChannelPlan);
3376                         break;
3377                 case EEPROM_CID_Nettronix:
3378                         priv->ScanDelay = 100;  //cosa add for scan
3379                         priv->CustomerID = RT_CID_Nettronix;
3380                         break;
3381                 case EEPROM_CID_Pronet:
3382                         priv->CustomerID = RT_CID_PRONET;
3383                         break;
3384                 case EEPROM_CID_DLINK:
3385                         priv->CustomerID = RT_CID_DLINK;
3386                         break;
3387
3388                 case EEPROM_CID_WHQL:
3389                         //Adapter->bInHctTest = TRUE;//do not supported
3390
3391                         //priv->bSupportTurboMode = FALSE;
3392                         //priv->bAutoTurboBy8186 = FALSE;
3393
3394                         //pMgntInfo->PowerSaveControl.bInactivePs = FALSE;
3395                         //pMgntInfo->PowerSaveControl.bIPSModeBackup = FALSE;
3396                         //pMgntInfo->PowerSaveControl.bLeisurePs = FALSE;
3397
3398                         break;
3399                 default:
3400                         // value from RegCustomerID
3401                         break;
3402         }
3403
3404         //Avoid the channel plan array overflow, by Bruce, 2007-08-27.
3405         if(priv->ChannelPlan > CHANNEL_PLAN_LEN - 1)
3406                 priv->ChannelPlan = 0; //FCC
3407
3408         switch(priv->CustomerID)
3409         {
3410                 case RT_CID_DEFAULT:
3411                 #ifdef RTL8190P
3412                         priv->LedStrategy = HW_LED;
3413                 #else
3414                         #ifdef RTL8192E
3415                         priv->LedStrategy = SW_LED_MODE1;
3416                         #endif
3417                 #endif
3418                         break;
3419
3420                 case RT_CID_819x_CAMEO:
3421                         priv->LedStrategy = SW_LED_MODE2;
3422                         break;
3423
3424                 case RT_CID_819x_RUNTOP:
3425                         priv->LedStrategy = SW_LED_MODE3;
3426                         break;
3427
3428                 case RT_CID_819x_Netcore:
3429                         priv->LedStrategy = SW_LED_MODE4;
3430                         break;
3431
3432                 case RT_CID_Nettronix:
3433                         priv->LedStrategy = SW_LED_MODE5;
3434                         break;
3435
3436                 case RT_CID_PRONET:
3437                         priv->LedStrategy = SW_LED_MODE6;
3438                         break;
3439
3440                 case RT_CID_TOSHIBA:   //Modify by Jacken 2008/01/31
3441                         // Do nothing.
3442                         //break;
3443
3444                 default:
3445                 #ifdef RTL8190P
3446                         priv->LedStrategy = HW_LED;
3447                 #else
3448                         #ifdef RTL8192E
3449                         priv->LedStrategy = SW_LED_MODE1;
3450                         #endif
3451                 #endif
3452                         break;
3453         }
3454 /*
3455         //2008.06.03, for WOL
3456         if( priv->eeprom_vid == 0x1186 &&  priv->eeprom_did == 0x3304)
3457                 priv->ieee80211->bSupportRemoteWakeUp = TRUE;
3458         else
3459                 priv->ieee80211->bSupportRemoteWakeUp = FALSE;
3460 */
3461         RT_TRACE(COMP_INIT, "RegChannelPlan(%d)\n", priv->RegChannelPlan);
3462         RT_TRACE(COMP_INIT, "ChannelPlan = %d \n", priv->ChannelPlan);
3463         RT_TRACE(COMP_INIT, "LedStrategy = %d \n", priv->LedStrategy);
3464         RT_TRACE(COMP_TRACE, "<==== ReadAdapterInfo\n");
3465
3466         return ;
3467 }
3468
3469
3470 static short rtl8192_get_channel_map(struct net_device * dev)
3471 {
3472         struct r8192_priv *priv = ieee80211_priv(dev);
3473 #ifdef ENABLE_DOT11D
3474         if(priv->ChannelPlan> COUNTRY_CODE_GLOBAL_DOMAIN){
3475                 printk("rtl8180_init:Error channel plan! Set to default.\n");
3476                 priv->ChannelPlan= 0;
3477         }
3478         RT_TRACE(COMP_INIT, "Channel plan is %d\n",priv->ChannelPlan);
3479
3480         rtl819x_set_channel_map(priv->ChannelPlan, priv);
3481 #else
3482         int ch,i;
3483         //Set Default Channel Plan
3484         if(!channels){
3485                 DMESG("No channels, aborting");
3486                 return -1;
3487         }
3488         ch=channels;
3489         priv->ChannelPlan= 0;//hikaru
3490          // set channels 1..14 allowed in given locale
3491         for (i=1; i<=14; i++) {
3492                 (priv->ieee80211->channel_map)[i] = (u8)(ch & 0x01);
3493                 ch >>= 1;
3494         }
3495 #endif
3496         return 0;
3497 }
3498
3499 static short rtl8192_init(struct net_device *dev)
3500 {
3501         struct r8192_priv *priv = ieee80211_priv(dev);
3502         memset(&(priv->stats),0,sizeof(struct Stats));
3503         rtl8192_init_priv_variable(dev);
3504         rtl8192_init_priv_lock(priv);
3505         rtl8192_init_priv_task(dev);
3506         rtl8192_get_eeprom_size(dev);
3507         rtl8192_read_eeprom_info(dev);
3508         rtl8192_get_channel_map(dev);
3509         init_hal_dm(dev);
3510         init_timer(&priv->watch_dog_timer);
3511         priv->watch_dog_timer.data = (unsigned long)dev;
3512         priv->watch_dog_timer.function = watch_dog_timer_callback;
3513 #if defined(IRQF_SHARED)
3514         if(request_irq(dev->irq, (void*)rtl8192_interrupt, IRQF_SHARED, dev->name, dev)){
3515 #else
3516         if(request_irq(dev->irq, (void *)rtl8192_interrupt, SA_SHIRQ, dev->name, dev)){
3517 #endif
3518                 printk("Error allocating IRQ %d",dev->irq);
3519                 return -1;
3520         }else{
3521                 priv->irq=dev->irq;
3522                 printk("IRQ %d",dev->irq);
3523         }
3524         if(rtl8192_pci_initdescring(dev)!=0){
3525                 printk("Endopoints initialization failed");
3526                 return -1;
3527         }
3528
3529         //rtl8192_rx_enable(dev);
3530         //rtl8192_adapter_start(dev);
3531 //#ifdef DEBUG_EPROM
3532 //      dump_eprom(dev);
3533 //#endif
3534         //rtl8192_dump_reg(dev);
3535         return 0;
3536 }
3537
3538 /******************************************************************************
3539  *function:  This function actually only set RRSR, RATR and BW_OPMODE registers
3540  *           not to do all the hw config as its name says
3541  *   input:  net_device dev
3542  *  output:  none
3543  *  return:  none
3544  *  notice:  This part need to modified according to the rate set we filtered
3545  * ****************************************************************************/
3546 static void rtl8192_hwconfig(struct net_device* dev)
3547 {
3548         u32 regRATR = 0, regRRSR = 0;
3549         u8 regBwOpMode = 0, regTmp = 0;
3550         struct r8192_priv *priv = ieee80211_priv(dev);
3551
3552 // Set RRSR, RATR, and BW_OPMODE registers
3553         //
3554         switch(priv->ieee80211->mode)
3555         {
3556         case WIRELESS_MODE_B:
3557                 regBwOpMode = BW_OPMODE_20MHZ;
3558                 regRATR = RATE_ALL_CCK;
3559                 regRRSR = RATE_ALL_CCK;
3560                 break;
3561         case WIRELESS_MODE_A:
3562                 regBwOpMode = BW_OPMODE_5G |BW_OPMODE_20MHZ;
3563                 regRATR = RATE_ALL_OFDM_AG;
3564                 regRRSR = RATE_ALL_OFDM_AG;
3565                 break;
3566         case WIRELESS_MODE_G:
3567                 regBwOpMode = BW_OPMODE_20MHZ;
3568                 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
3569                 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
3570                 break;
3571         case WIRELESS_MODE_AUTO:
3572         case WIRELESS_MODE_N_24G:
3573                 // It support CCK rate by default.
3574                 // CCK rate will be filtered out only when associated AP does not support it.
3575                 regBwOpMode = BW_OPMODE_20MHZ;
3576                         regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
3577                         regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
3578                 break;
3579         case WIRELESS_MODE_N_5G:
3580                 regBwOpMode = BW_OPMODE_5G;
3581                 regRATR = RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
3582                 regRRSR = RATE_ALL_OFDM_AG;
3583                 break;
3584         }
3585
3586         write_nic_byte(dev, BW_OPMODE, regBwOpMode);
3587         {
3588                 u32 ratr_value = 0;
3589                 ratr_value = regRATR;
3590                 if (priv->rf_type == RF_1T2R)
3591                 {
3592                         ratr_value &= ~(RATE_ALL_OFDM_2SS);
3593                 }
3594                 write_nic_dword(dev, RATR0, ratr_value);
3595                 write_nic_byte(dev, UFWP, 1);
3596         }
3597         regTmp = read_nic_byte(dev, 0x313);
3598         regRRSR = ((regTmp) << 24) | (regRRSR & 0x00ffffff);
3599         write_nic_dword(dev, RRSR, regRRSR);
3600
3601         //
3602         // Set Retry Limit here
3603         //
3604         write_nic_word(dev, RETRY_LIMIT,
3605                         priv->ShortRetryLimit << RETRY_LIMIT_SHORT_SHIFT | \
3606                         priv->LongRetryLimit << RETRY_LIMIT_LONG_SHIFT);
3607         // Set Contention Window here
3608
3609         // Set Tx AGC
3610
3611         // Set Tx Antenna including Feedback control
3612
3613         // Set Auto Rate fallback control
3614
3615
3616 }
3617
3618
3619 static RT_STATUS rtl8192_adapter_start(struct net_device *dev)
3620 {
3621         struct r8192_priv *priv = ieee80211_priv(dev);
3622 //      struct ieee80211_device *ieee = priv->ieee80211;
3623         u32 ulRegRead;
3624         RT_STATUS rtStatus = RT_STATUS_SUCCESS;
3625 //      static char szMACPHYRegFile[] = RTL819X_PHY_MACPHY_REG;
3626 //      static char szMACPHYRegPGFile[] = RTL819X_PHY_MACPHY_REG_PG;
3627         //u8 eRFPath;
3628         u8 tmpvalue;
3629 #ifdef RTL8192E
3630         u8 ICVersion,SwitchingRegulatorOutput;
3631 #endif
3632         bool bfirmwareok = true;
3633 #ifdef RTL8190P
3634         u8 ucRegRead;
3635 #endif
3636         u32     tmpRegA, tmpRegC, TempCCk;
3637         int     i =0;
3638 //      u32 dwRegRead = 0;
3639
3640         RT_TRACE(COMP_INIT, "====>%s()\n", __FUNCTION__);
3641         priv->being_init_adapter = true;
3642         rtl8192_pci_resetdescring(dev);
3643         // 2007/11/02 MH Before initalizing RF. We can not use FW to do RF-R/W.
3644         priv->Rf_Mode = RF_OP_By_SW_3wire;
3645 #ifdef RTL8192E
3646         //dPLL on
3647         if(priv->ResetProgress == RESET_TYPE_NORESET)
3648         {
3649             write_nic_byte(dev, ANAPAR, 0x37);
3650             // Accordign to designer's explain, LBUS active will never > 10ms. We delay 10ms
3651             // Joseph increae the time to prevent firmware download fail
3652             mdelay(500);
3653         }
3654 #endif
3655         //PlatformSleepUs(10000);
3656         // For any kind of InitializeAdapter process, we shall use system now!!
3657         priv->pFirmware->firmware_status = FW_STATUS_0_INIT;
3658
3659         // Set to eRfoff in order not to count receive count.
3660         if(priv->RegRfOff == TRUE)
3661                 priv->ieee80211->eRFPowerState = eRfOff;
3662
3663         //
3664         //3 //Config CPUReset Register
3665         //3//
3666         //3 Firmware Reset Or Not
3667         ulRegRead = read_nic_dword(dev, CPU_GEN);
3668         if(priv->pFirmware->firmware_status == FW_STATUS_0_INIT)
3669         {       //called from MPInitialized. do nothing
3670                 ulRegRead |= CPU_GEN_SYSTEM_RESET;
3671         }else if(priv->pFirmware->firmware_status == FW_STATUS_5_READY)
3672                 ulRegRead |= CPU_GEN_FIRMWARE_RESET;    // Called from MPReset
3673         else
3674                 RT_TRACE(COMP_ERR, "ERROR in %s(): undefined firmware state(%d)\n", __FUNCTION__,   priv->pFirmware->firmware_status);
3675
3676 #ifdef RTL8190P
3677         //2008.06.03, for WOL 90 hw bug
3678         ulRegRead &= (~(CPU_GEN_GPIO_UART));
3679 #endif
3680
3681         write_nic_dword(dev, CPU_GEN, ulRegRead);
3682         //mdelay(100);
3683
3684 #ifdef RTL8192E
3685
3686         //3//
3687         //3 //Fix the issue of E-cut high temperature issue
3688         //3//
3689         // TODO: E cut only
3690         ICVersion = read_nic_byte(dev, IC_VERRSION);
3691         if(ICVersion >= 0x4) //E-cut only
3692         {
3693                 // HW SD suggest that we should not wirte this register too often, so driver
3694                 // should readback this register. This register will be modified only when
3695                 // power on reset
3696                 SwitchingRegulatorOutput = read_nic_byte(dev, SWREGULATOR);
3697                 if(SwitchingRegulatorOutput  != 0xb8)
3698                 {
3699                         write_nic_byte(dev, SWREGULATOR, 0xa8);
3700                         mdelay(1);
3701                         write_nic_byte(dev, SWREGULATOR, 0xb8);
3702                 }
3703         }
3704 #endif
3705
3706
3707         //3//
3708         //3// Initialize BB before MAC
3709         //3//
3710         //rtl8192_dump_reg(dev);
3711         RT_TRACE(COMP_INIT, "BB Config Start!\n");
3712         rtStatus = rtl8192_BBConfig(dev);
3713         if(rtStatus != RT_STATUS_SUCCESS)
3714         {
3715                 RT_TRACE(COMP_ERR, "BB Config failed\n");
3716                 return rtStatus;
3717         }
3718         RT_TRACE(COMP_INIT,"BB Config Finished!\n");
3719
3720         //rtl8192_dump_reg(dev);
3721         //
3722         //3//Set Loopback mode or Normal mode
3723         //3//
3724         //2006.12.13 by emily. Note!We should not merge these two CPU_GEN register writings
3725         //      because setting of System_Reset bit reset MAC to default transmission mode.
3726                 //Loopback mode or not
3727         priv->LoopbackMode = RTL819X_NO_LOOPBACK;
3728         //priv->LoopbackMode = RTL819X_MAC_LOOPBACK;
3729         if(priv->ResetProgress == RESET_TYPE_NORESET)
3730         {
3731         ulRegRead = read_nic_dword(dev, CPU_GEN);
3732         if(priv->LoopbackMode == RTL819X_NO_LOOPBACK)
3733         {
3734                 ulRegRead = ((ulRegRead & CPU_GEN_NO_LOOPBACK_MSK) | CPU_GEN_NO_LOOPBACK_SET);
3735         }
3736         else if (priv->LoopbackMode == RTL819X_MAC_LOOPBACK )
3737         {
3738                 ulRegRead |= CPU_CCK_LOOPBACK;
3739         }
3740         else
3741         {
3742                 RT_TRACE(COMP_ERR,"Serious error: wrong loopback mode setting\n");
3743         }
3744
3745         //2008.06.03, for WOL
3746         //ulRegRead &= (~(CPU_GEN_GPIO_UART));
3747         write_nic_dword(dev, CPU_GEN, ulRegRead);
3748
3749         // 2006.11.29. After reset cpu, we sholud wait for a second, otherwise, it may fail to write registers. Emily
3750         udelay(500);
3751         }
3752         //3Set Hardware(Do nothing now)
3753         rtl8192_hwconfig(dev);
3754         //2=======================================================
3755         // Common Setting for all of the FPGA platform. (part 1)
3756         //2=======================================================
3757         // If there is changes, please make sure it applies to all of the FPGA version
3758         //3 Turn on Tx/Rx
3759         write_nic_byte(dev, CMDR, CR_RE|CR_TE);
3760
3761         //2Set Tx dma burst
3762 #ifdef RTL8190P
3763         write_nic_byte(dev, PCIF, ((MXDMA2_NoLimit<<MXDMA2_RX_SHIFT) | \
3764                                                                                         (MXDMA2_NoLimit<<MXDMA2_TX_SHIFT) | \
3765                                                                                         (1<<MULRW_SHIFT)));
3766 #else
3767         #ifdef RTL8192E
3768         write_nic_byte(dev, PCIF, ((MXDMA2_NoLimit<<MXDMA2_RX_SHIFT) |\
3769                                    (MXDMA2_NoLimit<<MXDMA2_TX_SHIFT) ));
3770         #endif
3771 #endif
3772         //set IDR0 here
3773         write_nic_dword(dev, MAC0, ((u32*)dev->dev_addr)[0]);
3774         write_nic_word(dev, MAC4, ((u16*)(dev->dev_addr + 4))[0]);
3775         //set RCR
3776         write_nic_dword(dev, RCR, priv->ReceiveConfig);
3777
3778         //3 Initialize Number of Reserved Pages in Firmware Queue
3779         #ifdef TO_DO_LIST
3780         if(priv->bInHctTest)
3781         {
3782                 PlatformEFIOWrite4Byte(Adapter, RQPN1,  NUM_OF_PAGE_IN_FW_QUEUE_BK_DTM << RSVD_FW_QUEUE_PAGE_BK_SHIFT |\
3783                                         NUM_OF_PAGE_IN_FW_QUEUE_BE_DTM << RSVD_FW_QUEUE_PAGE_BE_SHIFT | \
3784                                         NUM_OF_PAGE_IN_FW_QUEUE_VI_DTM << RSVD_FW_QUEUE_PAGE_VI_SHIFT | \
3785                                         NUM_OF_PAGE_IN_FW_QUEUE_VO_DTM <<RSVD_FW_QUEUE_PAGE_VO_SHIFT);
3786                 PlatformEFIOWrite4Byte(Adapter, RQPN2, NUM_OF_PAGE_IN_FW_QUEUE_MGNT << RSVD_FW_QUEUE_PAGE_MGNT_SHIFT);
3787                 PlatformEFIOWrite4Byte(Adapter, RQPN3, APPLIED_RESERVED_QUEUE_IN_FW| \
3788                                         NUM_OF_PAGE_IN_FW_QUEUE_BCN<<RSVD_FW_QUEUE_PAGE_BCN_SHIFT|\
3789                                         NUM_OF_PAGE_IN_FW_QUEUE_PUB_DTM<<RSVD_FW_QUEUE_PAGE_PUB_SHIFT);
3790         }
3791         else
3792         #endif
3793         {
3794                 write_nic_dword(dev, RQPN1,  NUM_OF_PAGE_IN_FW_QUEUE_BK << RSVD_FW_QUEUE_PAGE_BK_SHIFT |\
3795                                         NUM_OF_PAGE_IN_FW_QUEUE_BE << RSVD_FW_QUEUE_PAGE_BE_SHIFT | \
3796                                         NUM_OF_PAGE_IN_FW_QUEUE_VI << RSVD_FW_QUEUE_PAGE_VI_SHIFT | \
3797                                         NUM_OF_PAGE_IN_FW_QUEUE_VO <<RSVD_FW_QUEUE_PAGE_VO_SHIFT);
3798                 write_nic_dword(dev, RQPN2, NUM_OF_PAGE_IN_FW_QUEUE_MGNT << RSVD_FW_QUEUE_PAGE_MGNT_SHIFT);
3799                 write_nic_dword(dev, RQPN3, APPLIED_RESERVED_QUEUE_IN_FW| \
3800                                         NUM_OF_PAGE_IN_FW_QUEUE_BCN<<RSVD_FW_QUEUE_PAGE_BCN_SHIFT|\
3801                                         NUM_OF_PAGE_IN_FW_QUEUE_PUB<<RSVD_FW_QUEUE_PAGE_PUB_SHIFT);
3802         }
3803
3804         rtl8192_tx_enable(dev);
3805         rtl8192_rx_enable(dev);
3806         //3Set Response Rate Setting Register
3807         // CCK rate is supported by default.
3808         // CCK rate will be filtered out only when associated AP does not support it.
3809         ulRegRead = (0xFFF00000 & read_nic_dword(dev, RRSR))  | RATE_ALL_OFDM_AG | RATE_ALL_CCK;
3810         write_nic_dword(dev, RRSR, ulRegRead);
3811         write_nic_dword(dev, RATR0+4*7, (RATE_ALL_OFDM_AG | RATE_ALL_CCK));
3812
3813         //2Set AckTimeout
3814         // TODO: (it value is only for FPGA version). need to be changed!!2006.12.18, by Emily
3815         write_nic_byte(dev, ACK_TIMEOUT, 0x30);
3816
3817         //rtl8192_actset_wirelessmode(dev,priv->RegWirelessMode);
3818         if(priv->ResetProgress == RESET_TYPE_NORESET)
3819         rtl8192_SetWirelessMode(dev, priv->ieee80211->mode);
3820         //-----------------------------------------------------------------------------
3821         // Set up security related. 070106, by rcnjko:
3822         // 1. Clear all H/W keys.
3823         // 2. Enable H/W encryption/decryption.
3824         //-----------------------------------------------------------------------------
3825         CamResetAllEntry(dev);
3826         {
3827                 u8 SECR_value = 0x0;
3828                 SECR_value |= SCR_TxEncEnable;
3829                 SECR_value |= SCR_RxDecEnable;
3830                 SECR_value |= SCR_NoSKMC;
3831                 write_nic_byte(dev, SECR, SECR_value);
3832         }
3833         //3Beacon related
3834         write_nic_word(dev, ATIMWND, 2);
3835         write_nic_word(dev, BCN_INTERVAL, 100);
3836         for (i=0; i<QOS_QUEUE_NUM; i++)
3837                 write_nic_dword(dev, WDCAPARA_ADD[i], 0x005e4332);
3838         //
3839         // Switching regulator controller: This is set temporarily.
3840         // It's not sure if this can be removed in the future.
3841         // PJ advised to leave it by default.
3842         //
3843         write_nic_byte(dev, 0xbe, 0xc0);
3844
3845         //2=======================================================
3846         // Set PHY related configuration defined in MAC register bank
3847         //2=======================================================
3848         rtl8192_phy_configmac(dev);
3849
3850         if (priv->card_8192_version > (u8) VERSION_8190_BD) {
3851                 rtl8192_phy_getTxPower(dev);
3852                 rtl8192_phy_setTxPower(dev, priv->chan);
3853         }
3854
3855         //if D or C cut
3856                 tmpvalue = read_nic_byte(dev, IC_VERRSION);
3857                 priv->IC_Cut = tmpvalue;
3858                 RT_TRACE(COMP_INIT, "priv->IC_Cut = 0x%x\n", priv->IC_Cut);
3859                 if(priv->IC_Cut >= IC_VersionCut_D)
3860                 {
3861                         //pHalData->bDcut = TRUE;
3862                         if(priv->IC_Cut == IC_VersionCut_D)
3863                                 RT_TRACE(COMP_INIT, "D-cut\n");
3864                         if(priv->IC_Cut == IC_VersionCut_E)
3865                         {
3866                                 RT_TRACE(COMP_INIT, "E-cut\n");
3867                                 // HW SD suggest that we should not wirte this register too often, so driver
3868                                 // should readback this register. This register will be modified only when
3869                                 // power on reset
3870                         }
3871                 }
3872                 else
3873                 {
3874                         //pHalData->bDcut = FALSE;
3875                         RT_TRACE(COMP_INIT, "Before C-cut\n");
3876                 }
3877
3878 #if 1
3879         //Firmware download
3880         RT_TRACE(COMP_INIT, "Load Firmware!\n");
3881         bfirmwareok = init_firmware(dev);
3882         if(bfirmwareok != true) {
3883                 rtStatus = RT_STATUS_FAILURE;
3884                 return rtStatus;
3885         }
3886         RT_TRACE(COMP_INIT, "Load Firmware finished!\n");
3887 #endif
3888         //RF config
3889         if(priv->ResetProgress == RESET_TYPE_NORESET)
3890         {
3891         RT_TRACE(COMP_INIT, "RF Config Started!\n");
3892         rtStatus = rtl8192_phy_RFConfig(dev);
3893         if(rtStatus != RT_STATUS_SUCCESS)
3894         {
3895                 RT_TRACE(COMP_ERR, "RF Config failed\n");
3896                         return rtStatus;
3897         }
3898         RT_TRACE(COMP_INIT, "RF Config Finished!\n");
3899         }
3900         rtl8192_phy_updateInitGain(dev);
3901
3902         /*---- Set CCK and OFDM Block "ON"----*/
3903         rtl8192_setBBreg(dev, rFPGA0_RFMOD, bCCKEn, 0x1);
3904         rtl8192_setBBreg(dev, rFPGA0_RFMOD, bOFDMEn, 0x1);
3905
3906 #ifdef RTL8192E
3907         //Enable Led
3908         write_nic_byte(dev, 0x87, 0x0);
3909 #endif
3910 #ifdef RTL8190P
3911         //2008.06.03, for WOL
3912         ucRegRead = read_nic_byte(dev, GPE);
3913         ucRegRead |= BIT0;
3914         write_nic_byte(dev, GPE, ucRegRead);
3915
3916         ucRegRead = read_nic_byte(dev, GPO);
3917         ucRegRead &= ~BIT0;
3918         write_nic_byte(dev, GPO, ucRegRead);
3919 #endif
3920
3921         //2=======================================================
3922         // RF Power Save
3923         //2=======================================================
3924 #ifdef ENABLE_IPS
3925
3926 {
3927         if(priv->RegRfOff == TRUE)
3928         { // User disable RF via registry.
3929                 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RegRfOff ----------\n",__FUNCTION__);
3930                 MgntActSet_RF_State(dev, eRfOff, RF_CHANGE_BY_SW);
3931 #if 0//cosa, ask SD3 willis and he doesn't know what is this for
3932                 // Those action will be discard in MgntActSet_RF_State because off the same state
3933         for(eRFPath = 0; eRFPath <pHalData->NumTotalRFPath; eRFPath++)
3934                 PHY_SetRFReg(Adapter, (RF90_RADIO_PATH_E)eRFPath, 0x4, 0xC00, 0x0);
3935 #endif
3936         }
3937         else if(priv->ieee80211->RfOffReason > RF_CHANGE_BY_PS)
3938         { // H/W or S/W RF OFF before sleep.
3939                 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RfOffReason(%d) ----------\n", __FUNCTION__,priv->ieee80211->RfOffReason);
3940                 MgntActSet_RF_State(dev, eRfOff, priv->ieee80211->RfOffReason);
3941         }
3942         else if(priv->ieee80211->RfOffReason >= RF_CHANGE_BY_IPS)
3943         { // H/W or S/W RF OFF before sleep.
3944                 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): Turn off RF for RfOffReason(%d) ----------\n", __FUNCTION__,priv->ieee80211->RfOffReason);
3945                 MgntActSet_RF_State(dev, eRfOff, priv->ieee80211->RfOffReason);
3946         }
3947         else
3948         {
3949                 RT_TRACE((COMP_INIT|COMP_RF|COMP_POWER), "%s(): RF-ON \n",__FUNCTION__);
3950                 priv->ieee80211->eRFPowerState = eRfOn;
3951                 priv->ieee80211->RfOffReason = 0;
3952                 //DrvIFIndicateCurrentPhyStatus(Adapter);
3953         // LED control
3954         //Adapter->HalFunc.LedControlHandler(Adapter, LED_CTL_POWER_ON);
3955
3956         //
3957         // If inactive power mode is enabled, disable rf while in disconnected state.
3958         // But we should still tell upper layer we are in rf on state.
3959         // 2007.07.16, by shien chang.
3960         //
3961                 //if(!Adapter->bInHctTest)
3962         //IPSEnter(Adapter);
3963
3964         }
3965 }
3966 #endif
3967         if(1){
3968 #ifdef RTL8192E
3969                         // We can force firmware to do RF-R/W
3970                         if(priv->ieee80211->FwRWRF)
3971                                 priv->Rf_Mode = RF_OP_By_FW;
3972                         else
3973                                 priv->Rf_Mode = RF_OP_By_SW_3wire;
3974 #else
3975                         priv->Rf_Mode = RF_OP_By_SW_3wire;
3976 #endif
3977         }
3978 #ifdef RTL8190P
3979         if(priv->ResetProgress == RESET_TYPE_NORESET)
3980         {
3981                 dm_initialize_txpower_tracking(dev);
3982
3983                 tmpRegA= rtl8192_QueryBBReg(dev,rOFDM0_XATxIQImbalance,bMaskDWord);
3984                 tmpRegC= rtl8192_QueryBBReg(dev,rOFDM0_XCTxIQImbalance,bMaskDWord);
3985
3986                 if(priv->rf_type == RF_2T4R){
3987                 for(i = 0; i<TxBBGainTableLength; i++)
3988                 {
3989                         if(tmpRegA == priv->txbbgain_table[i].txbbgain_value)
3990                         {
3991                                 priv->rfa_txpowertrackingindex= (u8)i;
3992                                 priv->rfa_txpowertrackingindex_real= (u8)i;
3993                                 priv->rfa_txpowertracking_default = priv->rfa_txpowertrackingindex;
3994                                 break;
3995                         }
3996                 }
3997                 }
3998                 for(i = 0; i<TxBBGainTableLength; i++)
3999                 {
4000                         if(tmpRegC == priv->txbbgain_table[i].txbbgain_value)
4001                         {
4002                                 priv->rfc_txpowertrackingindex= (u8)i;
4003                                 priv->rfc_txpowertrackingindex_real= (u8)i;
4004                                 priv->rfc_txpowertracking_default = priv->rfc_txpowertrackingindex;
4005                                 break;
4006                         }
4007                 }
4008                 TempCCk = rtl8192_QueryBBReg(dev, rCCK0_TxFilter1, bMaskByte2);
4009
4010                 for(i=0 ; i<CCKTxBBGainTableLength ; i++)
4011                 {
4012                         if(TempCCk == priv->cck_txbbgain_table[i].ccktxbb_valuearray[0])
4013                         {
4014                                 priv->CCKPresentAttentuation_20Mdefault =(u8) i;
4015                                 break;
4016                         }
4017                 }
4018                 priv->CCKPresentAttentuation_40Mdefault = 0;
4019                 priv->CCKPresentAttentuation_difference = 0;
4020                 priv->CCKPresentAttentuation = priv->CCKPresentAttentuation_20Mdefault;
4021                 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_initial = %d\n", priv->rfa_txpowertrackingindex);
4022                 RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_real__initial = %d\n", priv->rfa_txpowertrackingindex_real);
4023                 RT_TRACE(COMP_POWER_TRACKING, "priv->rfc_txpowertrackingindex_initial = %d\n", priv->rfc_txpowertrackingindex);
4024                 RT_TRACE(COMP_POWER_TRACKING, "priv->rfc_txpowertrackingindex_real_initial = %d\n", priv->rfc_txpowertrackingindex_real);
4025                 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_difference_initial = %d\n", priv->CCKPresentAttentuation_difference);
4026                 RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_initial = %d\n", priv->CCKPresentAttentuation);
4027         }
4028 #else
4029         #ifdef RTL8192E
4030         if(priv->ResetProgress == RESET_TYPE_NORESET)
4031         {
4032                 dm_initialize_txpower_tracking(dev);
4033
4034                 if(priv->IC_Cut >= IC_VersionCut_D)
4035                 {
4036                         tmpRegA= rtl8192_QueryBBReg(dev,rOFDM0_XATxIQImbalance,bMaskDWord);
4037                         tmpRegC= rtl8192_QueryBBReg(dev,rOFDM0_XCTxIQImbalance,bMaskDWord);
4038                         for(i = 0; i<TxBBGainTableLength; i++)
4039                         {
4040                                 if(tmpRegA == priv->txbbgain_table[i].txbbgain_value)
4041                                 {
4042                                         priv->rfa_txpowertrackingindex= (u8)i;
4043                                         priv->rfa_txpowertrackingindex_real= (u8)i;
4044                                         priv->rfa_txpowertracking_default = priv->rfa_txpowertrackingindex;
4045                                         break;
4046                                 }
4047                         }
4048
4049                 TempCCk = rtl8192_QueryBBReg(dev, rCCK0_TxFilter1, bMaskByte2);
4050
4051                 for(i=0 ; i<CCKTxBBGainTableLength ; i++)
4052                 {
4053                         if(TempCCk == priv->cck_txbbgain_table[i].ccktxbb_valuearray[0])
4054                         {
4055                                 priv->CCKPresentAttentuation_20Mdefault =(u8) i;
4056                                 break;
4057                         }
4058                 }
4059                 priv->CCKPresentAttentuation_40Mdefault = 0;
4060                 priv->CCKPresentAttentuation_difference = 0;
4061                 priv->CCKPresentAttentuation = priv->CCKPresentAttentuation_20Mdefault;
4062                         RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_initial = %d\n", priv->rfa_txpowertrackingindex);
4063                         RT_TRACE(COMP_POWER_TRACKING, "priv->rfa_txpowertrackingindex_real__initial = %d\n", priv->rfa_txpowertrackingindex_real);
4064                         RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_difference_initial = %d\n", priv->CCKPresentAttentuation_difference);
4065                         RT_TRACE(COMP_POWER_TRACKING, "priv->CCKPresentAttentuation_initial = %d\n", priv->CCKPresentAttentuation);
4066                         priv->btxpower_tracking = FALSE;//TEMPLY DISABLE
4067                 }
4068         }
4069         #endif
4070 #endif
4071         rtl8192_irq_enable(dev);
4072         priv->being_init_adapter = false;
4073         return rtStatus;
4074
4075 }
4076
4077 void rtl8192_prepare_beacon(struct r8192_priv *priv)
4078 {
4079         struct sk_buff *skb;
4080         //unsigned long flags;
4081         cb_desc *tcb_desc;
4082
4083         skb = ieee80211_get_beacon(priv->ieee80211);
4084         tcb_desc = (cb_desc *)(skb->cb + 8);
4085         //printk("===========> %s\n", __FUNCTION__);
4086         //spin_lock_irqsave(&priv->tx_lock,flags);
4087         /* prepare misc info for the beacon xmit */
4088         tcb_desc->queue_index = BEACON_QUEUE;
4089         /* IBSS does not support HT yet, use 1M defautly */
4090         tcb_desc->data_rate = 2;
4091         tcb_desc->RATRIndex = 7;
4092         tcb_desc->bTxDisableRateFallBack = 1;
4093         tcb_desc->bTxUseDriverAssingedRate = 1;
4094
4095         skb_push(skb, priv->ieee80211->tx_headroom);
4096         if(skb){
4097                 rtl8192_tx(priv->ieee80211->dev,skb);
4098         }
4099         //spin_unlock_irqrestore (&priv->tx_lock, flags);
4100 }
4101
4102 #if 0
4103 void rtl8192_beacon_tx_enable(struct net_device *dev)
4104 {
4105         struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
4106
4107         rtl8180_set_mode(dev,EPROM_CMD_CONFIG);
4108 #ifdef CONFIG_RTL8185B
4109         priv->dma_poll_stop_mask &= ~(TPPOLLSTOP_BQ);MgntQuery_MgntFrameTxRateMgntQuery_MgntFrameTxRate
4110         write_nic_byte(dev,TPPollStop, priv->dma_poll_mask);
4111 #else
4112         priv->dma_poll_mask &=~(1<<TX_DMA_STOP_BEACON_SHIFT);
4113         write_nic_byte(dev,TX_DMA_POLLING,priv->dma_poll_mask);
4114 #endif
4115         rtl8180_set_mode(dev,EPROM_CMD_NORMAL);
4116 }
4117 #endif
4118
4119
4120 /* this configures registers for beacon tx and enables it via
4121  * rtl8192_beacon_tx_enable(). rtl8192_beacon_tx_disable() might
4122  * be used to stop beacon transmission
4123  */
4124 void rtl8192_start_beacon(struct net_device *dev)
4125 {
4126         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4127         struct ieee80211_network *net = &priv->ieee80211->current_network;
4128         u16 BcnTimeCfg = 0;
4129         u16 BcnCW = 6;
4130         u16 BcnIFS = 0xf;
4131
4132         DMESG("Enabling beacon TX");
4133         //rtl8192_prepare_beacon(dev);
4134         rtl8192_irq_disable(dev);
4135         //rtl8192_beacon_tx_enable(dev);
4136
4137         /* ATIM window */
4138         write_nic_word(dev, ATIMWND, 2);
4139
4140         /* Beacon interval (in unit of TU) */
4141         write_nic_word(dev, BCN_INTERVAL, net->beacon_interval);
4142
4143         /*
4144          * DrvErlyInt (in unit of TU).
4145          * (Time to send interrupt to notify driver to c
4146          * hange beacon content)
4147          * */
4148         write_nic_word(dev, BCN_DRV_EARLY_INT, 10);
4149
4150         /*
4151          * BcnDMATIM(in unit of us).
4152          * Indicates the time before TBTT to perform beacon queue DMA
4153          * */
4154         write_nic_word(dev, BCN_DMATIME, 256);
4155
4156         /*
4157          * Force beacon frame transmission even after receiving
4158          * beacon frame from other ad hoc STA
4159          * */
4160         write_nic_byte(dev, BCN_ERR_THRESH, 100);
4161
4162         /* Set CW and IFS */
4163         BcnTimeCfg |= BcnCW<<BCN_TCFG_CW_SHIFT;
4164         BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
4165         write_nic_word(dev, BCN_TCFG, BcnTimeCfg);
4166
4167
4168         /* enable the interrupt for ad-hoc process */
4169         rtl8192_irq_enable(dev);
4170 }
4171 /***************************************************************************
4172     -------------------------------NET STUFF---------------------------
4173 ***************************************************************************/
4174 #if 0
4175 static struct net_device_stats *rtl8192_stats(struct net_device *dev)
4176 {
4177         struct r8192_priv *priv = ieee80211_priv(dev);
4178
4179         return &priv->ieee80211->stats;
4180 }
4181 #endif
4182
4183
4184
4185 static bool HalTxCheckStuck8190Pci(struct net_device *dev)
4186 {
4187         u16                             RegTxCounter = read_nic_word(dev, 0x128);
4188         struct r8192_priv *priv = ieee80211_priv(dev);
4189         bool                            bStuck = FALSE;
4190         RT_TRACE(COMP_RESET,"%s():RegTxCounter is %d,TxCounter is %d\n",__FUNCTION__,RegTxCounter,priv->TxCounter);
4191         if(priv->TxCounter==RegTxCounter)
4192                 bStuck = TRUE;
4193
4194         priv->TxCounter = RegTxCounter;
4195
4196         return bStuck;
4197 }
4198
4199 /*
4200 *       <Assumption: RT_TX_SPINLOCK is acquired.>
4201 *       First added: 2006.11.19 by emily
4202 */
4203 static RESET_TYPE
4204 TxCheckStuck(struct net_device *dev)
4205 {
4206         struct r8192_priv *priv = ieee80211_priv(dev);
4207         u8                      QueueID;
4208         ptx_ring                head=NULL,tail=NULL,txring = NULL;
4209         u8                      ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
4210         bool                    bCheckFwTxCnt = false;
4211         //unsigned long flags;
4212
4213         //
4214         // Decide Stuch threshold according to current power save mode
4215         //
4216         //printk("++++++++++++>%s()\n",__FUNCTION__);
4217         switch (priv->ieee80211->dot11PowerSaveMode)
4218         {
4219                 // The threshold value  may required to be adjusted .
4220                 case eActive:           // Active/Continuous access.
4221                         ResetThreshold = NIC_SEND_HANG_THRESHOLD_NORMAL;
4222                         break;
4223                 case eMaxPs:            // Max power save mode.
4224                         ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
4225                         break;
4226                 case eFastPs:   // Fast power save mode.
4227                         ResetThreshold = NIC_SEND_HANG_THRESHOLD_POWERSAVE;
4228                         break;
4229         }
4230
4231         //
4232         // Check whether specific tcb has been queued for a specific time
4233         //
4234         for(QueueID = 0; QueueID < MAX_TX_QUEUE; QueueID++)
4235         {
4236
4237
4238                 if(QueueID == TXCMD_QUEUE)
4239                         continue;
4240
4241                 switch(QueueID) {
4242                 case MGNT_QUEUE:
4243                         tail=priv->txmapringtail;
4244                         head=priv->txmapringhead;
4245                         break;
4246
4247                 case BK_QUEUE:
4248                         tail=priv->txbkpringtail;
4249                         head=priv->txbkpringhead;
4250                         break;
4251
4252                 case BE_QUEUE:
4253                         tail=priv->txbepringtail;
4254                         head=priv->txbepringhead;
4255                         break;
4256
4257                 case VI_QUEUE:
4258                         tail=priv->txvipringtail;
4259                         head=priv->txvipringhead;
4260                         break;
4261
4262                 case VO_QUEUE:
4263                         tail=priv->txvopringtail;
4264                         head=priv->txvopringhead;
4265                         break;
4266
4267                 default:
4268                         tail=head=NULL;
4269                         break;
4270                 }
4271
4272                 if(tail == head)
4273                         continue;
4274                 else
4275                 {
4276                         txring = head;
4277                         if(txring == NULL)
4278                         {
4279                                 RT_TRACE(COMP_ERR,"%s():txring is NULL , BUG!\n",__FUNCTION__);
4280                                 continue;
4281                         }
4282                         txring->nStuckCount++;
4283                         #if 0
4284                         if(txring->nStuckCount > ResetThreshold)
4285                         {
4286                                 RT_TRACE( COMP_RESET, "<== TxCheckStuck()\n" );
4287                                 return RESET_TYPE_NORMAL;
4288                         }
4289                         #endif
4290                         bCheckFwTxCnt = TRUE;
4291                 }
4292         }
4293 #if 1
4294         if(bCheckFwTxCnt)
4295         {
4296                 if(HalTxCheckStuck8190Pci(dev))
4297                 {
4298                         RT_TRACE(COMP_RESET, "TxCheckStuck(): Fw indicates no Tx condition! \n");
4299                         return RESET_TYPE_SILENT;
4300                 }
4301         }
4302 #endif
4303         return RESET_TYPE_NORESET;
4304 }
4305
4306
4307 static bool HalRxCheckStuck8190Pci(struct net_device *dev)
4308 {
4309         struct r8192_priv *priv = ieee80211_priv(dev);
4310         u16                             RegRxCounter = read_nic_word(dev, 0x130);
4311         bool                            bStuck = FALSE;
4312         static u8                       rx_chk_cnt = 0;
4313         RT_TRACE(COMP_RESET,"%s(): RegRxCounter is %d,RxCounter is %d\n",__FUNCTION__,RegRxCounter,priv->RxCounter);
4314         // If rssi is small, we should check rx for long time because of bad rx.
4315         // or maybe it will continuous silent reset every 2 seconds.
4316         rx_chk_cnt++;
4317         if(priv->undecorated_smoothed_pwdb >= (RateAdaptiveTH_High+5))
4318         {
4319                 rx_chk_cnt = 0; //high rssi, check rx stuck right now.
4320         }
4321         else if(priv->undecorated_smoothed_pwdb < (RateAdaptiveTH_High+5) &&
4322                 ((priv->CurrentChannelBW!=HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb>=RateAdaptiveTH_Low_40M) ||
4323                 (priv->CurrentChannelBW==HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb>=RateAdaptiveTH_Low_20M)) )
4324
4325         {
4326                 if(rx_chk_cnt < 2)
4327                 {
4328                         return bStuck;
4329                 }
4330                 else
4331                 {
4332                         rx_chk_cnt = 0;
4333                 }
4334         }
4335         else if(((priv->CurrentChannelBW!=HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb<RateAdaptiveTH_Low_40M) ||
4336                 (priv->CurrentChannelBW==HT_CHANNEL_WIDTH_20&&priv->undecorated_smoothed_pwdb<RateAdaptiveTH_Low_20M)) &&
4337                 priv->undecorated_smoothed_pwdb >= VeryLowRSSI)
4338         {
4339                 if(rx_chk_cnt < 4)
4340                 {
4341                         //DbgPrint("RSSI < %d && RSSI >= %d, no check this time \n", RateAdaptiveTH_Low, VeryLowRSSI);
4342                         return bStuck;
4343                 }
4344                 else
4345                 {
4346                         rx_chk_cnt = 0;
4347                         //DbgPrint("RSSI < %d && RSSI >= %d, check this time \n", RateAdaptiveTH_Low, VeryLowRSSI);
4348                 }
4349         }
4350         else
4351         {
4352                 if(rx_chk_cnt < 8)
4353                 {
4354                         //DbgPrint("RSSI <= %d, no check this time \n", VeryLowRSSI);
4355                         return bStuck;
4356                 }
4357                 else
4358                 {
4359                         rx_chk_cnt = 0;
4360                         //DbgPrint("RSSI <= %d, check this time \n", VeryLowRSSI);
4361                 }
4362         }
4363 #if 0
4364         if (rx_chk_cnt < 2)
4365                 return bStuck;
4366         else
4367                 rx_chk_cnt = 0;
4368 #endif
4369         if(priv->RxCounter==RegRxCounter)
4370                 bStuck = TRUE;
4371
4372         priv->RxCounter = RegRxCounter;
4373
4374         return bStuck;
4375 }
4376
4377 static RESET_TYPE RxCheckStuck(struct net_device *dev)
4378 {
4379
4380         if(HalRxCheckStuck8190Pci(dev))
4381         {
4382                 RT_TRACE(COMP_RESET, "RxStuck Condition\n");
4383                 return RESET_TYPE_SILENT;
4384         }
4385
4386         return RESET_TYPE_NORESET;
4387 }
4388
4389 static RESET_TYPE
4390 rtl819x_ifcheck_resetornot(struct net_device *dev)
4391 {
4392         struct r8192_priv *priv = ieee80211_priv(dev);
4393         RESET_TYPE      TxResetType = RESET_TYPE_NORESET;
4394         RESET_TYPE      RxResetType = RESET_TYPE_NORESET;
4395         RT_RF_POWER_STATE       rfState;
4396
4397         rfState = priv->ieee80211->eRFPowerState;
4398
4399         TxResetType = TxCheckStuck(dev);
4400 #if 1
4401         if( rfState != eRfOff &&
4402                 /*ADAPTER_TEST_STATUS_FLAG(Adapter, ADAPTER_STATUS_FW_DOWNLOAD_FAILURE)) &&*/
4403                 (priv->ieee80211->iw_mode != IW_MODE_ADHOC))
4404         {
4405                 // If driver is in the status of firmware download failure , driver skips RF initialization and RF is
4406                 // in turned off state. Driver should check whether Rx stuck and do silent reset. And
4407                 // if driver is in firmware download failure status, driver should initialize RF in the following
4408                 // silent reset procedure Emily, 2008.01.21
4409
4410                 // Driver should not check RX stuck in IBSS mode because it is required to
4411                 // set Check BSSID in order to send beacon, however, if check BSSID is
4412                 // set, STA cannot hear any packet a all. Emily, 2008.04.12
4413                 RxResetType = RxCheckStuck(dev);
4414         }
4415 #endif
4416
4417         RT_TRACE(COMP_RESET,"%s(): TxResetType is %d, RxResetType is %d\n",__FUNCTION__,TxResetType,RxResetType);
4418         if(TxResetType==RESET_TYPE_NORMAL || RxResetType==RESET_TYPE_NORMAL)
4419                 return RESET_TYPE_NORMAL;
4420         else if(TxResetType==RESET_TYPE_SILENT || RxResetType==RESET_TYPE_SILENT)
4421                 return RESET_TYPE_SILENT;
4422         else
4423                 return RESET_TYPE_NORESET;
4424
4425 }
4426
4427
4428 static void CamRestoreAllEntry(struct net_device *dev)
4429 {
4430         u8 EntryId = 0;
4431         struct r8192_priv *priv = ieee80211_priv(dev);
4432         u8*     MacAddr = priv->ieee80211->current_network.bssid;
4433
4434         static u8       CAM_CONST_ADDR[4][6] = {
4435                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
4436                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
4437                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
4438                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x03}};
4439         static u8       CAM_CONST_BROAD[] =
4440                 {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
4441
4442         RT_TRACE(COMP_SEC, "CamRestoreAllEntry: \n");
4443
4444
4445         if ((priv->ieee80211->pairwise_key_type == KEY_TYPE_WEP40)||
4446             (priv->ieee80211->pairwise_key_type == KEY_TYPE_WEP104))
4447         {
4448
4449                 for(EntryId=0; EntryId<4; EntryId++)
4450                 {
4451                         {
4452                                 MacAddr = CAM_CONST_ADDR[EntryId];
4453                                 setKey(dev,
4454                                                 EntryId ,
4455                                                 EntryId,
4456                                                 priv->ieee80211->pairwise_key_type,
4457                                                 MacAddr,
4458                                                 0,
4459                                                 NULL);
4460                         }
4461                 }
4462
4463         }
4464         else if(priv->ieee80211->pairwise_key_type == KEY_TYPE_TKIP)
4465         {
4466
4467                 {
4468                         if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
4469                                 setKey(dev,
4470                                                 4,
4471                                                 0,
4472                                                 priv->ieee80211->pairwise_key_type,
4473                                                 (u8*)dev->dev_addr,
4474                                                 0,
4475                                                 NULL);
4476                         else
4477                                 setKey(dev,
4478                                                 4,
4479                                                 0,
4480                                                 priv->ieee80211->pairwise_key_type,
4481                                                 MacAddr,
4482                                                 0,
4483                                                 NULL);
4484                 }
4485         }
4486         else if(priv->ieee80211->pairwise_key_type == KEY_TYPE_CCMP)
4487         {
4488
4489                 {
4490                         if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
4491                                 setKey(dev,
4492                                                 4,
4493                                                 0,
4494                                                 priv->ieee80211->pairwise_key_type,
4495                                                 (u8*)dev->dev_addr,
4496                                                 0,
4497                                                 NULL);
4498                         else
4499                                 setKey(dev,
4500                                                 4,
4501                                                 0,
4502                                                 priv->ieee80211->pairwise_key_type,
4503                                                 MacAddr,
4504                                                 0,
4505                                                 NULL);
4506                 }
4507         }
4508
4509
4510
4511         if(priv->ieee80211->group_key_type == KEY_TYPE_TKIP)
4512         {
4513                 MacAddr = CAM_CONST_BROAD;
4514                 for(EntryId=1 ; EntryId<4 ; EntryId++)
4515                 {
4516                         {
4517                                 setKey(dev,
4518                                                 EntryId,
4519                                                 EntryId,
4520                                                 priv->ieee80211->group_key_type,
4521                                                 MacAddr,
4522                                                 0,
4523                                                 NULL);
4524                         }
4525                 }
4526                 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
4527                                 setKey(dev,
4528                                                 0,
4529                                                 0,
4530                                                 priv->ieee80211->group_key_type,
4531                                                 CAM_CONST_ADDR[0],
4532                                                 0,
4533                                                 NULL);
4534         }
4535         else if(priv->ieee80211->group_key_type == KEY_TYPE_CCMP)
4536         {
4537                 MacAddr = CAM_CONST_BROAD;
4538                 for(EntryId=1; EntryId<4 ; EntryId++)
4539                 {
4540                         {
4541                                 setKey(dev,
4542                                                 EntryId ,
4543                                                 EntryId,
4544                                                 priv->ieee80211->group_key_type,
4545                                                 MacAddr,
4546                                                 0,
4547                                                 NULL);
4548                         }
4549                 }
4550
4551                 if(priv->ieee80211->iw_mode == IW_MODE_ADHOC)
4552                                 setKey(dev,
4553                                                 0 ,
4554                                                 0,
4555                                                 priv->ieee80211->group_key_type,
4556                                                 CAM_CONST_ADDR[0],
4557                                                 0,
4558                                                 NULL);
4559         }
4560 }
4561
4562 void rtl8192_cancel_deferred_work(struct r8192_priv* priv);
4563 int _rtl8192_up(struct net_device *dev);
4564
4565 /*
4566  * This function is used to fix Tx/Rx stop bug temporarily.
4567  * This function will do "system reset" to NIC when Tx or Rx is stuck.
4568  * The method checking Tx/Rx stuck of this function is supported by FW,
4569  * which reports Tx and Rx counter to register 0x128 and 0x130.
4570  * */
4571 static void rtl819x_ifsilentreset(struct net_device *dev)
4572 {
4573         struct r8192_priv *priv = ieee80211_priv(dev);
4574         u8      reset_times = 0;
4575         int reset_status = 0;
4576         struct ieee80211_device *ieee = priv->ieee80211;
4577
4578
4579         // 2007.07.20. If we need to check CCK stop, please uncomment this line.
4580         //bStuck = Adapter->HalFunc.CheckHWStopHandler(Adapter);
4581
4582         if(priv->ResetProgress==RESET_TYPE_NORESET)
4583         {
4584 RESET_START:
4585
4586                 RT_TRACE(COMP_RESET,"=========>Reset progress!! \n");
4587
4588                 // Set the variable for reset.
4589                 priv->ResetProgress = RESET_TYPE_SILENT;
4590 //              rtl8192_close(dev);
4591 #if 1
4592                 down(&priv->wx_sem);
4593                 if(priv->up == 0)
4594                 {
4595                         RT_TRACE(COMP_ERR,"%s():the driver is not up! return\n",__FUNCTION__);
4596                         up(&priv->wx_sem);
4597                         return ;
4598                 }
4599                 priv->up = 0;
4600                 RT_TRACE(COMP_RESET,"%s():======>start to down the driver\n",__FUNCTION__);
4601                 if(!netif_queue_stopped(dev))
4602                         netif_stop_queue(dev);
4603
4604                 dm_backup_dynamic_mechanism_state(dev);
4605
4606                 rtl8192_irq_disable(dev);
4607                 rtl8192_cancel_deferred_work(priv);
4608                 deinit_hal_dm(dev);
4609                 del_timer_sync(&priv->watch_dog_timer);
4610                 ieee->sync_scan_hurryup = 1;
4611                 if(ieee->state == IEEE80211_LINKED)
4612                 {
4613                         down(&ieee->wx_sem);
4614                         printk("ieee->state is IEEE80211_LINKED\n");
4615                         ieee80211_stop_send_beacons(priv->ieee80211);
4616                         del_timer_sync(&ieee->associate_timer);
4617 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
4618                         cancel_delayed_work(&ieee->associate_retry_wq);
4619 #endif
4620                         ieee80211_stop_scan(ieee);
4621                         netif_carrier_off(dev);
4622                         up(&ieee->wx_sem);
4623                 }
4624                 else{
4625                         printk("ieee->state is NOT LINKED\n");
4626                         ieee80211_softmac_stop_protocol(priv->ieee80211);
4627                 }
4628                 rtl8192_rtx_disable(dev);
4629                 up(&priv->wx_sem);
4630                 RT_TRACE(COMP_RESET,"%s():<==========down process is finished\n",__FUNCTION__);
4631                 RT_TRACE(COMP_RESET,"%s():===========>start to up the driver\n",__FUNCTION__);
4632                 reset_status = _rtl8192_up(dev);
4633
4634                 RT_TRACE(COMP_RESET,"%s():<===========up process is finished\n",__FUNCTION__);
4635                 if(reset_status == -1)
4636                 {
4637                         if(reset_times < 3)
4638                         {
4639                                 reset_times++;
4640                                 goto RESET_START;
4641                         }
4642                         else
4643                         {
4644                                 RT_TRACE(COMP_ERR," ERR!!! %s():  Reset Failed!!\n",__FUNCTION__);
4645                         }
4646                 }
4647 #endif
4648                 ieee->is_silent_reset = 1;
4649 #if 1
4650                 EnableHWSecurityConfig8192(dev);
4651 #if 1
4652                 if(ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_INFRA)
4653                 {
4654                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
4655
4656 #if 1
4657 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
4658                         queue_work(ieee->wq, &ieee->associate_complete_wq);
4659 #else
4660                         schedule_task(&ieee->associate_complete_wq);
4661 #endif
4662 #endif
4663
4664                 }
4665                 else if(ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_ADHOC)
4666                 {
4667                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
4668                         ieee->link_change(ieee->dev);
4669
4670                 //      notify_wx_assoc_event(ieee);
4671
4672                         ieee80211_start_send_beacons(ieee);
4673
4674                         if (ieee->data_hard_resume)
4675                                 ieee->data_hard_resume(ieee->dev);
4676                         netif_carrier_on(ieee->dev);
4677                 }
4678 #endif
4679
4680                 CamRestoreAllEntry(dev);
4681
4682                 // Restore the previous setting for all dynamic mechanism
4683                 dm_restore_dynamic_mechanism_state(dev);
4684
4685                 priv->ResetProgress = RESET_TYPE_NORESET;
4686                 priv->reset_count++;
4687
4688                 priv->bForcedSilentReset =false;
4689                 priv->bResetInProgress = false;
4690
4691                 // For test --> force write UFWP.
4692                 write_nic_byte(dev, UFWP, 1);
4693                 RT_TRACE(COMP_RESET, "Reset finished!! ====>[%d]\n", priv->reset_count);
4694 #endif
4695         }
4696 }
4697
4698 #ifdef ENABLE_IPS
4699 void InactivePsWorkItemCallback(struct net_device *dev)
4700 {
4701         struct r8192_priv *priv = ieee80211_priv(dev);
4702         PRT_POWER_SAVE_CONTROL  pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
4703         //u8                                                    index = 0;
4704
4705         RT_TRACE(COMP_POWER, "InactivePsWorkItemCallback() ---------> \n");
4706         //
4707         // This flag "bSwRfProcessing", indicates the status of IPS procedure, should be set if the IPS workitem
4708         // is really scheduled.
4709         // The old code, sets this flag before scheduling the IPS workitem and however, at the same time the
4710         // previous IPS workitem did not end yet, fails to schedule the current workitem. Thus, bSwRfProcessing
4711         // blocks the IPS procedure of switching RF.
4712         // By Bruce, 2007-12-25.
4713         //
4714         pPSC->bSwRfProcessing = TRUE;
4715
4716         RT_TRACE(COMP_RF, "InactivePsWorkItemCallback(): Set RF to %s.\n", \
4717                         pPSC->eInactivePowerState == eRfOff?"OFF":"ON");
4718
4719
4720         MgntActSet_RF_State(dev, pPSC->eInactivePowerState, RF_CHANGE_BY_IPS);
4721
4722         //
4723         // To solve CAM values miss in RF OFF, rewrite CAM values after RF ON. By Bruce, 2007-09-20.
4724         //
4725 #if 0
4726         if(pPSC->eInactivePowerState == eRfOn)
4727                 CamRestoreAllEntry(dev);
4728 #endif
4729         pPSC->bSwRfProcessing = FALSE;
4730         RT_TRACE(COMP_POWER, "InactivePsWorkItemCallback() <--------- \n");
4731 }
4732
4733 //
4734 //      Description:
4735 //              Enter the inactive power save mode. RF will be off
4736 //      2007.08.17, by shien chang.
4737 //
4738 void
4739 IPSEnter(struct net_device *dev)
4740 {
4741         struct r8192_priv *priv = ieee80211_priv(dev);
4742         PRT_POWER_SAVE_CONTROL          pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
4743         RT_RF_POWER_STATE                       rtState;
4744
4745         if (pPSC->bInactivePs)
4746         {
4747                 rtState = priv->ieee80211->eRFPowerState;
4748                 //
4749                 // Added by Bruce, 2007-12-25.
4750                 // Do not enter IPS in the following conditions:
4751                 // (1) RF is already OFF or Sleep
4752                 // (2) bSwRfProcessing (indicates the IPS is still under going)
4753                 // (3) Connectted (only disconnected can trigger IPS)
4754                 // (4) IBSS (send Beacon)
4755                 // (5) AP mode (send Beacon)
4756                 //
4757                 if (rtState == eRfOn && !pPSC->bSwRfProcessing
4758                         && (priv->ieee80211->state != IEEE80211_LINKED) )
4759                 {
4760                         RT_TRACE(COMP_RF,"IPSEnter(): Turn off RF.\n");
4761                         pPSC->eInactivePowerState = eRfOff;
4762 //                      queue_work(priv->priv_wq,&(pPSC->InactivePsWorkItem));
4763                         InactivePsWorkItemCallback(dev);
4764                 }
4765         }
4766 }
4767
4768 //
4769 //      Description:
4770 //              Leave the inactive power save mode, RF will be on.
4771 //      2007.08.17, by shien chang.
4772 //
4773 void
4774 IPSLeave(struct net_device *dev)
4775 {
4776         struct r8192_priv *priv = ieee80211_priv(dev);
4777         PRT_POWER_SAVE_CONTROL  pPSC = (PRT_POWER_SAVE_CONTROL)(&(priv->ieee80211->PowerSaveControl));
4778         RT_RF_POWER_STATE       rtState;
4779
4780         if (pPSC->bInactivePs)
4781         {
4782                 rtState = priv->ieee80211->eRFPowerState;
4783                 if (rtState != eRfOn  && !pPSC->bSwRfProcessing && priv->ieee80211->RfOffReason <= RF_CHANGE_BY_IPS)
4784                 {
4785                         RT_TRACE(COMP_POWER, "IPSLeave(): Turn on RF.\n");
4786                         pPSC->eInactivePowerState = eRfOn;
4787 //                      queue_work(priv->priv_wq,&(pPSC->InactivePsWorkItem));
4788                         InactivePsWorkItemCallback(dev);
4789                 }
4790         }
4791 }
4792 #endif
4793 static void CAM_read_entry(
4794         struct net_device *dev,
4795         u32                     iIndex
4796 )
4797 {
4798         u32 target_command=0;
4799          u32 target_content=0;
4800          u8 entry_i=0;
4801          u32 ulStatus;
4802         s32 i=100;
4803 //      printk("=======>start read CAM\n");
4804         for(entry_i=0;entry_i<CAM_CONTENT_COUNT;entry_i++)
4805         {
4806         // polling bit, and No Write enable, and address
4807                 target_command= entry_i+CAM_CONTENT_COUNT*iIndex;
4808                 target_command= target_command | BIT31;
4809
4810         //Check polling bit is clear
4811 //      mdelay(1);
4812 #if 1
4813                 while((i--)>=0)
4814                 {
4815                         ulStatus = read_nic_dword(dev, RWCAM);
4816                         if(ulStatus & BIT31){
4817                                 continue;
4818                         }
4819                         else{
4820                                 break;
4821                         }
4822                 }
4823 #endif
4824                 write_nic_dword(dev, RWCAM, target_command);
4825                 RT_TRACE(COMP_SEC,"CAM_read_entry(): WRITE A0: %x \n",target_command);
4826          //     printk("CAM_read_entry(): WRITE A0: %lx \n",target_command);
4827                 target_content = read_nic_dword(dev, RCAMO);
4828                 RT_TRACE(COMP_SEC, "CAM_read_entry(): WRITE A8: %x \n",target_content);
4829          //     printk("CAM_read_entry(): WRITE A8: %lx \n",target_content);
4830         }
4831         printk("\n");
4832 }
4833
4834 static void rtl819x_update_rxcounts(
4835         struct r8192_priv *priv,
4836         u32* TotalRxBcnNum,
4837         u32* TotalRxDataNum
4838 )
4839 {
4840         u16                     SlotIndex;
4841         u8                      i;
4842
4843         *TotalRxBcnNum = 0;
4844         *TotalRxDataNum = 0;
4845
4846         SlotIndex = (priv->ieee80211->LinkDetectInfo.SlotIndex++)%(priv->ieee80211->LinkDetectInfo.SlotNum);
4847         priv->ieee80211->LinkDetectInfo.RxBcnNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvBcnInPeriod;
4848         priv->ieee80211->LinkDetectInfo.RxDataNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvDataInPeriod;
4849         for( i=0; i<priv->ieee80211->LinkDetectInfo.SlotNum; i++ ){
4850                 *TotalRxBcnNum += priv->ieee80211->LinkDetectInfo.RxBcnNum[i];
4851                 *TotalRxDataNum += priv->ieee80211->LinkDetectInfo.RxDataNum[i];
4852         }
4853 }
4854
4855
4856 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
4857 void rtl819x_watchdog_wqcallback(struct work_struct *work)
4858 {
4859         struct delayed_work *dwork = container_of(work,struct delayed_work,work);
4860        struct r8192_priv *priv = container_of(dwork,struct r8192_priv,watch_dog_wq);
4861        struct net_device *dev = priv->ieee80211->dev;
4862 #else
4863 extern  void    rtl819x_watchdog_wqcallback(struct net_device *dev)
4864 {
4865         struct r8192_priv *priv = ieee80211_priv(dev);
4866 #endif
4867         struct ieee80211_device* ieee = priv->ieee80211;
4868         RESET_TYPE      ResetType = RESET_TYPE_NORESET;
4869         static u8       check_reset_cnt=0;
4870         unsigned long flags;
4871         bool bBusyTraffic = false;
4872         static u8 last_time = 0;
4873         if(!priv->up)
4874                 return;
4875         hal_dm_watchdog(dev);
4876 #ifdef ENABLE_IPS
4877 //      printk("watch_dog ENABLE_IPS\n");
4878         if(ieee->actscanning == false){
4879                 if((ieee->iw_mode != IW_MODE_ADHOC) && (ieee->state == IEEE80211_NOLINK) && (ieee->beinretry == false) && (ieee->eRFPowerState == eRfOn) && !ieee->is_set_key){
4880                         if(ieee->PowerSaveControl.ReturnPoint == IPS_CALLBACK_NONE){
4881                                 printk("====================>haha:IPSEnter()\n");
4882                                 IPSEnter(dev);
4883                                 //ieee80211_stop_scan(priv->ieee80211);
4884                         }
4885                 }
4886         }
4887 #endif
4888         {//to get busy traffic condition
4889                 if(ieee->state == IEEE80211_LINKED)
4890                 {
4891                         if(     ieee->LinkDetectInfo.NumRxOkInPeriod> 666 ||
4892                                 ieee->LinkDetectInfo.NumTxOkInPeriod> 666 ) {
4893                                 bBusyTraffic = true;
4894                         }
4895
4896                 }
4897                 ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
4898                 ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
4899                 ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
4900         }
4901
4902
4903         //added by amy for AP roaming
4904         if (1)
4905         {
4906                 if(ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_INFRA)
4907                 {
4908                         u32     TotalRxBcnNum = 0;
4909                         u32     TotalRxDataNum = 0;
4910
4911                         rtl819x_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
4912                         if((TotalRxBcnNum+TotalRxDataNum) == 0)
4913                         {
4914                                 if( ieee->eRFPowerState == eRfOff)
4915                                         RT_TRACE(COMP_ERR,"========>%s()\n",__FUNCTION__);
4916                                 printk("===>%s(): AP is power off,connect another one\n",__FUNCTION__);
4917                 //              Dot11d_Reset(dev);
4918                                 ieee->state = IEEE80211_ASSOCIATING;
4919                                 notify_wx_assoc_event(priv->ieee80211);
4920                                 RemovePeerTS(priv->ieee80211,priv->ieee80211->current_network.bssid);
4921                                 ieee->is_roaming = true;
4922                                 ieee->is_set_key = false;
4923                              ieee->link_change(dev);
4924 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
4925                                 queue_work(ieee->wq, &ieee->associate_procedure_wq);
4926 #else
4927                                 schedule_task(&ieee->associate_procedure_wq);
4928 #endif
4929                         }
4930                 }
4931               ieee->LinkDetectInfo.NumRecvBcnInPeriod=0;
4932               ieee->LinkDetectInfo.NumRecvDataInPeriod=0;
4933
4934         }
4935 //      CAM_read_entry(dev,0);
4936         //check if reset the driver
4937         spin_lock_irqsave(&priv->tx_lock,flags);
4938         if(check_reset_cnt++ >= 3 && !ieee->is_roaming && (last_time != 1))
4939         {
4940                 ResetType = rtl819x_ifcheck_resetornot(dev);
4941                 check_reset_cnt = 3;
4942                 //DbgPrint("Start to check silent reset\n");
4943         }
4944         spin_unlock_irqrestore(&priv->tx_lock,flags);
4945         if(!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_NORMAL)
4946         {
4947                 priv->ResetProgress = RESET_TYPE_NORMAL;
4948                 RT_TRACE(COMP_RESET,"%s(): NOMAL RESET\n",__FUNCTION__);
4949                 return;
4950         }
4951         /* disable silent reset temply 2008.9.11*/
4952 #if 1
4953         if( ((priv->force_reset) || (!priv->bDisableNormalResetCheck && ResetType==RESET_TYPE_SILENT))) // This is control by OID set in Pomelo
4954         {
4955                 last_time = 1;
4956                 rtl819x_ifsilentreset(dev);
4957         }
4958         else
4959                 last_time = 0;
4960 #endif
4961         priv->force_reset = false;
4962         priv->bForcedSilentReset = false;
4963         priv->bResetInProgress = false;
4964         RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
4965
4966 }
4967
4968 void watch_dog_timer_callback(unsigned long data)
4969 {
4970         struct r8192_priv *priv = ieee80211_priv((struct net_device *) data);
4971 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
4972         queue_delayed_work(priv->priv_wq,&priv->watch_dog_wq,0);
4973 #else
4974 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
4975         schedule_task(&priv->watch_dog_wq);
4976 #else
4977         queue_work(priv->priv_wq,&priv->watch_dog_wq);
4978 #endif
4979 #endif
4980         mod_timer(&priv->watch_dog_timer, jiffies + MSECS(IEEE80211_WATCH_DOG_TIME));
4981
4982 }
4983 int _rtl8192_up(struct net_device *dev)
4984 {
4985         struct r8192_priv *priv = ieee80211_priv(dev);
4986         //int i;
4987         RT_STATUS init_status = RT_STATUS_SUCCESS;
4988         priv->up=1;
4989         priv->ieee80211->ieee_up=1;
4990         RT_TRACE(COMP_INIT, "Bringing up iface");
4991
4992         init_status = rtl8192_adapter_start(dev);
4993         if(init_status != RT_STATUS_SUCCESS)
4994         {
4995                 RT_TRACE(COMP_ERR,"ERR!!! %s(): initialization is failed!\n",__FUNCTION__);
4996                 return -1;
4997         }
4998         RT_TRACE(COMP_INIT, "start adapter finished\n");
4999 #ifdef RTL8192E
5000         if(priv->ieee80211->eRFPowerState!=eRfOn)
5001                 MgntActSet_RF_State(dev, eRfOn, priv->ieee80211->RfOffReason);
5002 #endif
5003         if(priv->ieee80211->state != IEEE80211_LINKED)
5004         ieee80211_softmac_start_protocol(priv->ieee80211);
5005         ieee80211_reset_queue(priv->ieee80211);
5006         watch_dog_timer_callback((unsigned long) dev);
5007         if(!netif_queue_stopped(dev))
5008                 netif_start_queue(dev);
5009         else
5010                 netif_wake_queue(dev);
5011
5012         return 0;
5013 }
5014
5015
5016 static int rtl8192_open(struct net_device *dev)
5017 {
5018         struct r8192_priv *priv = ieee80211_priv(dev);
5019         int ret;
5020
5021         down(&priv->wx_sem);
5022         ret = rtl8192_up(dev);
5023         up(&priv->wx_sem);
5024         return ret;
5025
5026 }
5027
5028
5029 int rtl8192_up(struct net_device *dev)
5030 {
5031         struct r8192_priv *priv = ieee80211_priv(dev);
5032
5033         if (priv->up == 1) return -1;
5034
5035         return _rtl8192_up(dev);
5036 }
5037
5038
5039 static int rtl8192_close(struct net_device *dev)
5040 {
5041         struct r8192_priv *priv = ieee80211_priv(dev);
5042         int ret;
5043
5044         down(&priv->wx_sem);
5045
5046         ret = rtl8192_down(dev);
5047
5048         up(&priv->wx_sem);
5049
5050         return ret;
5051
5052 }
5053
5054 int rtl8192_down(struct net_device *dev)
5055 {
5056         struct r8192_priv *priv = ieee80211_priv(dev);
5057 //      int i;
5058 #if 0
5059         u8      ucRegRead;
5060         u32     ulRegRead;
5061 #endif
5062         if (priv->up == 0) return -1;
5063
5064         priv->up=0;
5065         priv->ieee80211->ieee_up = 0;
5066         RT_TRACE(COMP_DOWN, "==========>%s()\n", __FUNCTION__);
5067 /* FIXME */
5068         if (!netif_queue_stopped(dev))
5069                 netif_stop_queue(dev);
5070
5071         rtl8192_irq_disable(dev);
5072 #if 0
5073         if(!priv->ieee80211->bSupportRemoteWakeUp) {
5074                 MgntActSet_RF_State(dev, eRfOff, RF_CHANGE_BY_INIT);
5075                 // 2006.11.30. System reset bit
5076                 ulRegRead = read_nic_dword(dev, CPU_GEN);
5077                 ulRegRead|=CPU_GEN_SYSTEM_RESET;
5078                 write_nic_dword(dev, CPU_GEN, ulRegRead);
5079         } else {
5080                 //2008.06.03 for WOL
5081                 write_nic_dword(dev, WFCRC0, 0xffffffff);
5082                 write_nic_dword(dev, WFCRC1, 0xffffffff);
5083                 write_nic_dword(dev, WFCRC2, 0xffffffff);
5084 #ifdef RTL8190P
5085                 //GPIO 0 = TRUE
5086                 ucRegRead = read_nic_byte(dev, GPO);
5087                 ucRegRead |= BIT0;
5088                 write_nic_byte(dev, GPO, ucRegRead);
5089 #endif
5090                 //Write PMR register
5091                 write_nic_byte(dev, PMR, 0x5);
5092                 //Disable tx, enanble rx
5093                 write_nic_byte(dev, MacBlkCtrl, 0xa);
5094         }
5095 #endif
5096 //      flush_scheduled_work();
5097         rtl8192_cancel_deferred_work(priv);
5098         deinit_hal_dm(dev);
5099         del_timer_sync(&priv->watch_dog_timer);
5100
5101         ieee80211_softmac_stop_protocol(priv->ieee80211);
5102 #ifdef ENABLE_IPS
5103         MgntActSet_RF_State(dev, eRfOff, RF_CHANGE_BY_INIT);
5104 #endif
5105         rtl8192_rtx_disable(dev);
5106         memset(&priv->ieee80211->current_network, 0 , offsetof(struct ieee80211_network, list));
5107
5108         RT_TRACE(COMP_DOWN, "<==========%s()\n", __FUNCTION__);
5109
5110                 return 0;
5111 }
5112
5113
5114 void rtl8192_commit(struct net_device *dev)
5115 {
5116         struct r8192_priv *priv = ieee80211_priv(dev);
5117
5118         if (priv->up == 0) return ;
5119
5120
5121         ieee80211_softmac_stop_protocol(priv->ieee80211);
5122
5123         rtl8192_irq_disable(dev);
5124         rtl8192_rtx_disable(dev);
5125         _rtl8192_up(dev);
5126 }
5127
5128 /*
5129 void rtl8192_restart(struct net_device *dev)
5130 {
5131         struct r8192_priv *priv = ieee80211_priv(dev);
5132 */
5133 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
5134 void rtl8192_restart(struct work_struct *work)
5135 {
5136         struct r8192_priv *priv = container_of(work, struct r8192_priv, reset_wq);
5137         struct net_device *dev = priv->ieee80211->dev;
5138 #else
5139 void rtl8192_restart(struct net_device *dev)
5140 {
5141
5142         struct r8192_priv *priv = ieee80211_priv(dev);
5143 #endif
5144
5145         down(&priv->wx_sem);
5146
5147         rtl8192_commit(dev);
5148
5149         up(&priv->wx_sem);
5150 }
5151
5152 static void r8192_set_multicast(struct net_device *dev)
5153 {
5154         struct r8192_priv *priv = ieee80211_priv(dev);
5155         short promisc;
5156
5157         //down(&priv->wx_sem);
5158
5159         /* FIXME FIXME */
5160
5161         promisc = (dev->flags & IFF_PROMISC) ? 1:0;
5162
5163         if (promisc != priv->promisc) {
5164                 ;
5165         //      rtl8192_commit(dev);
5166         }
5167
5168         priv->promisc = promisc;
5169
5170         //schedule_work(&priv->reset_wq);
5171         //up(&priv->wx_sem);
5172 }
5173
5174
5175 static int r8192_set_mac_adr(struct net_device *dev, void *mac)
5176 {
5177         struct r8192_priv *priv = ieee80211_priv(dev);
5178         struct sockaddr *addr = mac;
5179
5180         down(&priv->wx_sem);
5181
5182         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
5183
5184 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
5185         schedule_work(&priv->reset_wq);
5186 #else
5187         schedule_task(&priv->reset_wq);
5188 #endif
5189         up(&priv->wx_sem);
5190
5191         return 0;
5192 }
5193
5194 /* based on ipw2200 driver */
5195 static int rtl8192_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5196 {
5197         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
5198         struct iwreq *wrq = (struct iwreq *)rq;
5199         int ret=-1;
5200         struct ieee80211_device *ieee = priv->ieee80211;
5201         u32 key[4];
5202         u8 broadcast_addr[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
5203         struct iw_point *p = &wrq->u.data;
5204         struct ieee_param *ipw = NULL;//(struct ieee_param *)wrq->u.data.pointer;
5205
5206         down(&priv->wx_sem);
5207
5208
5209      if (p->length < sizeof(struct ieee_param) || !p->pointer){
5210              ret = -EINVAL;
5211              goto out;
5212      }
5213
5214      ipw = (struct ieee_param *)kmalloc(p->length, GFP_KERNEL);
5215      if (ipw == NULL){
5216              ret = -ENOMEM;
5217              goto out;
5218      }
5219      if (copy_from_user(ipw, p->pointer, p->length)) {
5220             kfree(ipw);
5221             ret = -EFAULT;
5222             goto out;
5223      }
5224
5225         switch (cmd) {
5226             case RTL_IOCTL_WPA_SUPPLICANT:
5227                 //parse here for HW security
5228                         if (ipw->cmd == IEEE_CMD_SET_ENCRYPTION)
5229                         {
5230                                 if (ipw->u.crypt.set_tx)
5231                                 {
5232                                         if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
5233                                                 ieee->pairwise_key_type = KEY_TYPE_CCMP;
5234                                         else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
5235                                                 ieee->pairwise_key_type = KEY_TYPE_TKIP;
5236                                         else if (strcmp(ipw->u.crypt.alg, "WEP") == 0)
5237                                         {
5238                                                 if (ipw->u.crypt.key_len == 13)
5239                                                         ieee->pairwise_key_type = KEY_TYPE_WEP104;
5240                                                 else if (ipw->u.crypt.key_len == 5)
5241                                                         ieee->pairwise_key_type = KEY_TYPE_WEP40;
5242                                         }
5243                                         else
5244                                                 ieee->pairwise_key_type = KEY_TYPE_NA;
5245
5246                                         if (ieee->pairwise_key_type)
5247                                         {
5248                                                 memcpy((u8*)key, ipw->u.crypt.key, 16);
5249                                                 EnableHWSecurityConfig8192(dev);
5250                                         //we fill both index entry and 4th entry for pairwise key as in IPW interface, adhoc will only get here, so we need index entry for its default key serching!
5251                                         //added by WB.
5252                                                 setKey(dev, 4, ipw->u.crypt.idx, ieee->pairwise_key_type, (u8*)ieee->ap_mac_addr, 0, key);
5253                                                 if (ieee->auth_mode != 2)  //LEAP WEP will never set this.
5254                                                 setKey(dev, ipw->u.crypt.idx, ipw->u.crypt.idx, ieee->pairwise_key_type, (u8*)ieee->ap_mac_addr, 0, key);
5255                                         }
5256                                         if ((ieee->pairwise_key_type == KEY_TYPE_CCMP) && ieee->pHTInfo->bCurrentHTSupport){
5257                                                         write_nic_byte(dev, 0x173, 1); //fix aes bug
5258                                                 }
5259
5260                                 }
5261                                 else //if (ipw->u.crypt.idx) //group key use idx > 0
5262                                 {
5263                                         memcpy((u8*)key, ipw->u.crypt.key, 16);
5264                                         if (strcmp(ipw->u.crypt.alg, "CCMP") == 0)
5265                                                 ieee->group_key_type= KEY_TYPE_CCMP;
5266                                         else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0)
5267                                                 ieee->group_key_type = KEY_TYPE_TKIP;
5268                                         else if (strcmp(ipw->u.crypt.alg, "WEP") == 0)
5269                                         {
5270                                                 if (ipw->u.crypt.key_len == 13)
5271                                                         ieee->group_key_type = KEY_TYPE_WEP104;
5272                                                 else if (ipw->u.crypt.key_len == 5)
5273                                                         ieee->group_key_type = KEY_TYPE_WEP40;
5274                                         }
5275                                         else
5276                                                 ieee->group_key_type = KEY_TYPE_NA;
5277
5278                                         if (ieee->group_key_type)
5279                                         {
5280                                                         setKey( dev,
5281                                                                 ipw->u.crypt.idx,
5282                                                                 ipw->u.crypt.idx,               //KeyIndex
5283                                                                 ieee->group_key_type,   //KeyType
5284                                                                 broadcast_addr, //MacAddr
5285                                                                 0,              //DefaultKey
5286                                                                 key);           //KeyContent
5287                                         }
5288                                 }
5289                         }
5290 #ifdef JOHN_DEBUG
5291                 //john's test 0711
5292         {
5293                 int i;
5294                 printk("@@ wrq->u pointer = ");
5295                 for(i=0;i<wrq->u.data.length;i++){
5296                         if(i%10==0) printk("\n");
5297                         printk( "%8x|", ((u32*)wrq->u.data.pointer)[i] );
5298                 }
5299                 printk("\n");
5300         }
5301 #endif /*JOHN_DEBUG*/
5302                 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211, &wrq->u.data);
5303                 break;
5304
5305             default:
5306                 ret = -EOPNOTSUPP;
5307                 break;
5308         }
5309
5310         kfree(ipw);
5311 out:
5312         up(&priv->wx_sem);
5313
5314         return ret;
5315 }
5316
5317 static u8 HwRateToMRate90(bool bIsHT, u8 rate)
5318 {
5319         u8  ret_rate = 0x02;
5320
5321         if(!bIsHT) {
5322                 switch(rate) {
5323                         case DESC90_RATE1M:   ret_rate = MGN_1M;         break;
5324                         case DESC90_RATE2M:   ret_rate = MGN_2M;         break;
5325                         case DESC90_RATE5_5M: ret_rate = MGN_5_5M;       break;
5326                         case DESC90_RATE11M:  ret_rate = MGN_11M;        break;
5327                         case DESC90_RATE6M:   ret_rate = MGN_6M;         break;
5328                         case DESC90_RATE9M:   ret_rate = MGN_9M;         break;
5329                         case DESC90_RATE12M:  ret_rate = MGN_12M;        break;
5330                         case DESC90_RATE18M:  ret_rate = MGN_18M;        break;
5331                         case DESC90_RATE24M:  ret_rate = MGN_24M;        break;
5332                         case DESC90_RATE36M:  ret_rate = MGN_36M;        break;
5333                         case DESC90_RATE48M:  ret_rate = MGN_48M;        break;
5334                         case DESC90_RATE54M:  ret_rate = MGN_54M;        break;
5335
5336                         default:
5337                                               RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n", rate, bIsHT);
5338                                               break;
5339                 }
5340
5341         } else {
5342                 switch(rate) {
5343                         case DESC90_RATEMCS0:   ret_rate = MGN_MCS0;    break;
5344                         case DESC90_RATEMCS1:   ret_rate = MGN_MCS1;    break;
5345                         case DESC90_RATEMCS2:   ret_rate = MGN_MCS2;    break;
5346                         case DESC90_RATEMCS3:   ret_rate = MGN_MCS3;    break;
5347                         case DESC90_RATEMCS4:   ret_rate = MGN_MCS4;    break;
5348                         case DESC90_RATEMCS5:   ret_rate = MGN_MCS5;    break;
5349                         case DESC90_RATEMCS6:   ret_rate = MGN_MCS6;    break;
5350                         case DESC90_RATEMCS7:   ret_rate = MGN_MCS7;    break;
5351                         case DESC90_RATEMCS8:   ret_rate = MGN_MCS8;    break;
5352                         case DESC90_RATEMCS9:   ret_rate = MGN_MCS9;    break;
5353                         case DESC90_RATEMCS10:  ret_rate = MGN_MCS10;   break;
5354                         case DESC90_RATEMCS11:  ret_rate = MGN_MCS11;   break;
5355                         case DESC90_RATEMCS12:  ret_rate = MGN_MCS12;   break;
5356                         case DESC90_RATEMCS13:  ret_rate = MGN_MCS13;   break;
5357                         case DESC90_RATEMCS14:  ret_rate = MGN_MCS14;   break;
5358                         case DESC90_RATEMCS15:  ret_rate = MGN_MCS15;   break;
5359                         case DESC90_RATEMCS32:  ret_rate = (0x80|0x20); break;
5360
5361                         default:
5362                                                 RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n",rate, bIsHT);
5363                                                 break;
5364                 }
5365         }
5366
5367         return ret_rate;
5368 }
5369
5370 /**
5371  * Function:     UpdateRxPktTimeStamp
5372  * Overview:     Recored down the TSF time stamp when receiving a packet
5373  *
5374  * Input:
5375  *       PADAPTER        Adapter
5376  *       PRT_RFD         pRfd,
5377  *
5378  * Output:
5379  *       PRT_RFD         pRfd
5380  *                               (pRfd->Status.TimeStampHigh is updated)
5381  *                               (pRfd->Status.TimeStampLow is updated)
5382  * Return:
5383  *               None
5384  */
5385 static void UpdateRxPktTimeStamp8190 (struct net_device *dev, struct ieee80211_rx_stats *stats)
5386 {
5387         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
5388
5389         if(stats->bIsAMPDU && !stats->bFirstMPDU) {
5390                 stats->mac_time[0] = priv->LastRxDescTSFLow;
5391                 stats->mac_time[1] = priv->LastRxDescTSFHigh;
5392         } else {
5393                 priv->LastRxDescTSFLow = stats->mac_time[0];
5394                 priv->LastRxDescTSFHigh = stats->mac_time[1];
5395         }
5396 }
5397
5398 static long rtl819x_translate_todbm(u8 signal_strength_index)// 0-100 index.
5399 {
5400         long    signal_power; // in dBm.
5401
5402         // Translate to dBm (x=0.5y-95).
5403         signal_power = (long)((signal_strength_index + 1) >> 1);
5404         signal_power -= 95;
5405
5406         return signal_power;
5407 }
5408
5409 //
5410 //      Description:
5411 //              Update Rx signal related information in the packet reeived
5412 //              to RxStats. User application can query RxStats to realize
5413 //              current Rx signal status.
5414 //
5415 //      Assumption:
5416 //              In normal operation, user only care about the information of the BSS
5417 //              and we shall invoke this function if the packet received is from the BSS.
5418 //
5419 static void
5420 rtl819x_update_rxsignalstatistics8190pci(
5421         struct r8192_priv * priv,
5422         struct ieee80211_rx_stats * pprevious_stats
5423         )
5424 {
5425         int weighting = 0;
5426
5427         //2 <ToDo> Update Rx Statistics (such as signal strength and signal quality).
5428
5429         // Initila state
5430         if(priv->stats.recv_signal_power == 0)
5431                 priv->stats.recv_signal_power = pprevious_stats->RecvSignalPower;
5432
5433         // To avoid the past result restricting the statistics sensitivity, weight the current power (5/6) to speed up the
5434         // reaction of smoothed Signal Power.
5435         if(pprevious_stats->RecvSignalPower > priv->stats.recv_signal_power)
5436                 weighting = 5;
5437         else if(pprevious_stats->RecvSignalPower < priv->stats.recv_signal_power)
5438                 weighting = (-5);
5439         //
5440         // We need more correct power of received packets and the  "SignalStrength" of RxStats have been beautified or translated,
5441         // so we record the correct power in Dbm here. By Bruce, 2008-03-07.
5442         //
5443         priv->stats.recv_signal_power = (priv->stats.recv_signal_power * 5 + pprevious_stats->RecvSignalPower + weighting) / 6;
5444 }
5445
5446 static void
5447 rtl8190_process_cck_rxpathsel(
5448         struct r8192_priv * priv,
5449         struct ieee80211_rx_stats * pprevious_stats
5450         )
5451 {
5452 #ifdef RTL8190P //Only 90P 2T4R need to check
5453         char                            last_cck_adc_pwdb[4]={0,0,0,0};
5454         u8                              i;
5455 //cosa add for Rx path selection
5456                 if(priv->rf_type == RF_2T4R && DM_RxPathSelTable.Enable)
5457                 {
5458                         if(pprevious_stats->bIsCCK &&
5459                                 (pprevious_stats->bPacketToSelf ||pprevious_stats->bPacketBeacon))
5460                         {
5461                                 /* record the cck adc_pwdb to the sliding window. */
5462                                 if(priv->stats.cck_adc_pwdb.TotalNum++ >= PHY_RSSI_SLID_WIN_MAX)
5463                                 {
5464                                         priv->stats.cck_adc_pwdb.TotalNum = PHY_RSSI_SLID_WIN_MAX;
5465                                         for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
5466                                         {
5467                                                 last_cck_adc_pwdb[i] = priv->stats.cck_adc_pwdb.elements[i][priv->stats.cck_adc_pwdb.index];
5468                                                 priv->stats.cck_adc_pwdb.TotalVal[i] -= last_cck_adc_pwdb[i];
5469                                         }
5470                                 }
5471                                 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
5472                                 {
5473                                         priv->stats.cck_adc_pwdb.TotalVal[i] += pprevious_stats->cck_adc_pwdb[i];
5474                                         priv->stats.cck_adc_pwdb.elements[i][priv->stats.cck_adc_pwdb.index] = pprevious_stats->cck_adc_pwdb[i];
5475                                 }
5476                                 priv->stats.cck_adc_pwdb.index++;
5477                                 if(priv->stats.cck_adc_pwdb.index >= PHY_RSSI_SLID_WIN_MAX)
5478                                         priv->stats.cck_adc_pwdb.index = 0;
5479
5480                                 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
5481                                 {
5482                                         DM_RxPathSelTable.cck_pwdb_sta[i] = priv->stats.cck_adc_pwdb.TotalVal[i]/priv->stats.cck_adc_pwdb.TotalNum;
5483                                 }
5484
5485                                 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
5486                                 {
5487                                         if(pprevious_stats->cck_adc_pwdb[i]  > (char)priv->undecorated_smoothed_cck_adc_pwdb[i])
5488                                         {
5489                                                 priv->undecorated_smoothed_cck_adc_pwdb[i] =
5490                                                         ( (priv->undecorated_smoothed_cck_adc_pwdb[i]*(Rx_Smooth_Factor-1)) +
5491                                                         (pprevious_stats->cck_adc_pwdb[i])) /(Rx_Smooth_Factor);
5492                                                 priv->undecorated_smoothed_cck_adc_pwdb[i] = priv->undecorated_smoothed_cck_adc_pwdb[i] + 1;
5493                                         }
5494                                         else
5495                                         {
5496                                                 priv->undecorated_smoothed_cck_adc_pwdb[i] =
5497                                                         ( (priv->undecorated_smoothed_cck_adc_pwdb[i]*(Rx_Smooth_Factor-1)) +
5498                                                         (pprevious_stats->cck_adc_pwdb[i])) /(Rx_Smooth_Factor);
5499                                         }
5500                                 }
5501                         }
5502                 }
5503 #endif
5504 }
5505
5506
5507 /* 2008/01/22 MH We can not delcare RSSI/EVM total value of sliding window to
5508         be a local static. Otherwise, it may increase when we return from S3/S4. The
5509         value will be kept in memory or disk. We must delcare the value in adapter
5510         and it will be reinitialized when return from S3/S4. */
5511 static void rtl8192_process_phyinfo(struct r8192_priv * priv, u8* buffer,struct ieee80211_rx_stats * pprevious_stats, struct ieee80211_rx_stats * pcurrent_stats)
5512 {
5513         bool bcheck = false;
5514         u8      rfpath;
5515         u32 nspatial_stream, tmp_val;
5516         //u8    i;
5517         static u32 slide_rssi_index=0, slide_rssi_statistics=0;
5518         static u32 slide_evm_index=0, slide_evm_statistics=0;
5519         static u32 last_rssi=0, last_evm=0;
5520         //cosa add for rx path selection
5521 //      static long slide_cck_adc_pwdb_index=0, slide_cck_adc_pwdb_statistics=0;
5522 //      static char last_cck_adc_pwdb[4]={0,0,0,0};
5523         //cosa add for beacon rssi smoothing
5524         static u32 slide_beacon_adc_pwdb_index=0, slide_beacon_adc_pwdb_statistics=0;
5525         static u32 last_beacon_adc_pwdb=0;
5526
5527         struct ieee80211_hdr_3addr *hdr;
5528         u16 sc ;
5529         unsigned int frag,seq;
5530         hdr = (struct ieee80211_hdr_3addr *)buffer;
5531         sc = le16_to_cpu(hdr->seq_ctl);
5532         frag = WLAN_GET_SEQ_FRAG(sc);
5533         seq = WLAN_GET_SEQ_SEQ(sc);
5534         //cosa add 04292008 to record the sequence number
5535         pcurrent_stats->Seq_Num = seq;
5536         //
5537         // Check whether we should take the previous packet into accounting
5538         //
5539         if(!pprevious_stats->bIsAMPDU)
5540         {
5541                 // if previous packet is not aggregated packet
5542                 bcheck = true;
5543         }else
5544         {
5545 //remve for that we don't use AMPDU to calculate PWDB,because the reported PWDB of some AP is fault.
5546 #if 0
5547                 // if previous packet is aggregated packet, and current packet
5548                 //      (1) is not AMPDU
5549                 //      (2) is the first packet of one AMPDU
5550                 // that means the previous packet is the last one aggregated packet
5551                 if( !pcurrent_stats->bIsAMPDU || pcurrent_stats->bFirstMPDU)
5552                         bcheck = true;
5553 #endif
5554         }
5555
5556         if(slide_rssi_statistics++ >= PHY_RSSI_SLID_WIN_MAX)
5557         {
5558                 slide_rssi_statistics = PHY_RSSI_SLID_WIN_MAX;
5559                 last_rssi = priv->stats.slide_signal_strength[slide_rssi_index];
5560                 priv->stats.slide_rssi_total -= last_rssi;
5561         }
5562         priv->stats.slide_rssi_total += pprevious_stats->SignalStrength;
5563
5564         priv->stats.slide_signal_strength[slide_rssi_index++] = pprevious_stats->SignalStrength;
5565         if(slide_rssi_index >= PHY_RSSI_SLID_WIN_MAX)
5566                 slide_rssi_index = 0;
5567
5568         // <1> Showed on UI for user, in dbm
5569         tmp_val = priv->stats.slide_rssi_total/slide_rssi_statistics;
5570         priv->stats.signal_strength = rtl819x_translate_todbm((u8)tmp_val);
5571         pcurrent_stats->rssi = priv->stats.signal_strength;
5572         //
5573         // If the previous packet does not match the criteria, neglect it
5574         //
5575         if(!pprevious_stats->bPacketMatchBSSID)
5576         {
5577                 if(!pprevious_stats->bToSelfBA)
5578                         return;
5579         }
5580
5581         if(!bcheck)
5582                 return;
5583
5584         rtl8190_process_cck_rxpathsel(priv,pprevious_stats);
5585
5586         //
5587         // Check RSSI
5588         //
5589         priv->stats.num_process_phyinfo++;
5590 #if 0
5591         /* record the general signal strength to the sliding window. */
5592         if(slide_rssi_statistics++ >= PHY_RSSI_SLID_WIN_MAX)
5593         {
5594                 slide_rssi_statistics = PHY_RSSI_SLID_WIN_MAX;
5595                 last_rssi = priv->stats.slide_signal_strength[slide_rssi_index];
5596                 priv->stats.slide_rssi_total -= last_rssi;
5597         }
5598         priv->stats.slide_rssi_total += pprevious_stats->SignalStrength;
5599
5600         priv->stats.slide_signal_strength[slide_rssi_index++] = pprevious_stats->SignalStrength;
5601         if(slide_rssi_index >= PHY_RSSI_SLID_WIN_MAX)
5602                 slide_rssi_index = 0;
5603
5604         // <1> Showed on UI for user, in dbm
5605         tmp_val = priv->stats.slide_rssi_total/slide_rssi_statistics;
5606         priv->stats.signal_strength = rtl819x_translate_todbm((u8)tmp_val);
5607
5608 #endif
5609         // <2> Showed on UI for engineering
5610         // hardware does not provide rssi information for each rf path in CCK
5611         if(!pprevious_stats->bIsCCK && pprevious_stats->bPacketToSelf)
5612         {
5613                 for (rfpath = RF90_PATH_A; rfpath < RF90_PATH_C; rfpath++)
5614                 {
5615                         if (!rtl8192_phy_CheckIsLegalRFPath(priv->ieee80211->dev, rfpath))
5616                                 continue;
5617                         RT_TRACE(COMP_DBG,"Jacken -> pPreviousstats->RxMIMOSignalStrength[rfpath]  = %d \n" ,pprevious_stats->RxMIMOSignalStrength[rfpath] );
5618                         //Fixed by Jacken 2008-03-20
5619                         if(priv->stats.rx_rssi_percentage[rfpath] == 0)
5620                         {
5621                                 priv->stats.rx_rssi_percentage[rfpath] = pprevious_stats->RxMIMOSignalStrength[rfpath];
5622                                 //DbgPrint("MIMO RSSI initialize \n");
5623                         }
5624                         if(pprevious_stats->RxMIMOSignalStrength[rfpath]  > priv->stats.rx_rssi_percentage[rfpath])
5625                         {
5626                                 priv->stats.rx_rssi_percentage[rfpath] =
5627                                         ( (priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
5628                                         (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
5629                                 priv->stats.rx_rssi_percentage[rfpath] = priv->stats.rx_rssi_percentage[rfpath]  + 1;
5630                         }
5631                         else
5632                         {
5633                                 priv->stats.rx_rssi_percentage[rfpath] =
5634                                         ( (priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
5635                                         (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
5636                         }
5637                         RT_TRACE(COMP_DBG,"Jacken -> priv->RxStats.RxRSSIPercentage[rfPath]  = %d \n" ,priv->stats.rx_rssi_percentage[rfpath] );
5638                 }
5639         }
5640
5641
5642         //
5643         // Check PWDB.
5644         //
5645         //cosa add for beacon rssi smoothing by average.
5646         if(pprevious_stats->bPacketBeacon)
5647         {
5648                 /* record the beacon pwdb to the sliding window. */
5649                 if(slide_beacon_adc_pwdb_statistics++ >= PHY_Beacon_RSSI_SLID_WIN_MAX)
5650                 {
5651                         slide_beacon_adc_pwdb_statistics = PHY_Beacon_RSSI_SLID_WIN_MAX;
5652                         last_beacon_adc_pwdb = priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index];
5653                         priv->stats.Slide_Beacon_Total -= last_beacon_adc_pwdb;
5654                         //DbgPrint("slide_beacon_adc_pwdb_index = %d, last_beacon_adc_pwdb = %d, Adapter->RxStats.Slide_Beacon_Total = %d\n",
5655                         //      slide_beacon_adc_pwdb_index, last_beacon_adc_pwdb, Adapter->RxStats.Slide_Beacon_Total);
5656                 }
5657                 priv->stats.Slide_Beacon_Total += pprevious_stats->RxPWDBAll;
5658                 priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index] = pprevious_stats->RxPWDBAll;
5659                 //DbgPrint("slide_beacon_adc_pwdb_index = %d, pPreviousRfd->Status.RxPWDBAll = %d\n", slide_beacon_adc_pwdb_index, pPreviousRfd->Status.RxPWDBAll);
5660                 slide_beacon_adc_pwdb_index++;
5661                 if(slide_beacon_adc_pwdb_index >= PHY_Beacon_RSSI_SLID_WIN_MAX)
5662                         slide_beacon_adc_pwdb_index = 0;
5663                 pprevious_stats->RxPWDBAll = priv->stats.Slide_Beacon_Total/slide_beacon_adc_pwdb_statistics;
5664                 if(pprevious_stats->RxPWDBAll >= 3)
5665                         pprevious_stats->RxPWDBAll -= 3;
5666         }
5667
5668         RT_TRACE(COMP_RXDESC, "Smooth %s PWDB = %d\n",
5669                                 pprevious_stats->bIsCCK? "CCK": "OFDM",
5670                                 pprevious_stats->RxPWDBAll);
5671
5672         if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA)
5673         {
5674                 if(priv->undecorated_smoothed_pwdb < 0) // initialize
5675                 {
5676                         priv->undecorated_smoothed_pwdb = pprevious_stats->RxPWDBAll;
5677                         //DbgPrint("First pwdb initialize \n");
5678                 }
5679 #if 1
5680                 if(pprevious_stats->RxPWDBAll > (u32)priv->undecorated_smoothed_pwdb)
5681                 {
5682                         priv->undecorated_smoothed_pwdb =
5683                                         ( ((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
5684                                         (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
5685                         priv->undecorated_smoothed_pwdb = priv->undecorated_smoothed_pwdb + 1;
5686                 }
5687                 else
5688                 {
5689                         priv->undecorated_smoothed_pwdb =
5690                                         ( ((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
5691                                         (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
5692                 }
5693 #else
5694                 //Fixed by Jacken 2008-03-20
5695                 if(pPreviousRfd->Status.RxPWDBAll > (u32)pHalData->UndecoratedSmoothedPWDB)
5696                 {
5697                         pHalData->UndecoratedSmoothedPWDB =
5698                                         ( ((pHalData->UndecoratedSmoothedPWDB)* 5) + (pPreviousRfd->Status.RxPWDBAll)) / 6;
5699                         pHalData->UndecoratedSmoothedPWDB = pHalData->UndecoratedSmoothedPWDB + 1;
5700                 }
5701                 else
5702                 {
5703                         pHalData->UndecoratedSmoothedPWDB =
5704                                         ( ((pHalData->UndecoratedSmoothedPWDB)* 5) + (pPreviousRfd->Status.RxPWDBAll)) / 6;
5705                 }
5706 #endif
5707                 rtl819x_update_rxsignalstatistics8190pci(priv,pprevious_stats);
5708         }
5709
5710         //
5711         // Check EVM
5712         //
5713         /* record the general EVM to the sliding window. */
5714         if(pprevious_stats->SignalQuality == 0)
5715         {
5716         }
5717         else
5718         {
5719                 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA){
5720                         if(slide_evm_statistics++ >= PHY_RSSI_SLID_WIN_MAX){
5721                                 slide_evm_statistics = PHY_RSSI_SLID_WIN_MAX;
5722                                 last_evm = priv->stats.slide_evm[slide_evm_index];
5723                                 priv->stats.slide_evm_total -= last_evm;
5724                         }
5725
5726                         priv->stats.slide_evm_total += pprevious_stats->SignalQuality;
5727
5728                         priv->stats.slide_evm[slide_evm_index++] = pprevious_stats->SignalQuality;
5729                         if(slide_evm_index >= PHY_RSSI_SLID_WIN_MAX)
5730                                 slide_evm_index = 0;
5731
5732                         // <1> Showed on UI for user, in percentage.
5733                         tmp_val = priv->stats.slide_evm_total/slide_evm_statistics;
5734                         priv->stats.signal_quality = tmp_val;
5735                         //cosa add 10/11/2007, Showed on UI for user in Windows Vista, for Link quality.
5736                         priv->stats.last_signal_strength_inpercent = tmp_val;
5737                 }
5738
5739                 // <2> Showed on UI for engineering
5740                 if(pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA)
5741                 {
5742                         for(nspatial_stream = 0; nspatial_stream<2 ; nspatial_stream++) // 2 spatial stream
5743                         {
5744                                 if(pprevious_stats->RxMIMOSignalQuality[nspatial_stream] != -1)
5745                                 {
5746                                         if(priv->stats.rx_evm_percentage[nspatial_stream] == 0) // initialize
5747                                         {
5748                                                 priv->stats.rx_evm_percentage[nspatial_stream] = pprevious_stats->RxMIMOSignalQuality[nspatial_stream];
5749                                         }
5750                                         priv->stats.rx_evm_percentage[nspatial_stream] =
5751                                                 ( (priv->stats.rx_evm_percentage[nspatial_stream]* (Rx_Smooth_Factor-1)) +
5752                                                 (pprevious_stats->RxMIMOSignalQuality[nspatial_stream]* 1)) / (Rx_Smooth_Factor);
5753                                 }
5754                         }
5755                 }
5756         }
5757
5758 }
5759
5760 /*-----------------------------------------------------------------------------
5761  * Function:    rtl819x_query_rxpwrpercentage()
5762  *
5763  * Overview:
5764  *
5765  * Input:               char            antpower
5766  *
5767  * Output:              NONE
5768  *
5769  * Return:              0-100 percentage
5770  *
5771  * Revised History:
5772  *      When            Who     Remark
5773  *      05/26/2008      amy     Create Version 0 porting from windows code.
5774  *
5775  *---------------------------------------------------------------------------*/
5776 static u8 rtl819x_query_rxpwrpercentage(
5777         char            antpower
5778         )
5779 {
5780         if ((antpower <= -100) || (antpower >= 20))
5781         {
5782                 return  0;
5783         }
5784         else if (antpower >= 0)
5785         {
5786                 return  100;
5787         }
5788         else
5789         {
5790                 return  (100+antpower);
5791         }
5792
5793 }       /* QueryRxPwrPercentage */
5794
5795 static u8
5796 rtl819x_evm_dbtopercentage(
5797         char value
5798         )
5799 {
5800         char ret_val;
5801
5802         ret_val = value;
5803
5804         if(ret_val >= 0)
5805                 ret_val = 0;
5806         if(ret_val <= -33)
5807                 ret_val = -33;
5808         ret_val = 0 - ret_val;
5809         ret_val*=3;
5810         if(ret_val == 99)
5811                 ret_val = 100;
5812         return(ret_val);
5813 }
5814
5815 //
5816 //      Description:
5817 //      We want good-looking for signal strength/quality
5818 //      2007/7/19 01:09, by cosa.
5819 //
5820 static long rtl819x_signal_scale_mapping(long currsig)
5821 {
5822         long retsig;
5823
5824         // Step 1. Scale mapping.
5825         if(currsig >= 61 && currsig <= 100)
5826         {
5827                 retsig = 90 + ((currsig - 60) / 4);
5828         }
5829         else if(currsig >= 41 && currsig <= 60)
5830         {
5831                 retsig = 78 + ((currsig - 40) / 2);
5832         }
5833         else if(currsig >= 31 && currsig <= 40)
5834         {
5835                 retsig = 66 + (currsig - 30);
5836         }
5837         else if(currsig >= 21 && currsig <= 30)
5838         {
5839                 retsig = 54 + (currsig - 20);
5840         }
5841         else if(currsig >= 5 && currsig <= 20)
5842         {
5843                 retsig = 42 + (((currsig - 5) * 2) / 3);
5844         }
5845         else if(currsig == 4)
5846         {
5847                 retsig = 36;
5848         }
5849         else if(currsig == 3)
5850         {
5851                 retsig = 27;
5852         }
5853         else if(currsig == 2)
5854         {
5855                 retsig = 18;
5856         }
5857         else if(currsig == 1)
5858         {
5859                 retsig = 9;
5860         }
5861         else
5862         {
5863                 retsig = currsig;
5864         }
5865
5866         return retsig;
5867 }
5868
5869 static void rtl8192_query_rxphystatus(
5870         struct r8192_priv * priv,
5871         struct ieee80211_rx_stats * pstats,
5872         prx_desc_819x_pci  pdesc,
5873         prx_fwinfo_819x_pci   pdrvinfo,
5874         struct ieee80211_rx_stats * precord_stats,
5875         bool bpacket_match_bssid,
5876         bool bpacket_toself,
5877         bool bPacketBeacon,
5878         bool bToSelfBA
5879         )
5880 {
5881         //PRT_RFD_STATUS                pRtRfdStatus = &(pRfd->Status);
5882         phy_sts_ofdm_819xpci_t* pofdm_buf;
5883         phy_sts_cck_819xpci_t   *       pcck_buf;
5884         phy_ofdm_rx_status_rxsc_sgien_exintfflag* prxsc;
5885         u8                              *prxpkt;
5886         u8                              i,max_spatial_stream, tmp_rxsnr, tmp_rxevm, rxsc_sgien_exflg;
5887         char                            rx_pwr[4], rx_pwr_all=0;
5888         //long                          rx_avg_pwr = 0;
5889         char                            rx_snrX, rx_evmX;
5890         u8                              evm, pwdb_all;
5891         u32                     RSSI, total_rssi=0;//, total_evm=0;
5892 //      long                            signal_strength_index = 0;
5893         u8                              is_cck_rate=0;
5894         u8                              rf_rx_num = 0;
5895
5896         /* 2007/07/04 MH For OFDM RSSI. For high power or not. */
5897         static  u8              check_reg824 = 0;
5898         static  u32             reg824_bit9 = 0;
5899
5900         priv->stats.numqry_phystatus++;
5901
5902         is_cck_rate = rx_hal_is_cck_rate(pdrvinfo);
5903
5904         // Record it for next packet processing
5905         memset(precord_stats, 0, sizeof(struct ieee80211_rx_stats));
5906         pstats->bPacketMatchBSSID = precord_stats->bPacketMatchBSSID = bpacket_match_bssid;
5907         pstats->bPacketToSelf = precord_stats->bPacketToSelf = bpacket_toself;
5908         pstats->bIsCCK = precord_stats->bIsCCK = is_cck_rate;//RX_HAL_IS_CCK_RATE(pDrvInfo);
5909         pstats->bPacketBeacon = precord_stats->bPacketBeacon = bPacketBeacon;
5910         pstats->bToSelfBA = precord_stats->bToSelfBA = bToSelfBA;
5911         /*2007.08.30 requested by SD3 Jerry */
5912         if(check_reg824 == 0)
5913         {
5914                 reg824_bit9 = rtl8192_QueryBBReg(priv->ieee80211->dev, rFPGA0_XA_HSSIParameter2, 0x200);
5915                 check_reg824 = 1;
5916         }
5917
5918
5919         prxpkt = (u8*)pdrvinfo;
5920
5921         /* Move pointer to the 16th bytes. Phy status start address. */
5922         prxpkt += sizeof(rx_fwinfo_819x_pci);
5923
5924         /* Initial the cck and ofdm buffer pointer */
5925         pcck_buf = (phy_sts_cck_819xpci_t *)prxpkt;
5926         pofdm_buf = (phy_sts_ofdm_819xpci_t *)prxpkt;
5927
5928         pstats->RxMIMOSignalQuality[0] = -1;
5929         pstats->RxMIMOSignalQuality[1] = -1;
5930         precord_stats->RxMIMOSignalQuality[0] = -1;
5931         precord_stats->RxMIMOSignalQuality[1] = -1;
5932
5933         if(is_cck_rate)
5934         {
5935                 //
5936                 // (1)Hardware does not provide RSSI for CCK
5937                 //
5938
5939                 //
5940                 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
5941                 //
5942                 u8 report;//, cck_agc_rpt;
5943 #ifdef RTL8190P
5944                 u8 tmp_pwdb;
5945                 char cck_adc_pwdb[4];
5946 #endif
5947                 priv->stats.numqry_phystatusCCK++;
5948
5949 #ifdef RTL8190P //Only 90P 2T4R need to check
5950                 if(priv->rf_type == RF_2T4R && DM_RxPathSelTable.Enable && bpacket_match_bssid)
5951                 {
5952                         for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
5953                         {
5954                                 tmp_pwdb = pcck_buf->adc_pwdb_X[i];
5955                                 cck_adc_pwdb[i] = (char)tmp_pwdb;
5956                                 cck_adc_pwdb[i] /= 2;
5957                                 pstats->cck_adc_pwdb[i] = precord_stats->cck_adc_pwdb[i] = cck_adc_pwdb[i];
5958                                 //DbgPrint("RF-%d tmp_pwdb = 0x%x, cck_adc_pwdb = %d", i, tmp_pwdb, cck_adc_pwdb[i]);
5959                         }
5960                 }
5961 #endif
5962
5963                 if(!reg824_bit9)
5964                 {
5965                         report = pcck_buf->cck_agc_rpt & 0xc0;
5966                         report = report>>6;
5967                         switch(report)
5968                         {
5969                                 //Fixed by Jacken from Bryant 2008-03-20
5970                                 //Original value is -38 , -26 , -14 , -2
5971                                 //Fixed value is -35 , -23 , -11 , 6
5972                                 case 0x3:
5973                                         rx_pwr_all = -35 - (pcck_buf->cck_agc_rpt & 0x3e);
5974                                         break;
5975                                 case 0x2:
5976                                         rx_pwr_all = -23 - (pcck_buf->cck_agc_rpt & 0x3e);
5977                                         break;
5978                                 case 0x1:
5979                                         rx_pwr_all = -11 - (pcck_buf->cck_agc_rpt & 0x3e);
5980                                         break;
5981                                 case 0x0:
5982                                         rx_pwr_all = 8 - (pcck_buf->cck_agc_rpt & 0x3e);
5983                                         break;
5984                         }
5985                 }
5986                 else
5987                 {
5988                         report = pcck_buf->cck_agc_rpt & 0x60;
5989                         report = report>>5;
5990                         switch(report)
5991                         {
5992                                 case 0x3:
5993                                         rx_pwr_all = -35 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
5994                                         break;
5995                                 case 0x2:
5996                                         rx_pwr_all = -23 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1);
5997                                         break;
5998                                 case 0x1:
5999                                         rx_pwr_all = -11 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
6000                                         break;
6001                                 case 0x0:
6002                                         rx_pwr_all = -8 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1) ;
6003                                         break;
6004                         }
6005                 }
6006
6007                 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
6008                 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
6009                 pstats->RecvSignalPower = rx_pwr_all;
6010
6011                 //
6012                 // (3) Get Signal Quality (EVM)
6013                 //
6014                 if(bpacket_match_bssid)
6015                 {
6016                         u8      sq;
6017
6018                         if(pstats->RxPWDBAll > 40)
6019                         {
6020                                 sq = 100;
6021                         }else
6022                         {
6023                                 sq = pcck_buf->sq_rpt;
6024
6025                                 if(pcck_buf->sq_rpt > 64)
6026                                         sq = 0;
6027                                 else if (pcck_buf->sq_rpt < 20)
6028                                         sq = 100;
6029                                 else
6030                                         sq = ((64-sq) * 100) / 44;
6031                         }
6032                         pstats->SignalQuality = precord_stats->SignalQuality = sq;
6033                         pstats->RxMIMOSignalQuality[0] = precord_stats->RxMIMOSignalQuality[0] = sq;
6034                         pstats->RxMIMOSignalQuality[1] = precord_stats->RxMIMOSignalQuality[1] = -1;
6035                 }
6036         }
6037         else
6038         {
6039                 priv->stats.numqry_phystatusHT++;
6040                 //
6041                 // (1)Get RSSI for HT rate
6042                 //
6043                 for(i=RF90_PATH_A; i<RF90_PATH_MAX; i++)
6044                 {
6045                         // 2008/01/30 MH we will judge RF RX path now.
6046                         if (priv->brfpath_rxenable[i])
6047                                 rf_rx_num++;
6048                         //else
6049                                 //continue;
6050
6051                         //Fixed by Jacken from Bryant 2008-03-20
6052                         //Original value is 106
6053 #ifdef RTL8190P    //Modify by Jacken 2008/03/31
6054                         rx_pwr[i] = ((pofdm_buf->trsw_gain_X[i]&0x3F)*2) - 106;
6055 #else
6056                         rx_pwr[i] = ((pofdm_buf->trsw_gain_X[i]&0x3F)*2) - 110;
6057 #endif
6058
6059                         //Get Rx snr value in DB
6060                         tmp_rxsnr = pofdm_buf->rxsnr_X[i];
6061                         rx_snrX = (char)(tmp_rxsnr);
6062                         rx_snrX /= 2;
6063                         priv->stats.rxSNRdB[i] = (long)rx_snrX;
6064
6065                         /* Translate DBM to percentage. */
6066                         RSSI = rtl819x_query_rxpwrpercentage(rx_pwr[i]);
6067                         if (priv->brfpath_rxenable[i])
6068                                 total_rssi += RSSI;
6069
6070                         /* Record Signal Strength for next packet */
6071                         if(bpacket_match_bssid)
6072                         {
6073                                 pstats->RxMIMOSignalStrength[i] =(u8) RSSI;
6074                                 precord_stats->RxMIMOSignalStrength[i] =(u8) RSSI;
6075                         }
6076                 }
6077
6078
6079                 //
6080                 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
6081                 //
6082                 //Fixed by Jacken from Bryant 2008-03-20
6083                 //Original value is 106
6084                 rx_pwr_all = (((pofdm_buf->pwdb_all ) >> 1 )& 0x7f) -106;
6085                 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
6086
6087                 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
6088                 pstats->RxPower = precord_stats->RxPower =      rx_pwr_all;
6089                 pstats->RecvSignalPower = rx_pwr_all;
6090                 //
6091                 // (3)EVM of HT rate
6092                 //
6093                 if(pdrvinfo->RxHT && pdrvinfo->RxRate>=DESC90_RATEMCS8 &&
6094                         pdrvinfo->RxRate<=DESC90_RATEMCS15)
6095                         max_spatial_stream = 2; //both spatial stream make sense
6096                 else
6097                         max_spatial_stream = 1; //only spatial stream 1 makes sense
6098
6099                 for(i=0; i<max_spatial_stream; i++)
6100                 {
6101                         tmp_rxevm = pofdm_buf->rxevm_X[i];
6102                         rx_evmX = (char)(tmp_rxevm);
6103
6104                         // Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment
6105                         // fill most significant bit to "zero" when doing shifting operation which may change a negative
6106                         // value to positive one, then the dbm value (which is supposed to be negative)  is not correct anymore.
6107                         rx_evmX /= 2;   //dbm
6108
6109                         evm = rtl819x_evm_dbtopercentage(rx_evmX);
6110 #if 0
6111                         EVM = SignalScaleMapping(EVM);//make it good looking, from 0~100
6112 #endif
6113                         if(bpacket_match_bssid)
6114                         {
6115                                 if(i==0) // Fill value in RFD, Get the first spatial stream only
6116                                         pstats->SignalQuality = precord_stats->SignalQuality = (u8)(evm & 0xff);
6117                                 pstats->RxMIMOSignalQuality[i] = precord_stats->RxMIMOSignalQuality[i] = (u8)(evm & 0xff);
6118                         }
6119                 }
6120
6121
6122                 /* record rx statistics for debug */
6123                 rxsc_sgien_exflg = pofdm_buf->rxsc_sgien_exflg;
6124                 prxsc = (phy_ofdm_rx_status_rxsc_sgien_exintfflag *)&rxsc_sgien_exflg;
6125                 if(pdrvinfo->BW)        //40M channel
6126                         priv->stats.received_bwtype[1+prxsc->rxsc]++;
6127                 else                            //20M channel
6128                         priv->stats.received_bwtype[0]++;
6129         }
6130
6131         //UI BSS List signal strength(in percentage), make it good looking, from 0~100.
6132         //It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp().
6133         if(is_cck_rate)
6134         {
6135                 pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)pwdb_all));//PWDB_ALL;
6136
6137         }
6138         else
6139         {
6140                 //pRfd->Status.SignalStrength = pRecordRfd->Status.SignalStrength = (u1Byte)(SignalScaleMapping(total_rssi/=RF90_PATH_MAX));//(u1Byte)(total_rssi/=RF90_PATH_MAX);
6141                 // We can judge RX path number now.
6142                 if (rf_rx_num != 0)
6143                         pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)(total_rssi/=rf_rx_num)));
6144         }
6145 }       /* QueryRxPhyStatus8190Pci */
6146
6147 static void
6148 rtl8192_record_rxdesc_forlateruse(
6149         struct ieee80211_rx_stats * psrc_stats,
6150         struct ieee80211_rx_stats * ptarget_stats
6151 )
6152 {
6153         ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
6154         ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
6155         //ptarget_stats->Seq_Num = psrc_stats->Seq_Num;
6156 }
6157
6158
6159
6160 static void TranslateRxSignalStuff819xpci(struct net_device *dev,
6161         struct sk_buff *skb,
6162         struct ieee80211_rx_stats * pstats,
6163         prx_desc_819x_pci pdesc,
6164         prx_fwinfo_819x_pci pdrvinfo)
6165 {
6166     // TODO: We must only check packet for current MAC address. Not finish
6167     struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
6168     bool bpacket_match_bssid, bpacket_toself;
6169     bool bPacketBeacon=false, bToSelfBA=false;
6170     static struct ieee80211_rx_stats  previous_stats;
6171     struct ieee80211_hdr_3addr *hdr;
6172     u16 fc,type;
6173
6174     // Get Signal Quality for only RX data queue (but not command queue)
6175
6176     u8* tmp_buf;
6177     u8  *praddr;
6178
6179     /* Get MAC frame start address. */
6180     tmp_buf = skb->data;
6181
6182     hdr = (struct ieee80211_hdr_3addr *)tmp_buf;
6183     fc = le16_to_cpu(hdr->frame_ctl);
6184     type = WLAN_FC_GET_TYPE(fc);
6185     praddr = hdr->addr1;
6186
6187     /* Check if the received packet is acceptabe. */
6188     bpacket_match_bssid = ((IEEE80211_FTYPE_CTL != type) &&
6189             (eqMacAddr(priv->ieee80211->current_network.bssid,  (fc & IEEE80211_FCTL_TODS)? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS )? hdr->addr2 : hdr->addr3))
6190             && (!pstats->bHwError) && (!pstats->bCRC)&& (!pstats->bICV));
6191     bpacket_toself =  bpacket_match_bssid & (eqMacAddr(praddr, priv->ieee80211->dev->dev_addr));
6192 #if 1//cosa
6193     if(WLAN_FC_GET_FRAMETYPE(fc)== IEEE80211_STYPE_BEACON)
6194     {
6195         bPacketBeacon = true;
6196         //DbgPrint("Beacon 2, MatchBSSID = %d, ToSelf = %d \n", bPacketMatchBSSID, bPacketToSelf);
6197     }
6198     if(WLAN_FC_GET_FRAMETYPE(fc) == IEEE80211_STYPE_BLOCKACK)
6199     {
6200         if((eqMacAddr(praddr,dev->dev_addr)))
6201             bToSelfBA = true;
6202         //DbgPrint("BlockAck, MatchBSSID = %d, ToSelf = %d \n", bPacketMatchBSSID, bPacketToSelf);
6203     }
6204
6205 #endif
6206     if(bpacket_match_bssid)
6207     {
6208         priv->stats.numpacket_matchbssid++;
6209     }
6210     if(bpacket_toself){
6211         priv->stats.numpacket_toself++;
6212     }
6213     //
6214     // Process PHY information for previous packet (RSSI/PWDB/EVM)
6215     //
6216     // Because phy information is contained in the last packet of AMPDU only, so driver
6217     // should process phy information of previous packet
6218     rtl8192_process_phyinfo(priv, tmp_buf,&previous_stats, pstats);
6219     rtl8192_query_rxphystatus(priv, pstats, pdesc, pdrvinfo, &previous_stats, bpacket_match_bssid,
6220             bpacket_toself ,bPacketBeacon, bToSelfBA);
6221     rtl8192_record_rxdesc_forlateruse(pstats, &previous_stats);
6222
6223 }
6224
6225
6226 static void rtl8192_tx_resume(struct net_device *dev)
6227 {
6228         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
6229         struct ieee80211_device *ieee = priv->ieee80211;
6230         struct sk_buff *skb;
6231         int queue_index;
6232
6233         for(queue_index = BK_QUEUE; queue_index < TXCMD_QUEUE;queue_index++) {
6234                 while((!skb_queue_empty(&ieee->skb_waitQ[queue_index]))&&
6235                                 (priv->ieee80211->check_nic_enough_desc(dev,queue_index) > 0)) {
6236                         /* 1. dequeue the packet from the wait queue */
6237                         skb = skb_dequeue(&ieee->skb_waitQ[queue_index]);
6238                         /* 2. tx the packet directly */
6239                         ieee->softmac_data_hard_start_xmit(skb,dev,0/* rate useless now*/);
6240                         #if 0
6241                         if(queue_index!=MGNT_QUEUE) {
6242                                 ieee->stats.tx_packets++;
6243                                 ieee->stats.tx_bytes += skb->len;
6244                         }
6245                         #endif
6246                 }
6247         }
6248 }
6249
6250 void rtl8192_irq_tx_tasklet(struct r8192_priv *priv)
6251 {
6252        rtl8192_tx_resume(priv->ieee80211->dev);
6253 }
6254
6255 /**
6256 * Function:     UpdateReceivedRateHistogramStatistics
6257 * Overview:     Recored down the received data rate
6258 *
6259 * Input:
6260 *       PADAPTER        Adapter
6261 *       PRT_RFD         pRfd,
6262 *
6263 * Output:
6264 *       PRT_TCB         Adapter
6265 *                               (Adapter->RxStats.ReceivedRateHistogram[] is updated)
6266 * Return:
6267 *               None
6268 */
6269 static void UpdateReceivedRateHistogramStatistics8190(
6270         struct net_device *dev,
6271         struct ieee80211_rx_stats* pstats
6272         )
6273 {
6274         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
6275         u32 rcvType=1;   //0: Total, 1:OK, 2:CRC, 3:ICV
6276         u32 rateIndex;
6277         u32 preamble_guardinterval;  //1: short preamble/GI, 0: long preamble/GI
6278
6279         /* 2007/03/09 MH We will not update rate of packet from rx cmd queue. */
6280         #if 0
6281         if (pRfd->queue_id == CMPK_RX_QUEUE_ID)
6282                 return;
6283         #endif
6284         if(pstats->bCRC)
6285                 rcvType = 2;
6286         else if(pstats->bICV)
6287                 rcvType = 3;
6288
6289         if(pstats->bShortPreamble)
6290                 preamble_guardinterval = 1;// short
6291         else
6292                 preamble_guardinterval = 0;// long
6293
6294         switch(pstats->rate)
6295         {
6296                 //
6297                 // CCK rate
6298                 //
6299                 case MGN_1M:    rateIndex = 0;  break;
6300                 case MGN_2M:    rateIndex = 1;  break;
6301                 case MGN_5_5M:  rateIndex = 2;  break;
6302                 case MGN_11M:   rateIndex = 3;  break;
6303                 //
6304                 // Legacy OFDM rate
6305                 //
6306                 case MGN_6M:    rateIndex = 4;  break;
6307                 case MGN_9M:    rateIndex = 5;  break;
6308                 case MGN_12M:   rateIndex = 6;  break;
6309                 case MGN_18M:   rateIndex = 7;  break;
6310                 case MGN_24M:   rateIndex = 8;  break;
6311                 case MGN_36M:   rateIndex = 9;  break;
6312                 case MGN_48M:   rateIndex = 10; break;
6313                 case MGN_54M:   rateIndex = 11; break;
6314                 //
6315                 // 11n High throughput rate
6316                 //
6317                 case MGN_MCS0:  rateIndex = 12; break;
6318                 case MGN_MCS1:  rateIndex = 13; break;
6319                 case MGN_MCS2:  rateIndex = 14; break;
6320                 case MGN_MCS3:  rateIndex = 15; break;
6321                 case MGN_MCS4:  rateIndex = 16; break;
6322                 case MGN_MCS5:  rateIndex = 17; break;
6323                 case MGN_MCS6:  rateIndex = 18; break;
6324                 case MGN_MCS7:  rateIndex = 19; break;
6325                 case MGN_MCS8:  rateIndex = 20; break;
6326                 case MGN_MCS9:  rateIndex = 21; break;
6327                 case MGN_MCS10: rateIndex = 22; break;
6328                 case MGN_MCS11: rateIndex = 23; break;
6329                 case MGN_MCS12: rateIndex = 24; break;
6330                 case MGN_MCS13: rateIndex = 25; break;
6331                 case MGN_MCS14: rateIndex = 26; break;
6332                 case MGN_MCS15: rateIndex = 27; break;
6333                 default:        rateIndex = 28; break;
6334         }
6335         priv->stats.received_preamble_GI[preamble_guardinterval][rateIndex]++;
6336         priv->stats.received_rate_histogram[0][rateIndex]++; //total
6337         priv->stats.received_rate_histogram[rcvType][rateIndex]++;
6338 }
6339
6340 static void rtl8192_rx(struct net_device *dev)
6341 {
6342     struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
6343     struct ieee80211_hdr_1addr *ieee80211_hdr = NULL;
6344     bool unicast_packet = false;
6345     struct ieee80211_rx_stats stats = {
6346         .signal = 0,
6347         .noise = -98,
6348         .rate = 0,
6349         .freq = IEEE80211_24GHZ_BAND,
6350     };
6351     unsigned int count = priv->rxringcount;
6352
6353     stats.nic_type = NIC_8192E;
6354
6355     while (count--) {
6356         rx_desc_819x_pci *pdesc = &priv->rx_ring[priv->rx_idx];//rx descriptor
6357         struct sk_buff *skb = priv->rx_buf[priv->rx_idx];//rx pkt
6358
6359         if (pdesc->OWN){
6360             /* wait data to be filled by hardware */
6361             return;
6362         } else {
6363             stats.bICV = pdesc->ICV;
6364             stats.bCRC = pdesc->CRC32;
6365             stats.bHwError = pdesc->CRC32 | pdesc->ICV;
6366
6367             stats.Length = pdesc->Length;
6368             if(stats.Length < 24)
6369                 stats.bHwError |= 1;
6370
6371             if(stats.bHwError) {
6372                 stats.bShift = false;
6373
6374                 if(pdesc->CRC32) {
6375                     if (pdesc->Length <500)
6376                         priv->stats.rxcrcerrmin++;
6377                     else if (pdesc->Length >1000)
6378                         priv->stats.rxcrcerrmax++;
6379                     else
6380                         priv->stats.rxcrcerrmid++;
6381                 }
6382                 goto done;
6383             } else {
6384                 prx_fwinfo_819x_pci pDrvInfo = NULL;
6385                 struct sk_buff *new_skb = dev_alloc_skb(priv->rxbuffersize);
6386
6387                 if (unlikely(!new_skb)) {
6388                     goto done;
6389                 }
6390
6391                 stats.RxDrvInfoSize = pdesc->RxDrvInfoSize;
6392                 stats.RxBufShift = ((pdesc->Shift)&0x03);
6393                 stats.Decrypted = !pdesc->SWDec;
6394
6395 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
6396                 pci_dma_sync_single_for_cpu(priv->pdev,
6397 #else
6398                     pci_unmap_single(priv->pdev,
6399 #endif
6400                      *((dma_addr_t *)skb->cb),
6401                      priv->rxbuffersize,
6402                      PCI_DMA_FROMDEVICE);
6403                 skb_put(skb, pdesc->Length);
6404                 pDrvInfo = (rx_fwinfo_819x_pci *)(skb->data + stats.RxBufShift);
6405                 skb_reserve(skb, stats.RxDrvInfoSize + stats.RxBufShift);
6406
6407                 stats.rate = HwRateToMRate90((bool)pDrvInfo->RxHT, (u8)pDrvInfo->RxRate);
6408                 stats.bShortPreamble = pDrvInfo->SPLCP;
6409
6410                 /* it is debug only. It should be disabled in released driver.
6411                  * 2007.1.11 by Emily
6412                  * */
6413                 UpdateReceivedRateHistogramStatistics8190(dev, &stats);
6414
6415                 stats.bIsAMPDU = (pDrvInfo->PartAggr==1);
6416                 stats.bFirstMPDU = (pDrvInfo->PartAggr==1) && (pDrvInfo->FirstAGGR==1);
6417
6418                 stats.TimeStampLow = pDrvInfo->TSFL;
6419                 stats.TimeStampHigh = read_nic_dword(dev, TSFR+4);
6420
6421                 UpdateRxPktTimeStamp8190(dev, &stats);
6422
6423                 //
6424                 // Get Total offset of MPDU Frame Body
6425                 //
6426                 if((stats.RxBufShift + stats.RxDrvInfoSize) > 0)
6427                     stats.bShift = 1;
6428
6429                 stats.RxIs40MHzPacket = pDrvInfo->BW;
6430
6431                 /* ???? */
6432                 TranslateRxSignalStuff819xpci(dev,skb, &stats, pdesc, pDrvInfo);
6433
6434                 /* Rx A-MPDU */
6435                 if(pDrvInfo->FirstAGGR==1 || pDrvInfo->PartAggr == 1)
6436                     RT_TRACE(COMP_RXDESC, "pDrvInfo->FirstAGGR = %d, pDrvInfo->PartAggr = %d\n",
6437                             pDrvInfo->FirstAGGR, pDrvInfo->PartAggr);
6438                    skb_trim(skb, skb->len - 4/*sCrcLng*/);
6439                 /* rx packets statistics */
6440                 ieee80211_hdr = (struct ieee80211_hdr_1addr *)skb->data;
6441                 unicast_packet = false;
6442
6443                 if(is_broadcast_ether_addr(ieee80211_hdr->addr1)) {
6444                     //TODO
6445                 }else if(is_multicast_ether_addr(ieee80211_hdr->addr1)){
6446                     //TODO
6447                 }else {
6448                     /* unicast packet */
6449                     unicast_packet = true;
6450                 }
6451
6452                 stats.packetlength = stats.Length-4;
6453                 stats.fraglength = stats.packetlength;
6454                 stats.fragoffset = 0;
6455                 stats.ntotalfrag = 1;
6456
6457                 if(!ieee80211_rx(priv->ieee80211, skb, &stats)){
6458                     dev_kfree_skb_any(skb);
6459                 } else {
6460                     priv->stats.rxok++;
6461                     if(unicast_packet) {
6462                         priv->stats.rxbytesunicast += skb->len;
6463                     }
6464                 }
6465
6466                 skb = new_skb;
6467                 priv->rx_buf[priv->rx_idx] = skb;
6468                 *((dma_addr_t *) skb->cb) = pci_map_single(priv->pdev, skb->tail, priv->rxbuffersize, PCI_DMA_FROMDEVICE);
6469 //                *((dma_addr_t *) skb->cb) = pci_map_single(priv->pdev, skb_tail_pointer(skb), priv->rxbuffersize, PCI_DMA_FROMDEVICE);
6470             }
6471
6472         }
6473 done:
6474         pdesc->BufferAddress = cpu_to_le32(*((dma_addr_t *)skb->cb));
6475         pdesc->OWN = 1;
6476         pdesc->Length = priv->rxbuffersize;
6477         if (priv->rx_idx == priv->rxringcount-1)
6478             pdesc->EOR = 1;
6479         priv->rx_idx = (priv->rx_idx + 1) % priv->rxringcount;
6480     }
6481
6482 }
6483
6484 void rtl8192_irq_rx_tasklet(struct r8192_priv *priv)
6485 {
6486        rtl8192_rx(priv->ieee80211->dev);
6487         /* unmask RDU */
6488        write_nic_dword(priv->ieee80211->dev, INTA_MASK,read_nic_dword(priv->ieee80211->dev, INTA_MASK) | IMR_RDU);
6489 }
6490
6491 static const struct net_device_ops rtl8192_netdev_ops = {
6492         .ndo_open =                     rtl8192_open,
6493         .ndo_stop =                     rtl8192_close,
6494 /*      .ndo_get_stats =                rtl8192_stats, */
6495         .ndo_tx_timeout =               tx_timeout,
6496         .ndo_do_ioctl =                 rtl8192_ioctl,
6497         .ndo_set_multicast_list =       r8192_set_multicast,
6498         .ndo_set_mac_address =          r8192_set_mac_adr,
6499         .ndo_start_xmit =               ieee80211_xmit,
6500 };
6501
6502 /****************************************************************************
6503      ---------------------------- PCI_STUFF---------------------------
6504 *****************************************************************************/
6505
6506 static int __devinit rtl8192_pci_probe(struct pci_dev *pdev,
6507                          const struct pci_device_id *id)
6508 {
6509         unsigned long ioaddr = 0;
6510         struct net_device *dev = NULL;
6511         struct r8192_priv *priv= NULL;
6512         u8 unit = 0;
6513
6514 #ifdef CONFIG_RTL8192_IO_MAP
6515         unsigned long pio_start, pio_len, pio_flags;
6516 #else
6517         unsigned long pmem_start, pmem_len, pmem_flags;
6518 #endif //end #ifdef RTL_IO_MAP
6519
6520         RT_TRACE(COMP_INIT,"Configuring chip resources");
6521
6522         if( pci_enable_device (pdev) ){
6523                 RT_TRACE(COMP_ERR,"Failed to enable PCI device");
6524                 return -EIO;
6525         }
6526
6527         pci_set_master(pdev);
6528         //pci_set_wmi(pdev);
6529         pci_set_dma_mask(pdev, 0xffffff00ULL);
6530 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
6531         pci_set_consistent_dma_mask(pdev,0xffffff00ULL);
6532 #endif
6533         dev = alloc_ieee80211(sizeof(struct r8192_priv));
6534         if (!dev)
6535                 return -ENOMEM;
6536
6537 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
6538         SET_MODULE_OWNER(dev);
6539 #endif
6540
6541         pci_set_drvdata(pdev, dev);
6542 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
6543         SET_NETDEV_DEV(dev, &pdev->dev);
6544 #endif
6545         priv = ieee80211_priv(dev);
6546 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
6547         priv->ieee80211 = netdev_priv(dev);
6548 #else
6549         priv->ieee80211 = (struct ieee80211_device *)dev->priv;
6550 #endif
6551         priv->pdev=pdev;
6552 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
6553         if((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK)&&(pdev->subsystem_device == 0x3304)){
6554                 priv->ieee80211->bSupportRemoteWakeUp = 1;
6555         } else
6556 #endif
6557         {
6558                 priv->ieee80211->bSupportRemoteWakeUp = 0;
6559         }
6560
6561 #ifdef CONFIG_RTL8192_IO_MAP
6562
6563         pio_start = (unsigned long)pci_resource_start (pdev, 0);
6564         pio_len = (unsigned long)pci_resource_len (pdev, 0);
6565         pio_flags = (unsigned long)pci_resource_flags (pdev, 0);
6566
6567         if (!(pio_flags & IORESOURCE_IO)) {
6568                 RT_TRACE(COMP_ERR,"region #0 not a PIO resource, aborting");
6569                 goto fail;
6570         }
6571
6572         //DMESG("IO space @ 0x%08lx", pio_start );
6573         if( ! request_region( pio_start, pio_len, RTL819xE_MODULE_NAME ) ){
6574                 RT_TRACE(COMP_ERR,"request_region failed!");
6575                 goto fail;
6576         }
6577
6578         ioaddr = pio_start;
6579         dev->base_addr = ioaddr; // device I/O address
6580
6581 #else
6582
6583         pmem_start = pci_resource_start(pdev, 1);
6584         pmem_len = pci_resource_len(pdev, 1);
6585         pmem_flags = pci_resource_flags (pdev, 1);
6586
6587         if (!(pmem_flags & IORESOURCE_MEM)) {
6588                 RT_TRACE(COMP_ERR,"region #1 not a MMIO resource, aborting");
6589                 goto fail;
6590         }
6591
6592         //DMESG("Memory mapped space @ 0x%08lx ", pmem_start);
6593         if( ! request_mem_region(pmem_start, pmem_len, RTL819xE_MODULE_NAME)) {
6594                 RT_TRACE(COMP_ERR,"request_mem_region failed!");
6595                 goto fail;
6596         }
6597
6598
6599         ioaddr = (unsigned long)ioremap_nocache( pmem_start, pmem_len);
6600         if( ioaddr == (unsigned long)NULL ){
6601                 RT_TRACE(COMP_ERR,"ioremap failed!");
6602                // release_mem_region( pmem_start, pmem_len );
6603                 goto fail1;
6604         }
6605
6606         dev->mem_start = ioaddr; // shared mem start
6607         dev->mem_end = ioaddr + pci_resource_len(pdev, 0); // shared mem end
6608
6609 #endif //end #ifdef RTL_IO_MAP
6610
6611         /* We disable the RETRY_TIMEOUT register (0x41) to keep
6612          * PCI Tx retries from interfering with C3 CPU state */
6613          pci_write_config_byte(pdev, 0x41, 0x00);
6614
6615
6616         pci_read_config_byte(pdev, 0x05, &unit);
6617         pci_write_config_byte(pdev, 0x05, unit & (~0x04));
6618
6619         dev->irq = pdev->irq;
6620         priv->irq = 0;
6621
6622         dev->netdev_ops = &rtl8192_netdev_ops;
6623 #if 0
6624         dev->open = rtl8192_open;
6625         dev->stop = rtl8192_close;
6626         //dev->hard_start_xmit = rtl8192_8023_hard_start_xmit;
6627         dev->tx_timeout = tx_timeout;
6628         //dev->wireless_handlers = &r8192_wx_handlers_def;
6629         dev->do_ioctl = rtl8192_ioctl;
6630         dev->set_multicast_list = r8192_set_multicast;
6631         dev->set_mac_address = r8192_set_mac_adr;
6632 #endif
6633
6634          //DMESG("Oops: i'm coming\n");
6635 #if WIRELESS_EXT >= 12
6636 #if WIRELESS_EXT < 17
6637         dev->get_wireless_stats = r8192_get_wireless_stats;
6638 #endif
6639         dev->wireless_handlers = (struct iw_handler_def *) &r8192_wx_handlers_def;
6640 #endif
6641        //dev->get_wireless_stats = r8192_get_wireless_stats;
6642         dev->type=ARPHRD_ETHER;
6643
6644         dev->watchdog_timeo = HZ*3;     //modified by john, 0805
6645
6646         if (dev_alloc_name(dev, ifname) < 0){
6647                 RT_TRACE(COMP_INIT, "Oops: devname already taken! Trying wlan%%d...\n");
6648                 ifname = "wlan%d";
6649                 dev_alloc_name(dev, ifname);
6650         }
6651
6652         RT_TRACE(COMP_INIT, "Driver probe completed1\n");
6653         if(rtl8192_init(dev)!=0){
6654                 RT_TRACE(COMP_ERR, "Initialization failed");
6655                 goto fail;
6656         }
6657
6658         netif_carrier_off(dev);
6659         netif_stop_queue(dev);
6660
6661         register_netdev(dev);
6662         RT_TRACE(COMP_INIT, "dev name=======> %s\n",dev->name);
6663         rtl8192_proc_init_one(dev);
6664
6665
6666         RT_TRACE(COMP_INIT, "Driver probe completed\n");
6667 //#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
6668 //      return dev;
6669 //#else
6670         return 0;
6671 //#endif
6672
6673 fail1:
6674
6675 #ifdef CONFIG_RTL8180_IO_MAP
6676
6677         if( dev->base_addr != 0 ){
6678
6679                 release_region(dev->base_addr,
6680                pci_resource_len(pdev, 0) );
6681         }
6682 #else
6683         if( dev->mem_start != (unsigned long)NULL ){
6684                 iounmap( (void *)dev->mem_start );
6685                 release_mem_region( pci_resource_start(pdev, 1),
6686                                     pci_resource_len(pdev, 1) );
6687         }
6688 #endif //end #ifdef RTL_IO_MAP
6689
6690 fail:
6691         if(dev){
6692
6693                 if (priv->irq) {
6694                         free_irq(dev->irq, dev);
6695                         dev->irq=0;
6696                 }
6697                 free_ieee80211(dev);
6698         }
6699
6700         pci_disable_device(pdev);
6701
6702         DMESG("wlan driver load failed\n");
6703         pci_set_drvdata(pdev, NULL);
6704         return -ENODEV;
6705
6706 }
6707
6708 /* detach all the work and timer structure declared or inititialized
6709  * in r8192_init function.
6710  * */
6711 void rtl8192_cancel_deferred_work(struct r8192_priv* priv)
6712 {
6713         /* call cancel_work_sync instead of cancel_delayed_work if and only if Linux_version_code
6714          * is  or is newer than 2.6.20 and work structure is defined to be struct work_struct.
6715          * Otherwise call cancel_delayed_work is enough.
6716          * FIXME (2.6.20 shoud 2.6.22, work_struct shoud not cancel)
6717          * */
6718 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
6719         cancel_delayed_work(&priv->watch_dog_wq);
6720         cancel_delayed_work(&priv->update_beacon_wq);
6721         cancel_delayed_work(&priv->ieee80211->hw_wakeup_wq);
6722         cancel_delayed_work(&priv->ieee80211->hw_sleep_wq);
6723 #ifdef RTL8192E
6724         cancel_delayed_work(&priv->gpio_change_rf_wq);
6725 #endif
6726 #endif
6727 #if LINUX_VERSION_CODE >=KERNEL_VERSION(2,6,22)
6728         cancel_work_sync(&priv->reset_wq);
6729         cancel_work_sync(&priv->qos_activate);
6730         //cancel_work_sync(&priv->SetBWModeWorkItem);
6731         //cancel_work_sync(&priv->SwChnlWorkItem);
6732 #else
6733 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
6734         cancel_delayed_work(&priv->reset_wq);
6735         cancel_delayed_work(&priv->qos_activate);
6736         //cancel_delayed_work(&priv->SetBWModeWorkItem);
6737         //cancel_delayed_work(&priv->SwChnlWorkItem);
6738 #endif
6739 #endif
6740
6741 }
6742
6743
6744 static void __devexit rtl8192_pci_disconnect(struct pci_dev *pdev)
6745 {
6746         struct net_device *dev = pci_get_drvdata(pdev);
6747         struct r8192_priv *priv ;
6748
6749         if(dev){
6750
6751                 unregister_netdev(dev);
6752
6753                 priv=ieee80211_priv(dev);
6754
6755                 rtl8192_proc_remove_one(dev);
6756
6757                 rtl8192_down(dev);
6758                 if (priv->pFirmware)
6759                 {
6760                         vfree(priv->pFirmware);
6761                         priv->pFirmware = NULL;
6762                 }
6763         //      priv->rf_close(dev);
6764         //      rtl8192_usb_deleteendpoints(dev);
6765 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)
6766                 destroy_workqueue(priv->priv_wq);
6767 #endif
6768                 /* redundant with rtl8192_down */
6769                // rtl8192_irq_disable(dev);
6770                // rtl8192_reset(dev);
6771                // mdelay(10);
6772                 {
6773                     u32 i;
6774                     /* free tx/rx rings */
6775                     rtl8192_free_rx_ring(dev);
6776                     for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
6777                         rtl8192_free_tx_ring(dev, i);
6778                     }
6779                 }
6780                 if(priv->irq){
6781
6782                         printk("Freeing irq %d\n",dev->irq);
6783                         free_irq(dev->irq, dev);
6784                         priv->irq=0;
6785
6786                 }
6787
6788
6789
6790         //      free_beacon_desc_ring(dev,priv->txbeaconcount);
6791
6792 #ifdef CONFIG_RTL8180_IO_MAP
6793
6794                 if( dev->base_addr != 0 ){
6795
6796                         release_region(dev->base_addr,
6797                                        pci_resource_len(pdev, 0) );
6798                 }
6799 #else
6800                 if( dev->mem_start != (unsigned long)NULL ){
6801                         iounmap( (void *)dev->mem_start );
6802                         release_mem_region( pci_resource_start(pdev, 1),
6803                                             pci_resource_len(pdev, 1) );
6804                 }
6805 #endif /*end #ifdef RTL_IO_MAP*/
6806                 free_ieee80211(dev);
6807
6808         }
6809
6810         pci_disable_device(pdev);
6811         RT_TRACE(COMP_DOWN, "wlan driver removed\n");
6812 }
6813
6814 extern int ieee80211_init(void);
6815 extern void ieee80211_exit(void);
6816
6817 static int __init rtl8192_pci_module_init(void)
6818 {
6819         int retval;
6820
6821         retval = ieee80211_init();
6822         if (retval)
6823                 return retval;
6824
6825         printk(KERN_INFO "\nLinux kernel driver for RTL8192 based WLAN cards\n");
6826         printk(KERN_INFO "Copyright (c) 2007-2008, Realsil Wlan\n");
6827         RT_TRACE(COMP_INIT, "Initializing module");
6828         RT_TRACE(COMP_INIT, "Wireless extensions version %d", WIRELESS_EXT);
6829         rtl8192_proc_module_init();
6830 #if(LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22))
6831       if(0!=pci_module_init(&rtl8192_pci_driver))
6832 #else
6833       if(0!=pci_register_driver(&rtl8192_pci_driver))
6834 #endif
6835         {
6836                 DMESG("No device found");
6837                 /*pci_unregister_driver (&rtl8192_pci_driver);*/
6838                 return -ENODEV;
6839         }
6840         return 0;
6841 }
6842
6843
6844 static void __exit rtl8192_pci_module_exit(void)
6845 {
6846         pci_unregister_driver(&rtl8192_pci_driver);
6847
6848         RT_TRACE(COMP_DOWN, "Exiting");
6849         rtl8192_proc_module_remove();
6850         ieee80211_exit();
6851 }
6852
6853 //warning message WB
6854 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
6855 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
6856 void rtl8192_interrupt(int irq, void *netdev, struct pt_regs *regs)
6857 #else
6858 irqreturn_t rtl8192_interrupt(int irq, void *netdev, struct pt_regs *regs)
6859 #endif
6860 #else
6861 irqreturn_t rtl8192_interrupt(int irq, void *netdev)
6862 #endif
6863 {
6864     struct net_device *dev = (struct net_device *) netdev;
6865     struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
6866     unsigned long flags;
6867     u32 inta;
6868     /* We should return IRQ_NONE, but for now let me keep this */
6869     if(priv->irq_enabled == 0){
6870 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
6871         return;
6872 #else
6873         return IRQ_HANDLED;
6874 #endif
6875     }
6876
6877     spin_lock_irqsave(&priv->irq_th_lock,flags);
6878
6879     //ISR: 4bytes
6880
6881     inta = read_nic_dword(dev, ISR);// & priv->IntrMask;
6882     write_nic_dword(dev,ISR,inta); // reset int situation
6883
6884     priv->stats.shints++;
6885     //DMESG("Enter interrupt, ISR value = 0x%08x", inta);
6886     if(!inta){
6887         spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6888 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
6889         return;
6890 #else
6891         return IRQ_HANDLED;
6892 #endif
6893         /*
6894            most probably we can safely return IRQ_NONE,
6895            but for now is better to avoid problems
6896            */
6897     }
6898
6899     if(inta == 0xffff){
6900         /* HW disappared */
6901         spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6902 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
6903         return;
6904 #else
6905         return IRQ_HANDLED;
6906 #endif
6907     }
6908
6909     priv->stats.ints++;
6910 #ifdef DEBUG_IRQ
6911     DMESG("NIC irq %x",inta);
6912 #endif
6913     //priv->irqpending = inta;
6914
6915
6916     if(!netif_running(dev)) {
6917         spin_unlock_irqrestore(&priv->irq_th_lock,flags);
6918 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
6919         return;
6920 #else
6921         return IRQ_HANDLED;
6922 #endif
6923     }
6924
6925     if(inta & IMR_TIMEOUT0){
6926         //              write_nic_dword(dev, TimerInt, 0);
6927         //DMESG("=================>waking up");
6928         //              rtl8180_hw_wakeup(dev);
6929     }
6930
6931     if(inta & IMR_TBDOK){
6932         RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
6933         rtl8192_tx_isr(dev, BEACON_QUEUE);
6934         priv->stats.txbeaconokint++;
6935     }
6936
6937     if(inta & IMR_TBDER){
6938         RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
6939         rtl8192_tx_isr(dev, BEACON_QUEUE);
6940         priv->stats.txbeaconerr++;
6941     }
6942
6943     if(inta  & IMR_MGNTDOK ) {
6944         RT_TRACE(COMP_INTR, "Manage ok interrupt!\n");
6945         priv->stats.txmanageokint++;
6946         rtl8192_tx_isr(dev,MGNT_QUEUE);
6947
6948     }
6949
6950     if(inta & IMR_COMDOK)
6951     {
6952         priv->stats.txcmdpktokint++;
6953         rtl8192_tx_isr(dev,TXCMD_QUEUE);
6954     }
6955
6956     if(inta & IMR_ROK){
6957 #ifdef DEBUG_RX
6958         DMESG("Frame arrived !");
6959 #endif
6960         priv->stats.rxint++;
6961         tasklet_schedule(&priv->irq_rx_tasklet);
6962     }
6963
6964     if(inta & IMR_BcnInt) {
6965         RT_TRACE(COMP_INTR, "prepare beacon for interrupt!\n");
6966         tasklet_schedule(&priv->irq_prepare_beacon_tasklet);
6967     }
6968
6969     if(inta & IMR_RDU){
6970         RT_TRACE(COMP_INTR, "rx descriptor unavailable!\n");
6971         priv->stats.rxrdu++;
6972         /* reset int situation */
6973         write_nic_dword(dev,INTA_MASK,read_nic_dword(dev, INTA_MASK) & ~IMR_RDU);
6974         tasklet_schedule(&priv->irq_rx_tasklet);
6975     }
6976
6977     if(inta & IMR_RXFOVW){
6978         RT_TRACE(COMP_INTR, "rx overflow !\n");
6979         priv->stats.rxoverflow++;
6980         tasklet_schedule(&priv->irq_rx_tasklet);
6981     }
6982
6983     if(inta & IMR_TXFOVW) priv->stats.txoverflow++;
6984
6985     if(inta & IMR_BKDOK){
6986         RT_TRACE(COMP_INTR, "BK Tx OK interrupt!\n");
6987         priv->stats.txbkokint++;
6988         priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
6989         rtl8192_tx_isr(dev,BK_QUEUE);
6990         rtl8192_try_wake_queue(dev, BK_QUEUE);
6991     }
6992
6993     if(inta & IMR_BEDOK){
6994         RT_TRACE(COMP_INTR, "BE TX OK interrupt!\n");
6995         priv->stats.txbeokint++;
6996         priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
6997         rtl8192_tx_isr(dev,BE_QUEUE);
6998         rtl8192_try_wake_queue(dev, BE_QUEUE);
6999     }
7000
7001     if(inta & IMR_VIDOK){
7002         RT_TRACE(COMP_INTR, "VI TX OK interrupt!\n");
7003         priv->stats.txviokint++;
7004         priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
7005         rtl8192_tx_isr(dev,VI_QUEUE);
7006         rtl8192_try_wake_queue(dev, VI_QUEUE);
7007     }
7008
7009     if(inta & IMR_VODOK){
7010         priv->stats.txvookint++;
7011         priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
7012         rtl8192_tx_isr(dev,VO_QUEUE);
7013         rtl8192_try_wake_queue(dev, VO_QUEUE);
7014     }
7015
7016     force_pci_posting(dev);
7017     spin_unlock_irqrestore(&priv->irq_th_lock,flags);
7018
7019 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
7020     return;
7021 #else
7022     return IRQ_HANDLED;
7023 #endif
7024 }
7025
7026 void rtl8192_try_wake_queue(struct net_device *dev, int pri)
7027 {
7028 #if 0
7029         unsigned long flags;
7030         short enough_desc;
7031         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
7032
7033         spin_lock_irqsave(&priv->tx_lock,flags);
7034         enough_desc = check_nic_enough_desc(dev,pri);
7035         spin_unlock_irqrestore(&priv->tx_lock,flags);
7036
7037         if(enough_desc)
7038                 ieee80211_wake_queue(priv->ieee80211);
7039 #endif
7040 }
7041
7042
7043 void EnableHWSecurityConfig8192(struct net_device *dev)
7044 {
7045         u8 SECR_value = 0x0;
7046         // struct ieee80211_device* ieee1 = container_of(&dev, struct ieee80211_device, dev);
7047          //printk("==>ieee1:%p, dev:%p\n", ieee1, dev);
7048         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
7049          struct ieee80211_device* ieee = priv->ieee80211;
7050          //printk("==>ieee:%p, dev:%p\n", ieee, dev);
7051         SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
7052 #if 1
7053         if (((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type)) && (priv->ieee80211->auth_mode != 2))
7054         {
7055                 SECR_value |= SCR_RxUseDK;
7056                 SECR_value |= SCR_TxUseDK;
7057         }
7058         else if ((ieee->iw_mode == IW_MODE_ADHOC) && (ieee->pairwise_key_type & (KEY_TYPE_CCMP | KEY_TYPE_TKIP)))
7059         {
7060                 SECR_value |= SCR_RxUseDK;
7061                 SECR_value |= SCR_TxUseDK;
7062         }
7063
7064 #endif
7065
7066         //add HWSec active enable here.
7067 //default using hwsec. when peer AP is in N mode only and pairwise_key_type is none_aes(which HT_IOT_ACT_PURE_N_MODE indicates it), use software security. when peer AP is in b,g,n mode mixed and pairwise_key_type is none_aes, use g mode hw security. WB on 2008.7.4
7068         ieee->hwsec_active = 1;
7069
7070         if ((ieee->pHTInfo->IOTAction&HT_IOT_ACT_PURE_N_MODE) || !hwwep)//!ieee->hwsec_support) //add hwsec_support flag to totol control hw_sec on/off
7071         {
7072                 ieee->hwsec_active = 0;
7073                 SECR_value &= ~SCR_RxDecEnable;
7074         }
7075
7076         RT_TRACE(COMP_SEC,"%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n", __FUNCTION__, \
7077                         ieee->hwsec_active, ieee->pairwise_key_type, SECR_value);
7078         {
7079                 write_nic_byte(dev, SECR,  SECR_value);//SECR_value |  SCR_UseDK );
7080         }
7081
7082 }
7083 #define TOTAL_CAM_ENTRY 32
7084 //#define CAM_CONTENT_COUNT 8
7085 void setKey(    struct net_device *dev,
7086                 u8 EntryNo,
7087                 u8 KeyIndex,
7088                 u16 KeyType,
7089                 u8 *MacAddr,
7090                 u8 DefaultKey,
7091                 u32 *KeyContent )
7092 {
7093         u32 TargetCommand = 0;
7094         u32 TargetContent = 0;
7095         u16 usConfig = 0;
7096         u8 i;
7097 #ifdef ENABLE_IPS
7098         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
7099         RT_RF_POWER_STATE       rtState;
7100         rtState = priv->ieee80211->eRFPowerState;
7101         if(priv->ieee80211->PowerSaveControl.bInactivePs){
7102                 if(rtState == eRfOff){
7103                         if(priv->ieee80211->RfOffReason > RF_CHANGE_BY_IPS)
7104                         {
7105                                 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__FUNCTION__);
7106                                 up(&priv->wx_sem);
7107                                 return ;
7108                         }
7109                         else{
7110                                 IPSLeave(dev);
7111                         }
7112                 }
7113         }
7114         priv->ieee80211->is_set_key = true;
7115 #endif
7116         if (EntryNo >= TOTAL_CAM_ENTRY)
7117                 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
7118
7119         RT_TRACE(COMP_SEC, "====>to setKey(), dev:%p, EntryNo:%d, KeyIndex:%d, KeyType:%d, MacAddr"MAC_FMT"\n", dev,EntryNo, KeyIndex, KeyType, MAC_ARG(MacAddr));
7120
7121         if (DefaultKey)
7122                 usConfig |= BIT15 | (KeyType<<2);
7123         else
7124                 usConfig |= BIT15 | (KeyType<<2) | KeyIndex;
7125 //      usConfig |= BIT15 | (KeyType<<2) | (DefaultKey<<5) | KeyIndex;
7126
7127
7128         for(i=0 ; i<CAM_CONTENT_COUNT; i++){
7129                 TargetCommand  = i+CAM_CONTENT_COUNT*EntryNo;
7130                 TargetCommand |= BIT31|BIT16;
7131
7132                 if(i==0){//MAC|Config
7133                         TargetContent = (u32)(*(MacAddr+0)) << 16|
7134                                         (u32)(*(MacAddr+1)) << 24|
7135                                         (u32)usConfig;
7136
7137                         write_nic_dword(dev, WCAMI, TargetContent);
7138                         write_nic_dword(dev, RWCAM, TargetCommand);
7139         //              printk("setkey cam =%8x\n", read_cam(dev, i+6*EntryNo));
7140                 }
7141                 else if(i==1){//MAC
7142                         TargetContent = (u32)(*(MacAddr+2))      |
7143                                         (u32)(*(MacAddr+3)) <<  8|
7144                                         (u32)(*(MacAddr+4)) << 16|
7145                                         (u32)(*(MacAddr+5)) << 24;
7146                         write_nic_dword(dev, WCAMI, TargetContent);
7147                         write_nic_dword(dev, RWCAM, TargetCommand);
7148                 }
7149                 else {  //Key Material
7150                         if(KeyContent != NULL)
7151                         {
7152                         write_nic_dword(dev, WCAMI, (u32)(*(KeyContent+i-2)) );
7153                         write_nic_dword(dev, RWCAM, TargetCommand);
7154                 }
7155         }
7156         }
7157         RT_TRACE(COMP_SEC,"=========>after set key, usconfig:%x\n", usConfig);
7158 //      CAM_read_entry(dev, 0);
7159 }
7160 // This function seems not ready! WB
7161 void CamPrintDbgReg(struct net_device* dev)
7162 {
7163         unsigned long rvalue;
7164         unsigned char ucValue;
7165         write_nic_dword(dev, DCAM, 0x80000000);
7166         msleep(40);
7167         rvalue = read_nic_dword(dev, DCAM);     //delay_ms(40);
7168         RT_TRACE(COMP_SEC, " TX CAM=%8lX ",rvalue);
7169         if((rvalue & 0x40000000) != 0x4000000)
7170                 RT_TRACE(COMP_SEC, "-->TX Key Not Found      ");
7171         msleep(20);
7172         write_nic_dword(dev, DCAM, 0x00000000); //delay_ms(40);
7173         rvalue = read_nic_dword(dev, DCAM);     //delay_ms(40);
7174         RT_TRACE(COMP_SEC, "RX CAM=%8lX ",rvalue);
7175         if((rvalue & 0x40000000) != 0x4000000)
7176                 RT_TRACE(COMP_SEC, "-->CAM Key Not Found   ");
7177         ucValue = read_nic_byte(dev, SECR);
7178         RT_TRACE(COMP_SEC, "WPA_Config=%x \n",ucValue);
7179 }
7180
7181
7182 /***************************************************************************
7183      ------------------- module init / exit stubs ----------------
7184 ****************************************************************************/
7185 module_init(rtl8192_pci_module_init);
7186 module_exit(rtl8192_pci_module_exit);