1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  *
4  * Based on the r8180 driver, which is:
5  * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * The full GNU General Public License is included in this distribution in the
16  * file called LICENSE.
17  *
18  * Contact Information:
19  * wlanfae <wlanfae@realtek.com>
20  *****************************************************************************/
21 #include <linux/uaccess.h>
22 #include <linux/pci.h>
23 #include <linux/vmalloc.h>
24 #include <linux/ieee80211.h>
25 #include "rtl_core.h"
26 #include "r8192E_phy.h"
27 #include "r8192E_phyreg.h"
28 #include "r8190P_rtl8256.h"
29 #include "r8192E_cmdpkt.h"
30 
31 #include "rtl_wx.h"
32 #include "rtl_dm.h"
33 
34 #include "rtl_pm.h"
35 
36 int hwwep = 1;
37 static int channels = 0x3fff;
38 static char *ifname = "wlan%d";
39 
40 
41 static const struct rtl819x_ops rtl819xp_ops = {
42 	.nic_type			= NIC_8192E,
43 	.get_eeprom_size		= rtl92e_get_eeprom_size,
44 	.init_adapter_variable		= rtl92e_init_variables,
45 	.initialize_adapter		= rtl92e_start_adapter,
46 	.link_change			= rtl92e_link_change,
47 	.tx_fill_descriptor		= rtl92e_fill_tx_desc,
48 	.tx_fill_cmd_descriptor		= rtl92e_fill_tx_cmd_desc,
49 	.rx_query_status_descriptor	= rtl92e_get_rx_stats,
50 	.rx_command_packet_handler = NULL,
51 	.stop_adapter			= rtl92e_stop_adapter,
52 	.update_ratr_table		= rtl92e_update_ratr_table,
53 	.irq_enable			= rtl92e_enable_irq,
54 	.irq_disable			= rtl92e_disable_irq,
55 	.irq_clear			= rtl92e_clear_irq,
56 	.rx_enable			= rtl92e_enable_rx,
57 	.tx_enable			= rtl92e_enable_tx,
58 	.interrupt_recognized		= rtl92e_ack_irq,
59 	.TxCheckStuckHandler		= rtl92e_is_tx_stuck,
60 	.RxCheckStuckHandler		= rtl92e_is_rx_stuck,
61 };
62 
63 static struct pci_device_id rtl8192_pci_id_tbl[] = {
64 	{RTL_PCI_DEVICE(0x10ec, 0x8192, rtl819xp_ops)},
65 	{RTL_PCI_DEVICE(0x07aa, 0x0044, rtl819xp_ops)},
66 	{RTL_PCI_DEVICE(0x07aa, 0x0047, rtl819xp_ops)},
67 	{}
68 };
69 
70 MODULE_DEVICE_TABLE(pci, rtl8192_pci_id_tbl);
71 
72 static int _rtl92e_pci_probe(struct pci_dev *pdev,
73 			     const struct pci_device_id *id);
74 static void _rtl92e_pci_disconnect(struct pci_dev *pdev);
75 static irqreturn_t _rtl92e_irq(int irq, void *netdev);
76 
77 static struct pci_driver rtl8192_pci_driver = {
78 	.name = DRV_NAME,	/* Driver name   */
79 	.id_table = rtl8192_pci_id_tbl,	/* PCI_ID table  */
80 	.probe	= _rtl92e_pci_probe,	/* probe fn      */
81 	.remove	 = _rtl92e_pci_disconnect,	/* remove fn */
82 	.suspend = rtl92e_suspend,	/* PM suspend fn */
83 	.resume = rtl92e_resume,                 /* PM resume fn  */
84 };
85 
86 static short _rtl92e_is_tx_queue_empty(struct net_device *dev);
87 static void _rtl92e_watchdog_wq_cb(void *data);
88 static void _rtl92e_watchdog_timer_cb(struct timer_list *t);
89 static void _rtl92e_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
90 				   int rate);
91 static int _rtl92e_hard_start_xmit(struct sk_buff *skb, struct net_device *dev);
92 static void _rtl92e_tx_cmd(struct net_device *dev, struct sk_buff *skb);
93 static short _rtl92e_tx(struct net_device *dev, struct sk_buff *skb);
94 static short _rtl92e_pci_initdescring(struct net_device *dev);
95 static void _rtl92e_irq_tx_tasklet(struct r8192_priv *priv);
96 static void _rtl92e_irq_rx_tasklet(struct r8192_priv *priv);
97 static void _rtl92e_cancel_deferred_work(struct r8192_priv *priv);
98 static int _rtl92e_up(struct net_device *dev, bool is_silent_reset);
99 static int _rtl92e_try_up(struct net_device *dev);
100 static int _rtl92e_down(struct net_device *dev, bool shutdownrf);
101 static void _rtl92e_restart(void *data);
102 
103 /****************************************************************************
104  *  -----------------------------IO STUFF-------------------------
105  ****************************************************************************/
106 
rtl92e_readb(struct net_device * dev,int x)107 u8 rtl92e_readb(struct net_device *dev, int x)
108 {
109 	return 0xff & readb((u8 __iomem *)dev->mem_start + x);
110 }
111 
rtl92e_readl(struct net_device * dev,int x)112 u32 rtl92e_readl(struct net_device *dev, int x)
113 {
114 	return readl((u8 __iomem *)dev->mem_start + x);
115 }
116 
rtl92e_readw(struct net_device * dev,int x)117 u16 rtl92e_readw(struct net_device *dev, int x)
118 {
119 	return readw((u8 __iomem *)dev->mem_start + x);
120 }
121 
rtl92e_writeb(struct net_device * dev,int x,u8 y)122 void rtl92e_writeb(struct net_device *dev, int x, u8 y)
123 {
124 	writeb(y, (u8 __iomem *)dev->mem_start + x);
125 
126 	udelay(20);
127 }
128 
rtl92e_writel(struct net_device * dev,int x,u32 y)129 void rtl92e_writel(struct net_device *dev, int x, u32 y)
130 {
131 	writel(y, (u8 __iomem *)dev->mem_start + x);
132 
133 	udelay(20);
134 }
135 
rtl92e_writew(struct net_device * dev,int x,u16 y)136 void rtl92e_writew(struct net_device *dev, int x, u16 y)
137 {
138 	writew(y, (u8 __iomem *)dev->mem_start + x);
139 
140 	udelay(20);
141 }
142 
143 /****************************************************************************
144  *  -----------------------------GENERAL FUNCTION-------------------------
145  ****************************************************************************/
rtl92e_set_rf_state(struct net_device * dev,enum rt_rf_power_state StateToSet,RT_RF_CHANGE_SOURCE ChangeSource)146 bool rtl92e_set_rf_state(struct net_device *dev,
147 			 enum rt_rf_power_state StateToSet,
148 			 RT_RF_CHANGE_SOURCE ChangeSource)
149 {
150 	struct r8192_priv *priv = rtllib_priv(dev);
151 	struct rtllib_device *ieee = priv->rtllib;
152 	bool			bActionAllowed = false;
153 	bool			bConnectBySSID = false;
154 	enum rt_rf_power_state rtState;
155 	u16			RFWaitCounter = 0;
156 	unsigned long flag;
157 
158 	RT_TRACE((COMP_PS | COMP_RF),
159 		 "===>rtl92e_set_rf_state(): StateToSet(%d)\n", StateToSet);
160 
161 	while (true) {
162 		spin_lock_irqsave(&priv->rf_ps_lock, flag);
163 		if (priv->RFChangeInProgress) {
164 			spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
165 			RT_TRACE((COMP_PS | COMP_RF),
166 				 "rtl92e_set_rf_state(): RF Change in progress! Wait to set..StateToSet(%d).\n",
167 				 StateToSet);
168 
169 			while (priv->RFChangeInProgress) {
170 				RFWaitCounter++;
171 				RT_TRACE((COMP_PS | COMP_RF),
172 					 "rtl92e_set_rf_state(): Wait 1 ms (%d times)...\n",
173 					 RFWaitCounter);
174 				mdelay(1);
175 
176 				if (RFWaitCounter > 100) {
177 					netdev_warn(dev,
178 						    "%s(): Timeout waiting for RF change.\n",
179 						    __func__);
180 					return false;
181 				}
182 			}
183 		} else {
184 			priv->RFChangeInProgress = true;
185 			spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
186 			break;
187 		}
188 	}
189 
190 	rtState = priv->rtllib->eRFPowerState;
191 
192 	switch (StateToSet) {
193 	case eRfOn:
194 		priv->rtllib->RfOffReason &= (~ChangeSource);
195 
196 		if ((ChangeSource == RF_CHANGE_BY_HW) && priv->bHwRadioOff)
197 			priv->bHwRadioOff = false;
198 
199 		if (!priv->rtllib->RfOffReason) {
200 			priv->rtllib->RfOffReason = 0;
201 			bActionAllowed = true;
202 
203 
204 			if (rtState == eRfOff &&
205 			    ChangeSource >= RF_CHANGE_BY_HW)
206 				bConnectBySSID = true;
207 		} else {
208 			RT_TRACE((COMP_PS | COMP_RF),
209 				 "rtl92e_set_rf_state - eRfon reject pMgntInfo->RfOffReason= 0x%x, ChangeSource=0x%X\n",
210 				  priv->rtllib->RfOffReason, ChangeSource);
211 	}
212 
213 		break;
214 
215 	case eRfOff:
216 
217 		if ((priv->rtllib->iw_mode == IW_MODE_INFRA) ||
218 		    (priv->rtllib->iw_mode == IW_MODE_ADHOC)) {
219 			if ((priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) ||
220 			    (ChangeSource > RF_CHANGE_BY_IPS)) {
221 				if (ieee->state == RTLLIB_LINKED)
222 					priv->blinked_ingpio = true;
223 				else
224 					priv->blinked_ingpio = false;
225 				rtllib_MgntDisconnect(priv->rtllib,
226 						      WLAN_REASON_DISASSOC_STA_HAS_LEFT);
227 			}
228 		}
229 		if ((ChangeSource == RF_CHANGE_BY_HW) && !priv->bHwRadioOff)
230 			priv->bHwRadioOff = true;
231 		priv->rtllib->RfOffReason |= ChangeSource;
232 		bActionAllowed = true;
233 		break;
234 
235 	case eRfSleep:
236 		priv->rtllib->RfOffReason |= ChangeSource;
237 		bActionAllowed = true;
238 		break;
239 
240 	default:
241 		break;
242 	}
243 
244 	if (bActionAllowed) {
245 		RT_TRACE((COMP_PS | COMP_RF),
246 			 "rtl92e_set_rf_state(): Action is allowed.... StateToSet(%d), RfOffReason(%#X)\n",
247 			 StateToSet, priv->rtllib->RfOffReason);
248 		PHY_SetRFPowerState(dev, StateToSet);
249 		if (StateToSet == eRfOn) {
250 
251 			if (bConnectBySSID && priv->blinked_ingpio) {
252 				schedule_delayed_work(
253 					 &ieee->associate_procedure_wq, 0);
254 				priv->blinked_ingpio = false;
255 			}
256 		}
257 	} else {
258 		RT_TRACE((COMP_PS | COMP_RF),
259 			 "rtl92e_set_rf_state(): Action is rejected.... StateToSet(%d), ChangeSource(%#X), RfOffReason(%#X)\n",
260 			 StateToSet, ChangeSource, priv->rtllib->RfOffReason);
261 	}
262 
263 	spin_lock_irqsave(&priv->rf_ps_lock, flag);
264 	priv->RFChangeInProgress = false;
265 	spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
266 
267 	RT_TRACE((COMP_PS | COMP_RF), "<===rtl92e_set_rf_state()\n");
268 	return bActionAllowed;
269 }
270 
_rtl92e_check_nic_enough_desc(struct net_device * dev,int prio)271 static short _rtl92e_check_nic_enough_desc(struct net_device *dev, int prio)
272 {
273 	struct r8192_priv *priv = rtllib_priv(dev);
274 	struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
275 
276 	if (ring->entries - skb_queue_len(&ring->queue) >= 2)
277 		return 1;
278 	return 0;
279 }
280 
_rtl92e_tx_timeout(struct net_device * dev)281 static void _rtl92e_tx_timeout(struct net_device *dev)
282 {
283 	struct r8192_priv *priv = rtllib_priv(dev);
284 
285 	schedule_work(&priv->reset_wq);
286 	netdev_info(dev, "TXTIMEOUT");
287 }
288 
rtl92e_irq_enable(struct net_device * dev)289 void rtl92e_irq_enable(struct net_device *dev)
290 {
291 	struct r8192_priv *priv = rtllib_priv(dev);
292 
293 	priv->irq_enabled = 1;
294 
295 	priv->ops->irq_enable(dev);
296 }
297 
rtl92e_irq_disable(struct net_device * dev)298 void rtl92e_irq_disable(struct net_device *dev)
299 {
300 	struct r8192_priv *priv = rtllib_priv(dev);
301 
302 	priv->ops->irq_disable(dev);
303 
304 	priv->irq_enabled = 0;
305 }
306 
_rtl92e_set_chan(struct net_device * dev,short ch)307 static void _rtl92e_set_chan(struct net_device *dev, short ch)
308 {
309 	struct r8192_priv *priv = rtllib_priv(dev);
310 
311 	RT_TRACE(COMP_CH, "=====>%s()====ch:%d\n", __func__, ch);
312 	if (priv->chan_forced)
313 		return;
314 
315 	priv->chan = ch;
316 
317 	if (priv->rf_set_chan)
318 		priv->rf_set_chan(dev, priv->chan);
319 }
320 
_rtl92e_update_cap(struct net_device * dev,u16 cap)321 static void _rtl92e_update_cap(struct net_device *dev, u16 cap)
322 {
323 	struct r8192_priv *priv = rtllib_priv(dev);
324 	struct rtllib_network *net = &priv->rtllib->current_network;
325 	bool		ShortPreamble;
326 
327 	if (cap & WLAN_CAPABILITY_SHORT_PREAMBLE) {
328 		if (priv->dot11CurrentPreambleMode != PREAMBLE_SHORT) {
329 			ShortPreamble = true;
330 			priv->dot11CurrentPreambleMode = PREAMBLE_SHORT;
331 			RT_TRACE(COMP_DBG,
332 				 "%s(): WLAN_CAPABILITY_SHORT_PREAMBLE\n",
333 				 __func__);
334 			priv->rtllib->SetHwRegHandler(dev, HW_VAR_ACK_PREAMBLE,
335 					(unsigned char *)&ShortPreamble);
336 		}
337 	} else {
338 		if (priv->dot11CurrentPreambleMode != PREAMBLE_LONG) {
339 			ShortPreamble = false;
340 			priv->dot11CurrentPreambleMode = PREAMBLE_LONG;
341 			RT_TRACE(COMP_DBG,
342 				 "%s(): WLAN_CAPABILITY_LONG_PREAMBLE\n",
343 				 __func__);
344 			priv->rtllib->SetHwRegHandler(dev, HW_VAR_ACK_PREAMBLE,
345 					      (unsigned char *)&ShortPreamble);
346 		}
347 	}
348 
349 	if (net->mode & (IEEE_G | IEEE_N_24G)) {
350 		u8	slot_time_val;
351 		u8	CurSlotTime = priv->slot_time;
352 
353 		if ((cap & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
354 		   (!priv->rtllib->pHTInfo->bCurrentRT2RTLongSlotTime)) {
355 			if (CurSlotTime != SHORT_SLOT_TIME) {
356 				slot_time_val = SHORT_SLOT_TIME;
357 				priv->rtllib->SetHwRegHandler(dev,
358 					 HW_VAR_SLOT_TIME, &slot_time_val);
359 			}
360 		} else {
361 			if (CurSlotTime != NON_SHORT_SLOT_TIME) {
362 				slot_time_val = NON_SHORT_SLOT_TIME;
363 				priv->rtllib->SetHwRegHandler(dev,
364 					 HW_VAR_SLOT_TIME, &slot_time_val);
365 			}
366 		}
367 	}
368 }
369 
370 static const struct rtllib_qos_parameters def_qos_parameters = {
371 	{cpu_to_le16(3), cpu_to_le16(3), cpu_to_le16(3), cpu_to_le16(3)},
372 	{cpu_to_le16(7), cpu_to_le16(7), cpu_to_le16(7), cpu_to_le16(7)},
373 	{2, 2, 2, 2},
374 	{0, 0, 0, 0},
375 	{0, 0, 0, 0}
376 };
377 
_rtl92e_update_beacon(void * data)378 static void _rtl92e_update_beacon(void *data)
379 {
380 	struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
381 				  update_beacon_wq.work);
382 	struct net_device *dev = priv->rtllib->dev;
383 	struct rtllib_device *ieee = priv->rtllib;
384 	struct rtllib_network *net = &ieee->current_network;
385 
386 	if (ieee->pHTInfo->bCurrentHTSupport)
387 		HT_update_self_and_peer_setting(ieee, net);
388 	ieee->pHTInfo->bCurrentRT2RTLongSlotTime =
389 		 net->bssht.bdRT2RTLongSlotTime;
390 	ieee->pHTInfo->RT2RT_HT_Mode = net->bssht.RT2RT_HT_Mode;
391 	_rtl92e_update_cap(dev, net->capability);
392 }
393 
_rtl92e_qos_activate(void * data)394 static void _rtl92e_qos_activate(void *data)
395 {
396 	struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
397 				  qos_activate);
398 	struct net_device *dev = priv->rtllib->dev;
399 	int i;
400 
401 	mutex_lock(&priv->mutex);
402 	if (priv->rtllib->state != RTLLIB_LINKED)
403 		goto success;
404 	RT_TRACE(COMP_QOS,
405 		 "qos active process with associate response received\n");
406 
407 	for (i = 0; i <  QOS_QUEUE_NUM; i++)
408 		priv->rtllib->SetHwRegHandler(dev, HW_VAR_AC_PARAM, (u8 *)(&i));
409 
410 
411 success:
412 	mutex_unlock(&priv->mutex);
413 }
414 
_rtl92e_qos_handle_probe_response(struct r8192_priv * priv,int active_network,struct rtllib_network * network)415 static int _rtl92e_qos_handle_probe_response(struct r8192_priv *priv,
416 					     int active_network,
417 					     struct rtllib_network *network)
418 {
419 	int ret = 0;
420 	u32 size = sizeof(struct rtllib_qos_parameters);
421 
422 	if (priv->rtllib->state != RTLLIB_LINKED)
423 		return ret;
424 
425 	if (priv->rtllib->iw_mode != IW_MODE_INFRA)
426 		return ret;
427 
428 	if (network->flags & NETWORK_HAS_QOS_MASK) {
429 		if (active_network &&
430 				(network->flags & NETWORK_HAS_QOS_PARAMETERS))
431 			network->qos_data.active = network->qos_data.supported;
432 
433 		if ((network->qos_data.active == 1) && (active_network == 1) &&
434 				(network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
435 				(network->qos_data.old_param_count !=
436 				network->qos_data.param_count)) {
437 			network->qos_data.old_param_count =
438 				network->qos_data.param_count;
439 	priv->rtllib->wmm_acm = network->qos_data.wmm_acm;
440 			schedule_work(&priv->qos_activate);
441 			RT_TRACE(COMP_QOS,
442 				 "QoS parameters change call qos_activate\n");
443 		}
444 	} else {
445 		memcpy(&priv->rtllib->current_network.qos_data.parameters,
446 		       &def_qos_parameters, size);
447 
448 		if ((network->qos_data.active == 1) && (active_network == 1)) {
449 			schedule_work(&priv->qos_activate);
450 			RT_TRACE(COMP_QOS,
451 				 "QoS was disabled call qos_activate\n");
452 		}
453 		network->qos_data.active = 0;
454 		network->qos_data.supported = 0;
455 	}
456 
457 	return 0;
458 }
459 
_rtl92e_handle_beacon(struct net_device * dev,struct rtllib_beacon * beacon,struct rtllib_network * network)460 static int _rtl92e_handle_beacon(struct net_device *dev,
461 				 struct rtllib_beacon *beacon,
462 				 struct rtllib_network *network)
463 {
464 	struct r8192_priv *priv = rtllib_priv(dev);
465 
466 	_rtl92e_qos_handle_probe_response(priv, 1, network);
467 
468 	schedule_delayed_work(&priv->update_beacon_wq, 0);
469 	return 0;
470 
471 }
472 
_rtl92e_qos_assoc_resp(struct r8192_priv * priv,struct rtllib_network * network)473 static int _rtl92e_qos_assoc_resp(struct r8192_priv *priv,
474 				  struct rtllib_network *network)
475 {
476 	unsigned long flags;
477 	u32 size = sizeof(struct rtllib_qos_parameters);
478 	int set_qos_param = 0;
479 
480 	if (!priv || !network)
481 		return 0;
482 
483 	if (priv->rtllib->state != RTLLIB_LINKED)
484 		return 0;
485 
486 	if (priv->rtllib->iw_mode != IW_MODE_INFRA)
487 		return 0;
488 
489 	spin_lock_irqsave(&priv->rtllib->lock, flags);
490 	if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
491 		memcpy(&priv->rtllib->current_network.qos_data.parameters,
492 		       &network->qos_data.parameters,
493 		       sizeof(struct rtllib_qos_parameters));
494 		priv->rtllib->current_network.qos_data.active = 1;
495 		priv->rtllib->wmm_acm = network->qos_data.wmm_acm;
496 		set_qos_param = 1;
497 		priv->rtllib->current_network.qos_data.old_param_count =
498 			priv->rtllib->current_network.qos_data.param_count;
499 		priv->rtllib->current_network.qos_data.param_count =
500 			network->qos_data.param_count;
501 	} else {
502 		memcpy(&priv->rtllib->current_network.qos_data.parameters,
503 		&def_qos_parameters, size);
504 		priv->rtllib->current_network.qos_data.active = 0;
505 		priv->rtllib->current_network.qos_data.supported = 0;
506 		set_qos_param = 1;
507 	}
508 
509 	spin_unlock_irqrestore(&priv->rtllib->lock, flags);
510 
511 	RT_TRACE(COMP_QOS, "%s: network->flags = %d,%d\n", __func__,
512 		 network->flags, priv->rtllib->current_network.qos_data.active);
513 	if (set_qos_param == 1) {
514 		rtl92e_dm_init_edca_turbo(priv->rtllib->dev);
515 		schedule_work(&priv->qos_activate);
516 	}
517 	return 0;
518 }
519 
_rtl92e_handle_assoc_response(struct net_device * dev,struct rtllib_assoc_response_frame * resp,struct rtllib_network * network)520 static int _rtl92e_handle_assoc_response(struct net_device *dev,
521 				 struct rtllib_assoc_response_frame *resp,
522 				 struct rtllib_network *network)
523 {
524 	struct r8192_priv *priv = rtllib_priv(dev);
525 
526 	_rtl92e_qos_assoc_resp(priv, network);
527 	return 0;
528 }
529 
_rtl92e_prepare_beacon(struct r8192_priv * priv)530 static void _rtl92e_prepare_beacon(struct r8192_priv *priv)
531 {
532 	struct net_device *dev = priv->rtllib->dev;
533 	struct sk_buff *pskb = NULL, *pnewskb = NULL;
534 	struct cb_desc *tcb_desc = NULL;
535 	struct rtl8192_tx_ring *ring = NULL;
536 	struct tx_desc *pdesc = NULL;
537 
538 	ring = &priv->tx_ring[BEACON_QUEUE];
539 	pskb = __skb_dequeue(&ring->queue);
540 	kfree_skb(pskb);
541 
542 	pnewskb = rtllib_get_beacon(priv->rtllib);
543 	if (!pnewskb)
544 		return;
545 
546 	tcb_desc = (struct cb_desc *)(pnewskb->cb + 8);
547 	tcb_desc->queue_index = BEACON_QUEUE;
548 	tcb_desc->data_rate = 2;
549 	tcb_desc->RATRIndex = 7;
550 	tcb_desc->bTxDisableRateFallBack = 1;
551 	tcb_desc->bTxUseDriverAssingedRate = 1;
552 	skb_push(pnewskb, priv->rtllib->tx_headroom);
553 
554 	pdesc = &ring->desc[0];
555 	priv->ops->tx_fill_descriptor(dev, pdesc, tcb_desc, pnewskb);
556 	__skb_queue_tail(&ring->queue, pnewskb);
557 	pdesc->OWN = 1;
558 }
559 
_rtl92e_stop_beacon(struct net_device * dev)560 static void _rtl92e_stop_beacon(struct net_device *dev)
561 {
562 }
563 
rtl92e_config_rate(struct net_device * dev,u16 * rate_config)564 void rtl92e_config_rate(struct net_device *dev, u16 *rate_config)
565 {
566 	struct r8192_priv *priv = rtllib_priv(dev);
567 	struct rtllib_network *net;
568 	u8 i = 0, basic_rate = 0;
569 
570 	net = &priv->rtllib->current_network;
571 
572 	for (i = 0; i < net->rates_len; i++) {
573 		basic_rate = net->rates[i] & 0x7f;
574 		switch (basic_rate) {
575 		case MGN_1M:
576 			*rate_config |= RRSR_1M;
577 			break;
578 		case MGN_2M:
579 			*rate_config |= RRSR_2M;
580 			break;
581 		case MGN_5_5M:
582 			*rate_config |= RRSR_5_5M;
583 			break;
584 		case MGN_11M:
585 			*rate_config |= RRSR_11M;
586 			break;
587 		case MGN_6M:
588 			*rate_config |= RRSR_6M;
589 			break;
590 		case MGN_9M:
591 			*rate_config |= RRSR_9M;
592 			break;
593 		case MGN_12M:
594 			*rate_config |= RRSR_12M;
595 			break;
596 		case MGN_18M:
597 			*rate_config |= RRSR_18M;
598 			break;
599 		case MGN_24M:
600 			*rate_config |= RRSR_24M;
601 			break;
602 		case MGN_36M:
603 			*rate_config |= RRSR_36M;
604 			break;
605 		case MGN_48M:
606 			*rate_config |= RRSR_48M;
607 			break;
608 		case MGN_54M:
609 			*rate_config |= RRSR_54M;
610 			break;
611 		}
612 	}
613 
614 	for (i = 0; i < net->rates_ex_len; i++) {
615 		basic_rate = net->rates_ex[i] & 0x7f;
616 		switch (basic_rate) {
617 		case MGN_1M:
618 			*rate_config |= RRSR_1M;
619 			break;
620 		case MGN_2M:
621 			*rate_config |= RRSR_2M;
622 			break;
623 		case MGN_5_5M:
624 			*rate_config |= RRSR_5_5M;
625 			break;
626 		case MGN_11M:
627 			*rate_config |= RRSR_11M;
628 			break;
629 		case MGN_6M:
630 			*rate_config |= RRSR_6M;
631 			break;
632 		case MGN_9M:
633 			*rate_config |= RRSR_9M;
634 			break;
635 		case MGN_12M:
636 			*rate_config |= RRSR_12M;
637 			break;
638 		case MGN_18M:
639 			*rate_config |= RRSR_18M;
640 			break;
641 		case MGN_24M:
642 			*rate_config |= RRSR_24M;
643 			break;
644 		case MGN_36M:
645 			*rate_config |= RRSR_36M;
646 			break;
647 		case MGN_48M:
648 			*rate_config |= RRSR_48M;
649 			break;
650 		case MGN_54M:
651 			*rate_config |= RRSR_54M;
652 			break;
653 		}
654 	}
655 }
656 
_rtl92e_refresh_support_rate(struct r8192_priv * priv)657 static void _rtl92e_refresh_support_rate(struct r8192_priv *priv)
658 {
659 	struct rtllib_device *ieee = priv->rtllib;
660 
661 	if (ieee->mode == WIRELESS_MODE_N_24G ||
662 	    ieee->mode == WIRELESS_MODE_N_5G) {
663 		memcpy(ieee->Regdot11HTOperationalRateSet,
664 		       ieee->RegHTSuppRateSet, 16);
665 		memcpy(ieee->Regdot11TxHTOperationalRateSet,
666 		       ieee->RegHTSuppRateSet, 16);
667 
668 	} else {
669 		memset(ieee->Regdot11HTOperationalRateSet, 0, 16);
670 	}
671 }
672 
_rtl92e_get_supported_wireless_mode(struct net_device * dev)673 static u8 _rtl92e_get_supported_wireless_mode(struct net_device *dev)
674 {
675 	struct r8192_priv *priv = rtllib_priv(dev);
676 	u8 ret = 0;
677 
678 	switch (priv->rf_chip) {
679 	case RF_8225:
680 	case RF_8256:
681 	case RF_6052:
682 	case RF_PSEUDO_11N:
683 		ret = (WIRELESS_MODE_N_24G | WIRELESS_MODE_G | WIRELESS_MODE_B);
684 		break;
685 	case RF_8258:
686 		ret = (WIRELESS_MODE_A | WIRELESS_MODE_N_5G);
687 		break;
688 	default:
689 		ret = WIRELESS_MODE_B;
690 		break;
691 	}
692 	return ret;
693 }
694 
rtl92e_set_wireless_mode(struct net_device * dev,u8 wireless_mode)695 void rtl92e_set_wireless_mode(struct net_device *dev, u8 wireless_mode)
696 {
697 	struct r8192_priv *priv = rtllib_priv(dev);
698 	u8 bSupportMode = _rtl92e_get_supported_wireless_mode(dev);
699 
700 	if ((wireless_mode == WIRELESS_MODE_AUTO) ||
701 	    ((wireless_mode & bSupportMode) == 0)) {
702 		if (bSupportMode & WIRELESS_MODE_N_24G) {
703 			wireless_mode = WIRELESS_MODE_N_24G;
704 		} else if (bSupportMode & WIRELESS_MODE_N_5G) {
705 			wireless_mode = WIRELESS_MODE_N_5G;
706 		} else if ((bSupportMode & WIRELESS_MODE_A)) {
707 			wireless_mode = WIRELESS_MODE_A;
708 		} else if ((bSupportMode & WIRELESS_MODE_G)) {
709 			wireless_mode = WIRELESS_MODE_G;
710 		} else if ((bSupportMode & WIRELESS_MODE_B)) {
711 			wireless_mode = WIRELESS_MODE_B;
712 		} else {
713 			netdev_info(dev,
714 				    "%s(): Unsupported mode requested. Fallback to 802.11b\n",
715 				    __func__);
716 			wireless_mode = WIRELESS_MODE_B;
717 		}
718 	}
719 
720 	if ((wireless_mode & (WIRELESS_MODE_B | WIRELESS_MODE_G)) ==
721 	    (WIRELESS_MODE_G | WIRELESS_MODE_B))
722 		wireless_mode = WIRELESS_MODE_G;
723 
724 	priv->rtllib->mode = wireless_mode;
725 
726 	if ((wireless_mode == WIRELESS_MODE_N_24G) ||
727 	    (wireless_mode == WIRELESS_MODE_N_5G)) {
728 		priv->rtllib->pHTInfo->bEnableHT = 1;
729 	RT_TRACE(COMP_DBG, "%s(), wireless_mode:%x, bEnableHT = 1\n",
730 		 __func__, wireless_mode);
731 	} else {
732 		priv->rtllib->pHTInfo->bEnableHT = 0;
733 		RT_TRACE(COMP_DBG, "%s(), wireless_mode:%x, bEnableHT = 0\n",
734 			 __func__, wireless_mode);
735 	}
736 
737 	RT_TRACE(COMP_INIT, "Current Wireless Mode is %x\n", wireless_mode);
738 	_rtl92e_refresh_support_rate(priv);
739 }
740 
_rtl92e_sta_up(struct net_device * dev,bool is_silent_reset)741 static int _rtl92e_sta_up(struct net_device *dev, bool is_silent_reset)
742 {
743 	struct r8192_priv *priv = rtllib_priv(dev);
744 	struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
745 					(&priv->rtllib->PowerSaveControl);
746 	bool init_status = true;
747 
748 	priv->bDriverIsGoingToUnload = false;
749 	priv->bdisable_nic = false;
750 
751 	priv->up = 1;
752 	priv->rtllib->ieee_up = 1;
753 
754 	priv->up_first_time = 0;
755 	RT_TRACE(COMP_INIT, "Bringing up iface");
756 	priv->bfirst_init = true;
757 	init_status = priv->ops->initialize_adapter(dev);
758 	if (!init_status) {
759 		netdev_err(dev, "%s(): Initialization failed!\n", __func__);
760 		priv->bfirst_init = false;
761 		return -1;
762 	}
763 
764 	RT_TRACE(COMP_INIT, "start adapter finished\n");
765 	RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
766 	priv->bfirst_init = false;
767 
768 	if (priv->polling_timer_on == 0)
769 		rtl92e_check_rfctrl_gpio_timer(&priv->gpio_polling_timer);
770 
771 	if (priv->rtllib->state != RTLLIB_LINKED)
772 		rtllib_softmac_start_protocol(priv->rtllib, 0);
773 	rtllib_reset_queue(priv->rtllib);
774 	_rtl92e_watchdog_timer_cb(&priv->watch_dog_timer);
775 
776 	if (!netif_queue_stopped(dev))
777 		netif_start_queue(dev);
778 	else
779 		netif_wake_queue(dev);
780 
781 	return 0;
782 }
783 
_rtl92e_sta_down(struct net_device * dev,bool shutdownrf)784 static int _rtl92e_sta_down(struct net_device *dev, bool shutdownrf)
785 {
786 	struct r8192_priv *priv = rtllib_priv(dev);
787 	unsigned long flags = 0;
788 	u8 RFInProgressTimeOut = 0;
789 
790 	if (priv->up == 0)
791 		return -1;
792 
793 	if (priv->rtllib->rtllib_ips_leave)
794 		priv->rtllib->rtllib_ips_leave(dev);
795 
796 	if (priv->rtllib->state == RTLLIB_LINKED)
797 		rtl92e_leisure_ps_leave(dev);
798 
799 	priv->bDriverIsGoingToUnload = true;
800 	priv->up = 0;
801 	priv->rtllib->ieee_up = 0;
802 	priv->bfirst_after_down = true;
803 	RT_TRACE(COMP_DOWN, "==========>%s()\n", __func__);
804 	if (!netif_queue_stopped(dev))
805 		netif_stop_queue(dev);
806 
807 	priv->rtllib->wpa_ie_len = 0;
808 	kfree(priv->rtllib->wpa_ie);
809 	priv->rtllib->wpa_ie = NULL;
810 	rtl92e_cam_reset(dev);
811 	memset(priv->rtllib->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
812 	rtl92e_irq_disable(dev);
813 
814 	del_timer_sync(&priv->watch_dog_timer);
815 	_rtl92e_cancel_deferred_work(priv);
816 	cancel_delayed_work(&priv->rtllib->hw_wakeup_wq);
817 
818 	rtllib_softmac_stop_protocol(priv->rtllib, 0, true);
819 	spin_lock_irqsave(&priv->rf_ps_lock, flags);
820 	while (priv->RFChangeInProgress) {
821 		spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
822 		if (RFInProgressTimeOut > 100) {
823 			spin_lock_irqsave(&priv->rf_ps_lock, flags);
824 			break;
825 		}
826 		RT_TRACE(COMP_DBG,
827 			 "===>%s():RF is in progress, need to wait until rf change is done.\n",
828 			 __func__);
829 		mdelay(1);
830 		RFInProgressTimeOut++;
831 		spin_lock_irqsave(&priv->rf_ps_lock, flags);
832 	}
833 	priv->RFChangeInProgress = true;
834 	spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
835 	priv->ops->stop_adapter(dev, false);
836 	spin_lock_irqsave(&priv->rf_ps_lock, flags);
837 	priv->RFChangeInProgress = false;
838 	spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
839 	udelay(100);
840 	memset(&priv->rtllib->current_network, 0,
841 	       offsetof(struct rtllib_network, list));
842 	RT_TRACE(COMP_DOWN, "<==========%s()\n", __func__);
843 
844 	return 0;
845 }
846 
_rtl92e_init_priv_handler(struct net_device * dev)847 static void _rtl92e_init_priv_handler(struct net_device *dev)
848 {
849 	struct r8192_priv *priv = rtllib_priv(dev);
850 
851 	priv->rtllib->softmac_hard_start_xmit	= _rtl92e_hard_start_xmit;
852 	priv->rtllib->set_chan			= _rtl92e_set_chan;
853 	priv->rtllib->link_change		= priv->ops->link_change;
854 	priv->rtllib->softmac_data_hard_start_xmit = _rtl92e_hard_data_xmit;
855 	priv->rtllib->check_nic_enough_desc	= _rtl92e_check_nic_enough_desc;
856 	priv->rtllib->handle_assoc_response	= _rtl92e_handle_assoc_response;
857 	priv->rtllib->handle_beacon		= _rtl92e_handle_beacon;
858 	priv->rtllib->SetWirelessMode		= rtl92e_set_wireless_mode;
859 	priv->rtllib->LeisurePSLeave		= rtl92e_leisure_ps_leave;
860 	priv->rtllib->SetBWModeHandler		= rtl92e_set_bw_mode;
861 	priv->rf_set_chan			= rtl92e_set_channel;
862 
863 	priv->rtllib->start_send_beacons = rtl92e_start_beacon;
864 	priv->rtllib->stop_send_beacons = _rtl92e_stop_beacon;
865 
866 	priv->rtllib->sta_wake_up = rtl92e_hw_wakeup;
867 	priv->rtllib->enter_sleep_state = rtl92e_enter_sleep;
868 	priv->rtllib->ps_is_queue_empty = _rtl92e_is_tx_queue_empty;
869 
870 	priv->rtllib->GetNmodeSupportBySecCfg = rtl92e_get_nmode_support_by_sec;
871 	priv->rtllib->GetHalfNmodeSupportByAPsHandler =
872 						rtl92e_is_halfn_supported_by_ap;
873 
874 	priv->rtllib->SetHwRegHandler = rtl92e_set_reg;
875 	priv->rtllib->AllowAllDestAddrHandler = rtl92e_set_monitor_mode;
876 	priv->rtllib->SetFwCmdHandler = NULL;
877 	priv->rtllib->InitialGainHandler = rtl92e_init_gain;
878 	priv->rtllib->rtllib_ips_leave_wq = rtl92e_rtllib_ips_leave_wq;
879 	priv->rtllib->rtllib_ips_leave = rtl92e_rtllib_ips_leave;
880 
881 	priv->rtllib->LedControlHandler = NULL;
882 	priv->rtllib->UpdateBeaconInterruptHandler = NULL;
883 
884 	priv->rtllib->ScanOperationBackupHandler = rtl92e_scan_op_backup;
885 }
886 
_rtl92e_init_priv_constant(struct net_device * dev)887 static void _rtl92e_init_priv_constant(struct net_device *dev)
888 {
889 	struct r8192_priv *priv = rtllib_priv(dev);
890 	struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
891 					&priv->rtllib->PowerSaveControl;
892 
893 	pPSC->RegMaxLPSAwakeIntvl = 5;
894 }
895 
896 
_rtl92e_init_priv_variable(struct net_device * dev)897 static void _rtl92e_init_priv_variable(struct net_device *dev)
898 {
899 	struct r8192_priv *priv = rtllib_priv(dev);
900 	u8 i;
901 
902 	priv->AcmMethod = eAcmWay2_SW;
903 	priv->dot11CurrentPreambleMode = PREAMBLE_AUTO;
904 	priv->rtllib->status = 0;
905 	priv->polling_timer_on = 0;
906 	priv->up_first_time = 1;
907 	priv->blinked_ingpio = false;
908 	priv->bDriverIsGoingToUnload = false;
909 	priv->being_init_adapter = false;
910 	priv->initialized_at_probe = false;
911 	priv->bdisable_nic = false;
912 	priv->bfirst_init = false;
913 	priv->txringcount = 64;
914 	priv->rxbuffersize = 9100;
915 	priv->rxringcount = MAX_RX_COUNT;
916 	priv->irq_enabled = 0;
917 	priv->chan = 1;
918 	priv->RegChannelPlan = 0xf;
919 	priv->rtllib->mode = WIRELESS_MODE_AUTO;
920 	priv->rtllib->iw_mode = IW_MODE_INFRA;
921 	priv->rtllib->bNetPromiscuousMode = false;
922 	priv->rtllib->IntelPromiscuousModeInfo.bPromiscuousOn = false;
923 	priv->rtllib->IntelPromiscuousModeInfo.bFilterSourceStationFrame =
924 								 false;
925 	priv->rtllib->ieee_up = 0;
926 	priv->retry_rts = DEFAULT_RETRY_RTS;
927 	priv->retry_data = DEFAULT_RETRY_DATA;
928 	priv->rtllib->rts = DEFAULT_RTS_THRESHOLD;
929 	priv->rtllib->rate = 110;
930 	priv->rtllib->short_slot = 1;
931 	priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
932 	priv->bcck_in_ch14 = false;
933 	priv->bfsync_processing  = false;
934 	priv->CCKPresentAttentuation = 0;
935 	priv->rfa_txpowertrackingindex = 0;
936 	priv->rfc_txpowertrackingindex = 0;
937 	priv->CckPwEnl = 6;
938 	priv->ScanDelay = 50;
939 	priv->ResetProgress = RESET_TYPE_NORESET;
940 	priv->bForcedSilentReset = false;
941 	priv->bDisableNormalResetCheck = false;
942 	priv->force_reset = false;
943 	memset(priv->rtllib->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
944 
945 	memset(&priv->InterruptLog, 0, sizeof(struct log_int_8190));
946 	priv->RxCounter = 0;
947 	priv->rtllib->wx_set_enc = 0;
948 	priv->bHwRadioOff = false;
949 	priv->RegRfOff = false;
950 	priv->isRFOff = false;
951 	priv->bInPowerSaveMode = false;
952 	priv->rtllib->RfOffReason = 0;
953 	priv->RFChangeInProgress = false;
954 	priv->bHwRfOffAction = 0;
955 	priv->SetRFPowerStateInProgress = false;
956 	priv->rtllib->PowerSaveControl.bInactivePs = true;
957 	priv->rtllib->PowerSaveControl.bIPSModeBackup = false;
958 	priv->rtllib->PowerSaveControl.bLeisurePs = true;
959 	priv->rtllib->PowerSaveControl.bFwCtrlLPS = false;
960 	priv->rtllib->LPSDelayCnt = 0;
961 	priv->rtllib->sta_sleep = LPS_IS_WAKE;
962 	priv->rtllib->eRFPowerState = eRfOn;
963 
964 	priv->rtllib->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
965 	priv->rtllib->iw_mode = IW_MODE_INFRA;
966 	priv->rtllib->active_scan = 1;
967 	priv->rtllib->be_scan_inprogress = false;
968 	priv->rtllib->modulation = RTLLIB_CCK_MODULATION |
969 				   RTLLIB_OFDM_MODULATION;
970 	priv->rtllib->host_encrypt = 1;
971 	priv->rtllib->host_decrypt = 1;
972 
973 	priv->rtllib->fts = DEFAULT_FRAG_THRESHOLD;
974 
975 	priv->card_type = PCI;
976 
977 	priv->pFirmware = vzalloc(sizeof(struct rt_firmware));
978 	if (!priv->pFirmware)
979 		netdev_err(dev,
980 			   "rtl8192e: Unable to allocate space for firmware\n");
981 
982 	skb_queue_head_init(&priv->skb_queue);
983 
984 	for (i = 0; i < MAX_QUEUE_SIZE; i++)
985 		skb_queue_head_init(&priv->rtllib->skb_waitQ[i]);
986 	for (i = 0; i < MAX_QUEUE_SIZE; i++)
987 		skb_queue_head_init(&priv->rtllib->skb_aggQ[i]);
988 }
989 
_rtl92e_init_priv_lock(struct r8192_priv * priv)990 static void _rtl92e_init_priv_lock(struct r8192_priv *priv)
991 {
992 	spin_lock_init(&priv->tx_lock);
993 	spin_lock_init(&priv->irq_th_lock);
994 	spin_lock_init(&priv->rf_ps_lock);
995 	spin_lock_init(&priv->ps_lock);
996 	mutex_init(&priv->wx_mutex);
997 	mutex_init(&priv->rf_mutex);
998 	mutex_init(&priv->mutex);
999 }
1000 
_rtl92e_init_priv_task(struct net_device * dev)1001 static void _rtl92e_init_priv_task(struct net_device *dev)
1002 {
1003 	struct r8192_priv *priv = rtllib_priv(dev);
1004 
1005 	INIT_WORK_RSL(&priv->reset_wq, (void *)_rtl92e_restart, dev);
1006 	INIT_WORK_RSL(&priv->rtllib->ips_leave_wq, (void *)rtl92e_ips_leave_wq,
1007 		      dev);
1008 	INIT_DELAYED_WORK_RSL(&priv->watch_dog_wq,
1009 			      (void *)_rtl92e_watchdog_wq_cb, dev);
1010 	INIT_DELAYED_WORK_RSL(&priv->txpower_tracking_wq,
1011 			      (void *)rtl92e_dm_txpower_tracking_wq, dev);
1012 	INIT_DELAYED_WORK_RSL(&priv->rfpath_check_wq,
1013 			      (void *)rtl92e_dm_rf_pathcheck_wq, dev);
1014 	INIT_DELAYED_WORK_RSL(&priv->update_beacon_wq,
1015 			      (void *)_rtl92e_update_beacon, dev);
1016 	INIT_WORK_RSL(&priv->qos_activate, (void *)_rtl92e_qos_activate, dev);
1017 	INIT_DELAYED_WORK_RSL(&priv->rtllib->hw_wakeup_wq,
1018 			      (void *)rtl92e_hw_wakeup_wq, dev);
1019 	INIT_DELAYED_WORK_RSL(&priv->rtllib->hw_sleep_wq,
1020 			      (void *)rtl92e_hw_sleep_wq, dev);
1021 	tasklet_init(&priv->irq_rx_tasklet,
1022 		     (void(*)(unsigned long))_rtl92e_irq_rx_tasklet,
1023 		     (unsigned long)priv);
1024 	tasklet_init(&priv->irq_tx_tasklet,
1025 		     (void(*)(unsigned long))_rtl92e_irq_tx_tasklet,
1026 		     (unsigned long)priv);
1027 	tasklet_init(&priv->irq_prepare_beacon_tasklet,
1028 		     (void(*)(unsigned long))_rtl92e_prepare_beacon,
1029 		     (unsigned long)priv);
1030 }
1031 
_rtl92e_get_channel_map(struct net_device * dev)1032 static short _rtl92e_get_channel_map(struct net_device *dev)
1033 {
1034 	int i;
1035 
1036 	struct r8192_priv *priv = rtllib_priv(dev);
1037 
1038 	if ((priv->rf_chip != RF_8225) && (priv->rf_chip != RF_8256) &&
1039 						(priv->rf_chip != RF_6052)) {
1040 		netdev_err(dev, "%s: unknown rf chip, can't set channel map\n",
1041 			   __func__);
1042 		return -1;
1043 	}
1044 
1045 	if (priv->ChannelPlan >= COUNTRY_CODE_MAX) {
1046 		netdev_info(dev,
1047 			    "rtl819x_init:Error channel plan! Set to default.\n");
1048 		priv->ChannelPlan = COUNTRY_CODE_FCC;
1049 	}
1050 	RT_TRACE(COMP_INIT, "Channel plan is %d\n", priv->ChannelPlan);
1051 	dot11d_init(priv->rtllib);
1052 	Dot11d_Channelmap(priv->ChannelPlan, priv->rtllib);
1053 	for (i = 1; i <= 11; i++)
1054 		(priv->rtllib->active_channel_map)[i] = 1;
1055 	(priv->rtllib->active_channel_map)[12] = 2;
1056 	(priv->rtllib->active_channel_map)[13] = 2;
1057 
1058 	return 0;
1059 }
1060 
_rtl92e_init(struct net_device * dev)1061 static short _rtl92e_init(struct net_device *dev)
1062 {
1063 	struct r8192_priv *priv = rtllib_priv(dev);
1064 
1065 	memset(&priv->stats, 0, sizeof(struct rt_stats));
1066 
1067 	_rtl92e_init_priv_handler(dev);
1068 	_rtl92e_init_priv_constant(dev);
1069 	_rtl92e_init_priv_variable(dev);
1070 	_rtl92e_init_priv_lock(priv);
1071 	_rtl92e_init_priv_task(dev);
1072 	priv->ops->get_eeprom_size(dev);
1073 	priv->ops->init_adapter_variable(dev);
1074 	_rtl92e_get_channel_map(dev);
1075 
1076 	rtl92e_dm_init(dev);
1077 
1078 	timer_setup(&priv->watch_dog_timer, _rtl92e_watchdog_timer_cb, 0);
1079 
1080 	timer_setup(&priv->gpio_polling_timer, rtl92e_check_rfctrl_gpio_timer,
1081 		    0);
1082 
1083 	rtl92e_irq_disable(dev);
1084 	if (request_irq(dev->irq, _rtl92e_irq, IRQF_SHARED, dev->name, dev)) {
1085 		netdev_err(dev, "Error allocating IRQ %d", dev->irq);
1086 		return -1;
1087 	}
1088 
1089 	priv->irq = dev->irq;
1090 	RT_TRACE(COMP_INIT, "IRQ %d\n", dev->irq);
1091 
1092 	if (_rtl92e_pci_initdescring(dev) != 0) {
1093 		netdev_err(dev, "Endopoints initialization failed");
1094 		free_irq(dev->irq, dev);
1095 		return -1;
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 /***************************************************************************
1102  * -------------------------------WATCHDOG STUFF---------------------------
1103  **************************************************************************/
_rtl92e_is_tx_queue_empty(struct net_device * dev)1104 static short _rtl92e_is_tx_queue_empty(struct net_device *dev)
1105 {
1106 	int i = 0;
1107 	struct r8192_priv *priv = rtllib_priv(dev);
1108 
1109 	for (i = 0; i <= MGNT_QUEUE; i++) {
1110 		if ((i == TXCMD_QUEUE) || (i == HCCA_QUEUE))
1111 			continue;
1112 		if (skb_queue_len(&(&priv->tx_ring[i])->queue) > 0) {
1113 			netdev_info(dev, "===>tx queue is not empty:%d, %d\n",
1114 			       i, skb_queue_len(&(&priv->tx_ring[i])->queue));
1115 			return 0;
1116 		}
1117 	}
1118 	return 1;
1119 }
1120 
_rtl92e_tx_check_stuck(struct net_device * dev)1121 static enum reset_type _rtl92e_tx_check_stuck(struct net_device *dev)
1122 {
1123 	struct r8192_priv *priv = rtllib_priv(dev);
1124 	u8	QueueID;
1125 	bool	bCheckFwTxCnt = false;
1126 	struct rtl8192_tx_ring  *ring = NULL;
1127 	struct sk_buff *skb = NULL;
1128 	struct cb_desc *tcb_desc = NULL;
1129 	unsigned long flags = 0;
1130 
1131 	switch (priv->rtllib->ps) {
1132 	case RTLLIB_PS_DISABLED:
1133 		break;
1134 	case (RTLLIB_PS_MBCAST | RTLLIB_PS_UNICAST):
1135 		break;
1136 	default:
1137 		break;
1138 	}
1139 	spin_lock_irqsave(&priv->irq_th_lock, flags);
1140 	for (QueueID = 0; QueueID < MAX_TX_QUEUE; QueueID++) {
1141 		if (QueueID == TXCMD_QUEUE)
1142 			continue;
1143 
1144 		if (QueueID == BEACON_QUEUE)
1145 			continue;
1146 
1147 		ring = &priv->tx_ring[QueueID];
1148 
1149 		if (skb_queue_len(&ring->queue) == 0) {
1150 			continue;
1151 		} else {
1152 			skb = (&ring->queue)->next;
1153 			tcb_desc = (struct cb_desc *)(skb->cb +
1154 				    MAX_DEV_ADDR_SIZE);
1155 			tcb_desc->nStuckCount++;
1156 			bCheckFwTxCnt = true;
1157 			if (tcb_desc->nStuckCount > 1)
1158 				netdev_info(dev,
1159 					    "%s: QueueID=%d tcb_desc->nStuckCount=%d\n",
1160 					    __func__, QueueID,
1161 					    tcb_desc->nStuckCount);
1162 		}
1163 	}
1164 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1165 
1166 	if (bCheckFwTxCnt) {
1167 		if (priv->ops->TxCheckStuckHandler(dev)) {
1168 			RT_TRACE(COMP_RESET,
1169 				 "TxCheckStuck(): Fw indicates no Tx condition!\n");
1170 			return RESET_TYPE_SILENT;
1171 		}
1172 	}
1173 
1174 	return RESET_TYPE_NORESET;
1175 }
1176 
_rtl92e_rx_check_stuck(struct net_device * dev)1177 static enum reset_type _rtl92e_rx_check_stuck(struct net_device *dev)
1178 {
1179 	struct r8192_priv *priv = rtllib_priv(dev);
1180 
1181 	if (priv->ops->RxCheckStuckHandler(dev)) {
1182 		RT_TRACE(COMP_RESET, "RxStuck Condition\n");
1183 		return RESET_TYPE_SILENT;
1184 	}
1185 
1186 	return RESET_TYPE_NORESET;
1187 }
1188 
_rtl92e_if_check_reset(struct net_device * dev)1189 static enum reset_type _rtl92e_if_check_reset(struct net_device *dev)
1190 {
1191 	struct r8192_priv *priv = rtllib_priv(dev);
1192 	enum reset_type TxResetType = RESET_TYPE_NORESET;
1193 	enum reset_type RxResetType = RESET_TYPE_NORESET;
1194 	enum rt_rf_power_state rfState;
1195 
1196 	rfState = priv->rtllib->eRFPowerState;
1197 
1198 	if (rfState == eRfOn)
1199 		TxResetType = _rtl92e_tx_check_stuck(dev);
1200 
1201 	if (rfState == eRfOn &&
1202 	    (priv->rtllib->iw_mode == IW_MODE_INFRA) &&
1203 	    (priv->rtllib->state == RTLLIB_LINKED))
1204 		RxResetType = _rtl92e_rx_check_stuck(dev);
1205 
1206 	if (TxResetType == RESET_TYPE_NORMAL ||
1207 	    RxResetType == RESET_TYPE_NORMAL) {
1208 		netdev_info(dev, "%s(): TxResetType is %d, RxResetType is %d\n",
1209 			    __func__, TxResetType, RxResetType);
1210 		return RESET_TYPE_NORMAL;
1211 	} else if (TxResetType == RESET_TYPE_SILENT ||
1212 		   RxResetType == RESET_TYPE_SILENT) {
1213 		netdev_info(dev, "%s(): TxResetType is %d, RxResetType is %d\n",
1214 			    __func__, TxResetType, RxResetType);
1215 		return RESET_TYPE_SILENT;
1216 	} else {
1217 		return RESET_TYPE_NORESET;
1218 	}
1219 
1220 }
1221 
_rtl92e_if_silent_reset(struct net_device * dev)1222 static void _rtl92e_if_silent_reset(struct net_device *dev)
1223 {
1224 	struct r8192_priv *priv = rtllib_priv(dev);
1225 	u8	reset_times = 0;
1226 	int reset_status = 0;
1227 	struct rtllib_device *ieee = priv->rtllib;
1228 	unsigned long flag;
1229 
1230 	if (priv->ResetProgress == RESET_TYPE_NORESET) {
1231 
1232 		RT_TRACE(COMP_RESET, "=========>Reset progress!!\n");
1233 
1234 		priv->ResetProgress = RESET_TYPE_SILENT;
1235 
1236 		spin_lock_irqsave(&priv->rf_ps_lock, flag);
1237 		if (priv->RFChangeInProgress) {
1238 			spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1239 			goto END;
1240 		}
1241 		priv->RFChangeInProgress = true;
1242 		priv->bResetInProgress = true;
1243 		spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1244 
1245 RESET_START:
1246 
1247 		mutex_lock(&priv->wx_mutex);
1248 
1249 		if (priv->rtllib->state == RTLLIB_LINKED)
1250 			rtl92e_leisure_ps_leave(dev);
1251 
1252 		if (priv->up) {
1253 			netdev_info(dev, "%s():the driver is not up.\n",
1254 				    __func__);
1255 			mutex_unlock(&priv->wx_mutex);
1256 			return;
1257 		}
1258 		priv->up = 0;
1259 
1260 		RT_TRACE(COMP_RESET, "%s():======>start to down the driver\n",
1261 			  __func__);
1262 		mdelay(1000);
1263 		RT_TRACE(COMP_RESET,
1264 			 "%s():111111111111111111111111======>start to down the driver\n",
1265 			 __func__);
1266 
1267 		if (!netif_queue_stopped(dev))
1268 			netif_stop_queue(dev);
1269 
1270 		rtl92e_irq_disable(dev);
1271 		del_timer_sync(&priv->watch_dog_timer);
1272 		_rtl92e_cancel_deferred_work(priv);
1273 		rtl92e_dm_deinit(dev);
1274 		rtllib_stop_scan_syncro(ieee);
1275 
1276 		if (ieee->state == RTLLIB_LINKED) {
1277 			mutex_lock(&ieee->wx_mutex);
1278 			netdev_info(dev, "ieee->state is RTLLIB_LINKED\n");
1279 			rtllib_stop_send_beacons(priv->rtllib);
1280 			del_timer_sync(&ieee->associate_timer);
1281 			cancel_delayed_work(&ieee->associate_retry_wq);
1282 			rtllib_stop_scan(ieee);
1283 			netif_carrier_off(dev);
1284 			mutex_unlock(&ieee->wx_mutex);
1285 		} else {
1286 			netdev_info(dev, "ieee->state is NOT LINKED\n");
1287 			rtllib_softmac_stop_protocol(priv->rtllib, 0, true);
1288 		}
1289 
1290 		rtl92e_dm_backup_state(dev);
1291 
1292 		mutex_unlock(&priv->wx_mutex);
1293 		RT_TRACE(COMP_RESET,
1294 			 "%s():<==========down process is finished\n",
1295 			 __func__);
1296 
1297 		RT_TRACE(COMP_RESET, "%s():<===========up process start\n",
1298 			 __func__);
1299 		reset_status = _rtl92e_up(dev, true);
1300 
1301 		RT_TRACE(COMP_RESET,
1302 			 "%s():<===========up process is finished\n", __func__);
1303 		if (reset_status == -1) {
1304 			if (reset_times < 3) {
1305 				reset_times++;
1306 				goto RESET_START;
1307 			} else {
1308 				netdev_warn(dev, "%s():	Reset Failed\n",
1309 					    __func__);
1310 			}
1311 		}
1312 
1313 		ieee->is_silent_reset = 1;
1314 
1315 		spin_lock_irqsave(&priv->rf_ps_lock, flag);
1316 		priv->RFChangeInProgress = false;
1317 		spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
1318 
1319 		rtl92e_enable_hw_security_config(dev);
1320 
1321 		if (ieee->state == RTLLIB_LINKED && ieee->iw_mode ==
1322 		    IW_MODE_INFRA) {
1323 			ieee->set_chan(ieee->dev,
1324 				       ieee->current_network.channel);
1325 
1326 			schedule_work(&ieee->associate_complete_wq);
1327 
1328 		} else if (ieee->state == RTLLIB_LINKED && ieee->iw_mode ==
1329 			   IW_MODE_ADHOC) {
1330 			ieee->set_chan(ieee->dev,
1331 				       ieee->current_network.channel);
1332 			ieee->link_change(ieee->dev);
1333 
1334 			notify_wx_assoc_event(ieee);
1335 
1336 			rtllib_start_send_beacons(ieee);
1337 
1338 			netif_carrier_on(ieee->dev);
1339 		}
1340 
1341 		rtl92e_cam_restore(dev);
1342 		rtl92e_dm_restore_state(dev);
1343 END:
1344 		priv->ResetProgress = RESET_TYPE_NORESET;
1345 		priv->reset_count++;
1346 
1347 		priv->bForcedSilentReset = false;
1348 		priv->bResetInProgress = false;
1349 
1350 		rtl92e_writeb(dev, UFWP, 1);
1351 		RT_TRACE(COMP_RESET, "Reset finished!! ====>[%d]\n",
1352 			 priv->reset_count);
1353 	}
1354 }
1355 
_rtl92e_update_rxcounts(struct r8192_priv * priv,u32 * TotalRxBcnNum,u32 * TotalRxDataNum)1356 static void _rtl92e_update_rxcounts(struct r8192_priv *priv, u32 *TotalRxBcnNum,
1357 				    u32 *TotalRxDataNum)
1358 {
1359 	u16	SlotIndex;
1360 	u8	i;
1361 
1362 	*TotalRxBcnNum = 0;
1363 	*TotalRxDataNum = 0;
1364 
1365 	SlotIndex = (priv->rtllib->LinkDetectInfo.SlotIndex++) %
1366 			(priv->rtllib->LinkDetectInfo.SlotNum);
1367 	priv->rtllib->LinkDetectInfo.RxBcnNum[SlotIndex] =
1368 			priv->rtllib->LinkDetectInfo.NumRecvBcnInPeriod;
1369 	priv->rtllib->LinkDetectInfo.RxDataNum[SlotIndex] =
1370 			priv->rtllib->LinkDetectInfo.NumRecvDataInPeriod;
1371 	for (i = 0; i < priv->rtllib->LinkDetectInfo.SlotNum; i++) {
1372 		*TotalRxBcnNum += priv->rtllib->LinkDetectInfo.RxBcnNum[i];
1373 		*TotalRxDataNum += priv->rtllib->LinkDetectInfo.RxDataNum[i];
1374 	}
1375 }
1376 
_rtl92e_watchdog_wq_cb(void * data)1377 static void _rtl92e_watchdog_wq_cb(void *data)
1378 {
1379 	struct r8192_priv *priv = container_of_dwork_rsl(data,
1380 				  struct r8192_priv, watch_dog_wq);
1381 	struct net_device *dev = priv->rtllib->dev;
1382 	struct rtllib_device *ieee = priv->rtllib;
1383 	enum reset_type ResetType = RESET_TYPE_NORESET;
1384 	static u8 check_reset_cnt;
1385 	unsigned long flags;
1386 	struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
1387 					(&priv->rtllib->PowerSaveControl);
1388 	bool bBusyTraffic = false;
1389 	bool	bHigherBusyTraffic = false;
1390 	bool	bHigherBusyRxTraffic = false;
1391 	bool bEnterPS = false;
1392 
1393 	if (!priv->up || priv->bHwRadioOff)
1394 		return;
1395 
1396 	if (priv->rtllib->state >= RTLLIB_LINKED) {
1397 		if (priv->rtllib->CntAfterLink < 2)
1398 			priv->rtllib->CntAfterLink++;
1399 	} else {
1400 		priv->rtllib->CntAfterLink = 0;
1401 	}
1402 
1403 	rtl92e_dm_watchdog(dev);
1404 
1405 	if (rtllib_act_scanning(priv->rtllib, false) == false) {
1406 		if ((ieee->iw_mode == IW_MODE_INFRA) && (ieee->state ==
1407 		     RTLLIB_NOLINK) &&
1408 		     (ieee->eRFPowerState == eRfOn) && !ieee->is_set_key &&
1409 		     (!ieee->proto_stoppping) && !ieee->wx_set_enc) {
1410 			if ((ieee->PowerSaveControl.ReturnPoint ==
1411 			     IPS_CALLBACK_NONE) &&
1412 			     (!ieee->bNetPromiscuousMode)) {
1413 				RT_TRACE(COMP_PS,
1414 					 "====================>haha: rtl92e_ips_enter()\n");
1415 				rtl92e_ips_enter(dev);
1416 			}
1417 		}
1418 	}
1419 	if ((ieee->state == RTLLIB_LINKED) && (ieee->iw_mode ==
1420 	     IW_MODE_INFRA) && (!ieee->bNetPromiscuousMode)) {
1421 		if (ieee->LinkDetectInfo.NumRxOkInPeriod > 100 ||
1422 		ieee->LinkDetectInfo.NumTxOkInPeriod > 100)
1423 			bBusyTraffic = true;
1424 
1425 
1426 		if (ieee->LinkDetectInfo.NumRxOkInPeriod > 4000 ||
1427 		    ieee->LinkDetectInfo.NumTxOkInPeriod > 4000) {
1428 			bHigherBusyTraffic = true;
1429 			if (ieee->LinkDetectInfo.NumRxOkInPeriod > 5000)
1430 				bHigherBusyRxTraffic = true;
1431 			else
1432 				bHigherBusyRxTraffic = false;
1433 		}
1434 
1435 		if (((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod +
1436 		    ieee->LinkDetectInfo.NumTxOkInPeriod) > 8) ||
1437 		    (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2))
1438 			bEnterPS = false;
1439 		else
1440 			bEnterPS = true;
1441 
1442 		if (ieee->current_network.beacon_interval < 95)
1443 			bEnterPS = false;
1444 
1445 		if (bEnterPS)
1446 			rtl92e_leisure_ps_enter(dev);
1447 		else
1448 			rtl92e_leisure_ps_leave(dev);
1449 
1450 	} else {
1451 		RT_TRACE(COMP_LPS, "====>no link LPS leave\n");
1452 		rtl92e_leisure_ps_leave(dev);
1453 	}
1454 
1455 	ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
1456 	ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
1457 	ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
1458 	ieee->LinkDetectInfo.bBusyTraffic = bBusyTraffic;
1459 
1460 	ieee->LinkDetectInfo.bHigherBusyTraffic = bHigherBusyTraffic;
1461 	ieee->LinkDetectInfo.bHigherBusyRxTraffic = bHigherBusyRxTraffic;
1462 
1463 	if (ieee->state == RTLLIB_LINKED && ieee->iw_mode == IW_MODE_INFRA) {
1464 		u32	TotalRxBcnNum = 0;
1465 		u32	TotalRxDataNum = 0;
1466 
1467 		_rtl92e_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
1468 
1469 		if ((TotalRxBcnNum + TotalRxDataNum) == 0)
1470 			priv->check_roaming_cnt++;
1471 		else
1472 			priv->check_roaming_cnt = 0;
1473 
1474 
1475 		if (priv->check_roaming_cnt > 0) {
1476 			if (ieee->eRFPowerState == eRfOff)
1477 				netdev_info(dev, "%s(): RF is off\n", __func__);
1478 
1479 			netdev_info(dev,
1480 				    "===>%s(): AP is power off, chan:%d, connect another one\n",
1481 				    __func__, priv->chan);
1482 
1483 			ieee->state = RTLLIB_ASSOCIATING;
1484 
1485 			RemovePeerTS(priv->rtllib,
1486 				     priv->rtllib->current_network.bssid);
1487 			ieee->is_roaming = true;
1488 			ieee->is_set_key = false;
1489 			ieee->link_change(dev);
1490 			if (ieee->LedControlHandler)
1491 				ieee->LedControlHandler(ieee->dev,
1492 							LED_CTL_START_TO_LINK);
1493 
1494 			notify_wx_assoc_event(ieee);
1495 
1496 			if (!(ieee->rtllib_ap_sec_type(ieee) &
1497 			     (SEC_ALG_CCMP | SEC_ALG_TKIP)))
1498 				schedule_delayed_work(
1499 					&ieee->associate_procedure_wq, 0);
1500 
1501 			priv->check_roaming_cnt = 0;
1502 		}
1503 		ieee->LinkDetectInfo.NumRecvBcnInPeriod = 0;
1504 		ieee->LinkDetectInfo.NumRecvDataInPeriod = 0;
1505 
1506 	}
1507 
1508 	spin_lock_irqsave(&priv->tx_lock, flags);
1509 	if ((check_reset_cnt++ >= 3) && (!ieee->is_roaming) &&
1510 	    (!priv->RFChangeInProgress) && (!pPSC->bSwRfProcessing)) {
1511 		ResetType = _rtl92e_if_check_reset(dev);
1512 		check_reset_cnt = 3;
1513 	}
1514 	spin_unlock_irqrestore(&priv->tx_lock, flags);
1515 
1516 	if (!priv->bDisableNormalResetCheck && ResetType == RESET_TYPE_NORMAL) {
1517 		priv->ResetProgress = RESET_TYPE_NORMAL;
1518 		RT_TRACE(COMP_RESET, "%s(): NOMAL RESET\n", __func__);
1519 		return;
1520 	}
1521 
1522 	if (((priv->force_reset) || (!priv->bDisableNormalResetCheck &&
1523 	      ResetType == RESET_TYPE_SILENT)))
1524 		_rtl92e_if_silent_reset(dev);
1525 	priv->force_reset = false;
1526 	priv->bForcedSilentReset = false;
1527 	priv->bResetInProgress = false;
1528 	RT_TRACE(COMP_TRACE, " <==RtUsbCheckForHangWorkItemCallback()\n");
1529 }
1530 
_rtl92e_watchdog_timer_cb(struct timer_list * t)1531 static void _rtl92e_watchdog_timer_cb(struct timer_list *t)
1532 {
1533 	struct r8192_priv *priv = from_timer(priv, t, watch_dog_timer);
1534 
1535 	schedule_delayed_work(&priv->watch_dog_wq, 0);
1536 	mod_timer(&priv->watch_dog_timer, jiffies +
1537 		  msecs_to_jiffies(RTLLIB_WATCH_DOG_TIME));
1538 }
1539 
1540 /****************************************************************************
1541  * ---------------------------- NIC TX/RX STUFF---------------------------
1542  ****************************************************************************/
rtl92e_rx_enable(struct net_device * dev)1543 void rtl92e_rx_enable(struct net_device *dev)
1544 {
1545 	struct r8192_priv *priv = rtllib_priv(dev);
1546 
1547 	priv->ops->rx_enable(dev);
1548 }
1549 
rtl92e_tx_enable(struct net_device * dev)1550 void rtl92e_tx_enable(struct net_device *dev)
1551 {
1552 	struct r8192_priv *priv = rtllib_priv(dev);
1553 
1554 	priv->ops->tx_enable(dev);
1555 
1556 	rtllib_reset_queue(priv->rtllib);
1557 }
1558 
1559 
_rtl92e_free_rx_ring(struct net_device * dev)1560 static void _rtl92e_free_rx_ring(struct net_device *dev)
1561 {
1562 	struct r8192_priv *priv = rtllib_priv(dev);
1563 	int i, rx_queue_idx;
1564 
1565 	for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE;
1566 	     rx_queue_idx++) {
1567 		for (i = 0; i < priv->rxringcount; i++) {
1568 			struct sk_buff *skb = priv->rx_buf[rx_queue_idx][i];
1569 
1570 			if (!skb)
1571 				continue;
1572 
1573 			pci_unmap_single(priv->pdev,
1574 				*((dma_addr_t *)skb->cb),
1575 				priv->rxbuffersize, PCI_DMA_FROMDEVICE);
1576 				kfree_skb(skb);
1577 		}
1578 
1579 		pci_free_consistent(priv->pdev,
1580 			sizeof(*priv->rx_ring[rx_queue_idx]) *
1581 			priv->rxringcount,
1582 			priv->rx_ring[rx_queue_idx],
1583 			priv->rx_ring_dma[rx_queue_idx]);
1584 		priv->rx_ring[rx_queue_idx] = NULL;
1585 	}
1586 }
1587 
_rtl92e_free_tx_ring(struct net_device * dev,unsigned int prio)1588 static void _rtl92e_free_tx_ring(struct net_device *dev, unsigned int prio)
1589 {
1590 	struct r8192_priv *priv = rtllib_priv(dev);
1591 	struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1592 
1593 	while (skb_queue_len(&ring->queue)) {
1594 		struct tx_desc *entry = &ring->desc[ring->idx];
1595 		struct sk_buff *skb = __skb_dequeue(&ring->queue);
1596 
1597 		pci_unmap_single(priv->pdev, entry->TxBuffAddr,
1598 			skb->len, PCI_DMA_TODEVICE);
1599 		kfree_skb(skb);
1600 		ring->idx = (ring->idx + 1) % ring->entries;
1601 	}
1602 
1603 	pci_free_consistent(priv->pdev, sizeof(*ring->desc) * ring->entries,
1604 	ring->desc, ring->dma);
1605 	ring->desc = NULL;
1606 }
1607 
_rtl92e_hard_data_xmit(struct sk_buff * skb,struct net_device * dev,int rate)1608 static void _rtl92e_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
1609 				   int rate)
1610 {
1611 	struct r8192_priv *priv = rtllib_priv(dev);
1612 	int ret;
1613 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1614 				    MAX_DEV_ADDR_SIZE);
1615 	u8 queue_index = tcb_desc->queue_index;
1616 
1617 	if ((priv->rtllib->eRFPowerState == eRfOff) || !priv->up ||
1618 	     priv->bResetInProgress) {
1619 		kfree_skb(skb);
1620 		return;
1621 	}
1622 
1623 	if (queue_index == TXCMD_QUEUE)
1624 		netdev_warn(dev, "%s(): queue index == TXCMD_QUEUE\n",
1625 			    __func__);
1626 
1627 	memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1628 	skb_push(skb, priv->rtllib->tx_headroom);
1629 	ret = _rtl92e_tx(dev, skb);
1630 	if (ret != 0)
1631 		kfree_skb(skb);
1632 
1633 	if (queue_index != MGNT_QUEUE) {
1634 		priv->rtllib->stats.tx_bytes += (skb->len -
1635 						 priv->rtllib->tx_headroom);
1636 		priv->rtllib->stats.tx_packets++;
1637 	}
1638 }
1639 
_rtl92e_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)1640 static int _rtl92e_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1641 {
1642 	struct r8192_priv *priv = rtllib_priv(dev);
1643 	int ret;
1644 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1645 				    MAX_DEV_ADDR_SIZE);
1646 	u8 queue_index = tcb_desc->queue_index;
1647 
1648 	if (queue_index != TXCMD_QUEUE) {
1649 		if ((priv->rtllib->eRFPowerState == eRfOff) ||
1650 		     !priv->up || priv->bResetInProgress) {
1651 			kfree_skb(skb);
1652 			return 0;
1653 		}
1654 	}
1655 
1656 	memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1657 	if (queue_index == TXCMD_QUEUE) {
1658 		_rtl92e_tx_cmd(dev, skb);
1659 		return 0;
1660 	}
1661 
1662 	tcb_desc->RATRIndex = 7;
1663 	tcb_desc->bTxDisableRateFallBack = 1;
1664 	tcb_desc->bTxUseDriverAssingedRate = 1;
1665 	tcb_desc->bTxEnableFwCalcDur = 1;
1666 	skb_push(skb, priv->rtllib->tx_headroom);
1667 	ret = _rtl92e_tx(dev, skb);
1668 	if (ret != 0)
1669 		kfree_skb(skb);
1670 	return ret;
1671 }
1672 
_rtl92e_tx_isr(struct net_device * dev,int prio)1673 static void _rtl92e_tx_isr(struct net_device *dev, int prio)
1674 {
1675 	struct r8192_priv *priv = rtllib_priv(dev);
1676 
1677 	struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1678 
1679 	while (skb_queue_len(&ring->queue)) {
1680 		struct tx_desc *entry = &ring->desc[ring->idx];
1681 		struct sk_buff *skb;
1682 
1683 		if (prio != BEACON_QUEUE) {
1684 			if (entry->OWN)
1685 				return;
1686 			ring->idx = (ring->idx + 1) % ring->entries;
1687 		}
1688 
1689 		skb = __skb_dequeue(&ring->queue);
1690 		pci_unmap_single(priv->pdev, entry->TxBuffAddr,
1691 		skb->len, PCI_DMA_TODEVICE);
1692 
1693 		kfree_skb(skb);
1694 	}
1695 	if (prio != BEACON_QUEUE)
1696 		tasklet_schedule(&priv->irq_tx_tasklet);
1697 }
1698 
_rtl92e_tx_cmd(struct net_device * dev,struct sk_buff * skb)1699 static void _rtl92e_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1700 {
1701 	struct r8192_priv *priv = rtllib_priv(dev);
1702 	struct rtl8192_tx_ring *ring;
1703 	struct tx_desc_cmd *entry;
1704 	unsigned int idx;
1705 	struct cb_desc *tcb_desc;
1706 	unsigned long flags;
1707 
1708 	spin_lock_irqsave(&priv->irq_th_lock, flags);
1709 	ring = &priv->tx_ring[TXCMD_QUEUE];
1710 
1711 	idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1712 	entry = (struct tx_desc_cmd *)&ring->desc[idx];
1713 
1714 	tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1715 
1716 	priv->ops->tx_fill_cmd_descriptor(dev, entry, tcb_desc, skb);
1717 
1718 	__skb_queue_tail(&ring->queue, skb);
1719 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1720 }
1721 
_rtl92e_tx(struct net_device * dev,struct sk_buff * skb)1722 static short _rtl92e_tx(struct net_device *dev, struct sk_buff *skb)
1723 {
1724 	struct r8192_priv *priv = rtllib_priv(dev);
1725 	struct rtl8192_tx_ring  *ring;
1726 	unsigned long flags;
1727 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1728 				    MAX_DEV_ADDR_SIZE);
1729 	struct tx_desc *pdesc = NULL;
1730 	struct rtllib_hdr_1addr *header = NULL;
1731 	u16 fc = 0, type = 0, stype = 0;
1732 	bool  multi_addr = false, broad_addr = false, uni_addr = false;
1733 	u8 *pda_addr = NULL;
1734 	int   idx;
1735 	u32 fwinfo_size = 0;
1736 
1737 	if (priv->bdisable_nic) {
1738 		netdev_warn(dev, "%s: Nic is disabled! Can't tx packet.\n",
1739 			    __func__);
1740 		return skb->len;
1741 	}
1742 
1743 	priv->rtllib->bAwakePktSent = true;
1744 
1745 	fwinfo_size = sizeof(struct tx_fwinfo_8190pci);
1746 
1747 	header = (struct rtllib_hdr_1addr *)(((u8 *)skb->data) + fwinfo_size);
1748 	fc = le16_to_cpu(header->frame_ctl);
1749 	type = WLAN_FC_GET_TYPE(fc);
1750 	stype = WLAN_FC_GET_STYPE(fc);
1751 	pda_addr = header->addr1;
1752 
1753 	if (is_broadcast_ether_addr(pda_addr))
1754 		broad_addr = true;
1755 	else if (is_multicast_ether_addr(pda_addr))
1756 		multi_addr = true;
1757 	else
1758 		uni_addr = true;
1759 
1760 	if (uni_addr)
1761 		priv->stats.txbytesunicast += skb->len - fwinfo_size;
1762 	else if (multi_addr)
1763 		priv->stats.txbytesmulticast += skb->len - fwinfo_size;
1764 	else
1765 		priv->stats.txbytesbroadcast += skb->len - fwinfo_size;
1766 
1767 	spin_lock_irqsave(&priv->irq_th_lock, flags);
1768 	ring = &priv->tx_ring[tcb_desc->queue_index];
1769 	if (tcb_desc->queue_index != BEACON_QUEUE)
1770 		idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1771 	else
1772 		idx = 0;
1773 
1774 	pdesc = &ring->desc[idx];
1775 	if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
1776 		netdev_warn(dev,
1777 			    "No more TX desc@%d, ring->idx = %d, idx = %d, skblen = 0x%x queuelen=%d",
1778 			    tcb_desc->queue_index, ring->idx, idx, skb->len,
1779 			    skb_queue_len(&ring->queue));
1780 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1781 		return skb->len;
1782 	}
1783 
1784 	if (type == RTLLIB_FTYPE_DATA) {
1785 		if (priv->rtllib->LedControlHandler)
1786 			priv->rtllib->LedControlHandler(dev, LED_CTL_TX);
1787 	}
1788 	priv->ops->tx_fill_descriptor(dev, pdesc, tcb_desc, skb);
1789 	__skb_queue_tail(&ring->queue, skb);
1790 	pdesc->OWN = 1;
1791 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1792 	netif_trans_update(dev);
1793 
1794 	rtl92e_writew(dev, TPPoll, 0x01 << tcb_desc->queue_index);
1795 	return 0;
1796 }
1797 
_rtl92e_alloc_rx_ring(struct net_device * dev)1798 static short _rtl92e_alloc_rx_ring(struct net_device *dev)
1799 {
1800 	struct r8192_priv *priv = rtllib_priv(dev);
1801 	struct rx_desc *entry = NULL;
1802 	int i, rx_queue_idx;
1803 
1804 	for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE; rx_queue_idx++) {
1805 		priv->rx_ring[rx_queue_idx] = pci_zalloc_consistent(priv->pdev,
1806 					      sizeof(*priv->rx_ring[rx_queue_idx]) * priv->rxringcount,
1807 					      &priv->rx_ring_dma[rx_queue_idx]);
1808 		if (!priv->rx_ring[rx_queue_idx] ||
1809 		    (unsigned long)priv->rx_ring[rx_queue_idx] & 0xFF) {
1810 			netdev_warn(dev, "Cannot allocate RX ring\n");
1811 			return -ENOMEM;
1812 		}
1813 
1814 		priv->rx_idx[rx_queue_idx] = 0;
1815 
1816 		for (i = 0; i < priv->rxringcount; i++) {
1817 			struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
1818 			dma_addr_t *mapping;
1819 
1820 			entry = &priv->rx_ring[rx_queue_idx][i];
1821 			if (!skb)
1822 				return 0;
1823 			skb->dev = dev;
1824 			priv->rx_buf[rx_queue_idx][i] = skb;
1825 			mapping = (dma_addr_t *)skb->cb;
1826 			*mapping = pci_map_single(priv->pdev,
1827 						  skb_tail_pointer_rsl(skb),
1828 						  priv->rxbuffersize,
1829 						  PCI_DMA_FROMDEVICE);
1830 			if (pci_dma_mapping_error(priv->pdev, *mapping)) {
1831 				dev_kfree_skb_any(skb);
1832 				return -1;
1833 			}
1834 			entry->BufferAddress = *mapping;
1835 
1836 			entry->Length = priv->rxbuffersize;
1837 			entry->OWN = 1;
1838 		}
1839 
1840 		if (entry)
1841 			entry->EOR = 1;
1842 	}
1843 	return 0;
1844 }
1845 
_rtl92e_alloc_tx_ring(struct net_device * dev,unsigned int prio,unsigned int entries)1846 static int _rtl92e_alloc_tx_ring(struct net_device *dev, unsigned int prio,
1847 				 unsigned int entries)
1848 {
1849 	struct r8192_priv *priv = rtllib_priv(dev);
1850 	struct tx_desc *ring;
1851 	dma_addr_t dma;
1852 	int i;
1853 
1854 	ring = pci_zalloc_consistent(priv->pdev, sizeof(*ring) * entries, &dma);
1855 	if (!ring || (unsigned long)ring & 0xFF) {
1856 		netdev_warn(dev, "Cannot allocate TX ring (prio = %d)\n", prio);
1857 		return -ENOMEM;
1858 	}
1859 
1860 	priv->tx_ring[prio].desc = ring;
1861 	priv->tx_ring[prio].dma = dma;
1862 	priv->tx_ring[prio].idx = 0;
1863 	priv->tx_ring[prio].entries = entries;
1864 	skb_queue_head_init(&priv->tx_ring[prio].queue);
1865 
1866 	for (i = 0; i < entries; i++)
1867 		ring[i].NextDescAddress =
1868 			(u32)dma + ((i + 1) % entries) *
1869 			sizeof(*ring);
1870 
1871 	return 0;
1872 }
1873 
_rtl92e_pci_initdescring(struct net_device * dev)1874 static short _rtl92e_pci_initdescring(struct net_device *dev)
1875 {
1876 	u32 ret;
1877 	int i;
1878 	struct r8192_priv *priv = rtllib_priv(dev);
1879 
1880 	ret = _rtl92e_alloc_rx_ring(dev);
1881 	if (ret)
1882 		return ret;
1883 
1884 	for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1885 		ret = _rtl92e_alloc_tx_ring(dev, i, priv->txringcount);
1886 		if (ret)
1887 			goto err_free_rings;
1888 	}
1889 
1890 	return 0;
1891 
1892 err_free_rings:
1893 	_rtl92e_free_rx_ring(dev);
1894 	for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
1895 		if (priv->tx_ring[i].desc)
1896 			_rtl92e_free_tx_ring(dev, i);
1897 	return 1;
1898 }
1899 
rtl92e_reset_desc_ring(struct net_device * dev)1900 void rtl92e_reset_desc_ring(struct net_device *dev)
1901 {
1902 	struct r8192_priv *priv = rtllib_priv(dev);
1903 	int i, rx_queue_idx;
1904 	unsigned long flags = 0;
1905 
1906 	for (rx_queue_idx = 0; rx_queue_idx < MAX_RX_QUEUE; rx_queue_idx++) {
1907 		if (priv->rx_ring[rx_queue_idx]) {
1908 			struct rx_desc *entry = NULL;
1909 
1910 			for (i = 0; i < priv->rxringcount; i++) {
1911 				entry = &priv->rx_ring[rx_queue_idx][i];
1912 				entry->OWN = 1;
1913 			}
1914 			priv->rx_idx[rx_queue_idx] = 0;
1915 		}
1916 	}
1917 
1918 	spin_lock_irqsave(&priv->irq_th_lock, flags);
1919 	for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1920 		if (priv->tx_ring[i].desc) {
1921 			struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
1922 
1923 			while (skb_queue_len(&ring->queue)) {
1924 				struct tx_desc *entry = &ring->desc[ring->idx];
1925 				struct sk_buff *skb =
1926 						 __skb_dequeue(&ring->queue);
1927 
1928 				pci_unmap_single(priv->pdev,
1929 						 entry->TxBuffAddr,
1930 						 skb->len, PCI_DMA_TODEVICE);
1931 				kfree_skb(skb);
1932 				ring->idx = (ring->idx + 1) % ring->entries;
1933 			}
1934 			ring->idx = 0;
1935 		}
1936 	}
1937 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1938 }
1939 
rtl92e_update_rx_pkt_timestamp(struct net_device * dev,struct rtllib_rx_stats * stats)1940 void rtl92e_update_rx_pkt_timestamp(struct net_device *dev,
1941 				    struct rtllib_rx_stats *stats)
1942 {
1943 	struct r8192_priv *priv = rtllib_priv(dev);
1944 
1945 	if (stats->bIsAMPDU && !stats->bFirstMPDU)
1946 		stats->mac_time = priv->LastRxDescTSF;
1947 	else
1948 		priv->LastRxDescTSF = stats->mac_time;
1949 }
1950 
rtl92e_translate_to_dbm(struct r8192_priv * priv,u8 signal_strength_index)1951 long rtl92e_translate_to_dbm(struct r8192_priv *priv, u8 signal_strength_index)
1952 {
1953 	long	signal_power;
1954 
1955 	signal_power = (long)((signal_strength_index + 1) >> 1);
1956 	signal_power -= 95;
1957 
1958 	return signal_power;
1959 }
1960 
1961 
rtl92e_update_rx_statistics(struct r8192_priv * priv,struct rtllib_rx_stats * pprevious_stats)1962 void rtl92e_update_rx_statistics(struct r8192_priv *priv,
1963 				 struct rtllib_rx_stats *pprevious_stats)
1964 {
1965 	int weighting = 0;
1966 
1967 
1968 	if (priv->stats.recv_signal_power == 0)
1969 		priv->stats.recv_signal_power =
1970 					 pprevious_stats->RecvSignalPower;
1971 
1972 	if (pprevious_stats->RecvSignalPower > priv->stats.recv_signal_power)
1973 		weighting = 5;
1974 	else if (pprevious_stats->RecvSignalPower <
1975 		 priv->stats.recv_signal_power)
1976 		weighting = (-5);
1977 	priv->stats.recv_signal_power = (priv->stats.recv_signal_power * 5 +
1978 					pprevious_stats->RecvSignalPower +
1979 					weighting) / 6;
1980 }
1981 
rtl92e_rx_db_to_percent(s8 antpower)1982 u8 rtl92e_rx_db_to_percent(s8 antpower)
1983 {
1984 	if ((antpower <= -100) || (antpower >= 20))
1985 		return	0;
1986 	else if (antpower >= 0)
1987 		return	100;
1988 	else
1989 		return	100 + antpower;
1990 
1991 }	/* QueryRxPwrPercentage */
1992 
rtl92e_evm_db_to_percent(s8 value)1993 u8 rtl92e_evm_db_to_percent(s8 value)
1994 {
1995 	s8 ret_val;
1996 
1997 	ret_val = value;
1998 
1999 	if (ret_val >= 0)
2000 		ret_val = 0;
2001 	if (ret_val <= -33)
2002 		ret_val = -33;
2003 	ret_val = 0 - ret_val;
2004 	ret_val *= 3;
2005 	if (ret_val == 99)
2006 		ret_val = 100;
2007 	return ret_val;
2008 }
2009 
rtl92e_copy_mpdu_stats(struct rtllib_rx_stats * psrc_stats,struct rtllib_rx_stats * ptarget_stats)2010 void rtl92e_copy_mpdu_stats(struct rtllib_rx_stats *psrc_stats,
2011 			    struct rtllib_rx_stats *ptarget_stats)
2012 {
2013 	ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
2014 	ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
2015 }
2016 
2017 
2018 
_rtl92e_rx_normal(struct net_device * dev)2019 static void _rtl92e_rx_normal(struct net_device *dev)
2020 {
2021 	struct r8192_priv *priv = rtllib_priv(dev);
2022 	struct rtllib_hdr_1addr *rtllib_hdr = NULL;
2023 	bool unicast_packet = false;
2024 	bool bLedBlinking = true;
2025 	u16 fc = 0, type = 0;
2026 	u32 skb_len = 0;
2027 	int rx_queue_idx = RX_MPDU_QUEUE;
2028 
2029 	struct rtllib_rx_stats stats = {
2030 		.signal = 0,
2031 		.noise = (u8)-98,
2032 		.rate = 0,
2033 		.freq = RTLLIB_24GHZ_BAND,
2034 	};
2035 	unsigned int count = priv->rxringcount;
2036 
2037 	stats.nic_type = NIC_8192E;
2038 
2039 	while (count--) {
2040 		struct rx_desc *pdesc = &priv->rx_ring[rx_queue_idx]
2041 					[priv->rx_idx[rx_queue_idx]];
2042 		struct sk_buff *skb = priv->rx_buf[rx_queue_idx]
2043 				      [priv->rx_idx[rx_queue_idx]];
2044 		struct sk_buff *new_skb;
2045 
2046 		if (pdesc->OWN)
2047 			return;
2048 		if (!priv->ops->rx_query_status_descriptor(dev, &stats,
2049 		pdesc, skb))
2050 			goto done;
2051 		new_skb = dev_alloc_skb(priv->rxbuffersize);
2052 		/* if allocation of new skb failed - drop current packet
2053 		 * and reuse skb
2054 		 */
2055 		if (unlikely(!new_skb))
2056 			goto done;
2057 
2058 		pci_unmap_single(priv->pdev,
2059 				*((dma_addr_t *)skb->cb),
2060 				priv->rxbuffersize,
2061 				PCI_DMA_FROMDEVICE);
2062 
2063 		skb_put(skb, pdesc->Length);
2064 		skb_reserve(skb, stats.RxDrvInfoSize +
2065 			stats.RxBufShift);
2066 		skb_trim(skb, skb->len - 4/*sCrcLng*/);
2067 		rtllib_hdr = (struct rtllib_hdr_1addr *)skb->data;
2068 		if (!is_multicast_ether_addr(rtllib_hdr->addr1)) {
2069 			/* unicast packet */
2070 			unicast_packet = true;
2071 		}
2072 		fc = le16_to_cpu(rtllib_hdr->frame_ctl);
2073 		type = WLAN_FC_GET_TYPE(fc);
2074 		if (type == RTLLIB_FTYPE_MGMT)
2075 			bLedBlinking = false;
2076 
2077 		if (bLedBlinking)
2078 			if (priv->rtllib->LedControlHandler)
2079 				priv->rtllib->LedControlHandler(dev,
2080 							LED_CTL_RX);
2081 
2082 		if (stats.bCRC) {
2083 			if (type != RTLLIB_FTYPE_MGMT)
2084 				priv->stats.rxdatacrcerr++;
2085 			else
2086 				priv->stats.rxmgmtcrcerr++;
2087 		}
2088 
2089 		skb_len = skb->len;
2090 
2091 		if (!rtllib_rx(priv->rtllib, skb, &stats)) {
2092 			dev_kfree_skb_any(skb);
2093 		} else {
2094 			priv->stats.rxok++;
2095 			if (unicast_packet)
2096 				priv->stats.rxbytesunicast += skb_len;
2097 		}
2098 
2099 		skb = new_skb;
2100 		skb->dev = dev;
2101 
2102 		priv->rx_buf[rx_queue_idx][priv->rx_idx[rx_queue_idx]] =
2103 								 skb;
2104 		*((dma_addr_t *)skb->cb) = pci_map_single(priv->pdev,
2105 					    skb_tail_pointer_rsl(skb),
2106 					    priv->rxbuffersize,
2107 					    PCI_DMA_FROMDEVICE);
2108 		if (pci_dma_mapping_error(priv->pdev,
2109 					  *((dma_addr_t *)skb->cb))) {
2110 			dev_kfree_skb_any(skb);
2111 			return;
2112 		}
2113 done:
2114 		pdesc->BufferAddress = *((dma_addr_t *)skb->cb);
2115 		pdesc->OWN = 1;
2116 		pdesc->Length = priv->rxbuffersize;
2117 		if (priv->rx_idx[rx_queue_idx] == priv->rxringcount - 1)
2118 			pdesc->EOR = 1;
2119 		priv->rx_idx[rx_queue_idx] = (priv->rx_idx[rx_queue_idx] + 1) %
2120 					      priv->rxringcount;
2121 	}
2122 
2123 }
2124 
_rtl92e_tx_resume(struct net_device * dev)2125 static void _rtl92e_tx_resume(struct net_device *dev)
2126 {
2127 	struct r8192_priv *priv = rtllib_priv(dev);
2128 	struct rtllib_device *ieee = priv->rtllib;
2129 	struct sk_buff *skb;
2130 	int queue_index;
2131 
2132 	for (queue_index = BK_QUEUE;
2133 	     queue_index < MAX_QUEUE_SIZE; queue_index++) {
2134 		while ((!skb_queue_empty(&ieee->skb_waitQ[queue_index])) &&
2135 		(priv->rtllib->check_nic_enough_desc(dev, queue_index) > 0)) {
2136 			skb = skb_dequeue(&ieee->skb_waitQ[queue_index]);
2137 			ieee->softmac_data_hard_start_xmit(skb, dev, 0);
2138 		}
2139 	}
2140 }
2141 
_rtl92e_irq_tx_tasklet(struct r8192_priv * priv)2142 static void _rtl92e_irq_tx_tasklet(struct r8192_priv *priv)
2143 {
2144 	_rtl92e_tx_resume(priv->rtllib->dev);
2145 }
2146 
_rtl92e_irq_rx_tasklet(struct r8192_priv * priv)2147 static void _rtl92e_irq_rx_tasklet(struct r8192_priv *priv)
2148 {
2149 	_rtl92e_rx_normal(priv->rtllib->dev);
2150 
2151 	rtl92e_writel(priv->rtllib->dev, INTA_MASK,
2152 		      rtl92e_readl(priv->rtllib->dev, INTA_MASK) | IMR_RDU);
2153 }
2154 
2155 /****************************************************************************
2156  * ---------------------------- NIC START/CLOSE STUFF---------------------------
2157  ****************************************************************************/
_rtl92e_cancel_deferred_work(struct r8192_priv * priv)2158 static void _rtl92e_cancel_deferred_work(struct r8192_priv *priv)
2159 {
2160 	cancel_delayed_work_sync(&priv->watch_dog_wq);
2161 	cancel_delayed_work_sync(&priv->update_beacon_wq);
2162 	cancel_delayed_work(&priv->rtllib->hw_sleep_wq);
2163 	cancel_work_sync(&priv->reset_wq);
2164 	cancel_work_sync(&priv->qos_activate);
2165 }
2166 
_rtl92e_up(struct net_device * dev,bool is_silent_reset)2167 static int _rtl92e_up(struct net_device *dev, bool is_silent_reset)
2168 {
2169 	if (_rtl92e_sta_up(dev, is_silent_reset) == -1)
2170 		return -1;
2171 	return 0;
2172 }
2173 
_rtl92e_open(struct net_device * dev)2174 static int _rtl92e_open(struct net_device *dev)
2175 {
2176 	struct r8192_priv *priv = rtllib_priv(dev);
2177 	int ret;
2178 
2179 	mutex_lock(&priv->wx_mutex);
2180 	ret = _rtl92e_try_up(dev);
2181 	mutex_unlock(&priv->wx_mutex);
2182 	return ret;
2183 
2184 }
2185 
_rtl92e_try_up(struct net_device * dev)2186 static int _rtl92e_try_up(struct net_device *dev)
2187 {
2188 	struct r8192_priv *priv = rtllib_priv(dev);
2189 
2190 	if (priv->up == 1)
2191 		return -1;
2192 	return _rtl92e_up(dev, false);
2193 }
2194 
2195 
_rtl92e_close(struct net_device * dev)2196 static int _rtl92e_close(struct net_device *dev)
2197 {
2198 	struct r8192_priv *priv = rtllib_priv(dev);
2199 	int ret;
2200 
2201 	if ((rtllib_act_scanning(priv->rtllib, false)) &&
2202 		!(priv->rtllib->softmac_features & IEEE_SOFTMAC_SCAN)) {
2203 		rtllib_stop_scan(priv->rtllib);
2204 	}
2205 
2206 	mutex_lock(&priv->wx_mutex);
2207 
2208 	ret = _rtl92e_down(dev, true);
2209 
2210 	mutex_unlock(&priv->wx_mutex);
2211 
2212 	return ret;
2213 
2214 }
2215 
_rtl92e_down(struct net_device * dev,bool shutdownrf)2216 static int _rtl92e_down(struct net_device *dev, bool shutdownrf)
2217 {
2218 	if (_rtl92e_sta_down(dev, shutdownrf) == -1)
2219 		return -1;
2220 
2221 	return 0;
2222 }
2223 
rtl92e_commit(struct net_device * dev)2224 void rtl92e_commit(struct net_device *dev)
2225 {
2226 	struct r8192_priv *priv = rtllib_priv(dev);
2227 
2228 	if (priv->up == 0)
2229 		return;
2230 	rtllib_softmac_stop_protocol(priv->rtllib, 0, true);
2231 	rtl92e_irq_disable(dev);
2232 	priv->ops->stop_adapter(dev, true);
2233 	_rtl92e_up(dev, false);
2234 }
2235 
_rtl92e_restart(void * data)2236 static void _rtl92e_restart(void *data)
2237 {
2238 	struct r8192_priv *priv = container_of_work_rsl(data, struct r8192_priv,
2239 				  reset_wq);
2240 	struct net_device *dev = priv->rtllib->dev;
2241 
2242 	mutex_lock(&priv->wx_mutex);
2243 
2244 	rtl92e_commit(dev);
2245 
2246 	mutex_unlock(&priv->wx_mutex);
2247 }
2248 
_rtl92e_set_multicast(struct net_device * dev)2249 static void _rtl92e_set_multicast(struct net_device *dev)
2250 {
2251 	struct r8192_priv *priv = rtllib_priv(dev);
2252 	short promisc;
2253 
2254 	promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
2255 	priv->promisc = promisc;
2256 
2257 }
2258 
2259 
_rtl92e_set_mac_adr(struct net_device * dev,void * mac)2260 static int _rtl92e_set_mac_adr(struct net_device *dev, void *mac)
2261 {
2262 	struct r8192_priv *priv = rtllib_priv(dev);
2263 	struct sockaddr *addr = mac;
2264 
2265 	mutex_lock(&priv->wx_mutex);
2266 
2267 	ether_addr_copy(dev->dev_addr, addr->sa_data);
2268 
2269 	schedule_work(&priv->reset_wq);
2270 	mutex_unlock(&priv->wx_mutex);
2271 
2272 	return 0;
2273 }
2274 
_rtl92e_irq(int irq,void * netdev)2275 static irqreturn_t _rtl92e_irq(int irq, void *netdev)
2276 {
2277 	struct net_device *dev = netdev;
2278 	struct r8192_priv *priv = rtllib_priv(dev);
2279 	unsigned long flags;
2280 	u32 inta;
2281 	u32 intb;
2282 
2283 	intb = 0;
2284 
2285 	if (priv->irq_enabled == 0)
2286 		goto done;
2287 
2288 	spin_lock_irqsave(&priv->irq_th_lock, flags);
2289 
2290 	priv->ops->interrupt_recognized(dev, &inta, &intb);
2291 	priv->stats.shints++;
2292 
2293 	if (!inta) {
2294 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2295 		goto done;
2296 	}
2297 
2298 	if (inta == 0xffff) {
2299 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2300 		goto done;
2301 	}
2302 
2303 	priv->stats.ints++;
2304 
2305 	if (!netif_running(dev)) {
2306 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2307 		goto done;
2308 	}
2309 
2310 	if (inta & IMR_TBDOK) {
2311 		RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
2312 		priv->stats.txbeaconokint++;
2313 	}
2314 
2315 	if (inta & IMR_TBDER) {
2316 		RT_TRACE(COMP_INTR, "beacon ok interrupt!\n");
2317 		priv->stats.txbeaconerr++;
2318 	}
2319 
2320 	if (inta & IMR_BDOK)
2321 		RT_TRACE(COMP_INTR, "beacon interrupt!\n");
2322 
2323 	if (inta  & IMR_MGNTDOK) {
2324 		RT_TRACE(COMP_INTR, "Manage ok interrupt!\n");
2325 		priv->stats.txmanageokint++;
2326 		_rtl92e_tx_isr(dev, MGNT_QUEUE);
2327 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2328 		if (priv->rtllib->ack_tx_to_ieee) {
2329 			if (_rtl92e_is_tx_queue_empty(dev)) {
2330 				priv->rtllib->ack_tx_to_ieee = 0;
2331 				rtllib_ps_tx_ack(priv->rtllib, 1);
2332 			}
2333 		}
2334 		spin_lock_irqsave(&priv->irq_th_lock, flags);
2335 	}
2336 
2337 	if (inta & IMR_COMDOK) {
2338 		priv->stats.txcmdpktokint++;
2339 		_rtl92e_tx_isr(dev, TXCMD_QUEUE);
2340 	}
2341 
2342 	if (inta & IMR_HIGHDOK)
2343 		_rtl92e_tx_isr(dev, HIGH_QUEUE);
2344 
2345 	if (inta & IMR_ROK) {
2346 		priv->stats.rxint++;
2347 		priv->InterruptLog.nIMR_ROK++;
2348 		tasklet_schedule(&priv->irq_rx_tasklet);
2349 	}
2350 
2351 	if (inta & IMR_BcnInt) {
2352 		RT_TRACE(COMP_INTR, "prepare beacon for interrupt!\n");
2353 		tasklet_schedule(&priv->irq_prepare_beacon_tasklet);
2354 	}
2355 
2356 	if (inta & IMR_RDU) {
2357 		RT_TRACE(COMP_INTR, "rx descriptor unavailable!\n");
2358 		priv->stats.rxrdu++;
2359 		rtl92e_writel(dev, INTA_MASK,
2360 			      rtl92e_readl(dev, INTA_MASK) & ~IMR_RDU);
2361 		tasklet_schedule(&priv->irq_rx_tasklet);
2362 	}
2363 
2364 	if (inta & IMR_RXFOVW) {
2365 		RT_TRACE(COMP_INTR, "rx overflow !\n");
2366 		priv->stats.rxoverflow++;
2367 		tasklet_schedule(&priv->irq_rx_tasklet);
2368 	}
2369 
2370 	if (inta & IMR_TXFOVW)
2371 		priv->stats.txoverflow++;
2372 
2373 	if (inta & IMR_BKDOK) {
2374 		RT_TRACE(COMP_INTR, "BK Tx OK interrupt!\n");
2375 		priv->stats.txbkokint++;
2376 		priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2377 		_rtl92e_tx_isr(dev, BK_QUEUE);
2378 	}
2379 
2380 	if (inta & IMR_BEDOK) {
2381 		RT_TRACE(COMP_INTR, "BE TX OK interrupt!\n");
2382 		priv->stats.txbeokint++;
2383 		priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2384 		_rtl92e_tx_isr(dev, BE_QUEUE);
2385 	}
2386 
2387 	if (inta & IMR_VIDOK) {
2388 		RT_TRACE(COMP_INTR, "VI TX OK interrupt!\n");
2389 		priv->stats.txviokint++;
2390 		priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2391 		_rtl92e_tx_isr(dev, VI_QUEUE);
2392 	}
2393 
2394 	if (inta & IMR_VODOK) {
2395 		priv->stats.txvookint++;
2396 		RT_TRACE(COMP_INTR, "Vo TX OK interrupt!\n");
2397 		priv->rtllib->LinkDetectInfo.NumTxOkInPeriod++;
2398 		_rtl92e_tx_isr(dev, VO_QUEUE);
2399 	}
2400 
2401 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
2402 
2403 done:
2404 
2405 	return IRQ_HANDLED;
2406 }
2407 
2408 
2409 
2410 /****************************************************************************
2411  * ---------------------------- PCI_STUFF---------------------------
2412  ****************************************************************************/
2413 static const struct net_device_ops rtl8192_netdev_ops = {
2414 	.ndo_open = _rtl92e_open,
2415 	.ndo_stop = _rtl92e_close,
2416 	.ndo_tx_timeout = _rtl92e_tx_timeout,
2417 	.ndo_set_rx_mode = _rtl92e_set_multicast,
2418 	.ndo_set_mac_address = _rtl92e_set_mac_adr,
2419 	.ndo_validate_addr = eth_validate_addr,
2420 	.ndo_start_xmit = rtllib_xmit,
2421 };
2422 
_rtl92e_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)2423 static int _rtl92e_pci_probe(struct pci_dev *pdev,
2424 			     const struct pci_device_id *id)
2425 {
2426 	unsigned long ioaddr = 0;
2427 	struct net_device *dev = NULL;
2428 	struct r8192_priv *priv = NULL;
2429 	struct rtl819x_ops *ops = (struct rtl819x_ops *)(id->driver_data);
2430 	unsigned long pmem_start, pmem_len, pmem_flags;
2431 	int err = -ENOMEM;
2432 	u8 revision_id;
2433 
2434 	RT_TRACE(COMP_INIT, "Configuring chip resources");
2435 
2436 	if (pci_enable_device(pdev)) {
2437 		dev_err(&pdev->dev, "Failed to enable PCI device");
2438 		return -EIO;
2439 	}
2440 
2441 	pci_set_master(pdev);
2442 
2443 	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
2444 		if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2445 			dev_info(&pdev->dev,
2446 				 "Unable to obtain 32bit DMA for consistent allocations\n");
2447 			goto err_pci_disable;
2448 		}
2449 	}
2450 	dev = alloc_rtllib(sizeof(struct r8192_priv));
2451 	if (!dev)
2452 		goto err_pci_disable;
2453 
2454 	err = -ENODEV;
2455 
2456 	pci_set_drvdata(pdev, dev);
2457 	SET_NETDEV_DEV(dev, &pdev->dev);
2458 	priv = rtllib_priv(dev);
2459 	priv->rtllib = (struct rtllib_device *)netdev_priv_rsl(dev);
2460 	priv->pdev = pdev;
2461 	priv->rtllib->pdev = pdev;
2462 	if ((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK) &&
2463 	    (pdev->subsystem_device == 0x3304))
2464 		priv->rtllib->bSupportRemoteWakeUp = 1;
2465 	else
2466 		priv->rtllib->bSupportRemoteWakeUp = 0;
2467 
2468 	pmem_start = pci_resource_start(pdev, 1);
2469 	pmem_len = pci_resource_len(pdev, 1);
2470 	pmem_flags = pci_resource_flags(pdev, 1);
2471 
2472 	if (!(pmem_flags & IORESOURCE_MEM)) {
2473 		netdev_err(dev, "region #1 not a MMIO resource, aborting");
2474 		goto err_rel_rtllib;
2475 	}
2476 
2477 	dev_info(&pdev->dev, "Memory mapped space start: 0x%08lx\n",
2478 		 pmem_start);
2479 	if (!request_mem_region(pmem_start, pmem_len, DRV_NAME)) {
2480 		netdev_err(dev, "request_mem_region failed!");
2481 		goto err_rel_rtllib;
2482 	}
2483 
2484 
2485 	ioaddr = (unsigned long)ioremap_nocache(pmem_start, pmem_len);
2486 	if (ioaddr == (unsigned long)NULL) {
2487 		netdev_err(dev, "ioremap failed!");
2488 		goto err_rel_mem;
2489 	}
2490 
2491 	dev->mem_start = ioaddr;
2492 	dev->mem_end = ioaddr + pci_resource_len(pdev, 0);
2493 
2494 	pci_read_config_byte(pdev, 0x08, &revision_id);
2495 	/* If the revisionid is 0x10, the device uses rtl8192se. */
2496 	if (pdev->device == 0x8192 && revision_id == 0x10)
2497 		goto err_unmap;
2498 
2499 	priv->ops = ops;
2500 
2501 	if (rtl92e_check_adapter(pdev, dev) == false)
2502 		goto err_unmap;
2503 
2504 	dev->irq = pdev->irq;
2505 	priv->irq = 0;
2506 
2507 	dev->netdev_ops = &rtl8192_netdev_ops;
2508 
2509 	dev->wireless_handlers = &r8192_wx_handlers_def;
2510 	dev->ethtool_ops = &rtl819x_ethtool_ops;
2511 
2512 	dev->type = ARPHRD_ETHER;
2513 	dev->watchdog_timeo = HZ * 3;
2514 
2515 	if (dev_alloc_name(dev, ifname) < 0) {
2516 		RT_TRACE(COMP_INIT,
2517 			 "Oops: devname already taken! Trying wlan%%d...\n");
2518 			dev_alloc_name(dev, ifname);
2519 	}
2520 
2521 	RT_TRACE(COMP_INIT, "Driver probe completed1\n");
2522 	if (_rtl92e_init(dev) != 0) {
2523 		netdev_warn(dev, "Initialization failed");
2524 		goto err_free_irq;
2525 	}
2526 
2527 	netif_carrier_off(dev);
2528 	netif_stop_queue(dev);
2529 
2530 	if (register_netdev(dev))
2531 		goto err_free_irq;
2532 	RT_TRACE(COMP_INIT, "dev name: %s\n", dev->name);
2533 
2534 	if (priv->polling_timer_on == 0)
2535 		rtl92e_check_rfctrl_gpio_timer(&priv->gpio_polling_timer);
2536 
2537 	RT_TRACE(COMP_INIT, "Driver probe completed\n");
2538 	return 0;
2539 
2540 err_free_irq:
2541 	free_irq(dev->irq, dev);
2542 	priv->irq = 0;
2543 err_unmap:
2544 	iounmap((void __iomem *)ioaddr);
2545 err_rel_mem:
2546 	release_mem_region(pmem_start, pmem_len);
2547 err_rel_rtllib:
2548 	free_rtllib(dev);
2549 err_pci_disable:
2550 	pci_disable_device(pdev);
2551 	return err;
2552 }
2553 
_rtl92e_pci_disconnect(struct pci_dev * pdev)2554 static void _rtl92e_pci_disconnect(struct pci_dev *pdev)
2555 {
2556 	struct net_device *dev = pci_get_drvdata(pdev);
2557 	struct r8192_priv *priv;
2558 	u32 i;
2559 
2560 	if (dev) {
2561 		unregister_netdev(dev);
2562 
2563 		priv = rtllib_priv(dev);
2564 
2565 		del_timer_sync(&priv->gpio_polling_timer);
2566 		cancel_delayed_work_sync(&priv->gpio_change_rf_wq);
2567 		priv->polling_timer_on = 0;
2568 		_rtl92e_down(dev, true);
2569 		rtl92e_dm_deinit(dev);
2570 		vfree(priv->pFirmware);
2571 		priv->pFirmware = NULL;
2572 		_rtl92e_free_rx_ring(dev);
2573 		for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
2574 			_rtl92e_free_tx_ring(dev, i);
2575 
2576 		if (priv->irq) {
2577 			dev_info(&pdev->dev, "Freeing irq %d\n", dev->irq);
2578 			free_irq(dev->irq, dev);
2579 			priv->irq = 0;
2580 		}
2581 		free_rtllib(dev);
2582 
2583 		if (dev->mem_start != 0) {
2584 			iounmap((void __iomem *)dev->mem_start);
2585 			release_mem_region(pci_resource_start(pdev, 1),
2586 					pci_resource_len(pdev, 1));
2587 		}
2588 	} else {
2589 		priv = rtllib_priv(dev);
2590 	}
2591 
2592 	pci_disable_device(pdev);
2593 	RT_TRACE(COMP_DOWN, "wlan driver removed\n");
2594 }
2595 
rtl92e_enable_nic(struct net_device * dev)2596 bool rtl92e_enable_nic(struct net_device *dev)
2597 {
2598 	bool init_status = true;
2599 	struct r8192_priv *priv = rtllib_priv(dev);
2600 	struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
2601 					(&priv->rtllib->PowerSaveControl);
2602 
2603 	if (!priv->up) {
2604 		netdev_warn(dev, "%s(): Driver is already down!\n", __func__);
2605 		priv->bdisable_nic = false;
2606 		return false;
2607 	}
2608 
2609 	RT_TRACE(COMP_PS, "===========>%s()\n", __func__);
2610 	priv->bfirst_init = true;
2611 	init_status = priv->ops->initialize_adapter(dev);
2612 	if (!init_status) {
2613 		netdev_warn(dev, "%s(): Initialization failed!\n", __func__);
2614 		priv->bdisable_nic = false;
2615 		return false;
2616 	}
2617 	RT_TRACE(COMP_INIT, "start adapter finished\n");
2618 	RT_CLEAR_PS_LEVEL(pPSC, RT_RF_OFF_LEVL_HALT_NIC);
2619 	priv->bfirst_init = false;
2620 
2621 	rtl92e_irq_enable(dev);
2622 	priv->bdisable_nic = false;
2623 	RT_TRACE(COMP_PS, "<===========%s()\n", __func__);
2624 	return init_status;
2625 }
2626 
rtl92e_disable_nic(struct net_device * dev)2627 bool rtl92e_disable_nic(struct net_device *dev)
2628 {
2629 	struct r8192_priv *priv = rtllib_priv(dev);
2630 	u8 tmp_state = 0;
2631 
2632 	RT_TRACE(COMP_PS, "=========>%s()\n", __func__);
2633 	priv->bdisable_nic = true;
2634 	tmp_state = priv->rtllib->state;
2635 	rtllib_softmac_stop_protocol(priv->rtllib, 0, false);
2636 	priv->rtllib->state = tmp_state;
2637 	_rtl92e_cancel_deferred_work(priv);
2638 	rtl92e_irq_disable(dev);
2639 
2640 	priv->ops->stop_adapter(dev, false);
2641 	RT_TRACE(COMP_PS, "<=========%s()\n", __func__);
2642 
2643 	return true;
2644 }
2645 
2646 module_pci_driver(rtl8192_pci_driver);
2647 
rtl92e_check_rfctrl_gpio_timer(struct timer_list * t)2648 void rtl92e_check_rfctrl_gpio_timer(struct timer_list *t)
2649 {
2650 	struct r8192_priv *priv = from_timer(priv, t, gpio_polling_timer);
2651 
2652 	priv->polling_timer_on = 1;
2653 
2654 	schedule_delayed_work(&priv->gpio_change_rf_wq, 0);
2655 
2656 	mod_timer(&priv->gpio_polling_timer, jiffies +
2657 		  msecs_to_jiffies(RTLLIB_WATCH_DOG_TIME));
2658 }
2659 
2660 /***************************************************************************
2661  * ------------------- module init / exit stubs ----------------
2662  ***************************************************************************/
2663 MODULE_DESCRIPTION("Linux driver for Realtek RTL819x WiFi cards");
2664 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
2665 MODULE_VERSION(DRV_VERSION);
2666 MODULE_LICENSE("GPL");
2667 MODULE_FIRMWARE(RTL8192E_BOOT_IMG_FW);
2668 MODULE_FIRMWARE(RTL8192E_MAIN_IMG_FW);
2669 MODULE_FIRMWARE(RTL8192E_DATA_IMG_FW);
2670 
2671 module_param(ifname, charp, 0644);
2672 module_param(hwwep, int, 0644);
2673 module_param(channels, int, 0644);
2674 
2675 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
2676 MODULE_PARM_DESC(hwwep, " Try to use hardware WEP support(default use hw. set 0 to use software security)");
2677 MODULE_PARM_DESC(channels, " Channel bitmask for specific locales. NYI");
2678