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