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