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