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