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