Merge remote-tracking branches 'asoc/topic/omap', 'asoc/topic/oom' and 'asoc/topic...
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rtl8192u / r8192U_core.c
1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  * Linux device driver for RTL8192U
4  *
5  * Based on the r8187 driver, which is:
6  * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, 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)
29 {
30         return i;
31 }
32
33 unsigned int __fixunsdfsi(double d)
34 {
35         return d;
36 }
37
38 double __adddf3(double a, double b)
39 {
40         return a+b;
41 }
42
43 double __addsf3(float a, float b)
44 {
45         return a+b;
46 }
47
48 double __subdf3(double a, double b)
49 {
50         return a-b;
51 }
52
53 double __extendsfdf2(float a)
54 {
55         return a;
56 }
57 #endif
58
59 #undef LOOP_TEST
60 #undef DUMP_RX
61 #undef DUMP_TX
62 #undef DEBUG_TX_DESC2
63 #undef RX_DONT_PASS_UL
64 #undef DEBUG_EPROM
65 #undef DEBUG_RX_VERBOSE
66 #undef DUMMY_RX
67 #undef DEBUG_ZERO_RX
68 #undef DEBUG_RX_SKB
69 #undef DEBUG_TX_FRAG
70 #undef DEBUG_RX_FRAG
71 #undef DEBUG_TX_FILLDESC
72 #undef DEBUG_TX
73 #undef DEBUG_IRQ
74 #undef DEBUG_RX
75 #undef DEBUG_RXALLOC
76 #undef DEBUG_REGISTERS
77 #undef DEBUG_RING
78 #undef DEBUG_IRQ_TASKLET
79 #undef DEBUG_TX_ALLOC
80 #undef DEBUG_TX_DESC
81
82 #define CONFIG_RTL8192_IO_MAP
83
84 #include <asm/uaccess.h>
85 #include "r8192U_hw.h"
86 #include "r8192U.h"
87 #include "r8190_rtl8256.h" /* RTL8225 Radio frontend */
88 #include "r8180_93cx6.h"   /* Card EEPROM */
89 #include "r8192U_wx.h"
90 #include "r819xU_phy.h" //added by WB 4.30.2008
91 #include "r819xU_phyreg.h"
92 #include "r819xU_cmdpkt.h"
93 #include "r8192U_dm.h"
94 #include <linux/usb.h>
95 #include <linux/slab.h>
96 #include <linux/proc_fs.h>
97 #include <linux/seq_file.h>
98 // FIXME: check if 2.6.7 is ok
99
100 #include "dot11d.h"
101 //set here to open your trace code. //WB
102 u32 rt_global_debug_component = COMP_DOWN       |
103                                 COMP_SEC        |
104                                 COMP_ERR; //always open err flags on
105
106 #define TOTAL_CAM_ENTRY 32
107 #define CAM_CONTENT_COUNT 8
108
109 static const struct usb_device_id rtl8192_usb_id_tbl[] = {
110         /* Realtek */
111         {USB_DEVICE(0x0bda, 0x8709)},
112         /* Corega */
113         {USB_DEVICE(0x07aa, 0x0043)},
114         /* Belkin */
115         {USB_DEVICE(0x050d, 0x805E)},
116         /* Sitecom */
117         {USB_DEVICE(0x0df6, 0x0031)},
118         /* EnGenius */
119         {USB_DEVICE(0x1740, 0x9201)},
120         /* Dlink */
121         {USB_DEVICE(0x2001, 0x3301)},
122         /* Zinwell */
123         {USB_DEVICE(0x5a57, 0x0290)},
124         /* LG */
125         {USB_DEVICE(0x043e, 0x7a01)},
126         {}
127 };
128
129 MODULE_LICENSE("GPL");
130 MODULE_VERSION("V 1.1");
131 MODULE_DEVICE_TABLE(usb, rtl8192_usb_id_tbl);
132 MODULE_DESCRIPTION("Linux driver for Realtek RTL8192 USB WiFi cards");
133
134 static char *ifname = "wlan%d";
135 static int hwwep = 1;  //default use hw. set 0 to use software security
136 static int channels = 0x3fff;
137
138
139
140 module_param(ifname, charp, S_IRUGO|S_IWUSR);
141 module_param(hwwep, int, S_IRUGO|S_IWUSR);
142 module_param(channels, int, S_IRUGO|S_IWUSR);
143
144 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
145 MODULE_PARM_DESC(hwwep, " Try to use hardware security support. ");
146 MODULE_PARM_DESC(channels, " Channel bitmask for specific locales. NYI");
147
148 static int rtl8192_usb_probe(struct usb_interface *intf,
149                              const struct usb_device_id *id);
150 static void rtl8192_usb_disconnect(struct usb_interface *intf);
151
152
153 static struct usb_driver rtl8192_usb_driver = {
154         .name           = RTL819xU_MODULE_NAME,           /* Driver name   */
155         .id_table       = rtl8192_usb_id_tbl,             /* PCI_ID table  */
156         .probe          = rtl8192_usb_probe,              /* probe fn      */
157         .disconnect     = rtl8192_usb_disconnect,         /* remove fn     */
158         .suspend        = NULL,                           /* PM suspend fn */
159         .resume         = NULL,                           /* PM resume fn  */
160 };
161
162
163 typedef struct _CHANNEL_LIST {
164         u8      Channel[32];
165         u8      Len;
166 } CHANNEL_LIST, *PCHANNEL_LIST;
167
168 static CHANNEL_LIST ChannelPlan[] = {
169         {{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
170         {{1,2,3,4,5,6,7,8,9,10,11},11},                                                 //IC
171         {{1,2,3,4,5,6,7,8,9,10,11,12,13,36,40,44,48,52,56,60,64},21},   //ETSI
172         {{1,2,3,4,5,6,7,8,9,10,11,12,13},13},    //Spain. Change to ETSI.
173         {{1,2,3,4,5,6,7,8,9,10,11,12,13},13},   //France. Change to ETSI.
174         {{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
175         {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64},22},//MKK1
176         {{1,2,3,4,5,6,7,8,9,10,11,12,13},13},   //Israel.
177         {{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
178         {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,36,40,44,48,52,56,60,64}, 22},    //MIC
179         {{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
180 };
181
182 static void rtl819x_set_channel_map(u8 channel_plan, struct r8192_priv *priv)
183 {
184         int i, max_chan = -1, min_chan = -1;
185         struct ieee80211_device *ieee = priv->ieee80211;
186         switch (channel_plan) {
187         case COUNTRY_CODE_FCC:
188         case COUNTRY_CODE_IC:
189         case COUNTRY_CODE_ETSI:
190         case COUNTRY_CODE_SPAIN:
191         case COUNTRY_CODE_FRANCE:
192         case COUNTRY_CODE_MKK:
193         case COUNTRY_CODE_MKK1:
194         case COUNTRY_CODE_ISRAEL:
195         case COUNTRY_CODE_TELEC:
196         case COUNTRY_CODE_MIC:
197                 Dot11d_Init(ieee);
198                 ieee->bGlobalDomain = false;
199                 //actually 8225 & 8256 rf chips only support B,G,24N mode
200                 if ((priv->rf_chip == RF_8225) || (priv->rf_chip == RF_8256)) {
201                         min_chan = 1;
202                         max_chan = 14;
203                 } else {
204                         RT_TRACE(COMP_ERR, "unknown rf chip, can't set channel map in function:%s()\n", __func__);
205                 }
206                 if (ChannelPlan[channel_plan].Len != 0) {
207                         // Clear old channel map
208                         memset(GET_DOT11D_INFO(ieee)->channel_map, 0, sizeof(GET_DOT11D_INFO(ieee)->channel_map));
209                         // Set new channel map
210                         for (i = 0; i < ChannelPlan[channel_plan].Len; i++) {
211                                 if (ChannelPlan[channel_plan].Channel[i] < min_chan || ChannelPlan[channel_plan].Channel[i] > max_chan)
212                                         break;
213                                 GET_DOT11D_INFO(ieee)->channel_map[ChannelPlan[channel_plan].Channel[i]] = 1;
214                         }
215                 }
216                 break;
217
218         case COUNTRY_CODE_GLOBAL_DOMAIN:
219                 GET_DOT11D_INFO(ieee)->bEnabled = 0;//this flag enabled to follow 11d country IE setting, otherwise, it shall follow global domain settings.
220                 Dot11d_Reset(ieee);
221                 ieee->bGlobalDomain = true;
222                 break;
223
224         default:
225                 break;
226         }
227 }
228
229
230
231
232 static void CamResetAllEntry(struct net_device *dev)
233 {
234         u32 ulcommand = 0;
235         //2004/02/11  In static WEP, OID_ADD_KEY or OID_ADD_WEP are set before STA associate to AP.
236         // However, ResetKey is called on OID_802_11_INFRASTRUCTURE_MODE and MlmeAssociateRequest
237         // In this condition, Cam can not be reset because upper layer will not set this static key again.
238         ulcommand |= BIT31|BIT30;
239         write_nic_dword(dev, RWCAM, ulcommand);
240
241 }
242
243
244 void write_cam(struct net_device *dev, u8 addr, u32 data)
245 {
246         write_nic_dword(dev, WCAMI, data);
247         write_nic_dword(dev, RWCAM, BIT31|BIT16|(addr&0xff));
248 }
249
250 u32 read_cam(struct net_device *dev, u8 addr)
251 {
252         u32 data;
253
254         write_nic_dword(dev, RWCAM, 0x80000000|(addr&0xff));
255         read_nic_dword(dev, 0xa8, &data);
256         return data;
257 }
258
259 void write_nic_byte_E(struct net_device *dev, int indx, u8 data)
260 {
261         int status;
262         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
263         struct usb_device *udev = priv->udev;
264
265         status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
266                                  RTL8187_REQ_SET_REGS, RTL8187_REQT_WRITE,
267                                  indx|0xfe00, 0, &data, 1, HZ / 2);
268
269         if (status < 0)
270                 netdev_err(dev, "write_nic_byte_E TimeOut! status: %d\n", status);
271 }
272
273 int read_nic_byte_E(struct net_device *dev, int indx, u8 *data)
274 {
275         int status;
276         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
277         struct usb_device *udev = priv->udev;
278
279         status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
280                                  RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
281                                  indx|0xfe00, 0, data, 1, HZ / 2);
282
283         if (status < 0) {
284                 netdev_err(dev, "%s failure status: %d\n", __func__, status);
285                 return status;
286         }
287
288         return 0;
289 }
290 //as 92U has extend page from 4 to 16, so modify functions below.
291 void write_nic_byte(struct net_device *dev, int indx, u8 data)
292 {
293         int status;
294
295         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
296         struct usb_device *udev = priv->udev;
297
298         status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
299                                  RTL8187_REQ_SET_REGS, RTL8187_REQT_WRITE,
300                                  (indx&0xff)|0xff00, (indx>>8)&0x0f, &data, 1, HZ / 2);
301
302         if (status < 0)
303                 netdev_err(dev, "write_nic_byte TimeOut! status: %d\n", status);
304
305
306 }
307
308
309 void write_nic_word(struct net_device *dev, int indx, u16 data)
310 {
311
312         int status;
313
314         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
315         struct usb_device *udev = priv->udev;
316
317         status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
318                                  RTL8187_REQ_SET_REGS, RTL8187_REQT_WRITE,
319                                  (indx&0xff)|0xff00, (indx>>8)&0x0f, &data, 2, HZ / 2);
320
321         if (status < 0)
322                 netdev_err(dev, "write_nic_word TimeOut! status: %d\n", status);
323
324 }
325
326
327 void write_nic_dword(struct net_device *dev, int indx, u32 data)
328 {
329
330         int status;
331
332         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
333         struct usb_device *udev = priv->udev;
334
335         status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
336                                  RTL8187_REQ_SET_REGS, RTL8187_REQT_WRITE,
337                                  (indx&0xff)|0xff00, (indx>>8)&0x0f, &data, 4, HZ / 2);
338
339
340         if (status < 0)
341                 netdev_err(dev, "write_nic_dword TimeOut! status: %d\n", status);
342
343 }
344
345
346
347 int read_nic_byte(struct net_device *dev, int indx, u8 *data)
348 {
349         int status;
350         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
351         struct usb_device *udev = priv->udev;
352
353         status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
354                                  RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
355                                  (indx&0xff)|0xff00, (indx>>8)&0x0f, data, 1, HZ / 2);
356
357         if (status < 0) {
358                 netdev_err(dev, "%s failure status: %d\n", __func__, status);
359                 return status;
360         }
361
362         return 0;
363 }
364
365
366
367 int read_nic_word(struct net_device *dev, int indx, u16 *data)
368 {
369         int status;
370         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
371         struct usb_device *udev = priv->udev;
372
373         status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
374                                  RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
375                                  (indx&0xff)|0xff00, (indx>>8)&0x0f,
376                                  data, 2, HZ / 2);
377
378         if (status < 0) {
379                 netdev_err(dev, "%s failure status: %d\n", __func__, status);
380                 return status;
381         }
382
383         return 0;
384 }
385
386 static int read_nic_word_E(struct net_device *dev, int indx, u16 *data)
387 {
388         int status;
389         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
390         struct usb_device *udev = priv->udev;
391
392         status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
393                                  RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
394                                  indx|0xfe00, 0, data, 2, HZ / 2);
395
396         if (status < 0) {
397                 netdev_err(dev, "%s failure status: %d\n", __func__, status);
398                 return status;
399         }
400
401         return 0;
402 }
403
404 int read_nic_dword(struct net_device *dev, int indx, u32 *data)
405 {
406         int status;
407
408         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
409         struct usb_device *udev = priv->udev;
410
411         status = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
412                                  RTL8187_REQ_GET_REGS, RTL8187_REQT_READ,
413                                  (indx&0xff)|0xff00, (indx>>8)&0x0f,
414                                  data, 4, HZ / 2);
415
416         if (status < 0) {
417                 netdev_err(dev, "%s failure status: %d\n", __func__, status);
418                 return status;
419         }
420
421         return 0;
422 }
423
424 /* u8 read_phy_cck(struct net_device *dev, u8 adr); */
425 /* u8 read_phy_ofdm(struct net_device *dev, u8 adr); */
426 /* this might still called in what was the PHY rtl8185/rtl8192 common code
427  * plans are to possibility turn it again in one common code...
428  */
429 inline void force_pci_posting(struct net_device *dev)
430 {
431 }
432
433 static struct net_device_stats *rtl8192_stats(struct net_device *dev);
434 void rtl8192_commit(struct net_device *dev);
435 void rtl8192_restart(struct work_struct *work);
436 void watch_dog_timer_callback(unsigned long data);
437
438 /****************************************************************************
439  *   -----------------------------PROCFS STUFF-------------------------
440 *****************************************************************************
441  */
442
443 static struct proc_dir_entry *rtl8192_proc;
444
445 static int proc_get_stats_ap(struct seq_file *m, void *v)
446 {
447         struct net_device *dev = m->private;
448         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
449         struct ieee80211_device *ieee = priv->ieee80211;
450         struct ieee80211_network *target;
451
452         list_for_each_entry(target, &ieee->network_list, list) {
453                 const char *wpa = "non_WPA";
454                 if (target->wpa_ie_len > 0 || target->rsn_ie_len > 0)
455                         wpa = "WPA";
456
457                 seq_printf(m, "%s %s\n", target->ssid, wpa);
458         }
459
460         return 0;
461 }
462
463 static int proc_get_registers(struct seq_file *m, void *v)
464 {
465         struct net_device *dev = m->private;
466         int i, n, max = 0xff;
467         u8 byte_rd;
468
469         seq_puts(m, "\n####################page 0##################\n ");
470
471         for (n = 0; n <= max;) {
472                 seq_printf(m, "\nD:  %2x > ", n);
473
474                 for (i = 0; i < 16 && n <= max; i++, n++) {
475                         read_nic_byte(dev, 0x000|n, &byte_rd);
476                         seq_printf(m, "%2x ", byte_rd);
477                 }
478         }
479
480         seq_puts(m, "\n####################page 1##################\n ");
481         for (n = 0; n <= max;) {
482                 seq_printf(m, "\nD:  %2x > ", n);
483
484                 for (i = 0; i < 16 && n <= max; i++, n++) {
485                         read_nic_byte(dev, 0x100|n, &byte_rd);
486                         seq_printf(m, "%2x ", byte_rd);
487                 }
488         }
489
490         seq_puts(m, "\n####################page 3##################\n ");
491         for (n = 0; n <= max;) {
492                 seq_printf(m, "\nD:  %2x > ", n);
493
494                 for (i = 0; i < 16 && n <= max; i++, n++) {
495                         read_nic_byte(dev, 0x300|n, &byte_rd);
496                         seq_printf(m, "%2x ", byte_rd);
497                 }
498         }
499
500         seq_putc(m, '\n');
501         return 0;
502 }
503
504 static int proc_get_stats_tx(struct seq_file *m, void *v)
505 {
506         struct net_device *dev = m->private;
507         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
508
509         seq_printf(m,
510                    "TX VI priority ok int: %lu\n"
511                    "TX VI priority error int: %lu\n"
512                    "TX VO priority ok int: %lu\n"
513                    "TX VO priority error int: %lu\n"
514                    "TX BE priority ok int: %lu\n"
515                    "TX BE priority error int: %lu\n"
516                    "TX BK priority ok int: %lu\n"
517                    "TX BK priority error int: %lu\n"
518                    "TX MANAGE priority ok int: %lu\n"
519                    "TX MANAGE priority error int: %lu\n"
520                    "TX BEACON priority ok int: %lu\n"
521                    "TX BEACON priority error int: %lu\n"
522                    "TX queue resume: %lu\n"
523                    "TX queue stopped?: %d\n"
524                    "TX fifo overflow: %lu\n"
525                    "TX VI queue: %d\n"
526                    "TX VO queue: %d\n"
527                    "TX BE queue: %d\n"
528                    "TX BK queue: %d\n"
529                    "TX VI dropped: %lu\n"
530                    "TX VO dropped: %lu\n"
531                    "TX BE dropped: %lu\n"
532                    "TX BK dropped: %lu\n"
533                    "TX total data packets %lu\n",
534                    priv->stats.txviokint,
535                    priv->stats.txvierr,
536                    priv->stats.txvookint,
537                    priv->stats.txvoerr,
538                    priv->stats.txbeokint,
539                    priv->stats.txbeerr,
540                    priv->stats.txbkokint,
541                    priv->stats.txbkerr,
542                    priv->stats.txmanageokint,
543                    priv->stats.txmanageerr,
544                    priv->stats.txbeaconokint,
545                    priv->stats.txbeaconerr,
546                    priv->stats.txresumed,
547                    netif_queue_stopped(dev),
548                    priv->stats.txoverflow,
549                    atomic_read(&(priv->tx_pending[VI_PRIORITY])),
550                    atomic_read(&(priv->tx_pending[VO_PRIORITY])),
551                    atomic_read(&(priv->tx_pending[BE_PRIORITY])),
552                    atomic_read(&(priv->tx_pending[BK_PRIORITY])),
553                    priv->stats.txvidrop,
554                    priv->stats.txvodrop,
555                    priv->stats.txbedrop,
556                    priv->stats.txbkdrop,
557                    priv->stats.txdatapkt
558                 );
559
560         return 0;
561 }
562
563 static int proc_get_stats_rx(struct seq_file *m, void *v)
564 {
565         struct net_device *dev = m->private;
566         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
567
568         seq_printf(m,
569                    "RX packets: %lu\n"
570                    "RX urb status error: %lu\n"
571                    "RX invalid urb error: %lu\n",
572                    priv->stats.rxoktotal,
573                    priv->stats.rxstaterr,
574                    priv->stats.rxurberr);
575
576         return 0;
577 }
578
579 static void rtl8192_proc_module_init(void)
580 {
581         RT_TRACE(COMP_INIT, "Initializing proc filesystem");
582         rtl8192_proc = proc_mkdir(RTL819xU_MODULE_NAME, init_net.proc_net);
583 }
584
585 /*
586  * seq_file wrappers for procfile show routines.
587  */
588 static int rtl8192_proc_open(struct inode *inode, struct file *file)
589 {
590         struct net_device *dev = proc_get_parent_data(inode);
591         int (*show)(struct seq_file *, void *) = PDE_DATA(inode);
592
593         return single_open(file, show, dev);
594 }
595
596 static const struct file_operations rtl8192_proc_fops = {
597         .open           = rtl8192_proc_open,
598         .read           = seq_read,
599         .llseek         = seq_lseek,
600         .release        = single_release,
601 };
602
603 /*
604  * Table of proc files we need to create.
605  */
606 struct rtl8192_proc_file {
607         char name[12];
608         int (*show)(struct seq_file *, void *);
609 };
610
611 static const struct rtl8192_proc_file rtl8192_proc_files[] = {
612         { "stats-rx",   &proc_get_stats_rx },
613         { "stats-tx",   &proc_get_stats_tx },
614         { "stats-ap",   &proc_get_stats_ap },
615         { "registers",  &proc_get_registers },
616         { "" }
617 };
618
619 static void rtl8192_proc_init_one(struct net_device *dev)
620 {
621         const struct rtl8192_proc_file *f;
622         struct proc_dir_entry *dir;
623
624         if (rtl8192_proc) {
625                 dir = proc_mkdir_data(dev->name, 0, rtl8192_proc, dev);
626                 if (!dir) {
627                         RT_TRACE(COMP_ERR, "Unable to initialize /proc/net/rtl8192/%s\n",
628                                  dev->name);
629                         return;
630                 }
631
632                 for (f = rtl8192_proc_files; f->name[0]; f++) {
633                         if (!proc_create_data(f->name, S_IFREG | S_IRUGO, dir,
634                                               &rtl8192_proc_fops, f->show)) {
635                                 RT_TRACE(COMP_ERR, "Unable to initialize "
636                                          "/proc/net/rtl8192/%s/%s\n",
637                                          dev->name, f->name);
638                                 return;
639                         }
640                 }
641         }
642 }
643
644 static void rtl8192_proc_remove_one(struct net_device *dev)
645 {
646         remove_proc_subtree(dev->name, rtl8192_proc);
647 }
648
649 /****************************************************************************
650    -----------------------------MISC STUFF-------------------------
651 *****************************************************************************/
652
653 short check_nic_enough_desc(struct net_device *dev, int queue_index)
654 {
655         struct r8192_priv *priv = ieee80211_priv(dev);
656         int used = atomic_read(&priv->tx_pending[queue_index]);
657
658         return (used < MAX_TX_URB);
659 }
660
661 static void tx_timeout(struct net_device *dev)
662 {
663         struct r8192_priv *priv = ieee80211_priv(dev);
664
665         schedule_work(&priv->reset_wq);
666 }
667
668
669 /* this is only for debug */
670 void dump_eprom(struct net_device *dev)
671 {
672         int i;
673         for (i = 0; i < 63; i++)
674                 RT_TRACE(COMP_EPROM, "EEPROM addr %x : %x", i, eprom_read(dev, i));
675 }
676
677 void rtl8192_update_msr(struct net_device *dev)
678 {
679         struct r8192_priv *priv = ieee80211_priv(dev);
680         u8 msr;
681
682         read_nic_byte(dev, MSR, &msr);
683         msr &= ~MSR_LINK_MASK;
684
685         /* do not change in link_state != WLAN_LINK_ASSOCIATED.
686          * msr must be updated if the state is ASSOCIATING.
687          * this is intentional and make sense for ad-hoc and
688          * master (see the create BSS/IBSS func)
689          */
690         if (priv->ieee80211->state == IEEE80211_LINKED) {
691
692                 if (priv->ieee80211->iw_mode == IW_MODE_INFRA)
693                         msr |= (MSR_LINK_MANAGED<<MSR_LINK_SHIFT);
694                 else if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
695                         msr |= (MSR_LINK_ADHOC<<MSR_LINK_SHIFT);
696                 else if (priv->ieee80211->iw_mode == IW_MODE_MASTER)
697                         msr |= (MSR_LINK_MASTER<<MSR_LINK_SHIFT);
698
699         } else {
700                 msr |= (MSR_LINK_NONE<<MSR_LINK_SHIFT);
701         }
702
703         write_nic_byte(dev, MSR, msr);
704 }
705
706 void rtl8192_set_chan(struct net_device *dev, short ch)
707 {
708         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
709         RT_TRACE(COMP_CH, "=====>%s()====ch:%d\n", __func__, ch);
710         priv->chan = ch;
711
712         /* this hack should avoid frame TX during channel setting*/
713
714 #ifndef LOOP_TEST
715         //need to implement rf set channel here WB
716
717         if (priv->rf_set_chan)
718                 priv->rf_set_chan(dev, priv->chan);
719         mdelay(10);
720 #endif
721 }
722
723 static void rtl8192_rx_isr(struct urb *urb);
724
725 static u32 get_rxpacket_shiftbytes_819xusb(struct ieee80211_rx_stats *pstats)
726 {
727
728 #ifdef USB_RX_AGGREGATION_SUPPORT
729         if (pstats->bisrxaggrsubframe)
730                 return (sizeof(rx_desc_819x_usb) + pstats->RxDrvInfoSize
731                         + pstats->RxBufShift + 8);
732         else
733 #endif
734                 return (sizeof(rx_desc_819x_usb) + pstats->RxDrvInfoSize
735                         + pstats->RxBufShift);
736
737 }
738 static int rtl8192_rx_initiate(struct net_device *dev)
739 {
740         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
741         struct urb *entry;
742         struct sk_buff *skb;
743         struct rtl8192_rx_info *info;
744
745         /* nomal packet rx procedure */
746         while (skb_queue_len(&priv->rx_queue) < MAX_RX_URB) {
747                 skb = __dev_alloc_skb(RX_URB_SIZE, GFP_KERNEL);
748                 if (!skb)
749                         break;
750                 entry = usb_alloc_urb(0, GFP_KERNEL);
751                 if (!entry) {
752                         kfree_skb(skb);
753                         break;
754                 }
755                 usb_fill_bulk_urb(entry, priv->udev,
756                                   usb_rcvbulkpipe(priv->udev, 3), skb_tail_pointer(skb),
757                                   RX_URB_SIZE, rtl8192_rx_isr, skb);
758                 info = (struct rtl8192_rx_info *) skb->cb;
759                 info->urb = entry;
760                 info->dev = dev;
761                 info->out_pipe = 3; //denote rx normal packet queue
762                 skb_queue_tail(&priv->rx_queue, skb);
763                 usb_submit_urb(entry, GFP_KERNEL);
764         }
765
766         /* command packet rx procedure */
767         while (skb_queue_len(&priv->rx_queue) < MAX_RX_URB + 3) {
768                 skb = __dev_alloc_skb(RX_URB_SIZE, GFP_KERNEL);
769                 if (!skb)
770                         break;
771                 entry = usb_alloc_urb(0, GFP_KERNEL);
772                 if (!entry) {
773                         kfree_skb(skb);
774                         break;
775                 }
776                 usb_fill_bulk_urb(entry, priv->udev,
777                                   usb_rcvbulkpipe(priv->udev, 9), skb_tail_pointer(skb),
778                                   RX_URB_SIZE, rtl8192_rx_isr, skb);
779                 info = (struct rtl8192_rx_info *) skb->cb;
780                 info->urb = entry;
781                 info->dev = dev;
782                 info->out_pipe = 9; //denote rx cmd packet queue
783                 skb_queue_tail(&priv->rx_queue, skb);
784                 usb_submit_urb(entry, GFP_KERNEL);
785         }
786
787         return 0;
788 }
789
790 void rtl8192_set_rxconf(struct net_device *dev)
791 {
792         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
793         u32 rxconf;
794
795         read_nic_dword(dev, RCR, &rxconf);
796         rxconf = rxconf & ~MAC_FILTER_MASK;
797         rxconf = rxconf | RCR_AMF;
798         rxconf = rxconf | RCR_ADF;
799         rxconf = rxconf | RCR_AB;
800         rxconf = rxconf | RCR_AM;
801
802         if (dev->flags & IFF_PROMISC)
803                 DMESG("NIC in promisc mode");
804
805         if (priv->ieee80211->iw_mode == IW_MODE_MONITOR ||
806             dev->flags & IFF_PROMISC) {
807                 rxconf = rxconf | RCR_AAP;
808         } else {
809                 rxconf = rxconf | RCR_APM;
810                 rxconf = rxconf | RCR_CBSSID;
811         }
812
813
814         if (priv->ieee80211->iw_mode == IW_MODE_MONITOR) {
815                 rxconf = rxconf | RCR_AICV;
816                 rxconf = rxconf | RCR_APWRMGT;
817         }
818
819         if (priv->crcmon == 1 && priv->ieee80211->iw_mode == IW_MODE_MONITOR)
820                 rxconf = rxconf | RCR_ACRC32;
821
822
823         rxconf = rxconf & ~RX_FIFO_THRESHOLD_MASK;
824         rxconf = rxconf | (RX_FIFO_THRESHOLD_NONE<<RX_FIFO_THRESHOLD_SHIFT);
825         rxconf = rxconf & ~MAX_RX_DMA_MASK;
826         rxconf = rxconf | ((u32)7<<RCR_MXDMA_OFFSET);
827
828         rxconf = rxconf | RCR_ONLYERLPKT;
829
830         write_nic_dword(dev, RCR, rxconf);
831 }
832 //wait to be removed
833 void rtl8192_rx_enable(struct net_device *dev)
834 {
835         rtl8192_rx_initiate(dev);
836 }
837
838
839 void rtl8192_tx_enable(struct net_device *dev)
840 {
841 }
842
843
844
845 void rtl8192_rtx_disable(struct net_device *dev)
846 {
847         u8 cmd;
848         struct r8192_priv *priv = ieee80211_priv(dev);
849         struct sk_buff *skb;
850         struct rtl8192_rx_info *info;
851
852         read_nic_byte(dev, CMDR, &cmd);
853         write_nic_byte(dev, CMDR, cmd & ~(CR_TE|CR_RE));
854         force_pci_posting(dev);
855         mdelay(10);
856
857         while ((skb = __skb_dequeue(&priv->rx_queue))) {
858                 info = (struct rtl8192_rx_info *) skb->cb;
859                 if (!info->urb)
860                         continue;
861
862                 usb_kill_urb(info->urb);
863                 kfree_skb(skb);
864         }
865
866         if (skb_queue_len(&priv->skb_queue))
867                 netdev_warn(dev, "skb_queue not empty\n");
868
869         skb_queue_purge(&priv->skb_queue);
870         return;
871 }
872
873 inline u16 ieeerate2rtlrate(int rate)
874 {
875         switch (rate) {
876         case 10:
877                 return 0;
878         case 20:
879                 return 1;
880         case 55:
881                 return 2;
882         case 110:
883                 return 3;
884         case 60:
885                 return 4;
886         case 90:
887                 return 5;
888         case 120:
889                 return 6;
890         case 180:
891                 return 7;
892         case 240:
893                 return 8;
894         case 360:
895                 return 9;
896         case 480:
897                 return 10;
898         case 540:
899                 return 11;
900         default:
901                 return 3;
902
903         }
904 }
905 static u16 rtl_rate[] = {10, 20, 55, 110, 60, 90, 120, 180, 240, 360, 480, 540};
906 inline u16 rtl8192_rate2rate(short rate)
907 {
908         if (rate > 11) return 0;
909         return rtl_rate[rate];
910 }
911
912
913 /* The prototype of rx_isr has changed since one version of Linux Kernel */
914 static void rtl8192_rx_isr(struct urb *urb)
915 {
916         struct sk_buff *skb = (struct sk_buff *) urb->context;
917         struct rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
918         struct net_device *dev = info->dev;
919         struct r8192_priv *priv = ieee80211_priv(dev);
920         int out_pipe = info->out_pipe;
921         int err;
922         if (!priv->up)
923                 return;
924         if (unlikely(urb->status)) {
925                 info->urb = NULL;
926                 priv->stats.rxstaterr++;
927                 priv->ieee80211->stats.rx_errors++;
928                 usb_free_urb(urb);
929                 return;
930         }
931         skb_unlink(skb, &priv->rx_queue);
932         skb_put(skb, urb->actual_length);
933
934         skb_queue_tail(&priv->skb_queue, skb);
935         tasklet_schedule(&priv->irq_rx_tasklet);
936
937         skb = dev_alloc_skb(RX_URB_SIZE);
938         if (unlikely(!skb)) {
939                 usb_free_urb(urb);
940                 netdev_err(dev, "%s(): can't alloc skb\n", __func__);
941                 /* TODO check rx queue length and refill *somewhere* */
942                 return;
943         }
944
945         usb_fill_bulk_urb(urb, priv->udev,
946                           usb_rcvbulkpipe(priv->udev, out_pipe), skb_tail_pointer(skb),
947                           RX_URB_SIZE, rtl8192_rx_isr, skb);
948
949         info = (struct rtl8192_rx_info *) skb->cb;
950         info->urb = urb;
951         info->dev = dev;
952         info->out_pipe = out_pipe;
953
954         urb->transfer_buffer = skb_tail_pointer(skb);
955         urb->context = skb;
956         skb_queue_tail(&priv->rx_queue, skb);
957         err = usb_submit_urb(urb, GFP_ATOMIC);
958         if (err && err != EPERM)
959                 netdev_err(dev, "can not submit rxurb, err is %x, URB status is %x\n", err, urb->status);
960 }
961
962 static u32 rtl819xusb_rx_command_packet(struct net_device *dev,
963                                         struct ieee80211_rx_stats *pstats)
964 {
965         u32     status;
966
967         status = cmpk_message_handle_rx(dev, pstats);
968         if (status)
969                 DMESG("rxcommandpackethandle819xusb: It is a command packet\n");
970
971         return status;
972 }
973
974
975 static void rtl8192_data_hard_stop(struct net_device *dev)
976 {
977         //FIXME !!
978 }
979
980
981 static void rtl8192_data_hard_resume(struct net_device *dev)
982 {
983         // FIXME !!
984 }
985
986 /* this function TX data frames when the ieee80211 stack requires this.
987  * It checks also if we need to stop the ieee tx queue, eventually do it
988  */
989 static void rtl8192_hard_data_xmit(struct sk_buff *skb, struct net_device *dev, int rate)
990 {
991         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
992         int ret;
993         unsigned long flags;
994         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
995         u8 queue_index = tcb_desc->queue_index;
996
997         /* shall not be referred by command packet */
998         RTL8192U_ASSERT(queue_index != TXCMD_QUEUE);
999
1000         spin_lock_irqsave(&priv->tx_lock, flags);
1001
1002         memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1003         tcb_desc->bTxEnableFwCalcDur = 1;
1004         skb_push(skb, priv->ieee80211->tx_headroom);
1005         ret = rtl8192_tx(dev, skb);
1006
1007         spin_unlock_irqrestore(&priv->tx_lock, flags);
1008
1009         return;
1010 }
1011
1012 /* This is a rough attempt to TX a frame
1013  * This is called by the ieee 80211 stack to TX management frames.
1014  * If the ring is full packet are dropped (for data frame the queue
1015  * is stopped before this can happen).
1016  */
1017 static int rtl8192_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1018 {
1019         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
1020         int ret;
1021         unsigned long flags;
1022         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1023         u8 queue_index = tcb_desc->queue_index;
1024
1025
1026         spin_lock_irqsave(&priv->tx_lock, flags);
1027
1028         memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1029         if (queue_index == TXCMD_QUEUE) {
1030                 skb_push(skb, USB_HWDESC_HEADER_LEN);
1031                 rtl819xU_tx_cmd(dev, skb);
1032                 ret = 1;
1033                 spin_unlock_irqrestore(&priv->tx_lock, flags);
1034                 return ret;
1035         } else {
1036                 skb_push(skb, priv->ieee80211->tx_headroom);
1037                 ret = rtl8192_tx(dev, skb);
1038         }
1039
1040         spin_unlock_irqrestore(&priv->tx_lock, flags);
1041
1042         return ret;
1043 }
1044
1045
1046 void rtl8192_try_wake_queue(struct net_device *dev, int pri);
1047
1048 #ifdef USB_TX_DRIVER_AGGREGATION_ENABLE
1049 u16 DrvAggr_PaddingAdd(struct net_device *dev, struct sk_buff *skb)
1050 {
1051         u16     PaddingNum =  256 - ((skb->len + TX_PACKET_DRVAGGR_SUBFRAME_SHIFT_BYTES) % 256);
1052         return  PaddingNum & 0xff;
1053 }
1054
1055 u8 MRateToHwRate8190Pci(u8 rate);
1056 u8 QueryIsShort(u8 TxHT, u8 TxRate, cb_desc *tcb_desc);
1057 u8 MapHwQueueToFirmwareQueue(u8 QueueID);
1058 struct sk_buff *DrvAggr_Aggregation(struct net_device *dev, struct ieee80211_drv_agg_txb *pSendList)
1059 {
1060         struct ieee80211_device *ieee = netdev_priv(dev);
1061         struct r8192_priv *priv = ieee80211_priv(dev);
1062         cb_desc         *tcb_desc = NULL;
1063         u8              i;
1064         u32             TotalLength;
1065         struct sk_buff  *skb;
1066         struct sk_buff  *agg_skb;
1067         tx_desc_819x_usb_aggr_subframe *tx_agg_desc = NULL;
1068         tx_fwinfo_819x_usb             *tx_fwinfo = NULL;
1069
1070         //
1071         // Local variable initialization.
1072         //
1073         /* first skb initialization */
1074         skb = pSendList->tx_agg_frames[0];
1075         TotalLength = skb->len;
1076
1077         /* Get the total aggregation length including the padding space and
1078          * sub frame header.
1079          */
1080         for (i = 1; i < pSendList->nr_drv_agg_frames; i++) {
1081                 TotalLength += DrvAggr_PaddingAdd(dev, skb);
1082                 skb = pSendList->tx_agg_frames[i];
1083                 TotalLength += (skb->len + TX_PACKET_DRVAGGR_SUBFRAME_SHIFT_BYTES);
1084         }
1085
1086         /* allocate skb to contain the aggregated packets */
1087         agg_skb = dev_alloc_skb(TotalLength + ieee->tx_headroom);
1088         memset(agg_skb->data, 0, agg_skb->len);
1089         skb_reserve(agg_skb, ieee->tx_headroom);
1090
1091         /* reserve info for first subframe Tx descriptor to be set in the tx function */
1092         skb = pSendList->tx_agg_frames[0];
1093         tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1094         tcb_desc->drv_agg_enable = 1;
1095         tcb_desc->pkt_size = skb->len;
1096         tcb_desc->DrvAggrNum = pSendList->nr_drv_agg_frames;
1097         netdev_dbg(dev, "DrvAggNum = %d\n", tcb_desc->DrvAggrNum);
1098         memcpy(agg_skb->cb, skb->cb, sizeof(skb->cb));
1099         memcpy(skb_put(agg_skb, skb->len), skb->data, skb->len);
1100
1101         for (i = 1; i < pSendList->nr_drv_agg_frames; i++) {
1102                 /* push the next sub frame to be 256 byte aline */
1103                 skb_put(agg_skb, DrvAggr_PaddingAdd(dev, skb));
1104
1105                 /* Subframe drv Tx descriptor and firmware info setting */
1106                 skb = pSendList->tx_agg_frames[i];
1107                 tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1108                 tx_agg_desc = (tx_desc_819x_usb_aggr_subframe *)skb_tail_pointer(agg_skb);
1109                 tx_fwinfo = (tx_fwinfo_819x_usb *)(skb_tail_pointer(agg_skb) + sizeof(tx_desc_819x_usb_aggr_subframe));
1110
1111                 memset(tx_fwinfo, 0, sizeof(tx_fwinfo_819x_usb));
1112                 /* DWORD 0 */
1113                 tx_fwinfo->TxHT = (tcb_desc->data_rate&0x80) ? 1 : 0;
1114                 tx_fwinfo->TxRate = MRateToHwRate8190Pci(tcb_desc->data_rate);
1115                 tx_fwinfo->EnableCPUDur = tcb_desc->bTxEnableFwCalcDur;
1116                 tx_fwinfo->Short = QueryIsShort(tx_fwinfo->TxHT, tx_fwinfo->TxRate, tcb_desc);
1117                 if (tcb_desc->bAMPDUEnable) {//AMPDU enabled
1118                         tx_fwinfo->AllowAggregation = 1;
1119                         /* DWORD 1 */
1120                         tx_fwinfo->RxMF = tcb_desc->ampdu_factor;
1121                         tx_fwinfo->RxAMD = tcb_desc->ampdu_density&0x07;//ampdudensity
1122                 } else {
1123                         tx_fwinfo->AllowAggregation = 0;
1124                         /* DWORD 1 */
1125                         tx_fwinfo->RxMF = 0;
1126                         tx_fwinfo->RxAMD = 0;
1127                 }
1128
1129                 /* Protection mode related */
1130                 tx_fwinfo->RtsEnable = (tcb_desc->bRTSEnable) ? 1 : 0;
1131                 tx_fwinfo->CtsEnable = (tcb_desc->bCTSEnable) ? 1 : 0;
1132                 tx_fwinfo->RtsSTBC = (tcb_desc->bRTSSTBC) ? 1 : 0;
1133                 tx_fwinfo->RtsHT = (tcb_desc->rts_rate&0x80) ? 1 : 0;
1134                 tx_fwinfo->RtsRate =  MRateToHwRate8190Pci((u8)tcb_desc->rts_rate);
1135                 tx_fwinfo->RtsSubcarrier = (tx_fwinfo->RtsHT == 0) ? (tcb_desc->RTSSC) : 0;
1136                 tx_fwinfo->RtsBandwidth = (tx_fwinfo->RtsHT == 1) ? ((tcb_desc->bRTSBW) ? 1 : 0) : 0;
1137                 tx_fwinfo->RtsShort = (tx_fwinfo->RtsHT == 0) ? (tcb_desc->bRTSUseShortPreamble ? 1 : 0) :
1138                                       (tcb_desc->bRTSUseShortGI ? 1 : 0);
1139
1140                 /* Set Bandwidth and sub-channel settings. */
1141                 if (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20_40) {
1142                         if (tcb_desc->bPacketBW) {
1143                                 tx_fwinfo->TxBandwidth = 1;
1144                                 tx_fwinfo->TxSubCarrier = 0;    //By SD3's Jerry suggestion, use duplicated mode
1145                         } else {
1146                                 tx_fwinfo->TxBandwidth = 0;
1147                                 tx_fwinfo->TxSubCarrier = priv->nCur40MhzPrimeSC;
1148                         }
1149                 } else {
1150                         tx_fwinfo->TxBandwidth = 0;
1151                         tx_fwinfo->TxSubCarrier = 0;
1152                 }
1153
1154                 /* Fill Tx descriptor */
1155                 memset(tx_agg_desc, 0, sizeof(tx_desc_819x_usb_aggr_subframe));
1156                 /* DWORD 0 */
1157                 tx_agg_desc->Offset =  sizeof(tx_fwinfo_819x_usb) + 8;
1158                 /* already raw data, need not to subtract header length */
1159                 tx_agg_desc->PktSize = skb->len & 0xffff;
1160
1161                 /*DWORD 1*/
1162                 tx_agg_desc->SecCAMID = 0;
1163                 tx_agg_desc->RATid = tcb_desc->RATRIndex;
1164                 tx_agg_desc->NoEnc = 1;
1165                 tx_agg_desc->SecType = 0x0;
1166
1167                 if (tcb_desc->bHwSec) {
1168                         switch (priv->ieee80211->pairwise_key_type) {
1169                         case KEY_TYPE_WEP40:
1170                         case KEY_TYPE_WEP104:
1171                                 tx_agg_desc->SecType = 0x1;
1172                                 tx_agg_desc->NoEnc = 0;
1173                                 break;
1174                         case KEY_TYPE_TKIP:
1175                                 tx_agg_desc->SecType = 0x2;
1176                                 tx_agg_desc->NoEnc = 0;
1177                                 break;
1178                         case KEY_TYPE_CCMP:
1179                                 tx_agg_desc->SecType = 0x3;
1180                                 tx_agg_desc->NoEnc = 0;
1181                                 break;
1182                         case KEY_TYPE_NA:
1183                                 tx_agg_desc->SecType = 0x0;
1184                                 tx_agg_desc->NoEnc = 1;
1185                                 break;
1186                         }
1187                 }
1188
1189                 tx_agg_desc->QueueSelect = MapHwQueueToFirmwareQueue(tcb_desc->queue_index);
1190                 tx_agg_desc->TxFWInfoSize =  sizeof(tx_fwinfo_819x_usb);
1191
1192                 tx_agg_desc->DISFB = tcb_desc->bTxDisableRateFallBack;
1193                 tx_agg_desc->USERATE = tcb_desc->bTxUseDriverAssingedRate;
1194
1195                 tx_agg_desc->OWN = 1;
1196
1197                 //DWORD 2
1198                 /* According windows driver, it seems that there no need to fill this field */
1199
1200                 /* to fill next packet */
1201                 skb_put(agg_skb, TX_PACKET_DRVAGGR_SUBFRAME_SHIFT_BYTES);
1202                 memcpy(skb_put(agg_skb, skb->len), skb->data, skb->len);
1203         }
1204
1205         for (i = 0; i < pSendList->nr_drv_agg_frames; i++)
1206                 dev_kfree_skb_any(pSendList->tx_agg_frames[i]);
1207
1208         return agg_skb;
1209 }
1210
1211 /* NOTE:
1212         This function return a list of PTCB which is proper to be aggregate with the input TCB.
1213         If no proper TCB is found to do aggregation, SendList will only contain the input TCB.
1214 */
1215 u8 DrvAggr_GetAggregatibleList(struct net_device *dev, struct sk_buff *skb,
1216                                struct ieee80211_drv_agg_txb *pSendList)
1217 {
1218         struct ieee80211_device *ieee = netdev_priv(dev);
1219         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
1220         u16             nMaxAggrNum = pHTInfo->UsbTxAggrNum;
1221         cb_desc         *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1222         u8              QueueID = tcb_desc->queue_index;
1223
1224         do {
1225                 pSendList->tx_agg_frames[pSendList->nr_drv_agg_frames++] = skb;
1226                 if (pSendList->nr_drv_agg_frames >= nMaxAggrNum)
1227                         break;
1228
1229         } while ((skb = skb_dequeue(&ieee->skb_drv_aggQ[QueueID])));
1230
1231         RT_TRACE(COMP_AMSDU, "DrvAggr_GetAggregatibleList, nAggrTcbNum = %d \n", pSendList->nr_drv_agg_frames);
1232         return pSendList->nr_drv_agg_frames;
1233 }
1234 #endif
1235
1236 static void rtl8192_tx_isr(struct urb *tx_urb)
1237 {
1238         struct sk_buff *skb = (struct sk_buff *)tx_urb->context;
1239         struct net_device *dev = NULL;
1240         struct r8192_priv *priv = NULL;
1241         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1242         u8  queue_index = tcb_desc->queue_index;
1243
1244         memcpy(&dev, (struct net_device *)(skb->cb), sizeof(struct net_device *));
1245         priv = ieee80211_priv(dev);
1246
1247         if (tcb_desc->queue_index != TXCMD_QUEUE) {
1248                 if (tx_urb->status == 0) {
1249                         dev->trans_start = jiffies;
1250                         priv->stats.txoktotal++;
1251                         priv->ieee80211->LinkDetectInfo.NumTxOkInPeriod++;
1252                         priv->stats.txbytesunicast += (skb->len - priv->ieee80211->tx_headroom);
1253                 } else {
1254                         priv->ieee80211->stats.tx_errors++;
1255                         /* TODO */
1256                 }
1257         }
1258
1259         /* free skb and tx_urb */
1260         if (skb != NULL) {
1261                 dev_kfree_skb_any(skb);
1262                 usb_free_urb(tx_urb);
1263                 atomic_dec(&priv->tx_pending[queue_index]);
1264         }
1265
1266         //
1267         // Handle HW Beacon:
1268         // We had transfer our beacon frame to host controller at this moment.
1269         //
1270         //
1271         // Caution:
1272         // Handling the wait queue of command packets.
1273         // For Tx command packets, we must not do TCB fragment because it is not handled right now.
1274         // We must cut the packets to match the size of TX_CMD_PKT before we send it.
1275         //
1276
1277         /* Handle MPDU in wait queue. */
1278         if (queue_index != BEACON_QUEUE) {
1279                 /* Don't send data frame during scanning.*/
1280                 if ((skb_queue_len(&priv->ieee80211->skb_waitQ[queue_index]) != 0) &&
1281                     (!(priv->ieee80211->queue_stop))) {
1282                         skb = skb_dequeue(&(priv->ieee80211->skb_waitQ[queue_index]));
1283                         if (skb)
1284                                 priv->ieee80211->softmac_hard_start_xmit(skb, dev);
1285
1286                         return; //modified by david to avoid further processing AMSDU
1287                 }
1288 #ifdef USB_TX_DRIVER_AGGREGATION_ENABLE
1289                 else if ((skb_queue_len(&priv->ieee80211->skb_drv_aggQ[queue_index]) != 0) &&
1290                          (!(priv->ieee80211->queue_stop))) {
1291                         // Tx Driver Aggregation process
1292                         /* The driver will aggregation the packets according to the following stats
1293                          * 1. check whether there's tx irq available, for it's a completion return
1294                          *    function, it should contain enough tx irq;
1295                          * 2. check packet type;
1296                          * 3. initialize sendlist, check whether the to-be send packet no greater than 1
1297                          * 4. aggregates the packets, and fill firmware info and tx desc into it, etc.
1298                          * 5. check whether the packet could be sent, otherwise just insert into wait head
1299                          * */
1300                         skb = skb_dequeue(&priv->ieee80211->skb_drv_aggQ[queue_index]);
1301                         if (!check_nic_enough_desc(dev, queue_index)) {
1302                                 skb_queue_head(&(priv->ieee80211->skb_drv_aggQ[queue_index]), skb);
1303                                 return;
1304                         }
1305
1306                         /*TODO*/
1307                         {
1308                                 struct ieee80211_drv_agg_txb SendList;
1309
1310                                 memset(&SendList, 0, sizeof(struct ieee80211_drv_agg_txb));
1311                                 if (DrvAggr_GetAggregatibleList(dev, skb, &SendList) > 1) {
1312                                         skb = DrvAggr_Aggregation(dev, &SendList);
1313
1314                                 }
1315                         }
1316                         priv->ieee80211->softmac_hard_start_xmit(skb, dev);
1317                 }
1318 #endif
1319         }
1320
1321 }
1322
1323 static void rtl8192_config_rate(struct net_device *dev, u16 *rate_config)
1324 {
1325         struct r8192_priv *priv = ieee80211_priv(dev);
1326         struct ieee80211_network *net;
1327         u8 i = 0, basic_rate = 0;
1328         net = &priv->ieee80211->current_network;
1329
1330         for (i = 0; i < net->rates_len; i++) {
1331                 basic_rate = net->rates[i]&0x7f;
1332                 switch (basic_rate) {
1333                 case MGN_1M:    *rate_config |= RRSR_1M;        break;
1334                 case MGN_2M:    *rate_config |= RRSR_2M;        break;
1335                 case MGN_5_5M:  *rate_config |= RRSR_5_5M;      break;
1336                 case MGN_11M:   *rate_config |= RRSR_11M;       break;
1337                 case MGN_6M:    *rate_config |= RRSR_6M;        break;
1338                 case MGN_9M:    *rate_config |= RRSR_9M;        break;
1339                 case MGN_12M:   *rate_config |= RRSR_12M;       break;
1340                 case MGN_18M:   *rate_config |= RRSR_18M;       break;
1341                 case MGN_24M:   *rate_config |= RRSR_24M;       break;
1342                 case MGN_36M:   *rate_config |= RRSR_36M;       break;
1343                 case MGN_48M:   *rate_config |= RRSR_48M;       break;
1344                 case MGN_54M:   *rate_config |= RRSR_54M;       break;
1345                 }
1346         }
1347         for (i = 0; i < net->rates_ex_len; i++) {
1348                 basic_rate = net->rates_ex[i]&0x7f;
1349                 switch (basic_rate) {
1350                 case MGN_1M:    *rate_config |= RRSR_1M;        break;
1351                 case MGN_2M:    *rate_config |= RRSR_2M;        break;
1352                 case MGN_5_5M:  *rate_config |= RRSR_5_5M;      break;
1353                 case MGN_11M:   *rate_config |= RRSR_11M;       break;
1354                 case MGN_6M:    *rate_config |= RRSR_6M;        break;
1355                 case MGN_9M:    *rate_config |= RRSR_9M;        break;
1356                 case MGN_12M:   *rate_config |= RRSR_12M;       break;
1357                 case MGN_18M:   *rate_config |= RRSR_18M;       break;
1358                 case MGN_24M:   *rate_config |= RRSR_24M;       break;
1359                 case MGN_36M:   *rate_config |= RRSR_36M;       break;
1360                 case MGN_48M:   *rate_config |= RRSR_48M;       break;
1361                 case MGN_54M:   *rate_config |= RRSR_54M;       break;
1362                 }
1363         }
1364 }
1365
1366
1367 #define SHORT_SLOT_TIME 9
1368 #define NON_SHORT_SLOT_TIME 20
1369
1370 static void rtl8192_update_cap(struct net_device *dev, u16 cap)
1371 {
1372         u32 tmp = 0;
1373         struct r8192_priv *priv = ieee80211_priv(dev);
1374         struct ieee80211_network *net = &priv->ieee80211->current_network;
1375         priv->short_preamble = cap & WLAN_CAPABILITY_SHORT_PREAMBLE;
1376         tmp = priv->basic_rate;
1377         if (priv->short_preamble)
1378                 tmp |= BRSR_AckShortPmb;
1379         write_nic_dword(dev, RRSR, tmp);
1380
1381         if (net->mode & (IEEE_G|IEEE_N_24G)) {
1382                 u8 slot_time = 0;
1383                 if ((cap & WLAN_CAPABILITY_SHORT_SLOT) && (!priv->ieee80211->pHTInfo->bCurrentRT2RTLongSlotTime)) //short slot time
1384                         slot_time = SHORT_SLOT_TIME;
1385                 else //long slot time
1386                         slot_time = NON_SHORT_SLOT_TIME;
1387                 priv->slot_time = slot_time;
1388                 write_nic_byte(dev, SLOT_TIME, slot_time);
1389         }
1390
1391 }
1392 static void rtl8192_net_update(struct net_device *dev)
1393 {
1394
1395         struct r8192_priv *priv = ieee80211_priv(dev);
1396         struct ieee80211_network *net;
1397         u16 BcnTimeCfg = 0, BcnCW = 6, BcnIFS = 0xf;
1398         u16 rate_config = 0;
1399         net = &priv->ieee80211->current_network;
1400
1401         rtl8192_config_rate(dev, &rate_config);
1402         priv->basic_rate = rate_config &= 0x15f;
1403
1404         write_nic_dword(dev, BSSIDR, ((u32 *)net->bssid)[0]);
1405         write_nic_word(dev, BSSIDR+4, ((u16 *)net->bssid)[2]);
1406
1407         rtl8192_update_msr(dev);
1408         if (priv->ieee80211->iw_mode == IW_MODE_ADHOC) {
1409                 write_nic_word(dev, ATIMWND, 2);
1410                 write_nic_word(dev, BCN_DMATIME, 1023);
1411                 write_nic_word(dev, BCN_INTERVAL, net->beacon_interval);
1412                 write_nic_word(dev, BCN_DRV_EARLY_INT, 1);
1413                 write_nic_byte(dev, BCN_ERR_THRESH, 100);
1414                 BcnTimeCfg |= (BcnCW<<BCN_TCFG_CW_SHIFT);
1415                 // TODO: BcnIFS may required to be changed on ASIC
1416                 BcnTimeCfg |= BcnIFS<<BCN_TCFG_IFS;
1417
1418                 write_nic_word(dev, BCN_TCFG, BcnTimeCfg);
1419         }
1420
1421
1422
1423 }
1424
1425 //temporary hw beacon is not used any more.
1426 //open it when necessary
1427 void rtl819xusb_beacon_tx(struct net_device *dev, u16  tx_rate)
1428 {
1429
1430 }
1431 inline u8 rtl8192_IsWirelessBMode(u16 rate)
1432 {
1433         if (((rate <= 110) && (rate != 60) && (rate != 90)) || (rate == 220))
1434                 return 1;
1435         else return 0;
1436 }
1437
1438 u16 N_DBPSOfRate(u16 DataRate);
1439
1440 u16 ComputeTxTime(u16 FrameLength, u16 DataRate, u8 bManagementFrame,
1441                   u8 bShortPreamble)
1442 {
1443         u16     FrameTime;
1444         u16     N_DBPS;
1445         u16     Ceiling;
1446
1447         if (rtl8192_IsWirelessBMode(DataRate)) {
1448                 if (bManagementFrame || !bShortPreamble || DataRate == 10) // long preamble
1449                         FrameTime = (u16)(144+48+(FrameLength*8/(DataRate/10)));
1450                 else // Short preamble
1451                         FrameTime = (u16)(72+24+(FrameLength*8/(DataRate/10)));
1452                 if ((FrameLength*8 % (DataRate/10)) != 0) //Get the Ceilling
1453                         FrameTime++;
1454         } else {        //802.11g DSSS-OFDM PLCP length field calculation.
1455                 N_DBPS = N_DBPSOfRate(DataRate);
1456                 Ceiling = (16 + 8*FrameLength + 6) / N_DBPS
1457                         + (((16 + 8*FrameLength + 6) % N_DBPS) ? 1 : 0);
1458                 FrameTime = (u16)(16 + 4 + 4*Ceiling + 6);
1459         }
1460         return FrameTime;
1461 }
1462
1463 u16 N_DBPSOfRate(u16 DataRate)
1464 {
1465         u16 N_DBPS = 24;
1466
1467         switch (DataRate) {
1468         case 60:
1469                 N_DBPS = 24;
1470                 break;
1471
1472         case 90:
1473                 N_DBPS = 36;
1474                 break;
1475
1476         case 120:
1477                 N_DBPS = 48;
1478                 break;
1479
1480         case 180:
1481                 N_DBPS = 72;
1482                 break;
1483
1484         case 240:
1485                 N_DBPS = 96;
1486                 break;
1487
1488         case 360:
1489                 N_DBPS = 144;
1490                 break;
1491
1492         case 480:
1493                 N_DBPS = 192;
1494                 break;
1495
1496         case 540:
1497                 N_DBPS = 216;
1498                 break;
1499
1500         default:
1501                 break;
1502         }
1503
1504         return N_DBPS;
1505 }
1506
1507 unsigned int txqueue2outpipe(struct r8192_priv *priv, unsigned int tx_queue)
1508 {
1509         if (tx_queue >= 9) {
1510                 RT_TRACE(COMP_ERR, "%s():Unknown queue ID!!!\n", __func__);
1511                 return 0x04;
1512         }
1513         return priv->txqueue_to_outpipemap[tx_queue];
1514 }
1515
1516 short rtl819xU_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1517 {
1518         struct r8192_priv *priv = ieee80211_priv(dev);
1519         int                     status;
1520         struct urb              *tx_urb;
1521         unsigned int            idx_pipe;
1522         tx_desc_cmd_819x_usb *pdesc = (tx_desc_cmd_819x_usb *)skb->data;
1523         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1524         u8 queue_index = tcb_desc->queue_index;
1525
1526         atomic_inc(&priv->tx_pending[queue_index]);
1527         tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
1528         if (!tx_urb) {
1529                 dev_kfree_skb(skb);
1530                 return -ENOMEM;
1531         }
1532
1533         memset(pdesc, 0, USB_HWDESC_HEADER_LEN);
1534         /* Tx descriptor ought to be set according to the skb->cb */
1535         pdesc->FirstSeg = 1;//bFirstSeg;
1536         pdesc->LastSeg = 1;//bLastSeg;
1537         pdesc->CmdInit = tcb_desc->bCmdOrInit;
1538         pdesc->TxBufferSize = tcb_desc->txbuf_size;
1539         pdesc->OWN = 1;
1540         pdesc->LINIP = tcb_desc->bLastIniPkt;
1541
1542         //----------------------------------------------------------------------------
1543         // Fill up USB_OUT_CONTEXT.
1544         //----------------------------------------------------------------------------
1545         // Get index to out pipe from specified QueueID.
1546 #ifndef USE_ONE_PIPE
1547         idx_pipe = txqueue2outpipe(priv, queue_index);
1548 #else
1549         idx_pipe = 0x04;
1550 #endif
1551         usb_fill_bulk_urb(tx_urb, priv->udev, usb_sndbulkpipe(priv->udev, idx_pipe),
1552                           skb->data, skb->len, rtl8192_tx_isr, skb);
1553
1554         status = usb_submit_urb(tx_urb, GFP_ATOMIC);
1555
1556         if (!status) {
1557                 return 0;
1558         } else {
1559                 DMESGE("Error TX CMD URB, error %d", status);
1560                 return -1;
1561         }
1562 }
1563
1564 /*
1565  * Mapping Software/Hardware descriptor queue id to "Queue Select Field"
1566  * in TxFwInfo data structure
1567  * 2006.10.30 by Emily
1568  *
1569  * \param QUEUEID       Software Queue
1570 */
1571 static u8 MapHwQueueToFirmwareQueue(u8 QueueID)
1572 {
1573         u8 QueueSelect = 0x0;       //defualt set to
1574
1575         switch (QueueID) {
1576         case BE_QUEUE:
1577                 QueueSelect = QSLT_BE;
1578                 break;
1579
1580         case BK_QUEUE:
1581                 QueueSelect = QSLT_BK;
1582                 break;
1583
1584         case VO_QUEUE:
1585                 QueueSelect = QSLT_VO;
1586                 break;
1587
1588         case VI_QUEUE:
1589                 QueueSelect = QSLT_VI;
1590                 break;
1591         case MGNT_QUEUE:
1592                 QueueSelect = QSLT_MGNT;
1593                 break;
1594
1595         case BEACON_QUEUE:
1596                 QueueSelect = QSLT_BEACON;
1597                 break;
1598
1599                 // TODO: 2006.10.30 mark other queue selection until we verify it is OK
1600                 // TODO: Remove Assertions
1601         case TXCMD_QUEUE:
1602                 QueueSelect = QSLT_CMD;
1603                 break;
1604         case HIGH_QUEUE:
1605                 QueueSelect = QSLT_HIGH;
1606                 break;
1607
1608         default:
1609                 RT_TRACE(COMP_ERR, "TransmitTCB(): Impossible Queue Selection: %d \n", QueueID);
1610                 break;
1611         }
1612         return QueueSelect;
1613 }
1614
1615 static u8 MRateToHwRate8190Pci(u8 rate)
1616 {
1617         u8  ret = DESC90_RATE1M;
1618
1619         switch (rate) {
1620         case MGN_1M:    ret = DESC90_RATE1M;    break;
1621         case MGN_2M:    ret = DESC90_RATE2M;    break;
1622         case MGN_5_5M:  ret = DESC90_RATE5_5M;  break;
1623         case MGN_11M:   ret = DESC90_RATE11M;   break;
1624         case MGN_6M:    ret = DESC90_RATE6M;    break;
1625         case MGN_9M:    ret = DESC90_RATE9M;    break;
1626         case MGN_12M:   ret = DESC90_RATE12M;   break;
1627         case MGN_18M:   ret = DESC90_RATE18M;   break;
1628         case MGN_24M:   ret = DESC90_RATE24M;   break;
1629         case MGN_36M:   ret = DESC90_RATE36M;   break;
1630         case MGN_48M:   ret = DESC90_RATE48M;   break;
1631         case MGN_54M:   ret = DESC90_RATE54M;   break;
1632
1633         // HT rate since here
1634         case MGN_MCS0:  ret = DESC90_RATEMCS0;  break;
1635         case MGN_MCS1:  ret = DESC90_RATEMCS1;  break;
1636         case MGN_MCS2:  ret = DESC90_RATEMCS2;  break;
1637         case MGN_MCS3:  ret = DESC90_RATEMCS3;  break;
1638         case MGN_MCS4:  ret = DESC90_RATEMCS4;  break;
1639         case MGN_MCS5:  ret = DESC90_RATEMCS5;  break;
1640         case MGN_MCS6:  ret = DESC90_RATEMCS6;  break;
1641         case MGN_MCS7:  ret = DESC90_RATEMCS7;  break;
1642         case MGN_MCS8:  ret = DESC90_RATEMCS8;  break;
1643         case MGN_MCS9:  ret = DESC90_RATEMCS9;  break;
1644         case MGN_MCS10: ret = DESC90_RATEMCS10; break;
1645         case MGN_MCS11: ret = DESC90_RATEMCS11; break;
1646         case MGN_MCS12: ret = DESC90_RATEMCS12; break;
1647         case MGN_MCS13: ret = DESC90_RATEMCS13; break;
1648         case MGN_MCS14: ret = DESC90_RATEMCS14; break;
1649         case MGN_MCS15: ret = DESC90_RATEMCS15; break;
1650         case (0x80|0x20): ret = DESC90_RATEMCS32; break;
1651
1652         default:       break;
1653         }
1654         return ret;
1655 }
1656
1657
1658 static u8 QueryIsShort(u8 TxHT, u8 TxRate, cb_desc *tcb_desc)
1659 {
1660         u8   tmp_Short;
1661
1662         tmp_Short = (TxHT == 1) ? ((tcb_desc->bUseShortGI) ? 1 : 0) : ((tcb_desc->bUseShortPreamble) ? 1 : 0);
1663
1664         if (TxHT == 1 && TxRate != DESC90_RATEMCS15)
1665                 tmp_Short = 0;
1666
1667         return tmp_Short;
1668 }
1669
1670 static void tx_zero_isr(struct urb *tx_urb)
1671 {
1672         return;
1673 }
1674
1675 /*
1676  * The tx procedure is just as following,
1677  * skb->cb will contain all the following information,
1678  * priority, morefrag, rate, &dev.
1679  * */
1680 short rtl8192_tx(struct net_device *dev, struct sk_buff *skb)
1681 {
1682         struct r8192_priv *priv = ieee80211_priv(dev);
1683         cb_desc *tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1684         tx_desc_819x_usb *tx_desc = (tx_desc_819x_usb *)skb->data;
1685         tx_fwinfo_819x_usb *tx_fwinfo = (tx_fwinfo_819x_usb *)(skb->data + USB_HWDESC_HEADER_LEN);
1686         struct usb_device *udev = priv->udev;
1687         int pend;
1688         int status;
1689         struct urb *tx_urb = NULL, *tx_urb_zero = NULL;
1690         unsigned int idx_pipe;
1691         pend = atomic_read(&priv->tx_pending[tcb_desc->queue_index]);
1692         /* we are locked here so the two atomic_read and inc are executed
1693          * without interleaves
1694          * !!! For debug purpose
1695          */
1696         if (pend > MAX_TX_URB) {
1697                 netdev_dbg(dev, "To discard skb packet!\n");
1698                 dev_kfree_skb_any(skb);
1699                 return -1;
1700         }
1701
1702         tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
1703         if (!tx_urb) {
1704                 dev_kfree_skb_any(skb);
1705                 return -ENOMEM;
1706         }
1707
1708         /* Fill Tx firmware info */
1709         memset(tx_fwinfo, 0, sizeof(tx_fwinfo_819x_usb));
1710         /* DWORD 0 */
1711         tx_fwinfo->TxHT = (tcb_desc->data_rate&0x80) ? 1 : 0;
1712         tx_fwinfo->TxRate = MRateToHwRate8190Pci(tcb_desc->data_rate);
1713         tx_fwinfo->EnableCPUDur = tcb_desc->bTxEnableFwCalcDur;
1714         tx_fwinfo->Short = QueryIsShort(tx_fwinfo->TxHT, tx_fwinfo->TxRate, tcb_desc);
1715         if (tcb_desc->bAMPDUEnable) {//AMPDU enabled
1716                 tx_fwinfo->AllowAggregation = 1;
1717                 /* DWORD 1 */
1718                 tx_fwinfo->RxMF = tcb_desc->ampdu_factor;
1719                 tx_fwinfo->RxAMD = tcb_desc->ampdu_density&0x07;//ampdudensity
1720         } else {
1721                 tx_fwinfo->AllowAggregation = 0;
1722                 /* DWORD 1 */
1723                 tx_fwinfo->RxMF = 0;
1724                 tx_fwinfo->RxAMD = 0;
1725         }
1726
1727         /* Protection mode related */
1728         tx_fwinfo->RtsEnable = (tcb_desc->bRTSEnable) ? 1 : 0;
1729         tx_fwinfo->CtsEnable = (tcb_desc->bCTSEnable) ? 1 : 0;
1730         tx_fwinfo->RtsSTBC = (tcb_desc->bRTSSTBC) ? 1 : 0;
1731         tx_fwinfo->RtsHT = (tcb_desc->rts_rate&0x80) ? 1 : 0;
1732         tx_fwinfo->RtsRate =  MRateToHwRate8190Pci((u8)tcb_desc->rts_rate);
1733         tx_fwinfo->RtsSubcarrier = (tx_fwinfo->RtsHT == 0) ? (tcb_desc->RTSSC) : 0;
1734         tx_fwinfo->RtsBandwidth = (tx_fwinfo->RtsHT == 1) ? ((tcb_desc->bRTSBW) ? 1 : 0) : 0;
1735         tx_fwinfo->RtsShort = (tx_fwinfo->RtsHT == 0) ? (tcb_desc->bRTSUseShortPreamble ? 1 : 0) :
1736                               (tcb_desc->bRTSUseShortGI ? 1 : 0);
1737
1738         /* Set Bandwidth and sub-channel settings. */
1739         if (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20_40) {
1740                 if (tcb_desc->bPacketBW) {
1741                         tx_fwinfo->TxBandwidth = 1;
1742                         tx_fwinfo->TxSubCarrier = 0;    //By SD3's Jerry suggestion, use duplicated mode
1743                 } else {
1744                         tx_fwinfo->TxBandwidth = 0;
1745                         tx_fwinfo->TxSubCarrier = priv->nCur40MhzPrimeSC;
1746                 }
1747         } else {
1748                 tx_fwinfo->TxBandwidth = 0;
1749                 tx_fwinfo->TxSubCarrier = 0;
1750         }
1751
1752 #ifdef USB_TX_DRIVER_AGGREGATION_ENABLE
1753         if (tcb_desc->drv_agg_enable)
1754                 tx_fwinfo->Tx_INFO_RSVD = (tcb_desc->DrvAggrNum & 0x1f) << 1;
1755 #endif
1756         /* Fill Tx descriptor */
1757         memset(tx_desc, 0, sizeof(tx_desc_819x_usb));
1758         /* DWORD 0 */
1759         tx_desc->LINIP = 0;
1760         tx_desc->CmdInit = 1;
1761         tx_desc->Offset =  sizeof(tx_fwinfo_819x_usb) + 8;
1762
1763 #ifdef USB_TX_DRIVER_AGGREGATION_ENABLE
1764         if (tcb_desc->drv_agg_enable)
1765                 tx_desc->PktSize = tcb_desc->pkt_size;
1766         else
1767 #endif
1768         {
1769                 tx_desc->PktSize = (skb->len - TX_PACKET_SHIFT_BYTES) & 0xffff;
1770         }
1771
1772         /*DWORD 1*/
1773         tx_desc->SecCAMID = 0;
1774         tx_desc->RATid = tcb_desc->RATRIndex;
1775         tx_desc->NoEnc = 1;
1776         tx_desc->SecType = 0x0;
1777         if (tcb_desc->bHwSec) {
1778                 switch (priv->ieee80211->pairwise_key_type) {
1779                 case KEY_TYPE_WEP40:
1780                 case KEY_TYPE_WEP104:
1781                         tx_desc->SecType = 0x1;
1782                         tx_desc->NoEnc = 0;
1783                         break;
1784                 case KEY_TYPE_TKIP:
1785                         tx_desc->SecType = 0x2;
1786                         tx_desc->NoEnc = 0;
1787                         break;
1788                 case KEY_TYPE_CCMP:
1789                         tx_desc->SecType = 0x3;
1790                         tx_desc->NoEnc = 0;
1791                         break;
1792                 case KEY_TYPE_NA:
1793                         tx_desc->SecType = 0x0;
1794                         tx_desc->NoEnc = 1;
1795                         break;
1796                 }
1797         }
1798
1799         tx_desc->QueueSelect = MapHwQueueToFirmwareQueue(tcb_desc->queue_index);
1800         tx_desc->TxFWInfoSize =  sizeof(tx_fwinfo_819x_usb);
1801
1802         tx_desc->DISFB = tcb_desc->bTxDisableRateFallBack;
1803         tx_desc->USERATE = tcb_desc->bTxUseDriverAssingedRate;
1804
1805         /* Fill fields that are required to be initialized in all of the descriptors */
1806         //DWORD 0
1807         tx_desc->FirstSeg = 1;
1808         tx_desc->LastSeg = 1;
1809         tx_desc->OWN = 1;
1810
1811 #ifdef USB_TX_DRIVER_AGGREGATION_ENABLE
1812         if (tcb_desc->drv_agg_enable) {
1813                 tx_desc->TxBufferSize = tcb_desc->pkt_size + sizeof(tx_fwinfo_819x_usb);
1814         } else
1815 #endif
1816         {
1817                 //DWORD 2
1818                 tx_desc->TxBufferSize = (u32)(skb->len - USB_HWDESC_HEADER_LEN);
1819         }
1820         /* Get index to out pipe from specified QueueID */
1821 #ifndef USE_ONE_PIPE
1822         idx_pipe = txqueue2outpipe(priv, tcb_desc->queue_index);
1823 #else
1824         idx_pipe = 0x5;
1825 #endif
1826
1827         /* To submit bulk urb */
1828         usb_fill_bulk_urb(tx_urb, udev,
1829                           usb_sndbulkpipe(udev, idx_pipe), skb->data,
1830                           skb->len, rtl8192_tx_isr, skb);
1831
1832         status = usb_submit_urb(tx_urb, GFP_ATOMIC);
1833         if (!status) {
1834                 //we need to send 0 byte packet whenever 512N bytes/64N(HIGN SPEED/NORMAL SPEED) bytes packet has been transmitted. Otherwise, it will be halt to wait for another packet. WB. 2008.08.27
1835                 bool bSend0Byte = false;
1836                 u8 zero = 0;
1837                 if (udev->speed == USB_SPEED_HIGH) {
1838                         if (skb->len > 0 && skb->len % 512 == 0)
1839                                 bSend0Byte = true;
1840                 } else {
1841                         if (skb->len > 0 && skb->len % 64 == 0)
1842                                 bSend0Byte = true;
1843                 }
1844                 if (bSend0Byte) {
1845                         tx_urb_zero = usb_alloc_urb(0, GFP_ATOMIC);
1846                         if (!tx_urb_zero) {
1847                                 RT_TRACE(COMP_ERR, "can't alloc urb for zero byte\n");
1848                                 return -ENOMEM;
1849                         }
1850                         usb_fill_bulk_urb(tx_urb_zero, udev,
1851                                           usb_sndbulkpipe(udev, idx_pipe), &zero,
1852                                           0, tx_zero_isr, dev);
1853                         status = usb_submit_urb(tx_urb_zero, GFP_ATOMIC);
1854                         if (status) {
1855                                 RT_TRACE(COMP_ERR, "Error TX URB for zero byte %d, error %d", atomic_read(&priv->tx_pending[tcb_desc->queue_index]), status);
1856                                 return -1;
1857                         }
1858                 }
1859                 dev->trans_start = jiffies;
1860                 atomic_inc(&priv->tx_pending[tcb_desc->queue_index]);
1861                 return 0;
1862         } else {
1863                 RT_TRACE(COMP_ERR, "Error TX URB %d, error %d", atomic_read(&priv->tx_pending[tcb_desc->queue_index]),
1864                          status);
1865                 return -1;
1866         }
1867 }
1868
1869 static short rtl8192_usb_initendpoints(struct net_device *dev)
1870 {
1871         struct r8192_priv *priv = ieee80211_priv(dev);
1872
1873         priv->rx_urb = kmalloc(sizeof(struct urb *) * (MAX_RX_URB+1),
1874                                GFP_KERNEL);
1875         if (priv->rx_urb == NULL)
1876                 return -ENOMEM;
1877
1878 #ifndef JACKSON_NEW_RX
1879         for (i = 0; i < (MAX_RX_URB+1); i++) {
1880
1881                 priv->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
1882
1883                 priv->rx_urb[i]->transfer_buffer = kmalloc(RX_URB_SIZE, GFP_KERNEL);
1884
1885                 priv->rx_urb[i]->transfer_buffer_length = RX_URB_SIZE;
1886         }
1887 #endif
1888
1889 #ifdef THOMAS_BEACON
1890         {
1891                 long align = 0;
1892                 void *oldaddr, *newaddr;
1893
1894                 priv->rx_urb[16] = usb_alloc_urb(0, GFP_KERNEL);
1895                 priv->oldaddr = kmalloc(16, GFP_KERNEL);
1896                 oldaddr = priv->oldaddr;
1897                 align = ((long)oldaddr) & 3;
1898                 if (align) {
1899                         newaddr = oldaddr + 4 - align;
1900                         priv->rx_urb[16]->transfer_buffer_length = 16 - 4 + align;
1901                 } else {
1902                         newaddr = oldaddr;
1903                         priv->rx_urb[16]->transfer_buffer_length = 16;
1904                 }
1905                 priv->rx_urb[16]->transfer_buffer = newaddr;
1906         }
1907 #endif
1908
1909         memset(priv->rx_urb, 0, sizeof(struct urb *) * MAX_RX_URB);
1910         priv->pp_rxskb = kcalloc(MAX_RX_URB, sizeof(struct sk_buff *),
1911                                  GFP_KERNEL);
1912         if (!priv->pp_rxskb) {
1913                 kfree(priv->rx_urb);
1914
1915                 priv->pp_rxskb = NULL;
1916                 priv->rx_urb = NULL;
1917
1918                 DMESGE("Endpoint Alloc Failure");
1919                 return -ENOMEM;
1920         }
1921
1922         netdev_dbg(dev, "End of initendpoints\n");
1923         return 0;
1924
1925 }
1926 #ifdef THOMAS_BEACON
1927 static void rtl8192_usb_deleteendpoints(struct net_device *dev)
1928 {
1929         int i;
1930         struct r8192_priv *priv = ieee80211_priv(dev);
1931
1932         if (priv->rx_urb) {
1933                 for (i = 0; i < (MAX_RX_URB+1); i++) {
1934                         usb_kill_urb(priv->rx_urb[i]);
1935                         usb_free_urb(priv->rx_urb[i]);
1936                 }
1937                 kfree(priv->rx_urb);
1938                 priv->rx_urb = NULL;
1939         }
1940         kfree(priv->oldaddr);
1941         priv->oldaddr = NULL;
1942         if (priv->pp_rxskb) {
1943                 kfree(priv->pp_rxskb);
1944                 priv->pp_rxskb = NULL;
1945         }
1946 }
1947 #else
1948 void rtl8192_usb_deleteendpoints(struct net_device *dev)
1949 {
1950         int i;
1951         struct r8192_priv *priv = ieee80211_priv(dev);
1952
1953 #ifndef JACKSON_NEW_RX
1954
1955         if (priv->rx_urb) {
1956                 for (i = 0; i < (MAX_RX_URB+1); i++) {
1957                         usb_kill_urb(priv->rx_urb[i]);
1958                         kfree(priv->rx_urb[i]->transfer_buffer);
1959                         usb_free_urb(priv->rx_urb[i]);
1960                 }
1961                 kfree(priv->rx_urb);
1962                 priv->rx_urb = NULL;
1963
1964         }
1965 #else
1966         kfree(priv->rx_urb);
1967         priv->rx_urb = NULL;
1968         kfree(priv->oldaddr);
1969         priv->oldaddr = NULL;
1970         if (priv->pp_rxskb) {
1971                 kfree(priv->pp_rxskb);
1972                 priv->pp_rxskb = 0;
1973
1974         }
1975
1976 #endif
1977 }
1978 #endif
1979
1980 extern void rtl8192_update_ratr_table(struct net_device *dev);
1981 static void rtl8192_link_change(struct net_device *dev)
1982 {
1983         struct r8192_priv *priv = ieee80211_priv(dev);
1984         struct ieee80211_device *ieee = priv->ieee80211;
1985         if (ieee->state == IEEE80211_LINKED) {
1986                 rtl8192_net_update(dev);
1987                 rtl8192_update_ratr_table(dev);
1988                 //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
1989                 if ((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type))
1990                         EnableHWSecurityConfig8192(dev);
1991         }
1992         /*update timing params*/
1993         if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1994                 u32 reg = 0;
1995                 read_nic_dword(dev, RCR, &reg);
1996                 if (priv->ieee80211->state == IEEE80211_LINKED)
1997                         priv->ReceiveConfig = reg |= RCR_CBSSID;
1998                 else
1999                         priv->ReceiveConfig = reg &= ~RCR_CBSSID;
2000                 write_nic_dword(dev, RCR, reg);
2001         }
2002 }
2003
2004 static struct ieee80211_qos_parameters def_qos_parameters = {
2005         {3, 3, 3, 3},/* cw_min */
2006         {7, 7, 7, 7},/* cw_max */
2007         {2, 2, 2, 2},/* aifs */
2008         {0, 0, 0, 0},/* flags */
2009         {0, 0, 0, 0} /* tx_op_limit */
2010 };
2011
2012
2013 static void rtl8192_update_beacon(struct work_struct *work)
2014 {
2015         struct r8192_priv *priv = container_of(work, struct r8192_priv, update_beacon_wq.work);
2016         struct net_device *dev = priv->ieee80211->dev;
2017         struct ieee80211_device *ieee = priv->ieee80211;
2018         struct ieee80211_network *net = &ieee->current_network;
2019
2020         if (ieee->pHTInfo->bCurrentHTSupport)
2021                 HTUpdateSelfAndPeerSetting(ieee, net);
2022         ieee->pHTInfo->bCurrentRT2RTLongSlotTime = net->bssht.bdRT2RTLongSlotTime;
2023         rtl8192_update_cap(dev, net->capability);
2024 }
2025 /*
2026 * background support to run QoS activate functionality
2027 */
2028 static int WDCAPARA_ADD[] = {EDCAPARA_BE, EDCAPARA_BK, EDCAPARA_VI, EDCAPARA_VO};
2029 static void rtl8192_qos_activate(struct work_struct *work)
2030 {
2031         struct r8192_priv *priv = container_of(work, struct r8192_priv, qos_activate);
2032         struct net_device *dev = priv->ieee80211->dev;
2033         struct ieee80211_qos_parameters *qos_parameters = &priv->ieee80211->current_network.qos_data.parameters;
2034         u8 mode = priv->ieee80211->current_network.mode;
2035         u8  u1bAIFS;
2036         u32 u4bAcParam;
2037         int i;
2038
2039         mutex_lock(&priv->mutex);
2040         if (priv->ieee80211->state != IEEE80211_LINKED)
2041                 goto success;
2042         RT_TRACE(COMP_QOS, "qos active process with associate response received\n");
2043         /* It better set slot time at first */
2044         /* For we just support b/g mode at present, let the slot time at 9/20 selection */
2045         /* update the ac parameter to related registers */
2046         for (i = 0; i <  QOS_QUEUE_NUM; i++) {
2047                 //Mode G/A: slotTimeTimer = 9; Mode B: 20
2048                 u1bAIFS = qos_parameters->aifs[i] * ((mode&(IEEE_G|IEEE_N_24G)) ? 9 : 20) + aSifsTime;
2049                 u4bAcParam = ((((u32)(qos_parameters->tx_op_limit[i]))<< AC_PARAM_TXOP_LIMIT_OFFSET)|
2050                               (((u32)(qos_parameters->cw_max[i]))<< AC_PARAM_ECW_MAX_OFFSET)|
2051                               (((u32)(qos_parameters->cw_min[i]))<< AC_PARAM_ECW_MIN_OFFSET)|
2052                               ((u32)u1bAIFS << AC_PARAM_AIFS_OFFSET));
2053
2054                 write_nic_dword(dev, WDCAPARA_ADD[i], u4bAcParam);
2055         }
2056
2057 success:
2058         mutex_unlock(&priv->mutex);
2059 }
2060
2061 static int rtl8192_qos_handle_probe_response(struct r8192_priv *priv,
2062                                              int active_network,
2063                                              struct ieee80211_network *network)
2064 {
2065         int ret = 0;
2066         u32 size = sizeof(struct ieee80211_qos_parameters);
2067
2068         if (priv->ieee80211->state != IEEE80211_LINKED)
2069                 return ret;
2070
2071         if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
2072                 return ret;
2073
2074         if (network->flags & NETWORK_HAS_QOS_MASK) {
2075                 if (active_network &&
2076                     (network->flags & NETWORK_HAS_QOS_PARAMETERS))
2077                         network->qos_data.active = network->qos_data.supported;
2078
2079                 if ((network->qos_data.active == 1) && (active_network == 1) &&
2080                     (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
2081                     (network->qos_data.old_param_count !=
2082                      network->qos_data.param_count)) {
2083                         network->qos_data.old_param_count =
2084                                 network->qos_data.param_count;
2085                         queue_work(priv->priv_wq, &priv->qos_activate);
2086                         RT_TRACE(COMP_QOS, "QoS parameters change call "
2087                                  "qos_activate\n");
2088                 }
2089         } else {
2090                 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
2091                        &def_qos_parameters, size);
2092
2093                 if ((network->qos_data.active == 1) && (active_network == 1)) {
2094                         queue_work(priv->priv_wq, &priv->qos_activate);
2095                         RT_TRACE(COMP_QOS, "QoS was disabled call qos_activate \n");
2096                 }
2097                 network->qos_data.active = 0;
2098                 network->qos_data.supported = 0;
2099         }
2100
2101         return 0;
2102 }
2103
2104 /* handle and manage frame from beacon and probe response */
2105 static int rtl8192_handle_beacon(struct net_device *dev,
2106                                  struct ieee80211_beacon *beacon,
2107                                  struct ieee80211_network *network)
2108 {
2109         struct r8192_priv *priv = ieee80211_priv(dev);
2110
2111         rtl8192_qos_handle_probe_response(priv, 1, network);
2112         queue_delayed_work(priv->priv_wq, &priv->update_beacon_wq, 0);
2113         return 0;
2114
2115 }
2116
2117 /*
2118 * handling the beaconing responses. if we get different QoS setting
2119 * off the network from the associated setting, adjust the QoS
2120 * setting
2121 */
2122 static int rtl8192_qos_association_resp(struct r8192_priv *priv,
2123                                         struct ieee80211_network *network)
2124 {
2125         int ret = 0;
2126         unsigned long flags;
2127         u32 size = sizeof(struct ieee80211_qos_parameters);
2128         int set_qos_param = 0;
2129
2130         if ((priv == NULL) || (network == NULL))
2131                 return ret;
2132
2133         if (priv->ieee80211->state != IEEE80211_LINKED)
2134                 return ret;
2135
2136         if ((priv->ieee80211->iw_mode != IW_MODE_INFRA))
2137                 return ret;
2138
2139         spin_lock_irqsave(&priv->ieee80211->lock, flags);
2140         if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
2141                 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
2142                        &network->qos_data.parameters,
2143                        sizeof(struct ieee80211_qos_parameters));
2144                 priv->ieee80211->current_network.qos_data.active = 1;
2145                 set_qos_param = 1;
2146                 /* update qos parameter for current network */
2147                 priv->ieee80211->current_network.qos_data.old_param_count =
2148                         priv->ieee80211->current_network.qos_data.param_count;
2149                 priv->ieee80211->current_network.qos_data.param_count =
2150                         network->qos_data.param_count;
2151         } else {
2152                 memcpy(&priv->ieee80211->current_network.qos_data.parameters,
2153                        &def_qos_parameters, size);
2154                 priv->ieee80211->current_network.qos_data.active = 0;
2155                 priv->ieee80211->current_network.qos_data.supported = 0;
2156                 set_qos_param = 1;
2157         }
2158
2159         spin_unlock_irqrestore(&priv->ieee80211->lock, flags);
2160
2161         RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n", __func__, network->flags, priv->ieee80211->current_network.qos_data.active);
2162         if (set_qos_param == 1)
2163                 queue_work(priv->priv_wq, &priv->qos_activate);
2164
2165
2166         return ret;
2167 }
2168
2169
2170 static int rtl8192_handle_assoc_response(struct net_device *dev,
2171                                          struct ieee80211_assoc_response_frame *resp,
2172                                          struct ieee80211_network *network)
2173 {
2174         struct r8192_priv *priv = ieee80211_priv(dev);
2175         rtl8192_qos_association_resp(priv, network);
2176         return 0;
2177 }
2178
2179
2180 void rtl8192_update_ratr_table(struct net_device *dev)
2181 {
2182         struct r8192_priv *priv = ieee80211_priv(dev);
2183         struct ieee80211_device *ieee = priv->ieee80211;
2184         u8 *pMcsRate = ieee->dot11HTOperationalRateSet;
2185         u32 ratr_value = 0;
2186         u8 rate_index = 0;
2187         rtl8192_config_rate(dev, (u16 *)(&ratr_value));
2188         ratr_value |= (*(u16 *)(pMcsRate)) << 12;
2189         switch (ieee->mode) {
2190         case IEEE_A:
2191                 ratr_value &= 0x00000FF0;
2192                 break;
2193         case IEEE_B:
2194                 ratr_value &= 0x0000000F;
2195                 break;
2196         case IEEE_G:
2197                 ratr_value &= 0x00000FF7;
2198                 break;
2199         case IEEE_N_24G:
2200         case IEEE_N_5G:
2201                 if (ieee->pHTInfo->PeerMimoPs == 0) {//MIMO_PS_STATIC
2202                         ratr_value &= 0x0007F007;
2203                 } else {
2204                         if (priv->rf_type == RF_1T2R)
2205                                 ratr_value &= 0x000FF007;
2206                         else
2207                                 ratr_value &= 0x0F81F007;
2208                 }
2209                 break;
2210         default:
2211                 break;
2212         }
2213         ratr_value &= 0x0FFFFFFF;
2214         if (ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI40MHz)
2215                 ratr_value |= 0x80000000;
2216         else if (!ieee->pHTInfo->bCurTxBW40MHz && ieee->pHTInfo->bCurShortGI20MHz)
2217                 ratr_value |= 0x80000000;
2218         write_nic_dword(dev, RATR0+rate_index*4, ratr_value);
2219         write_nic_byte(dev, UFWP, 1);
2220 }
2221
2222 static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
2223 static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
2224 static bool GetNmodeSupportBySecCfg8192(struct net_device *dev)
2225 {
2226         struct r8192_priv *priv = ieee80211_priv(dev);
2227         struct ieee80211_device *ieee = priv->ieee80211;
2228         struct ieee80211_network *network = &ieee->current_network;
2229         int wpa_ie_len = ieee->wpa_ie_len;
2230         struct ieee80211_crypt_data *crypt;
2231         int encrypt;
2232
2233         crypt = ieee->crypt[ieee->tx_keyidx];
2234         //we use connecting AP's capability instead of only security config on our driver to distinguish whether it should use N mode or G mode
2235         encrypt = (network->capability & WLAN_CAPABILITY_PRIVACY) || (ieee->host_encrypt && crypt && crypt->ops && (0 == strcmp(crypt->ops->name, "WEP")));
2236
2237         /* simply judge  */
2238         if (encrypt && (wpa_ie_len == 0)) {
2239                 /* wep encryption, no N mode setting */
2240                 return false;
2241         } else if ((wpa_ie_len != 0)) {
2242                 /* parse pairwise key type */
2243                 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))))
2244                         return true;
2245                 else
2246                         return false;
2247         } else {
2248                 return true;
2249         }
2250
2251         return true;
2252 }
2253
2254 static bool GetHalfNmodeSupportByAPs819xUsb(struct net_device *dev)
2255 {
2256         bool                    Reval;
2257         struct r8192_priv *priv = ieee80211_priv(dev);
2258         struct ieee80211_device *ieee = priv->ieee80211;
2259
2260         if (ieee->bHalfWirelessN24GMode == true)
2261                 Reval = true;
2262         else
2263                 Reval =  false;
2264
2265         return Reval;
2266 }
2267
2268 static void rtl8192_refresh_supportrate(struct r8192_priv *priv)
2269 {
2270         struct ieee80211_device *ieee = priv->ieee80211;
2271         //we do not consider set support rate for ABG mode, only HT MCS rate is set here.
2272         if (ieee->mode == WIRELESS_MODE_N_24G || ieee->mode == WIRELESS_MODE_N_5G)
2273                 memcpy(ieee->Regdot11HTOperationalRateSet, ieee->RegHTSuppRateSet, 16);
2274         else
2275                 memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
2276         return;
2277 }
2278
2279 static u8 rtl8192_getSupportedWireleeMode(struct net_device *dev)
2280 {
2281         struct r8192_priv *priv = ieee80211_priv(dev);
2282         u8 ret = 0;
2283         switch (priv->rf_chip) {
2284         case RF_8225:
2285         case RF_8256:
2286         case RF_PSEUDO_11N:
2287                 ret = (WIRELESS_MODE_N_24G|WIRELESS_MODE_G|WIRELESS_MODE_B);
2288                 break;
2289         case RF_8258:
2290                 ret = (WIRELESS_MODE_A|WIRELESS_MODE_N_5G);
2291                 break;
2292         default:
2293                 ret = WIRELESS_MODE_B;
2294                 break;
2295         }
2296         return ret;
2297 }
2298 static void rtl8192_SetWirelessMode(struct net_device *dev, u8 wireless_mode)
2299 {
2300         struct r8192_priv *priv = ieee80211_priv(dev);
2301         u8 bSupportMode = rtl8192_getSupportedWireleeMode(dev);
2302
2303         if ((wireless_mode == WIRELESS_MODE_AUTO) || ((wireless_mode&bSupportMode) == 0)) {
2304                 if (bSupportMode & WIRELESS_MODE_N_24G) {
2305                         wireless_mode = WIRELESS_MODE_N_24G;
2306                 } else if (bSupportMode & WIRELESS_MODE_N_5G) {
2307                         wireless_mode = WIRELESS_MODE_N_5G;
2308                 } else if ((bSupportMode & WIRELESS_MODE_A)) {
2309                         wireless_mode = WIRELESS_MODE_A;
2310                 } else if ((bSupportMode & WIRELESS_MODE_G)) {
2311                         wireless_mode = WIRELESS_MODE_G;
2312                 } else if ((bSupportMode & WIRELESS_MODE_B)) {
2313                         wireless_mode = WIRELESS_MODE_B;
2314                 } else {
2315                         RT_TRACE(COMP_ERR, "%s(), No valid wireless mode supported, SupportedWirelessMode(%x)!!!\n", __func__, bSupportMode);
2316                         wireless_mode = WIRELESS_MODE_B;
2317                 }
2318         }
2319 #ifdef TO_DO_LIST //// TODO: this function doesn't work well at this time, we should wait for FPGA
2320         ActUpdateChannelAccessSetting(pAdapter, pHalData->CurrentWirelessMode, &pAdapter->MgntInfo.Info8185.ChannelAccessSetting);
2321 #endif
2322         priv->ieee80211->mode = wireless_mode;
2323
2324         if ((wireless_mode == WIRELESS_MODE_N_24G) ||  (wireless_mode == WIRELESS_MODE_N_5G))
2325                 priv->ieee80211->pHTInfo->bEnableHT = 1;
2326         else
2327                 priv->ieee80211->pHTInfo->bEnableHT = 0;
2328         RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
2329         rtl8192_refresh_supportrate(priv);
2330
2331 }
2332 //init priv variables here. only non_zero value should be initialized here.
2333 static void rtl8192_init_priv_variable(struct net_device *dev)
2334 {
2335         struct r8192_priv *priv = ieee80211_priv(dev);
2336         u8 i;
2337         priv->card_8192 = NIC_8192U;
2338         priv->chan = 1; //set to channel 1
2339         priv->ieee80211->mode = WIRELESS_MODE_AUTO; //SET AUTO
2340         priv->ieee80211->iw_mode = IW_MODE_INFRA;
2341         priv->ieee80211->ieee_up = 0;
2342         priv->retry_rts = DEFAULT_RETRY_RTS;
2343         priv->retry_data = DEFAULT_RETRY_DATA;
2344         priv->ieee80211->rts = DEFAULT_RTS_THRESHOLD;
2345         priv->ieee80211->rate = 110; //11 mbps
2346         priv->ieee80211->short_slot = 1;
2347         priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
2348         priv->CckPwEnl = 6;
2349         //for silent reset
2350         priv->IrpPendingCount = 1;
2351         priv->ResetProgress = RESET_TYPE_NORESET;
2352         priv->bForcedSilentReset = 0;
2353         priv->bDisableNormalResetCheck = false;
2354         priv->force_reset = false;
2355
2356         priv->ieee80211->FwRWRF = 0;    //we don't use FW read/write RF until stable firmware is available.
2357         priv->ieee80211->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
2358         priv->ieee80211->iw_mode = IW_MODE_INFRA;
2359         priv->ieee80211->softmac_features  = IEEE_SOFTMAC_SCAN |
2360                 IEEE_SOFTMAC_ASSOCIATE | IEEE_SOFTMAC_PROBERQ |
2361                 IEEE_SOFTMAC_PROBERS | IEEE_SOFTMAC_TX_QUEUE |
2362                 IEEE_SOFTMAC_BEACONS;//added by amy 080604
2363
2364         priv->ieee80211->active_scan = 1;
2365         priv->ieee80211->modulation = IEEE80211_CCK_MODULATION | IEEE80211_OFDM_MODULATION;
2366         priv->ieee80211->host_encrypt = 1;
2367         priv->ieee80211->host_decrypt = 1;
2368         priv->ieee80211->start_send_beacons = NULL; //-by amy 080604
2369         priv->ieee80211->stop_send_beacons = NULL;  //-by amy 080604
2370         priv->ieee80211->softmac_hard_start_xmit = rtl8192_hard_start_xmit;
2371         priv->ieee80211->set_chan = rtl8192_set_chan;
2372         priv->ieee80211->link_change = rtl8192_link_change;
2373         priv->ieee80211->softmac_data_hard_start_xmit = rtl8192_hard_data_xmit;
2374         priv->ieee80211->data_hard_stop = rtl8192_data_hard_stop;
2375         priv->ieee80211->data_hard_resume = rtl8192_data_hard_resume;
2376         priv->ieee80211->init_wmmparam_flag = 0;
2377         priv->ieee80211->fts = DEFAULT_FRAG_THRESHOLD;
2378         priv->ieee80211->check_nic_enough_desc = check_nic_enough_desc;
2379         priv->ieee80211->tx_headroom = TX_PACKET_SHIFT_BYTES;
2380         priv->ieee80211->qos_support = 1;
2381
2382         //added by WB
2383         priv->ieee80211->SetBWModeHandler = rtl8192_SetBWMode;
2384         priv->ieee80211->handle_assoc_response = rtl8192_handle_assoc_response;
2385         priv->ieee80211->handle_beacon = rtl8192_handle_beacon;
2386         //added by david
2387         priv->ieee80211->GetNmodeSupportBySecCfg = GetNmodeSupportBySecCfg8192;
2388         priv->ieee80211->GetHalfNmodeSupportByAPsHandler = GetHalfNmodeSupportByAPs819xUsb;
2389         priv->ieee80211->SetWirelessMode = rtl8192_SetWirelessMode;
2390         //added by amy
2391         priv->ieee80211->InitialGainHandler = InitialGain819xUsb;
2392         priv->card_type = USB;
2393 #ifdef TO_DO_LIST
2394         if (Adapter->bInHctTest) {
2395                 pHalData->ShortRetryLimit = 7;
2396                 pHalData->LongRetryLimit = 7;
2397         }
2398 #endif
2399         priv->ShortRetryLimit = 0x30;
2400         priv->LongRetryLimit = 0x30;
2401         priv->EarlyRxThreshold = 7;
2402         priv->enable_gpio0 = 0;
2403         priv->TransmitConfig =
2404                 (TCR_MXDMA_2048<<TCR_MXDMA_OFFSET)|  // Max DMA Burst Size per Tx DMA Burst, 7: reserved.
2405                 (priv->ShortRetryLimit<<TCR_SRL_OFFSET)|        // Short retry limit
2406                 (priv->LongRetryLimit<<TCR_LRL_OFFSET) |        // Long retry limit
2407                 (false ? TCR_SAT : 0);  // FALSE: HW provides PLCP length and LENGEXT, TRUE: SW provides them
2408 #ifdef TO_DO_LIST
2409         if (Adapter->bInHctTest)
2410                 pHalData->ReceiveConfig =       pHalData->CSMethod |
2411                                                 RCR_AMF | RCR_ADF |     //accept management/data
2412                                                 //guangan200710
2413                                                 RCR_ACF |       //accept control frame for SW AP needs PS-poll, 2005.07.07, by rcnjko.
2414                                                 RCR_AB | RCR_AM | RCR_APM |             //accept BC/MC/UC
2415                                                 RCR_AICV | RCR_ACRC32 |                 //accept ICV/CRC error packet
2416                                                 ((u32)7<<RCR_MXDMA_OFFSET) | // Max DMA Burst Size per Rx DMA Burst, 7: unlimited.
2417                                                 (pHalData->EarlyRxThreshold<<RCR_FIFO_OFFSET) | // Rx FIFO Threshold, 7: No Rx threshold.
2418                                                 (pHalData->EarlyRxThreshold == 7 ? RCR_OnlyErlPkt : 0);
2419         else
2420
2421 #endif
2422         priv->ReceiveConfig     =
2423                 RCR_AMF | RCR_ADF |             //accept management/data
2424                 RCR_ACF |                       //accept control frame for SW AP needs PS-poll, 2005.07.07, by rcnjko.
2425                 RCR_AB | RCR_AM | RCR_APM |     //accept BC/MC/UC
2426                 ((u32)7<<RCR_MXDMA_OFFSET)| // Max DMA Burst Size per Rx DMA Burst, 7: unlimited.
2427                 (priv->EarlyRxThreshold<<RX_FIFO_THRESHOLD_SHIFT) | // Rx FIFO Threshold, 7: No Rx threshold.
2428                 (priv->EarlyRxThreshold == 7 ? RCR_ONLYERLPKT : 0);
2429
2430         priv->AcmControl = 0;
2431         priv->pFirmware = kzalloc(sizeof(rt_firmware), GFP_KERNEL);
2432
2433         /* rx related queue */
2434         skb_queue_head_init(&priv->rx_queue);
2435         skb_queue_head_init(&priv->skb_queue);
2436
2437         /* Tx related queue */
2438         for (i = 0; i < MAX_QUEUE_SIZE; i++)
2439                 skb_queue_head_init(&priv->ieee80211->skb_waitQ[i]);
2440         for (i = 0; i < MAX_QUEUE_SIZE; i++)
2441                 skb_queue_head_init(&priv->ieee80211->skb_aggQ[i]);
2442         for (i = 0; i < MAX_QUEUE_SIZE; i++)
2443                 skb_queue_head_init(&priv->ieee80211->skb_drv_aggQ[i]);
2444         priv->rf_set_chan = rtl8192_phy_SwChnl;
2445 }
2446
2447 //init lock here
2448 static void rtl8192_init_priv_lock(struct r8192_priv *priv)
2449 {
2450         spin_lock_init(&priv->tx_lock);
2451         spin_lock_init(&priv->irq_lock);//added by thomas
2452         sema_init(&priv->wx_sem, 1);
2453         sema_init(&priv->rf_sem, 1);
2454         mutex_init(&priv->mutex);
2455 }
2456
2457 extern  void    rtl819x_watchdog_wqcallback(struct work_struct *work);
2458
2459 void rtl8192_irq_rx_tasklet(struct r8192_priv *priv);
2460 //init tasklet and wait_queue here. only 2.6 above kernel is considered
2461 #define DRV_NAME "wlan0"
2462 static void rtl8192_init_priv_task(struct net_device *dev)
2463 {
2464         struct r8192_priv *priv = ieee80211_priv(dev);
2465
2466         priv->priv_wq = create_workqueue(DRV_NAME);
2467
2468         INIT_WORK(&priv->reset_wq, rtl8192_restart);
2469
2470         INIT_DELAYED_WORK(&priv->watch_dog_wq, rtl819x_watchdog_wqcallback);
2471         INIT_DELAYED_WORK(&priv->txpower_tracking_wq,  dm_txpower_trackingcallback);
2472         INIT_DELAYED_WORK(&priv->rfpath_check_wq,  dm_rf_pathcheck_workitemcallback);
2473         INIT_DELAYED_WORK(&priv->update_beacon_wq, rtl8192_update_beacon);
2474         INIT_DELAYED_WORK(&priv->initialgain_operate_wq, InitialGainOperateWorkItemCallBack);
2475         INIT_WORK(&priv->qos_activate, rtl8192_qos_activate);
2476
2477         tasklet_init(&priv->irq_rx_tasklet,
2478                      (void(*)(unsigned long))rtl8192_irq_rx_tasklet,
2479                      (unsigned long)priv);
2480 }
2481
2482 static void rtl8192_get_eeprom_size(struct net_device *dev)
2483 {
2484         u16 curCR = 0;
2485         struct r8192_priv *priv = ieee80211_priv(dev);
2486         RT_TRACE(COMP_EPROM, "===========>%s()\n", __func__);
2487         read_nic_word_E(dev, EPROM_CMD, &curCR);
2488         RT_TRACE(COMP_EPROM, "read from Reg EPROM_CMD(%x):%x\n", EPROM_CMD, curCR);
2489         //whether need I consider BIT5?
2490         priv->epromtype = (curCR & Cmd9346CR_9356SEL) ? EPROM_93c56 : EPROM_93c46;
2491         RT_TRACE(COMP_EPROM, "<===========%s(), epromtype:%d\n", __func__, priv->epromtype);
2492 }
2493
2494 //used to swap endian. as ntohl & htonl are not necessary to swap endian, so use this instead.
2495 static inline u16 endian_swap(u16 *data)
2496 {
2497         u16 tmp = *data;
2498         *data = (tmp >> 8) | (tmp << 8);
2499         return *data;
2500 }
2501 static void rtl8192_read_eeprom_info(struct net_device *dev)
2502 {
2503         u16 wEPROM_ID = 0;
2504         u8 bMac_Tmp_Addr[6] = {0x00, 0xe0, 0x4c, 0x00, 0x00, 0x02};
2505         u8 bLoad_From_EEPOM = false;
2506         struct r8192_priv *priv = ieee80211_priv(dev);
2507         u16 tmpValue = 0;
2508         int i;
2509         RT_TRACE(COMP_EPROM, "===========>%s()\n", __func__);
2510         wEPROM_ID = eprom_read(dev, 0); //first read EEPROM ID out;
2511         RT_TRACE(COMP_EPROM, "EEPROM ID is 0x%x\n", wEPROM_ID);
2512
2513         if (wEPROM_ID != RTL8190_EEPROM_ID) {
2514                 RT_TRACE(COMP_ERR, "EEPROM ID is invalid(is 0x%x(should be 0x%x)\n", wEPROM_ID, RTL8190_EEPROM_ID);
2515         } else {
2516                 bLoad_From_EEPOM = true;
2517         }
2518
2519         if (bLoad_From_EEPOM) {
2520                 tmpValue = eprom_read(dev, (EEPROM_VID>>1));
2521                 priv->eeprom_vid = endian_swap(&tmpValue);
2522                 priv->eeprom_pid = eprom_read(dev, (EEPROM_PID>>1));
2523                 tmpValue = eprom_read(dev, (EEPROM_ChannelPlan>>1));
2524                 priv->eeprom_ChannelPlan = ((tmpValue&0xff00)>>8);
2525                 priv->btxpowerdata_readfromEEPORM = true;
2526                 priv->eeprom_CustomerID = eprom_read(dev, (EEPROM_Customer_ID>>1)) >>8;
2527         } else {
2528                 priv->eeprom_vid = 0;
2529                 priv->eeprom_pid = 0;
2530                 priv->card_8192_version = VERSION_819xU_B;
2531                 priv->eeprom_ChannelPlan = 0;
2532                 priv->eeprom_CustomerID = 0;
2533         }
2534         RT_TRACE(COMP_EPROM, "vid:0x%4x, pid:0x%4x, CustomID:0x%2x, ChanPlan:0x%x\n", priv->eeprom_vid, priv->eeprom_pid, priv->eeprom_CustomerID, priv->eeprom_ChannelPlan);
2535         //set channelplan from eeprom
2536         priv->ChannelPlan = priv->eeprom_ChannelPlan;
2537         if (bLoad_From_EEPOM) {
2538                 int i;
2539                 for (i = 0; i < 6; i += 2) {
2540                         u16 tmp = 0;
2541                         tmp = eprom_read(dev, (u16)((EEPROM_NODE_ADDRESS_BYTE_0 + i)>>1));
2542                         *(u16 *)(&dev->dev_addr[i]) = tmp;
2543                 }
2544         } else {
2545                 memcpy(dev->dev_addr, bMac_Tmp_Addr, 6);
2546                 //should I set IDR0 here?
2547         }
2548         RT_TRACE(COMP_EPROM, "MAC addr:%pM\n", dev->dev_addr);
2549         priv->rf_type = RTL819X_DEFAULT_RF_TYPE; //default 1T2R
2550         priv->rf_chip = RF_8256;
2551
2552         if (priv->card_8192_version == (u8)VERSION_819xU_A) {
2553                 //read Tx power gain offset of legacy OFDM to HT rate
2554                 if (bLoad_From_EEPOM)
2555                         priv->EEPROMTxPowerDiff = (eprom_read(dev, (EEPROM_TxPowerDiff>>1))&0xff00) >> 8;
2556                 else
2557                         priv->EEPROMTxPowerDiff = EEPROM_Default_TxPower;
2558                 RT_TRACE(COMP_EPROM, "TxPowerDiff:%d\n", priv->EEPROMTxPowerDiff);
2559                 //read ThermalMeter from EEPROM
2560                 if (bLoad_From_EEPOM)
2561                         priv->EEPROMThermalMeter = (u8)(eprom_read(dev, (EEPROM_ThermalMeter>>1))&0x00ff);
2562                 else
2563                         priv->EEPROMThermalMeter = EEPROM_Default_ThermalMeter;
2564                 RT_TRACE(COMP_EPROM, "ThermalMeter:%d\n", priv->EEPROMThermalMeter);
2565                 //vivi, for tx power track
2566                 priv->TSSI_13dBm = priv->EEPROMThermalMeter *100;
2567                 //read antenna tx power offset of B/C/D to A from EEPROM
2568                 if (bLoad_From_EEPOM)
2569                         priv->EEPROMPwDiff = (eprom_read(dev, (EEPROM_PwDiff>>1))&0x0f00)>>8;
2570                 else
2571                         priv->EEPROMPwDiff = EEPROM_Default_PwDiff;
2572                 RT_TRACE(COMP_EPROM, "TxPwDiff:%d\n", priv->EEPROMPwDiff);
2573                 // Read CrystalCap from EEPROM
2574                 if (bLoad_From_EEPOM)
2575                         priv->EEPROMCrystalCap = (eprom_read(dev, (EEPROM_CrystalCap>>1))&0x0f);
2576                 else
2577                         priv->EEPROMCrystalCap = EEPROM_Default_CrystalCap;
2578                 RT_TRACE(COMP_EPROM, "CrystalCap = %d\n", priv->EEPROMCrystalCap);
2579                 //get per-channel Tx power level
2580                 if (bLoad_From_EEPOM)
2581                         priv->EEPROM_Def_Ver = (eprom_read(dev, (EEPROM_TxPwIndex_Ver>>1))&0xff00)>>8;
2582                 else
2583                         priv->EEPROM_Def_Ver = 1;
2584                 RT_TRACE(COMP_EPROM, "EEPROM_DEF_VER:%d\n", priv->EEPROM_Def_Ver);
2585                 if (priv->EEPROM_Def_Ver == 0) { //old eeprom definition
2586                         int i;
2587                         if (bLoad_From_EEPOM)
2588                                 priv->EEPROMTxPowerLevelCCK = (eprom_read(dev, (EEPROM_TxPwIndex_CCK>>1))&0xff) >> 8;
2589                         else
2590                                 priv->EEPROMTxPowerLevelCCK = 0x10;
2591                         RT_TRACE(COMP_EPROM, "CCK Tx Power Levl: 0x%02x\n", priv->EEPROMTxPowerLevelCCK);
2592                         for (i = 0; i < 3; i++) {
2593                                 if (bLoad_From_EEPOM) {
2594                                         tmpValue = eprom_read(dev, (EEPROM_TxPwIndex_OFDM_24G+i)>>1);
2595                                         if (((EEPROM_TxPwIndex_OFDM_24G+i) % 2) == 0)
2596                                                 tmpValue = tmpValue & 0x00ff;
2597                                         else
2598                                                 tmpValue = (tmpValue & 0xff00) >> 8;
2599                                 } else {
2600                                         tmpValue = 0x10;
2601                                 }
2602                                 priv->EEPROMTxPowerLevelOFDM24G[i] = (u8) tmpValue;
2603                                 RT_TRACE(COMP_EPROM, "OFDM 2.4G Tx Power Level, Index %d = 0x%02x\n", i, priv->EEPROMTxPowerLevelCCK);
2604                         }
2605                 } else if (priv->EEPROM_Def_Ver == 1) {
2606                         if (bLoad_From_EEPOM) {
2607                                 tmpValue = eprom_read(dev, (EEPROM_TxPwIndex_CCK_V1>>1));
2608                                 tmpValue = (tmpValue & 0xff00) >> 8;
2609                         } else {
2610                                 tmpValue = 0x10;
2611                         }
2612                         priv->EEPROMTxPowerLevelCCK_V1[0] = (u8)tmpValue;
2613
2614                         if (bLoad_From_EEPOM)
2615                                 tmpValue = eprom_read(dev, (EEPROM_TxPwIndex_CCK_V1 + 2)>>1);
2616                         else
2617                                 tmpValue = 0x1010;
2618                         *((u16 *)(&priv->EEPROMTxPowerLevelCCK_V1[1])) = tmpValue;
2619                         if (bLoad_From_EEPOM)
2620                                 tmpValue = eprom_read(dev, (EEPROM_TxPwIndex_OFDM_24G_V1>>1));
2621                         else
2622                                 tmpValue = 0x1010;
2623                         *((u16 *)(&priv->EEPROMTxPowerLevelOFDM24G[0])) = tmpValue;
2624                         if (bLoad_From_EEPOM)
2625                                 tmpValue = eprom_read(dev, (EEPROM_TxPwIndex_OFDM_24G_V1+2)>>1);
2626                         else
2627                                 tmpValue = 0x10;
2628                         priv->EEPROMTxPowerLevelOFDM24G[2] = (u8)tmpValue;
2629                 }//endif EEPROM_Def_Ver == 1
2630
2631                 //update HAL variables
2632                 //
2633                 for (i = 0; i < 14; i++) {
2634                         if (i <= 3)
2635                                 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[0];
2636                         else if (i >= 4 && i <= 9)
2637                                 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[1];
2638                         else
2639                                 priv->TxPowerLevelOFDM24G[i] = priv->EEPROMTxPowerLevelOFDM24G[2];
2640                 }
2641
2642                 for (i = 0; i < 14; i++) {
2643                         if (priv->EEPROM_Def_Ver == 0) {
2644                                 if (i <= 3)
2645                                         priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelOFDM24G[0] + (priv->EEPROMTxPowerLevelCCK - priv->EEPROMTxPowerLevelOFDM24G[1]);
2646                                 else if (i >= 4 && i <= 9)
2647                                         priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK;
2648                                 else
2649                                         priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelOFDM24G[2] + (priv->EEPROMTxPowerLevelCCK - priv->EEPROMTxPowerLevelOFDM24G[1]);
2650                         } else if (priv->EEPROM_Def_Ver == 1) {
2651                                 if (i <= 3)
2652                                         priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK_V1[0];
2653                                 else if (i >= 4 && i <= 9)
2654                                         priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK_V1[1];
2655                                 else
2656                                         priv->TxPowerLevelCCK[i] = priv->EEPROMTxPowerLevelCCK_V1[2];
2657                         }
2658                 }
2659                 priv->TxPowerDiff = priv->EEPROMPwDiff;
2660                 // Antenna B gain offset to antenna A, bit0~3
2661                 priv->AntennaTxPwDiff[0] = (priv->EEPROMTxPowerDiff & 0xf);
2662                 // Antenna C gain offset to antenna A, bit4~7
2663                 priv->AntennaTxPwDiff[1] = ((priv->EEPROMTxPowerDiff & 0xf0)>>4);
2664                 // CrystalCap, bit12~15
2665                 priv->CrystalCap = priv->EEPROMCrystalCap;
2666                 // ThermalMeter, bit0~3 for RFIC1, bit4~7 for RFIC2
2667                 // 92U does not enable TX power tracking.
2668                 priv->ThermalMeter[0] = priv->EEPROMThermalMeter;
2669         }//end if VersionID == VERSION_819xU_A
2670
2671         //added by vivi, for dlink led, 20080416
2672         switch (priv->eeprom_CustomerID) {
2673         case EEPROM_CID_RUNTOP:
2674                 priv->CustomerID = RT_CID_819x_RUNTOP;
2675                 break;
2676
2677         case EEPROM_CID_DLINK:
2678                 priv->CustomerID = RT_CID_DLINK;
2679                 break;
2680
2681         default:
2682                 priv->CustomerID = RT_CID_DEFAULT;
2683                 break;
2684
2685         }
2686
2687         switch (priv->CustomerID) {
2688         case RT_CID_819x_RUNTOP:
2689                 priv->LedStrategy = SW_LED_MODE2;
2690                 break;
2691
2692         case RT_CID_DLINK:
2693                 priv->LedStrategy = SW_LED_MODE4;
2694                 break;
2695
2696         default:
2697                 priv->LedStrategy = SW_LED_MODE0;
2698                 break;
2699
2700         }
2701
2702
2703         if (priv->rf_type == RF_1T2R) {
2704                 RT_TRACE(COMP_EPROM, "\n1T2R config\n");
2705         } else {
2706                 RT_TRACE(COMP_EPROM, "\n2T4R config\n");
2707         }
2708
2709         // 2008/01/16 MH We can only know RF type in the function. So we have to init
2710         // DIG RATR table again.
2711         init_rate_adaptive(dev);
2712         //we need init DIG RATR table here again.
2713
2714         RT_TRACE(COMP_EPROM, "<===========%s()\n", __func__);
2715         return;
2716 }
2717
2718 static short rtl8192_get_channel_map(struct net_device *dev)
2719 {
2720         struct r8192_priv *priv = ieee80211_priv(dev);
2721         if (priv->ChannelPlan > COUNTRY_CODE_GLOBAL_DOMAIN) {
2722                 netdev_err(dev, "rtl8180_init: Error channel plan! Set to default.\n");
2723                 priv->ChannelPlan = 0;
2724         }
2725         RT_TRACE(COMP_INIT, "Channel plan is %d\n", priv->ChannelPlan);
2726
2727         rtl819x_set_channel_map(priv->ChannelPlan, priv);
2728         return 0;
2729 }
2730
2731 static short rtl8192_init(struct net_device *dev)
2732 {
2733
2734         struct r8192_priv *priv = ieee80211_priv(dev);
2735
2736         memset(&(priv->stats), 0, sizeof(struct Stats));
2737         memset(priv->txqueue_to_outpipemap, 0, 9);
2738 #ifdef PIPE12
2739         {
2740                 int i = 0;
2741                 u8 queuetopipe[] = {3, 2, 1, 0, 4, 8, 7, 6, 5};
2742                 memcpy(priv->txqueue_to_outpipemap, queuetopipe, 9);
2743         }
2744 #else
2745         {
2746                 u8 queuetopipe[] = {3, 2, 1, 0, 4, 4, 0, 4, 4};
2747                 memcpy(priv->txqueue_to_outpipemap, queuetopipe, 9);
2748         }
2749 #endif
2750         rtl8192_init_priv_variable(dev);
2751         rtl8192_init_priv_lock(priv);
2752         rtl8192_init_priv_task(dev);
2753         rtl8192_get_eeprom_size(dev);
2754         rtl8192_read_eeprom_info(dev);
2755         rtl8192_get_channel_map(dev);
2756         init_hal_dm(dev);
2757         init_timer(&priv->watch_dog_timer);
2758         priv->watch_dog_timer.data = (unsigned long)dev;
2759         priv->watch_dog_timer.function = watch_dog_timer_callback;
2760         if (rtl8192_usb_initendpoints(dev) != 0) {
2761                 DMESG("Endopoints initialization failed");
2762                 return -ENOMEM;
2763         }
2764
2765 #ifdef DEBUG_EPROM
2766         dump_eprom(dev);
2767 #endif
2768         return 0;
2769 }
2770
2771 /******************************************************************************
2772  *function:  This function actually only set RRSR, RATR and BW_OPMODE registers
2773  *           not to do all the hw config as its name says
2774  *   input:  net_device dev
2775  *  output:  none
2776  *  return:  none
2777  *  notice:  This part need to modified according to the rate set we filtered
2778  * ****************************************************************************/
2779 static void rtl8192_hwconfig(struct net_device *dev)
2780 {
2781         u32 regRATR = 0, regRRSR = 0;
2782         u8 regBwOpMode = 0, regTmp = 0;
2783         struct r8192_priv *priv = ieee80211_priv(dev);
2784         u32 ratr_value = 0;
2785
2786         // Set RRSR, RATR, and BW_OPMODE registers
2787         //
2788         switch (priv->ieee80211->mode) {
2789         case WIRELESS_MODE_B:
2790                 regBwOpMode = BW_OPMODE_20MHZ;
2791                 regRATR = RATE_ALL_CCK;
2792                 regRRSR = RATE_ALL_CCK;
2793                 break;
2794         case WIRELESS_MODE_A:
2795                 regBwOpMode = BW_OPMODE_5G |BW_OPMODE_20MHZ;
2796                 regRATR = RATE_ALL_OFDM_AG;
2797                 regRRSR = RATE_ALL_OFDM_AG;
2798                 break;
2799         case WIRELESS_MODE_G:
2800                 regBwOpMode = BW_OPMODE_20MHZ;
2801                 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2802                 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2803                 break;
2804         case WIRELESS_MODE_AUTO:
2805 #ifdef TO_DO_LIST
2806                 if (Adapter->bInHctTest) {
2807                         regBwOpMode = BW_OPMODE_20MHZ;
2808                         regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2809                         regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2810                 }
2811                 else
2812 #endif
2813                 {
2814                         regBwOpMode = BW_OPMODE_20MHZ;
2815                         regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2816                         regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2817                 }
2818                 break;
2819         case WIRELESS_MODE_N_24G:
2820                 // It support CCK rate by default.
2821                 // CCK rate will be filtered out only when associated AP does not support it.
2822                 regBwOpMode = BW_OPMODE_20MHZ;
2823                 regRATR = RATE_ALL_CCK | RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2824                 regRRSR = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
2825                 break;
2826         case WIRELESS_MODE_N_5G:
2827                 regBwOpMode = BW_OPMODE_5G;
2828                 regRATR = RATE_ALL_OFDM_AG | RATE_ALL_OFDM_1SS | RATE_ALL_OFDM_2SS;
2829                 regRRSR = RATE_ALL_OFDM_AG;
2830                 break;
2831         }
2832
2833         write_nic_byte(dev, BW_OPMODE, regBwOpMode);
2834         ratr_value = regRATR;
2835         if (priv->rf_type == RF_1T2R)
2836                 ratr_value &= ~(RATE_ALL_OFDM_2SS);
2837         write_nic_dword(dev, RATR0, ratr_value);
2838         write_nic_byte(dev, UFWP, 1);
2839         read_nic_byte(dev, 0x313, &regTmp);
2840         regRRSR = ((regTmp) << 24) | (regRRSR & 0x00ffffff);
2841         write_nic_dword(dev, RRSR, regRRSR);
2842
2843         //
2844         // Set Retry Limit here
2845         //
2846         write_nic_word(dev, RETRY_LIMIT,
2847                        priv->ShortRetryLimit << RETRY_LIMIT_SHORT_SHIFT |
2848                        priv->LongRetryLimit << RETRY_LIMIT_LONG_SHIFT);
2849         // Set Contention Window here
2850
2851         // Set Tx AGC
2852
2853         // Set Tx Antenna including Feedback control
2854
2855         // Set Auto Rate fallback control
2856
2857
2858 }
2859
2860
2861 //InitializeAdapter and PhyCfg
2862 static bool rtl8192_adapter_start(struct net_device *dev)
2863 {
2864         struct r8192_priv *priv = ieee80211_priv(dev);
2865         u32 dwRegRead = 0;
2866         bool init_status = true;
2867         u8 SECR_value = 0x0;
2868         u8 tmp;
2869         RT_TRACE(COMP_INIT, "====>%s()\n", __func__);
2870         priv->Rf_Mode = RF_OP_By_SW_3wire;
2871         //for ASIC power on sequence
2872         write_nic_byte_E(dev, 0x5f, 0x80);
2873         mdelay(50);
2874         write_nic_byte_E(dev, 0x5f, 0xf0);
2875         write_nic_byte_E(dev, 0x5d, 0x00);
2876         write_nic_byte_E(dev, 0x5e, 0x80);
2877         write_nic_byte(dev, 0x17, 0x37);
2878         mdelay(10);
2879         priv->pFirmware->firmware_status = FW_STATUS_0_INIT;
2880         //config CPUReset Register
2881         //Firmware Reset or not?
2882         read_nic_dword(dev, CPU_GEN, &dwRegRead);
2883         if (priv->pFirmware->firmware_status == FW_STATUS_0_INIT)
2884                 dwRegRead |= CPU_GEN_SYSTEM_RESET; //do nothing here?
2885         else if (priv->pFirmware->firmware_status == FW_STATUS_5_READY)
2886                 dwRegRead |= CPU_GEN_FIRMWARE_RESET;
2887         else
2888                 RT_TRACE(COMP_ERR, "ERROR in %s(): undefined firmware state(%d)\n", __func__,   priv->pFirmware->firmware_status);
2889
2890         write_nic_dword(dev, CPU_GEN, dwRegRead);
2891         //config BB.
2892         rtl8192_BBConfig(dev);
2893
2894         //Loopback mode or not
2895         priv->LoopbackMode = RTL819xU_NO_LOOPBACK;
2896
2897         read_nic_dword(dev, CPU_GEN, &dwRegRead);
2898         if (priv->LoopbackMode == RTL819xU_NO_LOOPBACK)
2899                 dwRegRead = ((dwRegRead & CPU_GEN_NO_LOOPBACK_MSK) | CPU_GEN_NO_LOOPBACK_SET);
2900         else if (priv->LoopbackMode == RTL819xU_MAC_LOOPBACK)
2901                 dwRegRead |= CPU_CCK_LOOPBACK;
2902         else
2903                 RT_TRACE(COMP_ERR, "Serious error in %s(): wrong loopback mode setting(%d)\n", __func__,  priv->LoopbackMode);
2904
2905         write_nic_dword(dev, CPU_GEN, dwRegRead);
2906
2907         //after reset cpu, we need wait for a seconds to write in register.
2908         udelay(500);
2909
2910         //xiong add for new bitfile:usb suspend reset pin set to 1. //do we need?
2911         read_nic_byte_E(dev, 0x5f, &tmp);
2912         write_nic_byte_E(dev, 0x5f, tmp|0x20);
2913
2914         //Set Hardware
2915         rtl8192_hwconfig(dev);
2916
2917         //turn on Tx/Rx
2918         write_nic_byte(dev, CMDR, CR_RE|CR_TE);
2919
2920         //set IDR0 here
2921         write_nic_dword(dev, MAC0, ((u32 *)dev->dev_addr)[0]);
2922         write_nic_word(dev, MAC4, ((u16 *)(dev->dev_addr + 4))[0]);
2923
2924         //set RCR
2925         write_nic_dword(dev, RCR, priv->ReceiveConfig);
2926
2927         //Initialize Number of Reserved Pages in Firmware Queue
2928         write_nic_dword(dev, RQPN1,  NUM_OF_PAGE_IN_FW_QUEUE_BK << RSVD_FW_QUEUE_PAGE_BK_SHIFT |
2929                         NUM_OF_PAGE_IN_FW_QUEUE_BE << RSVD_FW_QUEUE_PAGE_BE_SHIFT |
2930                         NUM_OF_PAGE_IN_FW_QUEUE_VI << RSVD_FW_QUEUE_PAGE_VI_SHIFT |
2931                         NUM_OF_PAGE_IN_FW_QUEUE_VO <<RSVD_FW_QUEUE_PAGE_VO_SHIFT);
2932         write_nic_dword(dev, RQPN2, NUM_OF_PAGE_IN_FW_QUEUE_MGNT << RSVD_FW_QUEUE_PAGE_MGNT_SHIFT |
2933                         NUM_OF_PAGE_IN_FW_QUEUE_CMD << RSVD_FW_QUEUE_PAGE_CMD_SHIFT);
2934         write_nic_dword(dev, RQPN3, APPLIED_RESERVED_QUEUE_IN_FW|
2935                         NUM_OF_PAGE_IN_FW_QUEUE_BCN<<RSVD_FW_QUEUE_PAGE_BCN_SHIFT);
2936         write_nic_dword(dev, RATR0+4*7, (RATE_ALL_OFDM_AG | RATE_ALL_CCK));
2937
2938         //Set AckTimeout
2939         // TODO: (it value is only for FPGA version). need to be changed!!2006.12.18, by Emily
2940         write_nic_byte(dev, ACK_TIMEOUT, 0x30);
2941
2942         if (priv->ResetProgress == RESET_TYPE_NORESET)
2943                 rtl8192_SetWirelessMode(dev, priv->ieee80211->mode);
2944         if (priv->ResetProgress == RESET_TYPE_NORESET) {
2945                 CamResetAllEntry(dev);
2946                 SECR_value |= SCR_TxEncEnable;
2947                 SECR_value |= SCR_RxDecEnable;
2948                 SECR_value |= SCR_NoSKMC;
2949                 write_nic_byte(dev, SECR, SECR_value);
2950         }
2951
2952         //Beacon related
2953         write_nic_word(dev, ATIMWND, 2);
2954         write_nic_word(dev, BCN_INTERVAL, 100);
2955
2956 #define DEFAULT_EDCA 0x005e4332
2957         {
2958                 int i;
2959                 for (i = 0; i < QOS_QUEUE_NUM; i++)
2960                         write_nic_dword(dev, WDCAPARA_ADD[i], DEFAULT_EDCA);
2961         }
2962 #ifdef USB_RX_AGGREGATION_SUPPORT
2963         //3 For usb rx firmware aggregation control
2964         if (priv->ResetProgress == RESET_TYPE_NORESET) {
2965                 u32 ulValue;
2966                 PRT_HIGH_THROUGHPUT     pHTInfo = priv->ieee80211->pHTInfo;
2967                 ulValue = (pHTInfo->UsbRxFwAggrEn<<24) | (pHTInfo->UsbRxFwAggrPageNum<<16) |
2968                           (pHTInfo->UsbRxFwAggrPacketNum<<8) | (pHTInfo->UsbRxFwAggrTimeout);
2969                 /*
2970                  * If usb rx firmware aggregation is enabled,
2971                  * when anyone of three threshold conditions above is reached,
2972                  * firmware will send aggregated packet to driver.
2973                  */
2974                 write_nic_dword(dev, 0x1a8, ulValue);
2975                 priv->bCurrentRxAggrEnable = true;
2976         }
2977 #endif
2978
2979         rtl8192_phy_configmac(dev);
2980
2981         if (priv->card_8192_version == (u8) VERSION_819xU_A) {
2982                 rtl8192_phy_getTxPower(dev);
2983                 rtl8192_phy_setTxPower(dev, priv->chan);
2984         }
2985
2986         //Firmware download
2987         init_status = init_firmware(dev);
2988         if (!init_status) {
2989                 RT_TRACE(COMP_ERR, "ERR!!! %s(): Firmware download is failed\n", __func__);
2990                 return init_status;
2991         }
2992         RT_TRACE(COMP_INIT, "%s():after firmware download\n", __func__);
2993         //
2994 #ifdef TO_DO_LIST
2995         if (Adapter->ResetProgress == RESET_TYPE_NORESET) {
2996                 if (pMgntInfo->RegRfOff == TRUE) { // User disable RF via registry.
2997                         RT_TRACE((COMP_INIT|COMP_RF), DBG_LOUD, ("InitializeAdapter819xUsb(): Turn off RF for RegRfOff ----------\n"));
2998                         MgntActSet_RF_State(Adapter, eRfOff, RF_CHANGE_BY_SW);
2999                         // Those actions will be discard in MgntActSet_RF_State because of the same state
3000                         for (eRFPath = 0; eRFPath < pHalData->NumTotalRFPath; eRFPath++)
3001                                 PHY_SetRFReg(Adapter, (RF90_RADIO_PATH_E)eRFPath, 0x4, 0xC00, 0x0);
3002                 } else if (pMgntInfo->RfOffReason > RF_CHANGE_BY_PS) { // H/W or S/W RF OFF before sleep.
3003                         RT_TRACE((COMP_INIT|COMP_RF), DBG_LOUD, ("InitializeAdapter819xUsb(): Turn off RF for RfOffReason(%d) ----------\n", pMgntInfo->RfOffReason));
3004                         MgntActSet_RF_State(Adapter, eRfOff, pMgntInfo->RfOffReason);
3005                 } else {
3006                         pHalData->eRFPowerState = eRfOn;
3007                         pMgntInfo->RfOffReason = 0;
3008                         RT_TRACE((COMP_INIT|COMP_RF), DBG_LOUD, ("InitializeAdapter819xUsb(): RF is on ----------\n"));
3009                 }
3010         } else {
3011                 if (pHalData->eRFPowerState == eRfOff) {
3012                         MgntActSet_RF_State(Adapter, eRfOff, pMgntInfo->RfOffReason);
3013                         // Those actions will be discard in MgntActSet_RF_State because of the same state
3014                         for (eRFPath = 0; eRFPath < pHalData->NumTotalRFPath; eRFPath++)
3015                                 PHY_SetRFReg(Adapter, (RF90_RADIO_PATH_E)eRFPath, 0x4, 0xC00, 0x0);
3016                 }
3017         }
3018 #endif
3019         //config RF.
3020         if (priv->ResetProgress == RESET_TYPE_NORESET) {
3021                 rtl8192_phy_RFConfig(dev);
3022                 RT_TRACE(COMP_INIT, "%s():after phy RF config\n", __func__);
3023         }
3024
3025
3026         if (priv->ieee80211->FwRWRF)
3027                 // We can force firmware to do RF-R/W
3028                 priv->Rf_Mode = RF_OP_By_FW;
3029         else
3030                 priv->Rf_Mode = RF_OP_By_SW_3wire;
3031
3032
3033         rtl8192_phy_updateInitGain(dev);
3034         /*--set CCK and OFDM Block "ON"--*/
3035         rtl8192_setBBreg(dev, rFPGA0_RFMOD, bCCKEn, 0x1);
3036         rtl8192_setBBreg(dev, rFPGA0_RFMOD, bOFDMEn, 0x1);
3037
3038         if (priv->ResetProgress == RESET_TYPE_NORESET) {
3039                 //if D or C cut
3040                 u8 tmpvalue;
3041                 read_nic_byte(dev, 0x301, &tmpvalue);
3042                 if (tmpvalue == 0x03) {
3043                         priv->bDcut = TRUE;
3044                         RT_TRACE(COMP_POWER_TRACKING, "D-cut\n");
3045                 } else {
3046                         priv->bDcut = FALSE;
3047                         RT_TRACE(COMP_POWER_TRACKING, "C-cut\n");
3048                 }
3049                 dm_initialize_txpower_tracking(dev);
3050
3051                 if (priv->bDcut == TRUE) {
3052                         u32 i, TempCCk;
3053                         u32 tmpRegA = rtl8192_QueryBBReg(dev, rOFDM0_XATxIQImbalance, bMaskDWord);
3054                         for (i = 0; i < TxBBGainTableLength; i++) {
3055                                 if (tmpRegA == priv->txbbgain_table[i].txbbgain_value) {
3056                                         priv->rfa_txpowertrackingindex = (u8)i;
3057                                         priv->rfa_txpowertrackingindex_real = (u8)i;
3058                                         priv->rfa_txpowertracking_default = priv->rfa_txpowertrackingindex;
3059                                         break;
3060                                 }
3061                         }
3062
3063                         TempCCk = rtl8192_QueryBBReg(dev, rCCK0_TxFilter1, bMaskByte2);
3064
3065                         for (i = 0; i < CCKTxBBGainTableLength; i++) {
3066
3067                                 if (TempCCk == priv->cck_txbbgain_table[i].ccktxbb_valuearray[0]) {
3068                                         priv->cck_present_attentuation_20Mdefault = (u8) i;
3069                                         break;
3070                                 }
3071                         }
3072                         priv->cck_present_attentuation_40Mdefault = 0;
3073                         priv->cck_present_attentuation_difference = 0;
3074                         priv->cck_present_attentuation = priv->cck_present_attentuation_20Mdefault;
3075
3076                 }
3077         }
3078         write_nic_byte(dev, 0x87, 0x0);
3079
3080
3081         return init_status;
3082 }
3083
3084 /* this configures registers for beacon tx and enables it via
3085  * rtl8192_beacon_tx_enable(). rtl8192_beacon_tx_disable() might
3086  * be used to stop beacon transmission
3087  */
3088 /***************************************************************************
3089     -------------------------------NET STUFF---------------------------
3090 ***************************************************************************/
3091
3092 static struct net_device_stats *rtl8192_stats(struct net_device *dev)
3093 {
3094         struct r8192_priv *priv = ieee80211_priv(dev);
3095
3096         return &priv->ieee80211->stats;
3097 }
3098
3099 static bool HalTxCheckStuck819xUsb(struct net_device *dev)
3100 {
3101         struct r8192_priv *priv = ieee80211_priv(dev);
3102         u16             RegTxCounter;
3103         bool            bStuck = FALSE;
3104         read_nic_word(dev, 0x128, &RegTxCounter);
3105         RT_TRACE(COMP_RESET, "%s():RegTxCounter is %d,TxCounter is %d\n", __func__, RegTxCounter, priv->TxCounter);
3106         if (priv->TxCounter == RegTxCounter)
3107                 bStuck = TRUE;
3108
3109         priv->TxCounter = RegTxCounter;
3110
3111         return bStuck;
3112 }
3113
3114 /*
3115 *       <Assumption: RT_TX_SPINLOCK is acquired.>
3116 *       First added: 2006.11.19 by emily
3117 */
3118 static RESET_TYPE TxCheckStuck(struct net_device *dev)
3119 {
3120         struct r8192_priv *priv = ieee80211_priv(dev);
3121         u8                      QueueID;
3122         bool                    bCheckFwTxCnt = false;
3123
3124         //
3125         // Decide such threshold according to current power save mode
3126         //
3127
3128         for (QueueID = 0; QueueID <= BEACON_QUEUE; QueueID++) {
3129                 if (QueueID == TXCMD_QUEUE)
3130                         continue;
3131 #ifdef USB_TX_DRIVER_AGGREGATION_ENABLE
3132                 if ((skb_queue_len(&priv->ieee80211->skb_waitQ[QueueID]) == 0) && (skb_queue_len(&priv->ieee80211->skb_aggQ[QueueID]) == 0) && (skb_queue_len(&priv->ieee80211->skb_drv_aggQ[QueueID]) == 0))
3133 #else
3134                 if ((skb_queue_len(&priv->ieee80211->skb_waitQ[QueueID]) == 0)  && (skb_queue_len(&priv->ieee80211->skb_aggQ[QueueID]) == 0))
3135 #endif
3136                                 continue;
3137
3138                 bCheckFwTxCnt = true;
3139         }
3140         if (bCheckFwTxCnt) {
3141                 if (HalTxCheckStuck819xUsb(dev)) {
3142                         RT_TRACE(COMP_RESET, "TxCheckStuck(): Fw indicates no Tx condition! \n");
3143                         return RESET_TYPE_SILENT;
3144                 }
3145         }
3146         return RESET_TYPE_NORESET;
3147 }
3148
3149 static bool HalRxCheckStuck819xUsb(struct net_device *dev)
3150 {
3151         u16     RegRxCounter;
3152         struct r8192_priv *priv = ieee80211_priv(dev);
3153         bool bStuck = FALSE;
3154         static u8       rx_chk_cnt;
3155         read_nic_word(dev, 0x130, &RegRxCounter);
3156         RT_TRACE(COMP_RESET, "%s(): RegRxCounter is %d,RxCounter is %d\n", __func__, RegRxCounter, priv->RxCounter);
3157         // If rssi is small, we should check rx for long time because of bad rx.
3158         // or maybe it will continuous silent reset every 2 seconds.
3159         rx_chk_cnt++;
3160         if (priv->undecorated_smoothed_pwdb >= (RateAdaptiveTH_High+5)) {
3161                 rx_chk_cnt = 0; //high rssi, check rx stuck right now.
3162         } else if (priv->undecorated_smoothed_pwdb < (RateAdaptiveTH_High+5) &&
3163                    ((priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20 && priv->undecorated_smoothed_pwdb >= RateAdaptiveTH_Low_40M) ||
3164                     (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20 && priv->undecorated_smoothed_pwdb >= RateAdaptiveTH_Low_20M))) {
3165                 if (rx_chk_cnt < 2)
3166                         return bStuck;
3167                 else
3168                         rx_chk_cnt = 0;
3169         } else if (((priv->CurrentChannelBW != HT_CHANNEL_WIDTH_20 && priv->undecorated_smoothed_pwdb < RateAdaptiveTH_Low_40M) ||
3170                     (priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20 && priv->undecorated_smoothed_pwdb < RateAdaptiveTH_Low_20M)) &&
3171                      priv->undecorated_smoothed_pwdb >= VeryLowRSSI) {
3172                 if (rx_chk_cnt < 4)
3173                         return bStuck;
3174                 else
3175                         rx_chk_cnt = 0;
3176         } else {
3177                 if (rx_chk_cnt < 8)
3178                         return bStuck;
3179                 else
3180                         rx_chk_cnt = 0;
3181         }
3182
3183         if (priv->RxCounter == RegRxCounter)
3184                 bStuck = TRUE;
3185
3186         priv->RxCounter = RegRxCounter;
3187
3188         return bStuck;
3189 }
3190
3191 static RESET_TYPE RxCheckStuck(struct net_device *dev)
3192 {
3193         struct r8192_priv *priv = ieee80211_priv(dev);
3194         bool        bRxCheck = FALSE;
3195
3196         if (priv->IrpPendingCount > 1)
3197                 bRxCheck = TRUE;
3198
3199         if (bRxCheck) {
3200                 if (HalRxCheckStuck819xUsb(dev)) {
3201                         RT_TRACE(COMP_RESET, "RxStuck Condition\n");
3202                         return RESET_TYPE_SILENT;
3203                 }
3204         }
3205         return RESET_TYPE_NORESET;
3206 }
3207
3208
3209 /**
3210 *       This function is called by Checkforhang to check whether we should ask OS to reset driver
3211 *
3212 *       \param pAdapter The adapter context for this miniport
3213 *
3214 *       Note:NIC with USB interface sholud not call this function because we cannot scan descriptor
3215 *       to judge whether there is tx stuck.
3216 *       Note: This function may be required to be rewrite for Vista OS.
3217 *       <<<Assumption: Tx spinlock has been acquired >>>
3218 *
3219 *       8185 and 8185b does not implement this function. This is added by Emily at 2006.11.24
3220 */
3221 static RESET_TYPE rtl819x_ifcheck_resetornot(struct net_device *dev)
3222 {
3223         struct r8192_priv *priv = ieee80211_priv(dev);
3224         RESET_TYPE      TxResetType = RESET_TYPE_NORESET;
3225         RESET_TYPE      RxResetType = RESET_TYPE_NORESET;
3226         RT_RF_POWER_STATE       rfState;
3227
3228         rfState = priv->ieee80211->eRFPowerState;
3229
3230         TxResetType = TxCheckStuck(dev);
3231         if (rfState != eRfOff ||
3232             (priv->ieee80211->iw_mode != IW_MODE_ADHOC)) {
3233                 // If driver is in the status of firmware download failure , driver skips RF initialization and RF is
3234                 // in turned off state. Driver should check whether Rx stuck and do silent reset. And
3235                 // if driver is in firmware download failure status, driver should initialize RF in the following
3236                 // silent reset procedure Emily, 2008.01.21
3237
3238                 // Driver should not check RX stuck in IBSS mode because it is required to
3239                 // set Check BSSID in order to send beacon, however, if check BSSID is
3240                 // set, STA cannot hear any packet at all. Emily, 2008.04.12
3241                 RxResetType = RxCheckStuck(dev);
3242         }
3243         if (TxResetType == RESET_TYPE_NORMAL || RxResetType == RESET_TYPE_NORMAL) {
3244                 return RESET_TYPE_NORMAL;
3245         } else if (TxResetType == RESET_TYPE_SILENT || RxResetType == RESET_TYPE_SILENT) {
3246                 RT_TRACE(COMP_RESET, "%s():silent reset\n", __func__);
3247                 return RESET_TYPE_SILENT;
3248         } else {
3249                 return RESET_TYPE_NORESET;
3250         }
3251
3252 }
3253
3254 void rtl8192_cancel_deferred_work(struct r8192_priv *priv);
3255 int _rtl8192_up(struct net_device *dev);
3256 int rtl8192_close(struct net_device *dev);
3257
3258
3259
3260 static void CamRestoreAllEntry(struct net_device *dev)
3261 {
3262         u8 EntryId = 0;
3263         struct r8192_priv *priv = ieee80211_priv(dev);
3264         u8      *MacAddr = priv->ieee80211->current_network.bssid;
3265
3266         static u8       CAM_CONST_ADDR[4][6] = {
3267                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
3268                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
3269                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
3270                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x03} };
3271         static u8       CAM_CONST_BROAD[] = {
3272                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3273
3274         RT_TRACE(COMP_SEC, "CamRestoreAllEntry: \n");
3275
3276
3277         if ((priv->ieee80211->pairwise_key_type == KEY_TYPE_WEP40) ||
3278             (priv->ieee80211->pairwise_key_type == KEY_TYPE_WEP104)) {
3279
3280                 for (EntryId = 0; EntryId < 4; EntryId++) {
3281                         MacAddr = CAM_CONST_ADDR[EntryId];
3282                         setKey(dev, EntryId, EntryId,
3283                                priv->ieee80211->pairwise_key_type,
3284                                MacAddr, 0, NULL);
3285                 }
3286
3287         } else if (priv->ieee80211->pairwise_key_type == KEY_TYPE_TKIP) {
3288
3289                 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3290                         setKey(dev, 4, 0, priv->ieee80211->pairwise_key_type,
3291                                (u8 *)dev->dev_addr, 0, NULL);
3292                 else
3293                         setKey(dev, 4, 0, priv->ieee80211->pairwise_key_type,
3294                                MacAddr, 0, NULL);
3295         } else if (priv->ieee80211->pairwise_key_type == KEY_TYPE_CCMP) {
3296
3297                 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3298                         setKey(dev, 4, 0, priv->ieee80211->pairwise_key_type,
3299                                (u8 *)dev->dev_addr, 0, NULL);
3300                 else
3301                         setKey(dev, 4, 0, priv->ieee80211->pairwise_key_type,
3302                                MacAddr, 0, NULL);
3303         }
3304
3305
3306
3307         if (priv->ieee80211->group_key_type == KEY_TYPE_TKIP) {
3308                 MacAddr = CAM_CONST_BROAD;
3309                 for (EntryId = 1; EntryId < 4; EntryId++) {
3310                         setKey(dev, EntryId, EntryId,
3311                                priv->ieee80211->group_key_type,
3312                                MacAddr, 0, NULL);
3313                 }
3314                 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3315                         setKey(dev, 0, 0, priv->ieee80211->group_key_type,
3316                                CAM_CONST_ADDR[0], 0, NULL);
3317         } else if (priv->ieee80211->group_key_type == KEY_TYPE_CCMP) {
3318                 MacAddr = CAM_CONST_BROAD;
3319                 for (EntryId = 1; EntryId < 4; EntryId++) {
3320                         setKey(dev, EntryId, EntryId,
3321                                priv->ieee80211->group_key_type,
3322                                MacAddr, 0, NULL);
3323                 }
3324
3325                 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC)
3326                         setKey(dev, 0, 0, priv->ieee80211->group_key_type,
3327                                CAM_CONST_ADDR[0], 0, NULL);
3328         }
3329 }
3330 //////////////////////////////////////////////////////////////
3331 // This function is used to fix Tx/Rx stop bug temporarily.
3332 // This function will do "system reset" to NIC when Tx or Rx is stuck.
3333 // The method checking Tx/Rx stuck of this function is supported by FW,
3334 // which reports Tx and Rx counter to register 0x128 and 0x130.
3335 //////////////////////////////////////////////////////////////
3336 static void rtl819x_ifsilentreset(struct net_device *dev)
3337 {
3338         struct r8192_priv *priv = ieee80211_priv(dev);
3339         u8      reset_times = 0;
3340         int reset_status = 0;
3341         struct ieee80211_device *ieee = priv->ieee80211;
3342
3343
3344         // 2007.07.20. If we need to check CCK stop, please uncomment this line.
3345         //bStuck = Adapter->HalFunc.CheckHWStopHandler(Adapter);
3346
3347         if (priv->ResetProgress == RESET_TYPE_NORESET) {
3348 RESET_START:
3349
3350                 RT_TRACE(COMP_RESET, "=========>Reset progress!! \n");
3351
3352                 // Set the variable for reset.
3353                 priv->ResetProgress = RESET_TYPE_SILENT;
3354                 down(&priv->wx_sem);
3355                 if (priv->up == 0) {
3356                         RT_TRACE(COMP_ERR, "%s():the driver is not up! return\n", __func__);
3357                         up(&priv->wx_sem);
3358                         return;
3359                 }
3360                 priv->up = 0;
3361                 RT_TRACE(COMP_RESET, "%s():======>start to down the driver\n", __func__);
3362
3363                 rtl8192_rtx_disable(dev);
3364                 rtl8192_cancel_deferred_work(priv);
3365                 deinit_hal_dm(dev);
3366                 del_timer_sync(&priv->watch_dog_timer);
3367
3368                 ieee->sync_scan_hurryup = 1;
3369                 if (ieee->state == IEEE80211_LINKED) {
3370                         down(&ieee->wx_sem);
3371                         netdev_dbg(dev, "ieee->state is IEEE80211_LINKED\n");
3372                         ieee80211_stop_send_beacons(priv->ieee80211);
3373                         del_timer_sync(&ieee->associate_timer);
3374                         cancel_delayed_work(&ieee->associate_retry_wq);
3375                         ieee80211_stop_scan(ieee);
3376                         netif_carrier_off(dev);
3377                         up(&ieee->wx_sem);
3378                 } else {
3379                         netdev_dbg(dev, "ieee->state is NOT LINKED\n");
3380                         ieee80211_softmac_stop_protocol(priv->ieee80211);
3381                 }
3382                 up(&priv->wx_sem);
3383                 RT_TRACE(COMP_RESET, "%s():<==========down process is finished\n", __func__);
3384                 RT_TRACE(COMP_RESET, "%s():===========>start up the driver\n", __func__);
3385                 reset_status = _rtl8192_up(dev);
3386
3387                 RT_TRACE(COMP_RESET, "%s():<===========up process is finished\n", __func__);
3388                 if (reset_status == -EAGAIN) {
3389                         if (reset_times < 3) {
3390                                 reset_times++;
3391                                 goto RESET_START;
3392                         } else {
3393                                 RT_TRACE(COMP_ERR, " ERR!!! %s():  Reset Failed!!\n", __func__);
3394                         }
3395                 }
3396                 ieee->is_silent_reset = 1;
3397                 EnableHWSecurityConfig8192(dev);
3398                 if (ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_INFRA) {
3399                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
3400
3401                         queue_work(ieee->wq, &ieee->associate_complete_wq);
3402
3403                 } else if (ieee->state == IEEE80211_LINKED && ieee->iw_mode == IW_MODE_ADHOC) {
3404                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
3405                         ieee->link_change(ieee->dev);
3406
3407                         ieee80211_start_send_beacons(ieee);
3408
3409                         if (ieee->data_hard_resume)
3410                                 ieee->data_hard_resume(ieee->dev);
3411                         netif_carrier_on(ieee->dev);
3412                 }
3413
3414                 CamRestoreAllEntry(dev);
3415
3416                 priv->ResetProgress = RESET_TYPE_NORESET;
3417                 priv->reset_count++;
3418
3419                 priv->bForcedSilentReset = false;
3420                 priv->bResetInProgress = false;
3421
3422                 // For test --> force write UFWP.
3423                 write_nic_byte(dev, UFWP, 1);
3424                 RT_TRACE(COMP_RESET, "Reset finished!! ====>[%d]\n", priv->reset_count);
3425         }
3426 }
3427
3428 void CAM_read_entry(struct net_device *dev, u32 iIndex)
3429 {
3430         u32 target_command = 0;
3431         u32 target_content = 0;
3432         u8 entry_i = 0;
3433         u32 ulStatus;
3434         s32 i = 100;
3435         for (entry_i = 0; entry_i < CAM_CONTENT_COUNT; entry_i++) {
3436                 // polling bit, and No Write enable, and address
3437                 target_command = entry_i+CAM_CONTENT_COUNT*iIndex;
3438                 target_command = target_command | BIT31;
3439
3440                 //Check polling bit is clear
3441                 while ((i--) >= 0) {
3442                         read_nic_dword(dev, RWCAM, &ulStatus);
3443                         if (ulStatus & BIT31)
3444                                 continue;
3445                         else
3446                                 break;
3447                 }
3448                 write_nic_dword(dev, RWCAM, target_command);
3449                 RT_TRACE(COMP_SEC, "CAM_read_entry(): WRITE A0: %x \n", target_command);
3450                 read_nic_dword(dev, RCAMO, &target_content);
3451                 RT_TRACE(COMP_SEC, "CAM_read_entry(): WRITE A8: %x \n", target_content);
3452         }
3453         printk("\n");
3454 }
3455
3456 static void rtl819x_update_rxcounts(struct r8192_priv *priv, u32 *TotalRxBcnNum,
3457                              u32 *TotalRxDataNum)
3458 {
3459         u16                     SlotIndex;
3460         u8                      i;
3461
3462         *TotalRxBcnNum = 0;
3463         *TotalRxDataNum = 0;
3464
3465         SlotIndex = (priv->ieee80211->LinkDetectInfo.SlotIndex++)%(priv->ieee80211->LinkDetectInfo.SlotNum);
3466         priv->ieee80211->LinkDetectInfo.RxBcnNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvBcnInPeriod;
3467         priv->ieee80211->LinkDetectInfo.RxDataNum[SlotIndex] = priv->ieee80211->LinkDetectInfo.NumRecvDataInPeriod;
3468         for (i = 0; i < priv->ieee80211->LinkDetectInfo.SlotNum; i++) {
3469                 *TotalRxBcnNum += priv->ieee80211->LinkDetectInfo.RxBcnNum[i];
3470                 *TotalRxDataNum += priv->ieee80211->LinkDetectInfo.RxDataNum[i];
3471         }
3472 }
3473
3474
3475 void rtl819x_watchdog_wqcallback(struct work_struct *work)
3476 {
3477         struct delayed_work *dwork = container_of(work, struct delayed_work, work);
3478         struct r8192_priv *priv = container_of(dwork, struct r8192_priv, watch_dog_wq);
3479         struct net_device *dev = priv->ieee80211->dev;
3480         struct ieee80211_device *ieee = priv->ieee80211;
3481         RESET_TYPE      ResetType = RESET_TYPE_NORESET;
3482         static u8       check_reset_cnt;
3483         bool bBusyTraffic = false;
3484         u32     TotalRxBcnNum = 0;
3485         u32     TotalRxDataNum = 0;
3486
3487         if (!priv->up)
3488                 return;
3489         hal_dm_watchdog(dev);
3490
3491         //to get busy traffic condition
3492         if (ieee->state == IEEE80211_LINKED) {
3493                 if (ieee->LinkDetectInfo.NumRxOkInPeriod > 666 ||
3494                     ieee->LinkDetectInfo.NumTxOkInPeriod > 666 ) {
3495                         bBusyTraffic = true;
3496                 }
3497                 ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
3498                 ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
3499                 ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
3500         }
3501         //added by amy for AP roaming
3502         if (priv->ieee80211->state == IEEE80211_LINKED && priv->ieee80211->iw_mode == IW_MODE_INFRA) {
3503
3504                 rtl819x_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
3505                 if ((TotalRxBcnNum+TotalRxDataNum) == 0) {
3506 #ifdef TODO
3507                         if (rfState == eRfOff)
3508                                 RT_TRACE(COMP_ERR, "========>%s()\n", __func__);
3509 #endif
3510                         netdev_dbg(dev, "===>%s(): AP is power off, connect another one\n", __func__);
3511                         priv->ieee80211->state = IEEE80211_ASSOCIATING;
3512                         notify_wx_assoc_event(priv->ieee80211);
3513                         RemovePeerTS(priv->ieee80211, priv->ieee80211->current_network.bssid);
3514                         priv->ieee80211->link_change(dev);
3515                         queue_work(priv->ieee80211->wq, &priv->ieee80211->associate_procedure_wq);
3516
3517                 }
3518         }
3519         priv->ieee80211->LinkDetectInfo.NumRecvBcnInPeriod = 0;
3520         priv->ieee80211->LinkDetectInfo.NumRecvDataInPeriod = 0;
3521         //check if reset the driver
3522         if (check_reset_cnt++ >= 3) {
3523                 ResetType = rtl819x_ifcheck_resetornot(dev);
3524                 check_reset_cnt = 3;
3525         }
3526         if ((priv->force_reset) || (priv->ResetProgress == RESET_TYPE_NORESET &&
3527             (priv->bForcedSilentReset ||
3528             (!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_SILENT)))) { // This is control by OID set in Pomelo
3529                 RT_TRACE(COMP_RESET, "%s():priv->force_reset is %d,priv->ResetProgress is %d, priv->bForcedSilentReset is %d,priv->bDisableNormalResetCheck is %d,ResetType is %d\n", __func__, priv->force_reset, priv->ResetProgress, priv->bForcedSilentReset, priv->bDisableNormalResetCheck, ResetType);
3530                 rtl819x_ifsilentreset(dev);
3531         }
3532         priv->force_reset = false;
3533         priv->bForcedSilentReset = false;
3534         priv->bResetInProgress = false;
3535         RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
3536
3537 }
3538
3539 void watch_dog_timer_callback(unsigned long data)
3540 {
3541         struct r8192_priv *priv = ieee80211_priv((struct net_device *) data);
3542         queue_delayed_work(priv->priv_wq, &priv->watch_dog_wq, 0);
3543         mod_timer(&priv->watch_dog_timer, jiffies + MSECS(IEEE80211_WATCH_DOG_TIME));
3544 }
3545 int _rtl8192_up(struct net_device *dev)
3546 {
3547         struct r8192_priv *priv = ieee80211_priv(dev);
3548         int init_status = 0;
3549         priv->up = 1;
3550         priv->ieee80211->ieee_up = 1;
3551         RT_TRACE(COMP_INIT, "Bringing up iface");
3552         init_status = rtl8192_adapter_start(dev);
3553         if (!init_status) {
3554                 RT_TRACE(COMP_ERR, "ERR!!! %s(): initialization failed!\n", __func__);
3555                 priv->up = priv->ieee80211->ieee_up = 0;
3556                 return -EAGAIN;
3557         }
3558         RT_TRACE(COMP_INIT, "start adapter finished\n");
3559         rtl8192_rx_enable(dev);
3560         if (priv->ieee80211->state != IEEE80211_LINKED)
3561                 ieee80211_softmac_start_protocol(priv->ieee80211);
3562         ieee80211_reset_queue(priv->ieee80211);
3563         watch_dog_timer_callback((unsigned long) dev);
3564         if (!netif_queue_stopped(dev))
3565                 netif_start_queue(dev);
3566         else
3567                 netif_wake_queue(dev);
3568
3569         return 0;
3570 }
3571
3572
3573 static int rtl8192_open(struct net_device *dev)
3574 {
3575         struct r8192_priv *priv = ieee80211_priv(dev);
3576         int ret;
3577         down(&priv->wx_sem);
3578         ret = rtl8192_up(dev);
3579         up(&priv->wx_sem);
3580         return ret;
3581
3582 }
3583
3584
3585 int rtl8192_up(struct net_device *dev)
3586 {
3587         struct r8192_priv *priv = ieee80211_priv(dev);
3588
3589         if (priv->up == 1) return -1;
3590
3591         return _rtl8192_up(dev);
3592 }
3593
3594
3595 int rtl8192_close(struct net_device *dev)
3596 {
3597         struct r8192_priv *priv = ieee80211_priv(dev);
3598         int ret;
3599
3600         down(&priv->wx_sem);
3601
3602         ret = rtl8192_down(dev);
3603
3604         up(&priv->wx_sem);
3605
3606         return ret;
3607
3608 }
3609
3610 int rtl8192_down(struct net_device *dev)
3611 {
3612         struct r8192_priv *priv = ieee80211_priv(dev);
3613         int i;
3614
3615         if (priv->up == 0) return -1;
3616
3617         priv->up = 0;
3618         priv->ieee80211->ieee_up = 0;
3619         RT_TRACE(COMP_DOWN, "==========>%s()\n", __func__);
3620         /* FIXME */
3621         if (!netif_queue_stopped(dev))
3622                 netif_stop_queue(dev);
3623
3624         rtl8192_rtx_disable(dev);
3625
3626         /* Tx related queue release */
3627         for (i = 0; i < MAX_QUEUE_SIZE; i++)
3628                 skb_queue_purge(&priv->ieee80211->skb_waitQ[i]);
3629         for (i = 0; i < MAX_QUEUE_SIZE; i++)
3630                 skb_queue_purge(&priv->ieee80211->skb_aggQ[i]);
3631
3632         for (i = 0; i < MAX_QUEUE_SIZE; i++)
3633                 skb_queue_purge(&priv->ieee80211->skb_drv_aggQ[i]);
3634
3635         //as cancel_delayed_work will del work->timer, so if work is not defined as struct delayed_work, it will corrupt
3636         rtl8192_cancel_deferred_work(priv);
3637         deinit_hal_dm(dev);
3638         del_timer_sync(&priv->watch_dog_timer);
3639
3640
3641         ieee80211_softmac_stop_protocol(priv->ieee80211);
3642         memset(&priv->ieee80211->current_network, 0, offsetof(struct ieee80211_network, list));
3643         RT_TRACE(COMP_DOWN, "<==========%s()\n", __func__);
3644
3645         return 0;
3646 }
3647
3648
3649 void rtl8192_commit(struct net_device *dev)
3650 {
3651         struct r8192_priv *priv = ieee80211_priv(dev);
3652         int reset_status = 0;
3653         if (priv->up == 0) return;
3654         priv->up = 0;
3655
3656         rtl8192_cancel_deferred_work(priv);
3657         del_timer_sync(&priv->watch_dog_timer);
3658
3659         ieee80211_softmac_stop_protocol(priv->ieee80211);
3660
3661         rtl8192_rtx_disable(dev);
3662         reset_status = _rtl8192_up(dev);
3663
3664 }
3665
3666 void rtl8192_restart(struct work_struct *work)
3667 {
3668         struct r8192_priv *priv = container_of(work, struct r8192_priv, reset_wq);
3669         struct net_device *dev = priv->ieee80211->dev;
3670
3671         down(&priv->wx_sem);
3672
3673         rtl8192_commit(dev);
3674
3675         up(&priv->wx_sem);
3676 }
3677
3678 static void r8192_set_multicast(struct net_device *dev)
3679 {
3680         struct r8192_priv *priv = ieee80211_priv(dev);
3681         short promisc;
3682
3683         /* FIXME FIXME */
3684
3685         promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
3686
3687         if (promisc != priv->promisc)
3688
3689                 priv->promisc = promisc;
3690 }
3691
3692
3693 static int r8192_set_mac_adr(struct net_device *dev, void *mac)
3694 {
3695         struct r8192_priv *priv = ieee80211_priv(dev);
3696         struct sockaddr *addr = mac;
3697
3698         down(&priv->wx_sem);
3699
3700         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
3701
3702         schedule_work(&priv->reset_wq);
3703         up(&priv->wx_sem);
3704
3705         return 0;
3706 }
3707
3708 /* based on ipw2200 driver */
3709 static int rtl8192_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3710 {
3711         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3712         struct iwreq *wrq = (struct iwreq *)rq;
3713         int ret = -1;
3714         struct ieee80211_device *ieee = priv->ieee80211;
3715         u32 key[4];
3716         u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3717         struct iw_point *p = &wrq->u.data;
3718         struct ieee_param *ipw = NULL;
3719
3720         down(&priv->wx_sem);
3721
3722
3723         if (p->length < sizeof(struct ieee_param) || !p->pointer) {
3724                 ret = -EINVAL;
3725                 goto out;
3726         }
3727
3728         ipw = memdup_user(p->pointer, p->length);
3729         if (IS_ERR(ipw)) {
3730                 ret = PTR_ERR(ipw);
3731                 goto out;
3732         }
3733
3734         switch (cmd) {
3735         case RTL_IOCTL_WPA_SUPPLICANT:
3736                 //parse here for HW security
3737                 if (ipw->cmd == IEEE_CMD_SET_ENCRYPTION) {
3738                         if (ipw->u.crypt.set_tx) {
3739                                 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0) {
3740                                         ieee->pairwise_key_type = KEY_TYPE_CCMP;
3741                                 } else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0) {
3742                                         ieee->pairwise_key_type = KEY_TYPE_TKIP;
3743                                 } else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
3744                                         if (ipw->u.crypt.key_len == 13)
3745                                                 ieee->pairwise_key_type = KEY_TYPE_WEP104;
3746                                         else if (ipw->u.crypt.key_len == 5)
3747                                                 ieee->pairwise_key_type = KEY_TYPE_WEP40;
3748                                 } else {
3749                                         ieee->pairwise_key_type = KEY_TYPE_NA;
3750                                 }
3751
3752                                 if (ieee->pairwise_key_type) {
3753                                         memcpy((u8 *)key, ipw->u.crypt.key, 16);
3754                                         EnableHWSecurityConfig8192(dev);
3755                                         //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!
3756                                         //added by WB.
3757                                         setKey(dev, 4, ipw->u.crypt.idx, ieee->pairwise_key_type, (u8 *)ieee->ap_mac_addr, 0, key);
3758                                         if (ieee->auth_mode != 2)
3759                                                 setKey(dev, ipw->u.crypt.idx, ipw->u.crypt.idx, ieee->pairwise_key_type, (u8 *)ieee->ap_mac_addr, 0, key);
3760                                 }
3761                         } else {
3762                                 memcpy((u8 *)key, ipw->u.crypt.key, 16);
3763                                 if (strcmp(ipw->u.crypt.alg, "CCMP") == 0) {
3764                                         ieee->group_key_type = KEY_TYPE_CCMP;
3765                                 } else if (strcmp(ipw->u.crypt.alg, "TKIP") == 0) {
3766                                         ieee->group_key_type = KEY_TYPE_TKIP;
3767                                 } else if (strcmp(ipw->u.crypt.alg, "WEP") == 0) {
3768                                         if (ipw->u.crypt.key_len == 13)
3769                                                 ieee->group_key_type = KEY_TYPE_WEP104;
3770                                         else if (ipw->u.crypt.key_len == 5)
3771                                                 ieee->group_key_type = KEY_TYPE_WEP40;
3772                                 } else {
3773                                         ieee->group_key_type = KEY_TYPE_NA;
3774                                 }
3775
3776                                 if (ieee->group_key_type) {
3777                                         setKey(dev, ipw->u.crypt.idx,
3778                                                ipw->u.crypt.idx,                //KeyIndex
3779                                                ieee->group_key_type,    //KeyType
3780                                                broadcast_addr,  //MacAddr
3781                                                0,               //DefaultKey
3782                                                key);            //KeyContent
3783                                 }
3784                         }
3785                 }
3786                 ret = ieee80211_wpa_supplicant_ioctl(priv->ieee80211, &wrq->u.data);
3787                 break;
3788
3789         default:
3790                 ret = -EOPNOTSUPP;
3791                 break;
3792         }
3793         kfree(ipw);
3794         ipw = NULL;
3795 out:
3796         up(&priv->wx_sem);
3797         return ret;
3798 }
3799
3800 static u8 HwRateToMRate90(bool bIsHT, u8 rate)
3801 {
3802         u8  ret_rate = 0xff;
3803
3804         if (!bIsHT) {
3805                 switch (rate) {
3806                 case DESC90_RATE1M:   ret_rate = MGN_1M;         break;
3807                 case DESC90_RATE2M:   ret_rate = MGN_2M;         break;
3808                 case DESC90_RATE5_5M: ret_rate = MGN_5_5M;       break;
3809                 case DESC90_RATE11M:  ret_rate = MGN_11M;        break;
3810                 case DESC90_RATE6M:   ret_rate = MGN_6M;         break;
3811                 case DESC90_RATE9M:   ret_rate = MGN_9M;         break;
3812                 case DESC90_RATE12M:  ret_rate = MGN_12M;        break;
3813                 case DESC90_RATE18M:  ret_rate = MGN_18M;        break;
3814                 case DESC90_RATE24M:  ret_rate = MGN_24M;        break;
3815                 case DESC90_RATE36M:  ret_rate = MGN_36M;        break;
3816                 case DESC90_RATE48M:  ret_rate = MGN_48M;        break;
3817                 case DESC90_RATE54M:  ret_rate = MGN_54M;        break;
3818
3819                 default:
3820                         ret_rate = 0xff;
3821                         RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n", rate, bIsHT);
3822                         break;
3823                 }
3824
3825         } else {
3826                 switch (rate) {
3827                 case DESC90_RATEMCS0:   ret_rate = MGN_MCS0;    break;
3828                 case DESC90_RATEMCS1:   ret_rate = MGN_MCS1;    break;
3829                 case DESC90_RATEMCS2:   ret_rate = MGN_MCS2;    break;
3830                 case DESC90_RATEMCS3:   ret_rate = MGN_MCS3;    break;
3831                 case DESC90_RATEMCS4:   ret_rate = MGN_MCS4;    break;
3832                 case DESC90_RATEMCS5:   ret_rate = MGN_MCS5;    break;
3833                 case DESC90_RATEMCS6:   ret_rate = MGN_MCS6;    break;
3834                 case DESC90_RATEMCS7:   ret_rate = MGN_MCS7;    break;
3835                 case DESC90_RATEMCS8:   ret_rate = MGN_MCS8;    break;
3836                 case DESC90_RATEMCS9:   ret_rate = MGN_MCS9;    break;
3837                 case DESC90_RATEMCS10:  ret_rate = MGN_MCS10;   break;
3838                 case DESC90_RATEMCS11:  ret_rate = MGN_MCS11;   break;
3839                 case DESC90_RATEMCS12:  ret_rate = MGN_MCS12;   break;
3840                 case DESC90_RATEMCS13:  ret_rate = MGN_MCS13;   break;
3841                 case DESC90_RATEMCS14:  ret_rate = MGN_MCS14;   break;
3842                 case DESC90_RATEMCS15:  ret_rate = MGN_MCS15;   break;
3843                 case DESC90_RATEMCS32:  ret_rate = (0x80|0x20); break;
3844
3845                 default:
3846                         ret_rate = 0xff;
3847                         RT_TRACE(COMP_RECV, "HwRateToMRate90(): Non supported Rate [%x], bIsHT = %d!!!\n", rate, bIsHT);
3848                         break;
3849                 }
3850         }
3851
3852         return ret_rate;
3853 }
3854
3855 /**
3856  * Function:     UpdateRxPktTimeStamp
3857  * Overview:     Record the TSF time stamp when receiving a packet
3858  *
3859  * Input:
3860  *       PADAPTER        Adapter
3861  *       PRT_RFD         pRfd,
3862  *
3863  * Output:
3864  *       PRT_RFD         pRfd
3865  *                               (pRfd->Status.TimeStampHigh is updated)
3866  *                               (pRfd->Status.TimeStampLow is updated)
3867  * Return:
3868  *               None
3869  */
3870 static void UpdateRxPktTimeStamp8190(struct net_device *dev,
3871                                      struct ieee80211_rx_stats *stats)
3872 {
3873         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
3874
3875         if (stats->bIsAMPDU && !stats->bFirstMPDU) {
3876                 stats->mac_time[0] = priv->LastRxDescTSFLow;
3877                 stats->mac_time[1] = priv->LastRxDescTSFHigh;
3878         } else {
3879                 priv->LastRxDescTSFLow = stats->mac_time[0];
3880                 priv->LastRxDescTSFHigh = stats->mac_time[1];
3881         }
3882 }
3883
3884 //by amy 080606
3885
3886 static long rtl819x_translate_todbm(u8 signal_strength_index)// 0-100 index.
3887 {
3888         long    signal_power; // in dBm.
3889
3890         // Translate to dBm (x=0.5y-95).
3891         signal_power = (long)((signal_strength_index + 1) >> 1);
3892         signal_power -= 95;
3893
3894         return signal_power;
3895 }
3896
3897
3898 /* 2008/01/22 MH We can not declare RSSI/EVM total value of sliding window to
3899     be a local static. Otherwise, it may increase when we return from S3/S4. The
3900     value will be kept in memory or disk. Declare the value in the adaptor
3901     and it will be reinitialized when returned from S3/S4. */
3902 static void rtl8192_process_phyinfo(struct r8192_priv *priv, u8 *buffer,
3903                                     struct ieee80211_rx_stats *pprevious_stats,
3904                                     struct ieee80211_rx_stats *pcurrent_stats)
3905 {
3906         bool bcheck = false;
3907         u8      rfpath;
3908         u32     nspatial_stream, tmp_val;
3909         static u32 slide_rssi_index, slide_rssi_statistics;
3910         static u32 slide_evm_index, slide_evm_statistics;
3911         static u32 last_rssi, last_evm;
3912
3913         static u32 slide_beacon_adc_pwdb_index, slide_beacon_adc_pwdb_statistics;
3914         static u32 last_beacon_adc_pwdb;
3915
3916         struct ieee80211_hdr_3addr *hdr;
3917         u16 sc;
3918         unsigned int frag, seq;
3919         hdr = (struct ieee80211_hdr_3addr *)buffer;
3920         sc = le16_to_cpu(hdr->seq_ctl);
3921         frag = WLAN_GET_SEQ_FRAG(sc);
3922         seq = WLAN_GET_SEQ_SEQ(sc);
3923         //cosa add 04292008 to record the sequence number
3924         pcurrent_stats->Seq_Num = seq;
3925         //
3926         // Check whether we should take the previous packet into accounting
3927         //
3928         if (!pprevious_stats->bIsAMPDU) {
3929                 // if previous packet is not aggregated packet
3930                 bcheck = true;
3931         }
3932
3933         if (slide_rssi_statistics++ >= PHY_RSSI_SLID_WIN_MAX) {
3934                 slide_rssi_statistics = PHY_RSSI_SLID_WIN_MAX;
3935                 last_rssi = priv->stats.slide_signal_strength[slide_rssi_index];
3936                 priv->stats.slide_rssi_total -= last_rssi;
3937         }
3938         priv->stats.slide_rssi_total += pprevious_stats->SignalStrength;
3939
3940         priv->stats.slide_signal_strength[slide_rssi_index++] = pprevious_stats->SignalStrength;
3941         if (slide_rssi_index >= PHY_RSSI_SLID_WIN_MAX)
3942                 slide_rssi_index = 0;
3943
3944         // <1> Showed on UI for user, in dbm
3945         tmp_val = priv->stats.slide_rssi_total/slide_rssi_statistics;
3946         priv->stats.signal_strength = rtl819x_translate_todbm((u8)tmp_val);
3947         pcurrent_stats->rssi = priv->stats.signal_strength;
3948         //
3949         // If the previous packet does not match the criteria, neglect it
3950         //
3951         if (!pprevious_stats->bPacketMatchBSSID) {
3952                 if (!pprevious_stats->bToSelfBA)
3953                         return;
3954         }
3955
3956         if (!bcheck)
3957                 return;
3958
3959
3960         //rtl8190_process_cck_rxpathsel(priv,pprevious_stats);//only rtl8190 supported
3961
3962         //
3963         // Check RSSI
3964         //
3965         priv->stats.num_process_phyinfo++;
3966
3967         /* record the general signal strength to the sliding window. */
3968
3969
3970         // <2> Showed on UI for engineering
3971         // hardware does not provide rssi information for each rf path in CCK
3972         if (!pprevious_stats->bIsCCK && (pprevious_stats->bPacketToSelf || pprevious_stats->bToSelfBA)) {
3973                 for (rfpath = RF90_PATH_A; rfpath < priv->NumTotalRFPath; rfpath++) {
3974                         if (!rtl8192_phy_CheckIsLegalRFPath(priv->ieee80211->dev, rfpath))
3975                                 continue;
3976
3977                         //Fixed by Jacken 2008-03-20
3978                         if (priv->stats.rx_rssi_percentage[rfpath] == 0)
3979                                 priv->stats.rx_rssi_percentage[rfpath] = pprevious_stats->RxMIMOSignalStrength[rfpath];
3980                         if (pprevious_stats->RxMIMOSignalStrength[rfpath]  > priv->stats.rx_rssi_percentage[rfpath]) {
3981                                 priv->stats.rx_rssi_percentage[rfpath] =
3982                                         ((priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
3983                                          (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
3984                                 priv->stats.rx_rssi_percentage[rfpath] = priv->stats.rx_rssi_percentage[rfpath]  + 1;
3985                         } else {
3986                                 priv->stats.rx_rssi_percentage[rfpath] =
3987                                         ((priv->stats.rx_rssi_percentage[rfpath]*(Rx_Smooth_Factor-1)) +
3988                                          (pprevious_stats->RxMIMOSignalStrength[rfpath])) /(Rx_Smooth_Factor);
3989                         }
3990                         RT_TRACE(COMP_DBG, "priv->stats.rx_rssi_percentage[rfPath]  = %d \n", priv->stats.rx_rssi_percentage[rfpath]);
3991                 }
3992         }
3993
3994
3995         //
3996         // Check PWDB.
3997         //
3998         RT_TRACE(COMP_RXDESC, "Smooth %s PWDB = %d\n",
3999                  pprevious_stats->bIsCCK ? "CCK" : "OFDM",
4000                  pprevious_stats->RxPWDBAll);
4001
4002         if (pprevious_stats->bPacketBeacon) {
4003                 /* record the beacon pwdb to the sliding window. */
4004                 if (slide_beacon_adc_pwdb_statistics++ >= PHY_Beacon_RSSI_SLID_WIN_MAX) {
4005                         slide_beacon_adc_pwdb_statistics = PHY_Beacon_RSSI_SLID_WIN_MAX;
4006                         last_beacon_adc_pwdb = priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index];
4007                         priv->stats.Slide_Beacon_Total -= last_beacon_adc_pwdb;
4008                 }
4009                 priv->stats.Slide_Beacon_Total += pprevious_stats->RxPWDBAll;
4010                 priv->stats.Slide_Beacon_pwdb[slide_beacon_adc_pwdb_index] = pprevious_stats->RxPWDBAll;
4011                 slide_beacon_adc_pwdb_index++;
4012                 if (slide_beacon_adc_pwdb_index >= PHY_Beacon_RSSI_SLID_WIN_MAX)
4013                         slide_beacon_adc_pwdb_index = 0;
4014                 pprevious_stats->RxPWDBAll = priv->stats.Slide_Beacon_Total/slide_beacon_adc_pwdb_statistics;
4015                 if (pprevious_stats->RxPWDBAll >= 3)
4016                         pprevious_stats->RxPWDBAll -= 3;
4017         }
4018
4019         RT_TRACE(COMP_RXDESC, "Smooth %s PWDB = %d\n",
4020                  pprevious_stats->bIsCCK ? "CCK" : "OFDM",
4021                  pprevious_stats->RxPWDBAll);
4022
4023
4024         if (pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA) {
4025                 if (priv->undecorated_smoothed_pwdb < 0)        // initialize
4026                         priv->undecorated_smoothed_pwdb = pprevious_stats->RxPWDBAll;
4027                 if (pprevious_stats->RxPWDBAll > (u32)priv->undecorated_smoothed_pwdb) {
4028                         priv->undecorated_smoothed_pwdb =
4029                                 (((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
4030                                  (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
4031                         priv->undecorated_smoothed_pwdb = priv->undecorated_smoothed_pwdb + 1;
4032                 } else {
4033                         priv->undecorated_smoothed_pwdb =
4034                                 (((priv->undecorated_smoothed_pwdb)*(Rx_Smooth_Factor-1)) +
4035                                  (pprevious_stats->RxPWDBAll)) /(Rx_Smooth_Factor);
4036                 }
4037
4038         }
4039
4040         //
4041         // Check EVM
4042         //
4043         /* record the general EVM to the sliding window. */
4044         if (pprevious_stats->SignalQuality) {
4045                 if (pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA) {
4046                         if (slide_evm_statistics++ >= PHY_RSSI_SLID_WIN_MAX) {
4047                                 slide_evm_statistics = PHY_RSSI_SLID_WIN_MAX;
4048                                 last_evm = priv->stats.slide_evm[slide_evm_index];
4049                                 priv->stats.slide_evm_total -= last_evm;
4050                         }
4051
4052                         priv->stats.slide_evm_total += pprevious_stats->SignalQuality;
4053
4054                         priv->stats.slide_evm[slide_evm_index++] = pprevious_stats->SignalQuality;
4055                         if (slide_evm_index >= PHY_RSSI_SLID_WIN_MAX)
4056                                 slide_evm_index = 0;
4057
4058                         // <1> Showed on UI for user, in percentage.
4059                         tmp_val = priv->stats.slide_evm_total/slide_evm_statistics;
4060                         priv->stats.signal_quality = tmp_val;
4061                         //cosa add 10/11/2007, Showed on UI for user in Windows Vista, for Link quality.
4062                         priv->stats.last_signal_strength_inpercent = tmp_val;
4063                 }
4064
4065                 // <2> Showed on UI for engineering
4066                 if (pprevious_stats->bPacketToSelf || pprevious_stats->bPacketBeacon || pprevious_stats->bToSelfBA) {
4067                         for (nspatial_stream = 0; nspatial_stream < 2; nspatial_stream++) { // 2 spatial stream
4068                                 if (pprevious_stats->RxMIMOSignalQuality[nspatial_stream] != -1) {
4069                                         if (priv->stats.rx_evm_percentage[nspatial_stream] == 0) // initialize
4070                                                 priv->stats.rx_evm_percentage[nspatial_stream] = pprevious_stats->RxMIMOSignalQuality[nspatial_stream];
4071                                         priv->stats.rx_evm_percentage[nspatial_stream] =
4072                                                 ((priv->stats.rx_evm_percentage[nspatial_stream]* (Rx_Smooth_Factor-1)) +
4073                                                  (pprevious_stats->RxMIMOSignalQuality[nspatial_stream]* 1)) / (Rx_Smooth_Factor);
4074                                 }
4075                         }
4076                 }
4077         }
4078
4079
4080 }
4081
4082 /*-----------------------------------------------------------------------------
4083  * Function:    rtl819x_query_rxpwrpercentage()
4084  *
4085  * Overview:
4086  *
4087  * Input:               char            antpower
4088  *
4089  * Output:              NONE
4090  *
4091  * Return:              0-100 percentage
4092  *
4093  * Revised History:
4094  *      When            Who             Remark
4095  *      05/26/2008      amy             Create Version 0 porting from windows code.
4096  *
4097  *---------------------------------------------------------------------------*/
4098 static u8 rtl819x_query_rxpwrpercentage(char antpower)
4099 {
4100         if ((antpower <= -100) || (antpower >= 20))
4101                 return  0;
4102         else if (antpower >= 0)
4103                 return  100;
4104         else
4105                 return  100 + antpower;
4106
4107 }       /* QueryRxPwrPercentage */
4108
4109 static u8 rtl819x_evm_dbtopercentage(char value)
4110 {
4111         char ret_val;
4112
4113         ret_val = value;
4114
4115         if (ret_val >= 0)
4116                 ret_val = 0;
4117         if (ret_val <= -33)
4118                 ret_val = -33;
4119         ret_val = 0 - ret_val;
4120         ret_val *= 3;
4121         if (ret_val == 99)
4122                 ret_val = 100;
4123         return ret_val;
4124 }
4125 //
4126 //      Description:
4127 //      We want good-looking for signal strength/quality
4128 //      2007/7/19 01:09, by cosa.
4129 //
4130 static long rtl819x_signal_scale_mapping(long currsig)
4131 {
4132         long retsig;
4133
4134         // Step 1. Scale mapping.
4135         if (currsig >= 61 && currsig <= 100)
4136                 retsig = 90 + ((currsig - 60) / 4);
4137         else if (currsig >= 41 && currsig <= 60)
4138                 retsig = 78 + ((currsig - 40) / 2);
4139         else if (currsig >= 31 && currsig <= 40)
4140                 retsig = 66 + (currsig - 30);
4141         else if (currsig >= 21 && currsig <= 30)
4142                 retsig = 54 + (currsig - 20);
4143         else if (currsig >= 5 && currsig <= 20)
4144                 retsig = 42 + (((currsig - 5) * 2) / 3);
4145         else if (currsig == 4)
4146                 retsig = 36;
4147         else if (currsig == 3)
4148                 retsig = 27;
4149         else if (currsig == 2)
4150                 retsig = 18;
4151         else if (currsig == 1)
4152                 retsig = 9;
4153         else
4154                 retsig = currsig;
4155
4156         return retsig;
4157 }
4158
4159 static inline bool rx_hal_is_cck_rate(struct rx_drvinfo_819x_usb *pdrvinfo)
4160 {
4161         if (pdrvinfo->RxHT)
4162                 return false;
4163
4164         switch (pdrvinfo->RxRate) {
4165         case DESC90_RATE1M:
4166         case DESC90_RATE2M:
4167         case DESC90_RATE5_5M:
4168         case DESC90_RATE11M:
4169                 return true;
4170         default:
4171                 return false;
4172         }
4173 }
4174
4175 static void rtl8192_query_rxphystatus(struct r8192_priv *priv,
4176                                       struct ieee80211_rx_stats *pstats,
4177                                       rx_drvinfo_819x_usb  *pdrvinfo,
4178                                       struct ieee80211_rx_stats *precord_stats,
4179                                       bool bpacket_match_bssid,
4180                                       bool bpacket_toself,
4181                                       bool bPacketBeacon,
4182                                       bool bToSelfBA)
4183 {
4184         phy_sts_ofdm_819xusb_t *pofdm_buf;
4185         phy_sts_cck_819xusb_t   *pcck_buf;
4186         phy_ofdm_rx_status_rxsc_sgien_exintfflag *prxsc;
4187         u8                              *prxpkt;
4188         u8                              i, max_spatial_stream, tmp_rxsnr, tmp_rxevm, rxsc_sgien_exflg;
4189         char                            rx_pwr[4], rx_pwr_all = 0;
4190         char                            rx_snrX, rx_evmX;
4191         u8                              evm, pwdb_all;
4192         u32                             RSSI, total_rssi = 0;
4193         u8                              is_cck_rate = 0;
4194         u8                              rf_rx_num = 0;
4195         u8                              sq;
4196
4197
4198         priv->stats.numqry_phystatus++;
4199
4200         is_cck_rate = rx_hal_is_cck_rate(pdrvinfo);
4201
4202         // Record it for next packet processing
4203         memset(precord_stats, 0, sizeof(struct ieee80211_rx_stats));
4204         pstats->bPacketMatchBSSID = precord_stats->bPacketMatchBSSID = bpacket_match_bssid;
4205         pstats->bPacketToSelf = precord_stats->bPacketToSelf = bpacket_toself;
4206         pstats->bIsCCK = precord_stats->bIsCCK = is_cck_rate;
4207         pstats->bPacketBeacon = precord_stats->bPacketBeacon = bPacketBeacon;
4208         pstats->bToSelfBA = precord_stats->bToSelfBA = bToSelfBA;
4209
4210         prxpkt = (u8 *)pdrvinfo;
4211
4212         /* Move pointer to the 16th bytes. Phy status start address. */
4213         prxpkt += sizeof(rx_drvinfo_819x_usb);
4214
4215         /* Initial the cck and ofdm buffer pointer */
4216         pcck_buf = (phy_sts_cck_819xusb_t *)prxpkt;
4217         pofdm_buf = (phy_sts_ofdm_819xusb_t *)prxpkt;
4218
4219         pstats->RxMIMOSignalQuality[0] = -1;
4220         pstats->RxMIMOSignalQuality[1] = -1;
4221         precord_stats->RxMIMOSignalQuality[0] = -1;
4222         precord_stats->RxMIMOSignalQuality[1] = -1;
4223
4224         if (is_cck_rate) {
4225                 //
4226                 // (1)Hardware does not provide RSSI for CCK
4227                 //
4228
4229                 //
4230                 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
4231                 //
4232                 u8 report;
4233
4234                 priv->stats.numqry_phystatusCCK++;
4235
4236                 if (!priv->bCckHighPower) {
4237                         report = pcck_buf->cck_agc_rpt & 0xc0;
4238                         report = report>>6;
4239                         switch (report) {
4240                                 //Fixed by Jacken from Bryant 2008-03-20
4241                                 //Original value is -38 , -26 , -14 , -2
4242                                 //Fixed value is -35 , -23 , -11 , 6
4243                         case 0x3:
4244                                 rx_pwr_all = -35 - (pcck_buf->cck_agc_rpt & 0x3e);
4245                                 break;
4246                         case 0x2:
4247                                 rx_pwr_all = -23 - (pcck_buf->cck_agc_rpt & 0x3e);
4248                                 break;
4249                         case 0x1:
4250                                 rx_pwr_all = -11 - (pcck_buf->cck_agc_rpt & 0x3e);
4251                                 break;
4252                         case 0x0:
4253                                 rx_pwr_all = 6 - (pcck_buf->cck_agc_rpt & 0x3e);
4254                                 break;
4255                         }
4256                 } else {
4257                         report = pcck_buf->cck_agc_rpt & 0x60;
4258                         report = report>>5;
4259                         switch (report) {
4260                         case 0x3:
4261                                 rx_pwr_all = -35 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1);
4262                                 break;
4263                         case 0x2:
4264                                 rx_pwr_all = -23 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1);
4265                                 break;
4266                         case 0x1:
4267                                 rx_pwr_all = -11 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1);
4268                                 break;
4269                         case 0x0:
4270                                 rx_pwr_all = 6 - ((pcck_buf->cck_agc_rpt & 0x1f)<<1);
4271                                 break;
4272                         }
4273                 }
4274
4275                 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
4276                 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
4277                 pstats->RecvSignalPower = pwdb_all;
4278
4279                 //
4280                 // (3) Get Signal Quality (EVM)
4281                 //
4282
4283                 if (pstats->RxPWDBAll > 40) {
4284                         sq = 100;
4285                 } else {
4286                         sq = pcck_buf->sq_rpt;
4287
4288                         if (pcck_buf->sq_rpt > 64)
4289                                 sq = 0;
4290                         else if (pcck_buf->sq_rpt < 20)
4291                                 sq = 100;
4292                         else
4293                                 sq = ((64-sq) * 100) / 44;
4294                 }
4295                 pstats->SignalQuality = precord_stats->SignalQuality = sq;
4296                 pstats->RxMIMOSignalQuality[0] = precord_stats->RxMIMOSignalQuality[0] = sq;
4297                 pstats->RxMIMOSignalQuality[1] = precord_stats->RxMIMOSignalQuality[1] = -1;
4298
4299         } else {
4300                 priv->stats.numqry_phystatusHT++;
4301                 //
4302                 // (1)Get RSSI for HT rate
4303                 //
4304                 for (i = RF90_PATH_A; i < priv->NumTotalRFPath; i++) {
4305                         // 2008/01/30 MH we will judge RF RX path now.
4306                         if (priv->brfpath_rxenable[i])
4307                                 rf_rx_num++;
4308                         else
4309                                 continue;
4310
4311                         if (!rtl8192_phy_CheckIsLegalRFPath(priv->ieee80211->dev, i))
4312                                 continue;
4313
4314                         //Fixed by Jacken from Bryant 2008-03-20
4315                         //Original value is 106
4316                         rx_pwr[i] = ((pofdm_buf->trsw_gain_X[i]&0x3F)*2) - 106;
4317
4318                         //Get Rx snr value in DB
4319                         tmp_rxsnr =     pofdm_buf->rxsnr_X[i];
4320                         rx_snrX = (char)(tmp_rxsnr);
4321                         rx_snrX /= 2;
4322                         priv->stats.rxSNRdB[i] = (long)rx_snrX;
4323
4324                         /* Translate DBM to percentage. */
4325                         RSSI = rtl819x_query_rxpwrpercentage(rx_pwr[i]);
4326                         total_rssi += RSSI;
4327
4328                         /* Record Signal Strength for next packet */
4329                         pstats->RxMIMOSignalStrength[i] = (u8) RSSI;
4330                         precord_stats->RxMIMOSignalStrength[i] = (u8) RSSI;
4331                 }
4332
4333
4334                 //
4335                 // (2)PWDB, Average PWDB cacluated by hardware (for rate adaptive)
4336                 //
4337                 //Fixed by Jacken from Bryant 2008-03-20
4338                 //Original value is 106
4339                 rx_pwr_all = (((pofdm_buf->pwdb_all) >> 1)& 0x7f) -106;
4340                 pwdb_all = rtl819x_query_rxpwrpercentage(rx_pwr_all);
4341
4342                 pstats->RxPWDBAll = precord_stats->RxPWDBAll = pwdb_all;
4343                 pstats->RxPower = precord_stats->RxPower =  rx_pwr_all;
4344
4345                 //
4346                 // (3)EVM of HT rate
4347                 //
4348                 if (pdrvinfo->RxHT && pdrvinfo->RxRate >= DESC90_RATEMCS8 &&
4349                     pdrvinfo->RxRate <= DESC90_RATEMCS15)
4350                         max_spatial_stream = 2; //both spatial stream make sense
4351                 else
4352                         max_spatial_stream = 1; //only spatial stream 1 makes sense
4353
4354                 for (i = 0; i < max_spatial_stream; i++) {
4355                         tmp_rxevm =     pofdm_buf->rxevm_X[i];
4356                         rx_evmX = (char)(tmp_rxevm);
4357
4358                         // Do not use shift operation like "rx_evmX >>= 1" because the compiler of free build environment
4359                         // will set the most significant bit to "zero" when doing shifting operation which may change a negative
4360                         // value to positive one, then the dbm value (which is supposed to be negative)  is not correct anymore.
4361                         rx_evmX /= 2;   //dbm
4362
4363                         evm = rtl819x_evm_dbtopercentage(rx_evmX);
4364                         if (i == 0) // Fill value in RFD, Get the first spatial stream only
4365                                 pstats->SignalQuality = precord_stats->SignalQuality = (u8)(evm & 0xff);
4366                         pstats->RxMIMOSignalQuality[i] = precord_stats->RxMIMOSignalQuality[i] = (u8)(evm & 0xff);
4367                 }
4368
4369
4370                 /* record rx statistics for debug */
4371                 rxsc_sgien_exflg = pofdm_buf->rxsc_sgien_exflg;
4372                 prxsc = (phy_ofdm_rx_status_rxsc_sgien_exintfflag *)&rxsc_sgien_exflg;
4373                 if (pdrvinfo->BW)       //40M channel
4374                         priv->stats.received_bwtype[1+prxsc->rxsc]++;
4375                 else                            //20M channel
4376                         priv->stats.received_bwtype[0]++;
4377         }
4378
4379         //UI BSS List signal strength(in percentage), make it good looking, from 0~100.
4380         //It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp().
4381         if (is_cck_rate) {
4382                 pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)pwdb_all));
4383         } else {
4384                 // We can judge RX path number now.
4385                 if (rf_rx_num != 0)
4386                         pstats->SignalStrength = precord_stats->SignalStrength = (u8)(rtl819x_signal_scale_mapping((long)(total_rssi /= rf_rx_num)));
4387         }
4388 }       /* QueryRxPhyStatus8190Pci */
4389
4390 static void rtl8192_record_rxdesc_forlateruse(struct ieee80211_rx_stats *psrc_stats,
4391                                               struct ieee80211_rx_stats *ptarget_stats)
4392 {
4393         ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
4394         ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
4395         ptarget_stats->Seq_Num = psrc_stats->Seq_Num;
4396 }
4397
4398
4399 static void TranslateRxSignalStuff819xUsb(struct sk_buff *skb,
4400                                           struct ieee80211_rx_stats *pstats,
4401                                           rx_drvinfo_819x_usb  *pdrvinfo)
4402 {
4403         // TODO: We must only check packet for current MAC address. Not finish
4404         rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
4405         struct net_device *dev = info->dev;
4406         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4407         bool bpacket_match_bssid, bpacket_toself;
4408         bool bPacketBeacon = FALSE, bToSelfBA = FALSE;
4409         static struct ieee80211_rx_stats  previous_stats;
4410         struct ieee80211_hdr_3addr *hdr;//by amy
4411         u16 fc, type;
4412
4413         // Get Signal Quality for only RX data queue (but not command queue)
4414
4415         u8 *tmp_buf;
4416         u8  *praddr;
4417
4418         /* Get MAC frame start address. */
4419         tmp_buf = (u8 *)skb->data;
4420
4421         hdr = (struct ieee80211_hdr_3addr *)tmp_buf;
4422         fc = le16_to_cpu(hdr->frame_ctl);
4423         type = WLAN_FC_GET_TYPE(fc);
4424         praddr = hdr->addr1;
4425
4426         /* Check if the received packet is acceptable. */
4427         bpacket_match_bssid = ((IEEE80211_FTYPE_CTL != type) &&
4428                                (eqMacAddr(priv->ieee80211->current_network.bssid,  (fc & IEEE80211_FCTL_TODS) ? hdr->addr1 : (fc & IEEE80211_FCTL_FROMDS) ? hdr->addr2 : hdr->addr3))
4429                                && (!pstats->bHwError) && (!pstats->bCRC) && (!pstats->bICV));
4430         bpacket_toself =  bpacket_match_bssid & (eqMacAddr(praddr, priv->ieee80211->dev->dev_addr));
4431
4432         if (WLAN_FC_GET_FRAMETYPE(fc) == IEEE80211_STYPE_BEACON)
4433                 bPacketBeacon = true;
4434         if (WLAN_FC_GET_FRAMETYPE(fc) == IEEE80211_STYPE_BLOCKACK) {
4435                 if ((eqMacAddr(praddr, dev->dev_addr)))
4436                         bToSelfBA = true;
4437         }
4438
4439
4440
4441         if (bpacket_match_bssid)
4442                 priv->stats.numpacket_matchbssid++;
4443         if (bpacket_toself)
4444                 priv->stats.numpacket_toself++;
4445         //
4446         // Process PHY information for previous packet (RSSI/PWDB/EVM)
4447         //
4448         // Because phy information is contained in the last packet of AMPDU only, so driver
4449         // should process phy information of previous packet
4450         rtl8192_process_phyinfo(priv, tmp_buf, &previous_stats, pstats);
4451         rtl8192_query_rxphystatus(priv, pstats, pdrvinfo, &previous_stats, bpacket_match_bssid, bpacket_toself, bPacketBeacon, bToSelfBA);
4452         rtl8192_record_rxdesc_forlateruse(pstats, &previous_stats);
4453
4454 }
4455
4456 /**
4457 * Function:     UpdateReceivedRateHistogramStatistics
4458 * Overview:     Record the received data rate
4459 *
4460 * Input:
4461 *       struct net_device *dev
4462 *       struct ieee80211_rx_stats *stats
4463 *
4464 * Output:
4465 *
4466 *                       (priv->stats.ReceivedRateHistogram[] is updated)
4467 * Return:
4468 *               None
4469 */
4470 static void
4471 UpdateReceivedRateHistogramStatistics8190(struct net_device *dev,
4472                                           struct ieee80211_rx_stats *stats)
4473 {
4474         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4475         u32 rcvType = 1;   //0: Total, 1:OK, 2:CRC, 3:ICV
4476         u32 rateIndex;
4477         u32 preamble_guardinterval;  //1: short preamble/GI, 0: long preamble/GI
4478
4479
4480         if (stats->bCRC)
4481                 rcvType = 2;
4482         else if (stats->bICV)
4483                 rcvType = 3;
4484
4485         if (stats->bShortPreamble)
4486                 preamble_guardinterval = 1;// short
4487         else
4488                 preamble_guardinterval = 0;// long
4489
4490         switch (stats->rate) {
4491                 //
4492                 // CCK rate
4493                 //
4494         case MGN_1M:    rateIndex = 0;  break;
4495         case MGN_2M:    rateIndex = 1;  break;
4496         case MGN_5_5M:  rateIndex = 2;  break;
4497         case MGN_11M:   rateIndex = 3;  break;
4498                 //
4499                 // Legacy OFDM rate
4500                 //
4501         case MGN_6M:    rateIndex = 4;  break;
4502         case MGN_9M:    rateIndex = 5;  break;
4503         case MGN_12M:   rateIndex = 6;  break;
4504         case MGN_18M:   rateIndex = 7;  break;
4505         case MGN_24M:   rateIndex = 8;  break;
4506         case MGN_36M:   rateIndex = 9;  break;
4507         case MGN_48M:   rateIndex = 10; break;
4508         case MGN_54M:   rateIndex = 11; break;
4509                 //
4510                 // 11n High throughput rate
4511                 //
4512         case MGN_MCS0:  rateIndex = 12; break;
4513         case MGN_MCS1:  rateIndex = 13; break;
4514         case MGN_MCS2:  rateIndex = 14; break;
4515         case MGN_MCS3:  rateIndex = 15; break;
4516         case MGN_MCS4:  rateIndex = 16; break;
4517         case MGN_MCS5:  rateIndex = 17; break;
4518         case MGN_MCS6:  rateIndex = 18; break;
4519         case MGN_MCS7:  rateIndex = 19; break;
4520         case MGN_MCS8:  rateIndex = 20; break;
4521         case MGN_MCS9:  rateIndex = 21; break;
4522         case MGN_MCS10: rateIndex = 22; break;
4523         case MGN_MCS11: rateIndex = 23; break;
4524         case MGN_MCS12: rateIndex = 24; break;
4525         case MGN_MCS13: rateIndex = 25; break;
4526         case MGN_MCS14: rateIndex = 26; break;
4527         case MGN_MCS15: rateIndex = 27; break;
4528         default:        rateIndex = 28; break;
4529         }
4530         priv->stats.received_preamble_GI[preamble_guardinterval][rateIndex]++;
4531         priv->stats.received_rate_histogram[0][rateIndex]++; //total
4532         priv->stats.received_rate_histogram[rcvType][rateIndex]++;
4533 }
4534
4535
4536 static void query_rxdesc_status(struct sk_buff *skb,
4537                                 struct ieee80211_rx_stats *stats,
4538                                 bool bIsRxAggrSubframe)
4539 {
4540         rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
4541         struct net_device *dev = info->dev;
4542         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4543         rx_drvinfo_819x_usb  *driver_info = NULL;
4544
4545         //
4546         //Get Rx Descriptor Information
4547         //
4548 #ifdef USB_RX_AGGREGATION_SUPPORT
4549         if (bIsRxAggrSubframe) {
4550                 rx_desc_819x_usb_aggr_subframe *desc = (rx_desc_819x_usb_aggr_subframe *)skb->data;
4551                 stats->Length = desc->Length;
4552                 stats->RxDrvInfoSize = desc->RxDrvInfoSize;
4553                 stats->RxBufShift = 0; //RxBufShift = 2 in RxDesc, but usb didn't shift bytes in fact.
4554                 stats->bICV = desc->ICV;
4555                 stats->bCRC = desc->CRC32;
4556                 stats->bHwError = stats->bCRC|stats->bICV;
4557                 stats->Decrypted = !desc->SWDec;//RTL8190 set this bit to indicate that Hw does not decrypt packet
4558         } else
4559 #endif
4560         {
4561                 rx_desc_819x_usb *desc = (rx_desc_819x_usb *)skb->data;
4562
4563                 stats->Length = desc->Length;
4564                 stats->RxDrvInfoSize = desc->RxDrvInfoSize;
4565                 stats->RxBufShift = 0;
4566                 stats->bICV = desc->ICV;
4567                 stats->bCRC = desc->CRC32;
4568                 stats->bHwError = stats->bCRC|stats->bICV;
4569                 //RTL8190 set this bit to indicate that Hw does not decrypt packet
4570                 stats->Decrypted = !desc->SWDec;
4571         }
4572
4573         if ((priv->ieee80211->pHTInfo->bCurrentHTSupport == true) && (priv->ieee80211->pairwise_key_type == KEY_TYPE_CCMP))
4574                 stats->bHwError = false;
4575         else
4576                 stats->bHwError = stats->bCRC|stats->bICV;
4577
4578         if (stats->Length < 24 || stats->Length > MAX_8192U_RX_SIZE)
4579                 stats->bHwError |= 1;
4580         //
4581         //Get Driver Info
4582         //
4583         // TODO: Need to verify it on FGPA platform
4584         //Driver info are written to the RxBuffer following rx desc
4585         if (stats->RxDrvInfoSize != 0) {
4586                 driver_info = (rx_drvinfo_819x_usb *)(skb->data + sizeof(rx_desc_819x_usb) +
4587                                                       stats->RxBufShift);
4588                 /* unit: 0.5M */
4589                 /* TODO */
4590                 if (!stats->bHwError) {
4591                         u8      ret_rate;
4592                         ret_rate = HwRateToMRate90(driver_info->RxHT, driver_info->RxRate);
4593                         if (ret_rate == 0xff) {
4594                                 // Abnormal Case: Receive CRC OK packet with Rx descriptor indicating non supported rate.
4595                                 // Special Error Handling here, 2008.05.16, by Emily
4596
4597                                 stats->bHwError = 1;
4598                                 stats->rate = MGN_1M;   //Set 1M rate by default
4599                         } else {
4600                                 stats->rate = ret_rate;
4601                         }
4602                 } else {
4603                         stats->rate = 0x02;
4604                 }
4605
4606                 stats->bShortPreamble = driver_info->SPLCP;
4607
4608
4609                 UpdateReceivedRateHistogramStatistics8190(dev, stats);
4610
4611                 stats->bIsAMPDU = (driver_info->PartAggr == 1);
4612                 stats->bFirstMPDU = (driver_info->PartAggr == 1) && (driver_info->FirstAGGR == 1);
4613                 stats->TimeStampLow = driver_info->TSFL;
4614                 // xiong mask it, 070514
4615
4616                 UpdateRxPktTimeStamp8190(dev, stats);
4617
4618                 //
4619                 // Rx A-MPDU
4620                 //
4621                 if (driver_info->FirstAGGR == 1 || driver_info->PartAggr == 1)
4622                         RT_TRACE(COMP_RXDESC, "driver_info->FirstAGGR = %d, driver_info->PartAggr = %d\n",
4623                                  driver_info->FirstAGGR, driver_info->PartAggr);
4624
4625         }
4626
4627         skb_pull(skb, sizeof(rx_desc_819x_usb));
4628         //
4629         // Get Total offset of MPDU Frame Body
4630         //
4631         if ((stats->RxBufShift + stats->RxDrvInfoSize) > 0) {
4632                 stats->bShift = 1;
4633                 skb_pull(skb, stats->RxBufShift + stats->RxDrvInfoSize);
4634         }
4635
4636 #ifdef USB_RX_AGGREGATION_SUPPORT
4637         /* for the rx aggregated sub frame, the redundant space truly contained in the packet */
4638         if (bIsRxAggrSubframe)
4639                 skb_pull(skb, 8);
4640 #endif
4641         /* for debug 2008.5.29 */
4642
4643         //added by vivi, for MP, 20080108
4644         stats->RxIs40MHzPacket = driver_info->BW;
4645         if (stats->RxDrvInfoSize != 0)
4646                 TranslateRxSignalStuff819xUsb(skb, stats, driver_info);
4647
4648 }
4649
4650 u32 GetRxPacketShiftBytes819xUsb(struct ieee80211_rx_stats  *Status, bool bIsRxAggrSubframe)
4651 {
4652 #ifdef USB_RX_AGGREGATION_SUPPORT
4653         if (bIsRxAggrSubframe)
4654                 return (sizeof(rx_desc_819x_usb) + Status->RxDrvInfoSize
4655                         + Status->RxBufShift + 8);
4656         else
4657 #endif
4658                 return (sizeof(rx_desc_819x_usb) + Status->RxDrvInfoSize
4659                         + Status->RxBufShift);
4660 }
4661
4662 static void rtl8192_rx_nomal(struct sk_buff *skb)
4663 {
4664         rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
4665         struct net_device *dev = info->dev;
4666         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
4667         struct ieee80211_rx_stats stats = {
4668                 .signal = 0,
4669                 .noise = -98,
4670                 .rate = 0,
4671                 .freq = IEEE80211_24GHZ_BAND,
4672         };
4673         u32 rx_pkt_len = 0;
4674         struct ieee80211_hdr_1addr *ieee80211_hdr = NULL;
4675         bool unicast_packet = false;
4676 #ifdef USB_RX_AGGREGATION_SUPPORT
4677         struct sk_buff *agg_skb = NULL;
4678         u32  TotalLength = 0;
4679         u32  TempDWord = 0;
4680         u32  PacketLength = 0;
4681         u32  PacketOccupiedLendth = 0;
4682         u8   TempByte = 0;
4683         u32  PacketShiftBytes = 0;
4684         rx_desc_819x_usb_aggr_subframe *RxDescr = NULL;
4685         u8  PaddingBytes = 0;
4686         //add just for testing
4687         u8   testing;
4688
4689 #endif
4690
4691         /* 20 is for ps-poll */
4692         if ((skb->len >= (20 + sizeof(rx_desc_819x_usb))) && (skb->len < RX_URB_SIZE)) {
4693 #ifdef USB_RX_AGGREGATION_SUPPORT
4694                 TempByte = *(skb->data + sizeof(rx_desc_819x_usb));
4695 #endif
4696                 /* first packet should not contain Rx aggregation header */
4697                 query_rxdesc_status(skb, &stats, false);
4698                 /* TODO */
4699                 /* hardware related info */
4700 #ifdef USB_RX_AGGREGATION_SUPPORT
4701                 if (TempByte & BIT0) {
4702                         agg_skb = skb;
4703                         TotalLength = stats.Length - 4; /*sCrcLng*/
4704                         /* though the head pointer has passed this position  */
4705                         TempDWord = *(u32 *)(agg_skb->data - 4);
4706                         PacketLength = (u16)(TempDWord & 0x3FFF); /*sCrcLng*/
4707                         skb = dev_alloc_skb(PacketLength);
4708                         memcpy(skb_put(skb, PacketLength), agg_skb->data, PacketLength);
4709                         PacketShiftBytes = GetRxPacketShiftBytes819xUsb(&stats, false);
4710                 }
4711 #endif
4712                 /* Process the MPDU received */
4713                 skb_trim(skb, skb->len - 4/*sCrcLng*/);
4714
4715                 rx_pkt_len = skb->len;
4716                 ieee80211_hdr = (struct ieee80211_hdr_1addr *)skb->data;
4717                 unicast_packet = false;
4718                 if (is_broadcast_ether_addr(ieee80211_hdr->addr1)) {
4719                         //TODO
4720                 } else if (is_multicast_ether_addr(ieee80211_hdr->addr1)) {
4721                         //TODO
4722                 } else {
4723                         /* unicast packet */
4724                         unicast_packet = true;
4725                 }
4726
4727                 if (!ieee80211_rx(priv->ieee80211, skb, &stats)) {
4728                         dev_kfree_skb_any(skb);
4729                 } else {
4730                         priv->stats.rxoktotal++;
4731                         if (unicast_packet)
4732                                 priv->stats.rxbytesunicast += rx_pkt_len;
4733                 }
4734 #ifdef USB_RX_AGGREGATION_SUPPORT
4735                 testing = 1;
4736                 if (TotalLength > 0) {
4737                         PacketOccupiedLendth = PacketLength + (PacketShiftBytes + 8);
4738                         if ((PacketOccupiedLendth & 0xFF) != 0)
4739                                 PacketOccupiedLendth = (PacketOccupiedLendth & 0xFFFFFF00) + 256;
4740                         PacketOccupiedLendth -= 8;
4741                         TempDWord = PacketOccupiedLendth - PacketShiftBytes; /*- PacketLength */
4742                         if (agg_skb->len > TempDWord)
4743                                 skb_pull(agg_skb, TempDWord);
4744                         else
4745                                 agg_skb->len = 0;
4746
4747                         while (agg_skb->len >= GetRxPacketShiftBytes819xUsb(&stats, true)) {
4748                                 u8 tmpCRC = 0, tmpICV = 0;
4749                                 RxDescr = (rx_desc_819x_usb_aggr_subframe *)(agg_skb->data);
4750                                 tmpCRC = RxDescr->CRC32;
4751                                 tmpICV = RxDescr->ICV;
4752                                 memcpy(agg_skb->data, &agg_skb->data[44], 2);
4753                                 RxDescr->CRC32 = tmpCRC;
4754                                 RxDescr->ICV = tmpICV;
4755
4756                                 memset(&stats, 0, sizeof(struct ieee80211_rx_stats));
4757                                 stats.signal = 0;
4758                                 stats.noise = -98;
4759                                 stats.rate = 0;
4760                                 stats.freq = IEEE80211_24GHZ_BAND;
4761                                 query_rxdesc_status(agg_skb, &stats, true);
4762                                 PacketLength = stats.Length;
4763
4764                                 if (PacketLength > agg_skb->len)
4765                                         break;
4766                                 /* Process the MPDU received */
4767                                 skb = dev_alloc_skb(PacketLength);
4768                                 memcpy(skb_put(skb, PacketLength), agg_skb->data, PacketLength);
4769                                 skb_trim(skb, skb->len - 4/*sCrcLng*/);
4770
4771                                 rx_pkt_len = skb->len;
4772                                 ieee80211_hdr = (struct ieee80211_hdr_1addr *)skb->data;
4773                                 unicast_packet = false;
4774                                 if (is_broadcast_ether_addr(ieee80211_hdr->addr1)) {
4775                                         //TODO
4776                                 } else if (is_multicast_ether_addr(ieee80211_hdr->addr1)) {
4777                                         //TODO
4778                                 } else {
4779                                         /* unicast packet */
4780                                         unicast_packet = true;
4781                                 }
4782                                 if (!ieee80211_rx(priv->ieee80211, skb, &stats)) {
4783                                         dev_kfree_skb_any(skb);
4784                                 } else {
4785                                         priv->stats.rxoktotal++;
4786                                         if (unicast_packet)
4787                                                 priv->stats.rxbytesunicast += rx_pkt_len;
4788                                 }
4789                                 /* should trim the packet which has been copied to target skb */
4790                                 skb_pull(agg_skb, PacketLength);
4791                                 PacketShiftBytes = GetRxPacketShiftBytes819xUsb(&stats, true);
4792                                 PacketOccupiedLendth = PacketLength + PacketShiftBytes;
4793                                 if ((PacketOccupiedLendth & 0xFF) != 0) {
4794                                         PaddingBytes = 256 - (PacketOccupiedLendth & 0xFF);
4795                                         if (agg_skb->len > PaddingBytes)
4796                                                 skb_pull(agg_skb, PaddingBytes);
4797                                         else
4798                                                 agg_skb->len = 0;
4799                                 }
4800                         }
4801                         dev_kfree_skb(agg_skb);
4802                 }
4803 #endif
4804         } else {
4805                 priv->stats.rxurberr++;
4806                 netdev_dbg(dev, "actual_length: %d\n", skb->len);
4807                 dev_kfree_skb_any(skb);
4808         }
4809
4810 }
4811
4812 static void rtl819xusb_process_received_packet(struct net_device *dev,
4813                                                struct ieee80211_rx_stats *pstats)
4814 {
4815         u8      *frame;
4816         u16     frame_len = 0;
4817         struct r8192_priv *priv = ieee80211_priv(dev);
4818
4819         // Get shifted bytes of Starting address of 802.11 header. 2006.09.28, by Emily
4820         //porting by amy 080508
4821         pstats->virtual_address += get_rxpacket_shiftbytes_819xusb(pstats);
4822         frame = pstats->virtual_address;
4823         frame_len = pstats->packetlength;
4824 #ifdef TODO     // by amy about HCT
4825         if (!Adapter->bInHctTest)
4826                 CountRxErrStatistics(Adapter, pRfd);
4827 #endif
4828 #ifdef ENABLE_PS  //by amy for adding ps function in future
4829         RT_RF_POWER_STATE rtState;
4830         // When RF is off, we should not count the packet for hw/sw synchronize
4831         // reason, ie. there may be a duration while sw switch is changed and hw
4832         // switch is being changed. 2006.12.04, by shien chang.
4833         Adapter->HalFunc.GetHwRegHandler(Adapter, HW_VAR_RF_STATE, (u8 *)(&rtState));
4834         if (rtState == eRfOff)
4835                 return;
4836 #endif
4837         priv->stats.rxframgment++;
4838
4839 #ifdef TODO
4840         RmMonitorSignalStrength(Adapter, pRfd);
4841 #endif
4842         /* 2007/01/16 MH Add RX command packet handle here. */
4843         /* 2007/03/01 MH We have to release RFD and return if rx pkt is cmd pkt. */
4844         if (rtl819xusb_rx_command_packet(dev, pstats))
4845                 return;
4846
4847 #ifdef SW_CRC_CHECK
4848         SwCrcCheck();
4849 #endif
4850
4851
4852 }
4853
4854 static void query_rx_cmdpkt_desc_status(struct sk_buff *skb,
4855                                         struct ieee80211_rx_stats *stats)
4856 {
4857         rx_desc_819x_usb *desc = (rx_desc_819x_usb *)skb->data;
4858
4859         //
4860         //Get Rx Descriptor Information
4861         //
4862         stats->virtual_address = (u8 *)skb->data;
4863         stats->Length = desc->Length;
4864         stats->RxDrvInfoSize = 0;
4865         stats->RxBufShift = 0;
4866         stats->packetlength = stats->Length-scrclng;
4867         stats->fraglength = stats->packetlength;
4868         stats->fragoffset = 0;
4869         stats->ntotalfrag = 1;
4870 }
4871
4872
4873 static void rtl8192_rx_cmd(struct sk_buff *skb)
4874 {
4875         struct rtl8192_rx_info *info = (struct rtl8192_rx_info *)skb->cb;
4876         struct net_device *dev = info->dev;
4877         /* TODO */
4878         struct ieee80211_rx_stats stats = {
4879                 .signal = 0,
4880                 .noise = -98,
4881                 .rate = 0,
4882                 .freq = IEEE80211_24GHZ_BAND,
4883         };
4884
4885         if ((skb->len >= (20 + sizeof(rx_desc_819x_usb))) && (skb->len < RX_URB_SIZE)) {
4886
4887                 query_rx_cmdpkt_desc_status(skb, &stats);
4888                 // this is to be done by amy 080508     prfd->queue_id = 1;
4889
4890
4891                 //
4892                 //  Process the command packet received.
4893                 //
4894
4895                 rtl819xusb_process_received_packet(dev, &stats);
4896
4897                 dev_kfree_skb_any(skb);
4898         }
4899 }
4900
4901 void rtl8192_irq_rx_tasklet(struct r8192_priv *priv)
4902 {
4903         struct sk_buff *skb;
4904         struct rtl8192_rx_info *info;
4905
4906         while (NULL != (skb = skb_dequeue(&priv->skb_queue))) {
4907                 info = (struct rtl8192_rx_info *)skb->cb;
4908                 switch (info->out_pipe) {
4909                 /* Nomal packet pipe */
4910                 case 3:
4911                         priv->IrpPendingCount--;
4912                         rtl8192_rx_nomal(skb);
4913                         break;
4914
4915                 /* Command packet pipe */
4916                 case 9:
4917                         RT_TRACE(COMP_RECV, "command in-pipe index(%d)\n",
4918                                  info->out_pipe);
4919
4920                         rtl8192_rx_cmd(skb);
4921                         break;
4922
4923                 default: /* should never get here! */
4924                         RT_TRACE(COMP_ERR, "Unknown in-pipe index(%d)\n",
4925                                  info->out_pipe);
4926                         dev_kfree_skb(skb);
4927                         break;
4928
4929                 }
4930         }
4931 }
4932
4933 static const struct net_device_ops rtl8192_netdev_ops = {
4934         .ndo_open               = rtl8192_open,
4935         .ndo_stop               = rtl8192_close,
4936         .ndo_get_stats          = rtl8192_stats,
4937         .ndo_tx_timeout         = tx_timeout,
4938         .ndo_do_ioctl           = rtl8192_ioctl,
4939         .ndo_set_rx_mode        = r8192_set_multicast,
4940         .ndo_set_mac_address    = r8192_set_mac_adr,
4941         .ndo_validate_addr      = eth_validate_addr,
4942         .ndo_change_mtu         = eth_change_mtu,
4943         .ndo_start_xmit         = ieee80211_xmit,
4944 };
4945
4946
4947 /****************************************************************************
4948      ---------------------------- USB_STUFF---------------------------
4949 *****************************************************************************/
4950
4951 static int rtl8192_usb_probe(struct usb_interface *intf,
4952                              const struct usb_device_id *id)
4953 {
4954         struct net_device *dev = NULL;
4955         struct r8192_priv *priv = NULL;
4956         struct usb_device *udev = interface_to_usbdev(intf);
4957         int ret;
4958         RT_TRACE(COMP_INIT, "Oops: i'm coming\n");
4959
4960         dev = alloc_ieee80211(sizeof(struct r8192_priv));
4961         if (dev == NULL)
4962                 return -ENOMEM;
4963
4964         usb_set_intfdata(intf, dev);
4965         SET_NETDEV_DEV(dev, &intf->dev);
4966         priv = ieee80211_priv(dev);
4967         priv->ieee80211 = netdev_priv(dev);
4968         priv->udev = udev;
4969
4970         dev->netdev_ops = &rtl8192_netdev_ops;
4971
4972         dev->wireless_handlers = (struct iw_handler_def *) &r8192_wx_handlers_def;
4973
4974         dev->type = ARPHRD_ETHER;
4975
4976         dev->watchdog_timeo = HZ*3;     //modified by john, 0805
4977
4978         if (dev_alloc_name(dev, ifname) < 0) {
4979                 RT_TRACE(COMP_INIT, "Oops: devname already taken! Trying wlan%%d...\n");
4980                 ifname = "wlan%d";
4981                 dev_alloc_name(dev, ifname);
4982         }
4983
4984         RT_TRACE(COMP_INIT, "Driver probe completed1\n");
4985         if (rtl8192_init(dev) != 0) {
4986                 RT_TRACE(COMP_ERR, "Initialization failed");
4987                 ret = -ENODEV;
4988                 goto fail;
4989         }
4990         netif_carrier_off(dev);
4991         netif_stop_queue(dev);
4992
4993         ret = register_netdev(dev);
4994         if (ret)
4995                 goto fail2;
4996
4997         RT_TRACE(COMP_INIT, "dev name=======> %s\n", dev->name);
4998         rtl8192_proc_init_one(dev);
4999
5000
5001         RT_TRACE(COMP_INIT, "Driver probe completed\n");
5002         return 0;
5003
5004 fail2:
5005         rtl8192_down(dev);
5006         kfree(priv->pFirmware);
5007         priv->pFirmware = NULL;
5008         rtl8192_usb_deleteendpoints(dev);
5009         destroy_workqueue(priv->priv_wq);
5010         mdelay(10);
5011 fail:
5012         free_ieee80211(dev);
5013
5014         RT_TRACE(COMP_ERR, "wlan driver load failed\n");
5015         return ret;
5016 }
5017
5018 //detach all the work and timer structure declared or inititialize in r8192U_init function.
5019 void rtl8192_cancel_deferred_work(struct r8192_priv *priv)
5020 {
5021
5022         cancel_work_sync(&priv->reset_wq);
5023         cancel_delayed_work(&priv->watch_dog_wq);
5024         cancel_delayed_work(&priv->update_beacon_wq);
5025         cancel_work_sync(&priv->qos_activate);
5026 }
5027
5028
5029 static void rtl8192_usb_disconnect(struct usb_interface *intf)
5030 {
5031         struct net_device *dev = usb_get_intfdata(intf);
5032
5033         struct r8192_priv *priv = ieee80211_priv(dev);
5034         if (dev) {
5035
5036                 unregister_netdev(dev);
5037
5038                 RT_TRACE(COMP_DOWN, "=============>wlan driver to be removed\n");
5039                 rtl8192_proc_remove_one(dev);
5040
5041                 rtl8192_down(dev);
5042                 kfree(priv->pFirmware);
5043                 priv->pFirmware = NULL;
5044                 rtl8192_usb_deleteendpoints(dev);
5045                 destroy_workqueue(priv->priv_wq);
5046                 mdelay(10);
5047
5048         }
5049         free_ieee80211(dev);
5050         RT_TRACE(COMP_DOWN, "wlan driver removed\n");
5051 }
5052
5053 /* fun with the built-in ieee80211 stack... */
5054 extern int ieee80211_debug_init(void);
5055 extern void ieee80211_debug_exit(void);
5056 extern int ieee80211_crypto_init(void);
5057 extern void ieee80211_crypto_deinit(void);
5058 extern int ieee80211_crypto_tkip_init(void);
5059 extern void ieee80211_crypto_tkip_exit(void);
5060 extern int ieee80211_crypto_ccmp_init(void);
5061 extern void ieee80211_crypto_ccmp_exit(void);
5062 extern int ieee80211_crypto_wep_init(void);
5063 extern void ieee80211_crypto_wep_exit(void);
5064
5065 static int __init rtl8192_usb_module_init(void)
5066 {
5067         int ret;
5068
5069 #ifdef CONFIG_IEEE80211_DEBUG
5070         ret = ieee80211_debug_init();
5071         if (ret) {
5072                 pr_err("ieee80211_debug_init() failed %d\n", ret);
5073                 return ret;
5074         }
5075 #endif
5076         ret = ieee80211_crypto_init();
5077         if (ret) {
5078                 pr_err("ieee80211_crypto_init() failed %d\n", ret);
5079                 return ret;
5080         }
5081
5082         ret = ieee80211_crypto_tkip_init();
5083         if (ret) {
5084                 pr_err("ieee80211_crypto_tkip_init() failed %d\n", ret);
5085                 return ret;
5086         }
5087
5088         ret = ieee80211_crypto_ccmp_init();
5089         if (ret) {
5090                 pr_err("ieee80211_crypto_ccmp_init() failed %d\n", ret);
5091                 return ret;
5092         }
5093
5094         ret = ieee80211_crypto_wep_init();
5095         if (ret) {
5096                 pr_err("ieee80211_crypto_wep_init() failed %d\n", ret);
5097                 return ret;
5098         }
5099
5100         pr_info("\nLinux kernel driver for RTL8192 based WLAN cards\n");
5101         pr_info("Copyright (c) 2007-2008, Realsil Wlan\n");
5102         RT_TRACE(COMP_INIT, "Initializing module");
5103         RT_TRACE(COMP_INIT, "Wireless extensions version %d", WIRELESS_EXT);
5104         rtl8192_proc_module_init();
5105         return usb_register(&rtl8192_usb_driver);
5106 }
5107
5108
5109 static void __exit rtl8192_usb_module_exit(void)
5110 {
5111         usb_deregister(&rtl8192_usb_driver);
5112
5113         RT_TRACE(COMP_DOWN, "Exiting");
5114 }
5115
5116
5117 void rtl8192_try_wake_queue(struct net_device *dev, int pri)
5118 {
5119         unsigned long flags;
5120         short enough_desc;
5121         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
5122
5123         spin_lock_irqsave(&priv->tx_lock, flags);
5124         enough_desc = check_nic_enough_desc(dev, pri);
5125         spin_unlock_irqrestore(&priv->tx_lock, flags);
5126
5127         if (enough_desc)
5128                 ieee80211_wake_queue(priv->ieee80211);
5129 }
5130
5131 void EnableHWSecurityConfig8192(struct net_device *dev)
5132 {
5133         u8 SECR_value = 0x0;
5134         struct r8192_priv *priv = (struct r8192_priv *)ieee80211_priv(dev);
5135         struct ieee80211_device *ieee = priv->ieee80211;
5136         SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
5137         if (((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type)) && (priv->ieee80211->auth_mode != 2)) {
5138                 SECR_value |= SCR_RxUseDK;
5139                 SECR_value |= SCR_TxUseDK;
5140         } else if ((ieee->iw_mode == IW_MODE_ADHOC) && (ieee->pairwise_key_type & (KEY_TYPE_CCMP | KEY_TYPE_TKIP))) {
5141                 SECR_value |= SCR_RxUseDK;
5142                 SECR_value |= SCR_TxUseDK;
5143         }
5144         //add HWSec active enable here.
5145         //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
5146
5147         ieee->hwsec_active = 1;
5148
5149         if ((ieee->pHTInfo->IOTAction&HT_IOT_ACT_PURE_N_MODE) || !hwwep) { //add hwsec_support flag to totol control hw_sec on/off
5150                 ieee->hwsec_active = 0;
5151                 SECR_value &= ~SCR_RxDecEnable;
5152         }
5153         RT_TRACE(COMP_SEC, "%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n", __func__,
5154                  ieee->hwsec_active, ieee->pairwise_key_type, SECR_value);
5155         write_nic_byte(dev, SECR,  SECR_value);
5156 }
5157
5158
5159 void setKey(struct net_device *dev, u8 EntryNo, u8 KeyIndex, u16 KeyType,
5160             u8 *MacAddr, u8 DefaultKey, u32 *KeyContent)
5161 {
5162         u32 TargetCommand = 0;
5163         u32 TargetContent = 0;
5164         u16 usConfig = 0;
5165         u8 i;
5166         if (EntryNo >= TOTAL_CAM_ENTRY)
5167                 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
5168
5169         RT_TRACE(COMP_SEC, "====>to setKey(), dev:%p, EntryNo:%d, KeyIndex:%d, KeyType:%d, MacAddr%pM\n", dev, EntryNo, KeyIndex, KeyType, MacAddr);
5170
5171         if (DefaultKey)
5172                 usConfig |= BIT15 | (KeyType<<2);
5173         else
5174                 usConfig |= BIT15 | (KeyType<<2) | KeyIndex;
5175
5176
5177         for (i = 0; i < CAM_CONTENT_COUNT; i++) {
5178                 TargetCommand  = i+CAM_CONTENT_COUNT*EntryNo;
5179                 TargetCommand |= BIT31|BIT16;
5180
5181                 if (i == 0) { //MAC|Config
5182                         TargetContent = (u32)(*(MacAddr+0)) << 16|
5183                                         (u32)(*(MacAddr+1)) << 24|
5184                                         (u32)usConfig;
5185
5186                         write_nic_dword(dev, WCAMI, TargetContent);
5187                         write_nic_dword(dev, RWCAM, TargetCommand);
5188                 } else if (i == 1) { //MAC
5189                         TargetContent = (u32)(*(MacAddr+2))      |
5190                                         (u32)(*(MacAddr+3)) <<  8|
5191                                         (u32)(*(MacAddr+4)) << 16|
5192                                         (u32)(*(MacAddr+5)) << 24;
5193                         write_nic_dword(dev, WCAMI, TargetContent);
5194                         write_nic_dword(dev, RWCAM, TargetCommand);
5195                 } else {
5196                         //Key Material
5197                         if (KeyContent != NULL) {
5198                                 write_nic_dword(dev, WCAMI, (u32)(*(KeyContent+i-2)));
5199                                 write_nic_dword(dev, RWCAM, TargetCommand);
5200                         }
5201                 }
5202         }
5203
5204 }
5205
5206 /***************************************************************************
5207      ------------------- module init / exit stubs ----------------
5208 ****************************************************************************/
5209 module_init(rtl8192_usb_module_init);
5210 module_exit(rtl8192_usb_module_exit);