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 static const struct pci_device_id rtl8192_pci_id_tbl[] = {
29 	{PCI_DEVICE(0x10ec, 0x8192)},
30 	{PCI_DEVICE(0x07aa, 0x0044)},
31 	{PCI_DEVICE(0x07aa, 0x0047)},
32 	{}
33 };
34 
35 MODULE_DEVICE_TABLE(pci, rtl8192_pci_id_tbl);
36 
37 static int _rtl92e_pci_probe(struct pci_dev *pdev,
38 			     const struct pci_device_id *id);
39 static void _rtl92e_pci_disconnect(struct pci_dev *pdev);
40 static irqreturn_t _rtl92e_irq(int irq, void *netdev);
41 
42 static SIMPLE_DEV_PM_OPS(rtl92e_pm_ops, rtl92e_suspend, rtl92e_resume);
43 
44 static struct pci_driver rtl8192_pci_driver = {
45 	.name = DRV_NAME,	/* Driver name   */
46 	.id_table = rtl8192_pci_id_tbl,	/* PCI_ID table  */
47 	.probe	= _rtl92e_pci_probe,	/* probe fn      */
48 	.remove	 = _rtl92e_pci_disconnect,	/* remove fn */
49 	.driver.pm = &rtl92e_pm_ops,
50 };
51 
52 static short _rtl92e_is_tx_queue_empty(struct net_device *dev);
53 static void _rtl92e_watchdog_wq_cb(void *data);
54 static void _rtl92e_watchdog_timer_cb(struct timer_list *t);
55 static void _rtl92e_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
56 				   int rate);
57 static int _rtl92e_hard_start_xmit(struct sk_buff *skb, struct net_device *dev);
58 static void _rtl92e_tx_cmd(struct net_device *dev, struct sk_buff *skb);
59 static short _rtl92e_tx(struct net_device *dev, struct sk_buff *skb);
60 static short _rtl92e_pci_initdescring(struct net_device *dev);
61 static void _rtl92e_irq_tx_tasklet(struct tasklet_struct *t);
62 static void _rtl92e_irq_rx_tasklet(struct tasklet_struct *t);
63 static void _rtl92e_cancel_deferred_work(struct r8192_priv *priv);
64 static int _rtl92e_up(struct net_device *dev);
65 static int _rtl92e_try_up(struct net_device *dev);
66 static int _rtl92e_down(struct net_device *dev, bool shutdownrf);
67 static void _rtl92e_restart(void *data);
68 
69 /****************************************************************************
70  *  -----------------------------IO STUFF-------------------------
71  ****************************************************************************/
72 
rtl92e_readb(struct net_device * dev,int x)73 u8 rtl92e_readb(struct net_device *dev, int x)
74 {
75 	return 0xff & readb((u8 __iomem *)dev->mem_start + x);
76 }
77 
rtl92e_readl(struct net_device * dev,int x)78 u32 rtl92e_readl(struct net_device *dev, int x)
79 {
80 	return readl((u8 __iomem *)dev->mem_start + x);
81 }
82 
rtl92e_readw(struct net_device * dev,int x)83 u16 rtl92e_readw(struct net_device *dev, int x)
84 {
85 	return readw((u8 __iomem *)dev->mem_start + x);
86 }
87 
rtl92e_writeb(struct net_device * dev,int x,u8 y)88 void rtl92e_writeb(struct net_device *dev, int x, u8 y)
89 {
90 	writeb(y, (u8 __iomem *)dev->mem_start + x);
91 
92 	udelay(20);
93 }
94 
rtl92e_writel(struct net_device * dev,int x,u32 y)95 void rtl92e_writel(struct net_device *dev, int x, u32 y)
96 {
97 	writel(y, (u8 __iomem *)dev->mem_start + x);
98 
99 	udelay(20);
100 }
101 
rtl92e_writew(struct net_device * dev,int x,u16 y)102 void rtl92e_writew(struct net_device *dev, int x, u16 y)
103 {
104 	writew(y, (u8 __iomem *)dev->mem_start + x);
105 
106 	udelay(20);
107 }
108 
109 /****************************************************************************
110  *  -----------------------------GENERAL FUNCTION-------------------------
111  ****************************************************************************/
rtl92e_set_rf_state(struct net_device * dev,enum rt_rf_power_state state_to_set,RT_RF_CHANGE_SOURCE change_source)112 bool rtl92e_set_rf_state(struct net_device *dev,
113 			 enum rt_rf_power_state state_to_set,
114 			 RT_RF_CHANGE_SOURCE change_source)
115 {
116 	struct r8192_priv *priv = rtllib_priv(dev);
117 	struct rtllib_device *ieee = priv->rtllib;
118 	bool action_allowed = false;
119 	bool connect_by_ssid = false;
120 	enum rt_rf_power_state rt_state;
121 	u16 rf_wait_counter = 0;
122 	unsigned long flag;
123 
124 	while (true) {
125 		spin_lock_irqsave(&priv->rf_ps_lock, flag);
126 		if (priv->rf_change_in_progress) {
127 			spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
128 
129 			while (priv->rf_change_in_progress) {
130 				rf_wait_counter++;
131 				mdelay(1);
132 
133 				if (rf_wait_counter > 100) {
134 					netdev_warn(dev,
135 						    "%s(): Timeout waiting for RF change.\n",
136 						    __func__);
137 					return false;
138 				}
139 			}
140 		} else {
141 			priv->rf_change_in_progress = true;
142 			spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
143 			break;
144 		}
145 	}
146 
147 	rt_state = priv->rtllib->rf_power_state;
148 
149 	switch (state_to_set) {
150 	case rf_on:
151 		priv->rtllib->rf_off_reason &= (~change_source);
152 
153 		if ((change_source == RF_CHANGE_BY_HW) && priv->hw_radio_off)
154 			priv->hw_radio_off = false;
155 
156 		if (!priv->rtllib->rf_off_reason) {
157 			priv->rtllib->rf_off_reason = 0;
158 			action_allowed = true;
159 
160 			if (rt_state == rf_off &&
161 			    change_source >= RF_CHANGE_BY_HW)
162 				connect_by_ssid = true;
163 		}
164 		break;
165 
166 	case rf_off:
167 
168 		if (priv->rtllib->iw_mode == IW_MODE_INFRA) {
169 			if ((priv->rtllib->rf_off_reason > RF_CHANGE_BY_IPS) ||
170 			    (change_source > RF_CHANGE_BY_IPS)) {
171 				if (ieee->link_state == MAC80211_LINKED)
172 					priv->blinked_ingpio = true;
173 				else
174 					priv->blinked_ingpio = false;
175 				rtllib_mgnt_disconnect(priv->rtllib,
176 						       WLAN_REASON_DISASSOC_STA_HAS_LEFT);
177 			}
178 		}
179 		if ((change_source == RF_CHANGE_BY_HW) && !priv->hw_radio_off)
180 			priv->hw_radio_off = true;
181 		priv->rtllib->rf_off_reason |= change_source;
182 		action_allowed = true;
183 		break;
184 
185 	case rf_sleep:
186 		priv->rtllib->rf_off_reason |= change_source;
187 		action_allowed = true;
188 		break;
189 
190 	default:
191 		break;
192 	}
193 
194 	if (action_allowed) {
195 		rtl92e_set_rf_power_state(dev, state_to_set);
196 		if (state_to_set == rf_on) {
197 			if (connect_by_ssid && priv->blinked_ingpio) {
198 				schedule_delayed_work(
199 					 &ieee->associate_procedure_wq, 0);
200 				priv->blinked_ingpio = false;
201 			}
202 		}
203 	}
204 
205 	spin_lock_irqsave(&priv->rf_ps_lock, flag);
206 	priv->rf_change_in_progress = false;
207 	spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
208 	return action_allowed;
209 }
210 
_rtl92e_check_nic_enough_desc(struct net_device * dev,int prio)211 static short _rtl92e_check_nic_enough_desc(struct net_device *dev, int prio)
212 {
213 	struct r8192_priv *priv = rtllib_priv(dev);
214 	struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
215 
216 	if (ring->entries - skb_queue_len(&ring->queue) >= 2)
217 		return 1;
218 	return 0;
219 }
220 
_rtl92e_tx_timeout(struct net_device * dev,unsigned int txqueue)221 static void _rtl92e_tx_timeout(struct net_device *dev, unsigned int txqueue)
222 {
223 	struct r8192_priv *priv = rtllib_priv(dev);
224 
225 	schedule_work(&priv->reset_wq);
226 	netdev_info(dev, "TXTIMEOUT");
227 }
228 
_rtl92e_update_cap(struct net_device * dev,u16 cap)229 static void _rtl92e_update_cap(struct net_device *dev, u16 cap)
230 {
231 	struct r8192_priv *priv = rtllib_priv(dev);
232 	struct rtllib_network *net = &priv->rtllib->current_network;
233 	bool		ShortPreamble;
234 
235 	if (cap & WLAN_CAPABILITY_SHORT_PREAMBLE) {
236 		if (priv->dot11_current_preamble_mode != PREAMBLE_SHORT) {
237 			ShortPreamble = true;
238 			priv->dot11_current_preamble_mode = PREAMBLE_SHORT;
239 			priv->rtllib->set_hw_reg_handler(dev, HW_VAR_ACK_PREAMBLE,
240 					(unsigned char *)&ShortPreamble);
241 		}
242 	} else {
243 		if (priv->dot11_current_preamble_mode != PREAMBLE_LONG) {
244 			ShortPreamble = false;
245 			priv->dot11_current_preamble_mode = PREAMBLE_LONG;
246 			priv->rtllib->set_hw_reg_handler(dev, HW_VAR_ACK_PREAMBLE,
247 					      (unsigned char *)&ShortPreamble);
248 		}
249 	}
250 
251 	if (net->mode & (WIRELESS_MODE_G | WIRELESS_MODE_N_24G)) {
252 		u8	slot_time_val;
253 		u8	cur_slot_time = priv->slot_time;
254 
255 		if ((cap & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
256 		   (!priv->rtllib->ht_info->current_rt2rt_long_slot_time)) {
257 			if (cur_slot_time != SHORT_SLOT_TIME) {
258 				slot_time_val = SHORT_SLOT_TIME;
259 				priv->rtllib->set_hw_reg_handler(dev,
260 					 HW_VAR_SLOT_TIME, &slot_time_val);
261 			}
262 		} else {
263 			if (cur_slot_time != NON_SHORT_SLOT_TIME) {
264 				slot_time_val = NON_SHORT_SLOT_TIME;
265 				priv->rtllib->set_hw_reg_handler(dev,
266 					 HW_VAR_SLOT_TIME, &slot_time_val);
267 			}
268 		}
269 	}
270 }
271 
272 static const struct rtllib_qos_parameters def_qos_parameters = {
273 	{cpu_to_le16(3), cpu_to_le16(3), cpu_to_le16(3), cpu_to_le16(3)},
274 	{cpu_to_le16(7), cpu_to_le16(7), cpu_to_le16(7), cpu_to_le16(7)},
275 	{2, 2, 2, 2},
276 	{0, 0, 0, 0},
277 	{0, 0, 0, 0}
278 };
279 
_rtl92e_update_beacon(void * data)280 static void _rtl92e_update_beacon(void *data)
281 {
282 	struct r8192_priv *priv = container_of(data, struct r8192_priv, update_beacon_wq.work);
283 	struct net_device *dev = priv->rtllib->dev;
284 	struct rtllib_device *ieee = priv->rtllib;
285 	struct rtllib_network *net = &ieee->current_network;
286 
287 	if (ieee->ht_info->current_ht_support)
288 		HT_update_self_and_peer_setting(ieee, net);
289 	ieee->ht_info->current_rt2rt_long_slot_time = net->bssht.bd_rt2rt_long_slot_time;
290 	_rtl92e_update_cap(dev, net->capability);
291 }
292 
_rtl92e_qos_activate(void * data)293 static void _rtl92e_qos_activate(void *data)
294 {
295 	struct r8192_priv *priv = container_of(data, struct r8192_priv, qos_activate);
296 	struct net_device *dev = priv->rtllib->dev;
297 	int i;
298 
299 	mutex_lock(&priv->mutex);
300 	if (priv->rtllib->link_state != MAC80211_LINKED)
301 		goto success;
302 
303 	for (i = 0; i <  QOS_QUEUE_NUM; i++)
304 		priv->rtllib->set_hw_reg_handler(dev, HW_VAR_AC_PARAM, (u8 *)(&i));
305 
306 success:
307 	mutex_unlock(&priv->mutex);
308 }
309 
_rtl92e_qos_handle_probe_response(struct r8192_priv * priv,int active_network,struct rtllib_network * network)310 static int _rtl92e_qos_handle_probe_response(struct r8192_priv *priv,
311 					     int active_network,
312 					     struct rtllib_network *network)
313 {
314 	int ret = 0;
315 	u32 size = sizeof(struct rtllib_qos_parameters);
316 
317 	if (priv->rtllib->link_state != MAC80211_LINKED)
318 		return ret;
319 
320 	if (priv->rtllib->iw_mode != IW_MODE_INFRA)
321 		return ret;
322 
323 	if (network->flags & NETWORK_HAS_QOS_MASK) {
324 		if (active_network &&
325 		    (network->flags & NETWORK_HAS_QOS_PARAMETERS))
326 			network->qos_data.active = network->qos_data.supported;
327 
328 		if ((network->qos_data.active == 1) && (active_network == 1) &&
329 				(network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
330 				(network->qos_data.old_param_count !=
331 				network->qos_data.param_count)) {
332 			network->qos_data.old_param_count =
333 				network->qos_data.param_count;
334 			priv->rtllib->wmm_acm = network->qos_data.wmm_acm;
335 			schedule_work(&priv->qos_activate);
336 		}
337 	} else {
338 		memcpy(&priv->rtllib->current_network.qos_data.parameters,
339 		       &def_qos_parameters, size);
340 
341 		if ((network->qos_data.active == 1) && (active_network == 1))
342 			schedule_work(&priv->qos_activate);
343 
344 		network->qos_data.active = 0;
345 		network->qos_data.supported = 0;
346 	}
347 
348 	return 0;
349 }
350 
_rtl92e_handle_beacon(struct net_device * dev,struct rtllib_beacon * beacon,struct rtllib_network * network)351 static int _rtl92e_handle_beacon(struct net_device *dev,
352 				 struct rtllib_beacon *beacon,
353 				 struct rtllib_network *network)
354 {
355 	struct r8192_priv *priv = rtllib_priv(dev);
356 
357 	_rtl92e_qos_handle_probe_response(priv, 1, network);
358 
359 	schedule_delayed_work(&priv->update_beacon_wq, 0);
360 	return 0;
361 }
362 
_rtl92e_qos_assoc_resp(struct r8192_priv * priv,struct rtllib_network * network)363 static int _rtl92e_qos_assoc_resp(struct r8192_priv *priv,
364 				  struct rtllib_network *network)
365 {
366 	unsigned long flags;
367 	u32 size = sizeof(struct rtllib_qos_parameters);
368 	int set_qos_param = 0;
369 
370 	if (!priv || !network)
371 		return 0;
372 
373 	if (priv->rtllib->link_state != MAC80211_LINKED)
374 		return 0;
375 
376 	if (priv->rtllib->iw_mode != IW_MODE_INFRA)
377 		return 0;
378 
379 	spin_lock_irqsave(&priv->rtllib->lock, flags);
380 	if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
381 		memcpy(&priv->rtllib->current_network.qos_data.parameters,
382 		       &network->qos_data.parameters,
383 		       sizeof(struct rtllib_qos_parameters));
384 		priv->rtllib->current_network.qos_data.active = 1;
385 		priv->rtllib->wmm_acm = network->qos_data.wmm_acm;
386 		set_qos_param = 1;
387 		priv->rtllib->current_network.qos_data.old_param_count =
388 			priv->rtllib->current_network.qos_data.param_count;
389 		priv->rtllib->current_network.qos_data.param_count =
390 			network->qos_data.param_count;
391 	} else {
392 		memcpy(&priv->rtllib->current_network.qos_data.parameters,
393 		&def_qos_parameters, size);
394 		priv->rtllib->current_network.qos_data.active = 0;
395 		priv->rtllib->current_network.qos_data.supported = 0;
396 		set_qos_param = 1;
397 	}
398 
399 	spin_unlock_irqrestore(&priv->rtllib->lock, flags);
400 
401 	if (set_qos_param == 1) {
402 		rtl92e_dm_init_edca_turbo(priv->rtllib->dev);
403 		schedule_work(&priv->qos_activate);
404 	}
405 	return 0;
406 }
407 
_rtl92e_handle_assoc_response(struct net_device * dev,struct rtllib_assoc_response_frame * resp,struct rtllib_network * network)408 static int _rtl92e_handle_assoc_response(struct net_device *dev,
409 				 struct rtllib_assoc_response_frame *resp,
410 				 struct rtllib_network *network)
411 {
412 	struct r8192_priv *priv = rtllib_priv(dev);
413 
414 	_rtl92e_qos_assoc_resp(priv, network);
415 	return 0;
416 }
417 
rtl92e_config_rate(struct net_device * dev,u16 * rate_config)418 void rtl92e_config_rate(struct net_device *dev, u16 *rate_config)
419 {
420 	struct r8192_priv *priv = rtllib_priv(dev);
421 	struct rtllib_network *net;
422 	u8 i = 0, basic_rate = 0;
423 
424 	net = &priv->rtllib->current_network;
425 
426 	for (i = 0; i < net->rates_len; i++) {
427 		basic_rate = net->rates[i] & 0x7f;
428 		switch (basic_rate) {
429 		case MGN_1M:
430 			*rate_config |= RRSR_1M;
431 			break;
432 		case MGN_2M:
433 			*rate_config |= RRSR_2M;
434 			break;
435 		case MGN_5_5M:
436 			*rate_config |= RRSR_5_5M;
437 			break;
438 		case MGN_11M:
439 			*rate_config |= RRSR_11M;
440 			break;
441 		case MGN_6M:
442 			*rate_config |= RRSR_6M;
443 			break;
444 		case MGN_9M:
445 			*rate_config |= RRSR_9M;
446 			break;
447 		case MGN_12M:
448 			*rate_config |= RRSR_12M;
449 			break;
450 		case MGN_18M:
451 			*rate_config |= RRSR_18M;
452 			break;
453 		case MGN_24M:
454 			*rate_config |= RRSR_24M;
455 			break;
456 		case MGN_36M:
457 			*rate_config |= RRSR_36M;
458 			break;
459 		case MGN_48M:
460 			*rate_config |= RRSR_48M;
461 			break;
462 		case MGN_54M:
463 			*rate_config |= RRSR_54M;
464 			break;
465 		}
466 	}
467 
468 	for (i = 0; i < net->rates_ex_len; i++) {
469 		basic_rate = net->rates_ex[i] & 0x7f;
470 		switch (basic_rate) {
471 		case MGN_1M:
472 			*rate_config |= RRSR_1M;
473 			break;
474 		case MGN_2M:
475 			*rate_config |= RRSR_2M;
476 			break;
477 		case MGN_5_5M:
478 			*rate_config |= RRSR_5_5M;
479 			break;
480 		case MGN_11M:
481 			*rate_config |= RRSR_11M;
482 			break;
483 		case MGN_6M:
484 			*rate_config |= RRSR_6M;
485 			break;
486 		case MGN_9M:
487 			*rate_config |= RRSR_9M;
488 			break;
489 		case MGN_12M:
490 			*rate_config |= RRSR_12M;
491 			break;
492 		case MGN_18M:
493 			*rate_config |= RRSR_18M;
494 			break;
495 		case MGN_24M:
496 			*rate_config |= RRSR_24M;
497 			break;
498 		case MGN_36M:
499 			*rate_config |= RRSR_36M;
500 			break;
501 		case MGN_48M:
502 			*rate_config |= RRSR_48M;
503 			break;
504 		case MGN_54M:
505 			*rate_config |= RRSR_54M;
506 			break;
507 		}
508 	}
509 }
510 
_rtl92e_refresh_support_rate(struct r8192_priv * priv)511 static void _rtl92e_refresh_support_rate(struct r8192_priv *priv)
512 {
513 	struct rtllib_device *ieee = priv->rtllib;
514 
515 	if (ieee->mode == WIRELESS_MODE_N_24G) {
516 		memcpy(ieee->reg_dot11ht_oper_rate_set,
517 		       ieee->reg_ht_supp_rate_set, 16);
518 		memcpy(ieee->reg_dot11tx_ht_oper_rate_set,
519 		       ieee->reg_ht_supp_rate_set, 16);
520 
521 	} else {
522 		memset(ieee->reg_dot11ht_oper_rate_set, 0, 16);
523 	}
524 }
525 
rtl92e_set_wireless_mode(struct net_device * dev,u8 wireless_mode)526 void rtl92e_set_wireless_mode(struct net_device *dev, u8 wireless_mode)
527 {
528 	struct r8192_priv *priv = rtllib_priv(dev);
529 	u8 support_mode = (WIRELESS_MODE_N_24G | WIRELESS_MODE_G | WIRELESS_MODE_B);
530 
531 	if ((wireless_mode == WIRELESS_MODE_AUTO) || ((wireless_mode & support_mode) == 0))
532 		wireless_mode = WIRELESS_MODE_N_24G;
533 
534 	if ((wireless_mode & (WIRELESS_MODE_B | WIRELESS_MODE_G)) ==
535 	    (WIRELESS_MODE_G | WIRELESS_MODE_B))
536 		wireless_mode = WIRELESS_MODE_G;
537 
538 	priv->rtllib->mode = wireless_mode;
539 
540 	if (wireless_mode == WIRELESS_MODE_N_24G)
541 		priv->rtllib->ht_info->enable_ht = 1;
542 	else
543 		priv->rtllib->ht_info->enable_ht = 0;
544 
545 	_rtl92e_refresh_support_rate(priv);
546 }
547 
_rtl92e_sta_up(struct net_device * dev)548 static int _rtl92e_sta_up(struct net_device *dev)
549 {
550 	struct r8192_priv *priv = rtllib_priv(dev);
551 	struct rt_pwr_save_ctrl *psc = (struct rt_pwr_save_ctrl *)
552 					(&priv->rtllib->pwr_save_ctrl);
553 	bool init_status;
554 
555 	priv->up = 1;
556 	priv->rtllib->ieee_up = 1;
557 
558 	priv->up_first_time = 0;
559 	init_status = rtl92e_start_adapter(dev);
560 	if (!init_status) {
561 		netdev_err(dev, "%s(): Initialization failed!\n", __func__);
562 		return -1;
563 	}
564 
565 	RT_CLEAR_PS_LEVEL(psc, RT_RF_OFF_LEVL_HALT_NIC);
566 
567 	if (priv->polling_timer_on == 0)
568 		rtl92e_check_rfctrl_gpio_timer(&priv->gpio_polling_timer);
569 
570 	if (priv->rtllib->link_state != MAC80211_LINKED)
571 		rtllib_softmac_start_protocol(priv->rtllib);
572 	rtllib_reset_queue(priv->rtllib);
573 	_rtl92e_watchdog_timer_cb(&priv->watch_dog_timer);
574 
575 	if (!netif_queue_stopped(dev))
576 		netif_start_queue(dev);
577 	else
578 		netif_wake_queue(dev);
579 
580 	priv->bfirst_after_down = false;
581 	return 0;
582 }
583 
_rtl92e_sta_down(struct net_device * dev,bool shutdownrf)584 static int _rtl92e_sta_down(struct net_device *dev, bool shutdownrf)
585 {
586 	struct r8192_priv *priv = rtllib_priv(dev);
587 	unsigned long flags = 0;
588 	u8 rf_in_progress_timeout = 0;
589 
590 	if (priv->up == 0)
591 		return -1;
592 
593 	priv->rtllib->rtllib_ips_leave(dev);
594 
595 	if (priv->rtllib->link_state == MAC80211_LINKED)
596 		rtl92e_leisure_ps_leave(dev);
597 
598 	priv->up = 0;
599 	priv->rtllib->ieee_up = 0;
600 	priv->bfirst_after_down = true;
601 	if (!netif_queue_stopped(dev))
602 		netif_stop_queue(dev);
603 
604 	priv->rtllib->wpa_ie_len = 0;
605 	kfree(priv->rtllib->wpa_ie);
606 	priv->rtllib->wpa_ie = NULL;
607 	rtl92e_cam_reset(dev);
608 	memset(priv->rtllib->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
609 	rtl92e_irq_disable(dev);
610 
611 	del_timer_sync(&priv->watch_dog_timer);
612 	_rtl92e_cancel_deferred_work(priv);
613 	cancel_delayed_work(&priv->rtllib->hw_wakeup_wq);
614 
615 	rtllib_softmac_stop_protocol(priv->rtllib);
616 	spin_lock_irqsave(&priv->rf_ps_lock, flags);
617 	while (priv->rf_change_in_progress) {
618 		spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
619 		if (rf_in_progress_timeout > 100) {
620 			spin_lock_irqsave(&priv->rf_ps_lock, flags);
621 			break;
622 		}
623 		mdelay(1);
624 		rf_in_progress_timeout++;
625 		spin_lock_irqsave(&priv->rf_ps_lock, flags);
626 	}
627 	priv->rf_change_in_progress = true;
628 	spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
629 	rtl92e_stop_adapter(dev, false);
630 	spin_lock_irqsave(&priv->rf_ps_lock, flags);
631 	priv->rf_change_in_progress = false;
632 	spin_unlock_irqrestore(&priv->rf_ps_lock, flags);
633 	udelay(100);
634 	memset(&priv->rtllib->current_network, 0,
635 	       offsetof(struct rtllib_network, list));
636 
637 	return 0;
638 }
639 
_rtl92e_init_priv_handler(struct net_device * dev)640 static void _rtl92e_init_priv_handler(struct net_device *dev)
641 {
642 	struct r8192_priv *priv = rtllib_priv(dev);
643 
644 	priv->rtllib->softmac_hard_start_xmit	= _rtl92e_hard_start_xmit;
645 	priv->rtllib->set_chan			= rtl92e_set_channel;
646 	priv->rtllib->link_change		= rtl92e_link_change;
647 	priv->rtllib->softmac_data_hard_start_xmit = _rtl92e_hard_data_xmit;
648 	priv->rtllib->check_nic_enough_desc	= _rtl92e_check_nic_enough_desc;
649 	priv->rtllib->handle_assoc_response	= _rtl92e_handle_assoc_response;
650 	priv->rtllib->handle_beacon		= _rtl92e_handle_beacon;
651 	priv->rtllib->set_wireless_mode		= rtl92e_set_wireless_mode;
652 	priv->rtllib->leisure_ps_leave		= rtl92e_leisure_ps_leave;
653 	priv->rtllib->set_bw_mode_handler	= rtl92e_set_bw_mode;
654 
655 	priv->rtllib->sta_wake_up = rtl92e_hw_wakeup;
656 	priv->rtllib->enter_sleep_state = rtl92e_enter_sleep;
657 	priv->rtllib->ps_is_queue_empty = _rtl92e_is_tx_queue_empty;
658 
659 	priv->rtllib->get_nmode_support_by_sec_cfg = rtl92e_get_nmode_support_by_sec;
660 	priv->rtllib->get_half_nmode_support_by_aps_handler =
661 						rtl92e_is_halfn_supported_by_ap;
662 
663 	priv->rtllib->set_hw_reg_handler = rtl92e_set_reg;
664 	priv->rtllib->allow_all_dest_addr_handler = rtl92e_set_monitor_mode;
665 	priv->rtllib->init_gain_handler = rtl92e_init_gain;
666 	priv->rtllib->rtllib_ips_leave_wq = rtl92e_rtllib_ips_leave_wq;
667 	priv->rtllib->rtllib_ips_leave = rtl92e_rtllib_ips_leave;
668 	priv->rtllib->scan_operation_backup_handler = rtl92e_scan_op_backup;
669 }
670 
_rtl92e_init_priv_variable(struct net_device * dev)671 static void _rtl92e_init_priv_variable(struct net_device *dev)
672 {
673 	struct r8192_priv *priv = rtllib_priv(dev);
674 	u8 i;
675 
676 	priv->dot11_current_preamble_mode = PREAMBLE_AUTO;
677 	priv->rtllib->status = 0;
678 	priv->polling_timer_on = 0;
679 	priv->up_first_time = 1;
680 	priv->blinked_ingpio = false;
681 	priv->being_init_adapter = false;
682 	priv->txringcount = 64;
683 	priv->rxbuffersize = 9100;
684 	priv->rxringcount = MAX_RX_COUNT;
685 	priv->irq_enabled = 0;
686 	priv->chan = 1;
687 	priv->rtllib->mode = WIRELESS_MODE_AUTO;
688 	priv->rtllib->iw_mode = IW_MODE_INFRA;
689 	priv->rtllib->ieee_up = 0;
690 	priv->retry_rts = DEFAULT_RETRY_RTS;
691 	priv->retry_data = DEFAULT_RETRY_DATA;
692 	priv->rtllib->rts = DEFAULT_RTS_THRESHOLD;
693 	priv->rtllib->rate = 110;
694 	priv->promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
695 	priv->bcck_in_ch14 = false;
696 	priv->cck_present_attn = 0;
697 	priv->rfa_txpowertrackingindex = 0;
698 	priv->cck_pwr_enl = 6;
699 	memset(priv->rtllib->swcamtable, 0, sizeof(struct sw_cam_table) * 32);
700 	priv->rx_ctr = 0;
701 	priv->rtllib->wx_set_enc = 0;
702 	priv->hw_radio_off = false;
703 	priv->rtllib->rf_off_reason = 0;
704 	priv->rf_change_in_progress = false;
705 	priv->hw_rf_off_action = 0;
706 	priv->set_rf_pwr_state_in_progress = false;
707 	priv->rtllib->pwr_save_ctrl.bLeisurePs = true;
708 	priv->rtllib->lps_delay_cnt = 0;
709 	priv->rtllib->sta_sleep = LPS_IS_WAKE;
710 	priv->rtllib->rf_power_state = rf_on;
711 
712 	priv->rtllib->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
713 	priv->rtllib->iw_mode = IW_MODE_INFRA;
714 	priv->rtllib->be_scan_inprogress = false;
715 
716 	priv->rtllib->fts = DEFAULT_FRAG_THRESHOLD;
717 
718 	priv->fw_info = vzalloc(sizeof(struct rt_firmware));
719 	if (!priv->fw_info)
720 		netdev_err(dev,
721 			   "rtl8192e: Unable to allocate space for firmware\n");
722 
723 	skb_queue_head_init(&priv->skb_queue);
724 
725 	for (i = 0; i < MAX_QUEUE_SIZE; i++)
726 		skb_queue_head_init(&priv->rtllib->skb_waitq[i]);
727 }
728 
_rtl92e_init_priv_lock(struct r8192_priv * priv)729 static void _rtl92e_init_priv_lock(struct r8192_priv *priv)
730 {
731 	spin_lock_init(&priv->tx_lock);
732 	spin_lock_init(&priv->irq_th_lock);
733 	spin_lock_init(&priv->rf_ps_lock);
734 	spin_lock_init(&priv->ps_lock);
735 	mutex_init(&priv->wx_mutex);
736 	mutex_init(&priv->rf_mutex);
737 	mutex_init(&priv->mutex);
738 }
739 
_rtl92e_init_priv_task(struct net_device * dev)740 static void _rtl92e_init_priv_task(struct net_device *dev)
741 {
742 	struct r8192_priv *priv = rtllib_priv(dev);
743 
744 	INIT_WORK(&priv->reset_wq, (void *)_rtl92e_restart);
745 	INIT_WORK(&priv->rtllib->ips_leave_wq, (void *)rtl92e_ips_leave_wq);
746 	INIT_DELAYED_WORK(&priv->watch_dog_wq, (void *)_rtl92e_watchdog_wq_cb);
747 	INIT_DELAYED_WORK(&priv->txpower_tracking_wq, (void *)rtl92e_dm_txpower_tracking_wq);
748 	INIT_DELAYED_WORK(&priv->rfpath_check_wq, (void *)rtl92e_dm_rf_pathcheck_wq);
749 	INIT_DELAYED_WORK(&priv->update_beacon_wq, (void *)_rtl92e_update_beacon);
750 	INIT_WORK(&priv->qos_activate, (void *)_rtl92e_qos_activate);
751 	INIT_DELAYED_WORK(&priv->rtllib->hw_wakeup_wq, (void *)rtl92e_hw_wakeup_wq);
752 	INIT_DELAYED_WORK(&priv->rtllib->hw_sleep_wq, (void *)rtl92e_hw_sleep_wq);
753 	tasklet_setup(&priv->irq_rx_tasklet, _rtl92e_irq_rx_tasklet);
754 	tasklet_setup(&priv->irq_tx_tasklet, _rtl92e_irq_tx_tasklet);
755 }
756 
_rtl92e_get_channel_map(struct net_device * dev)757 static short _rtl92e_get_channel_map(struct net_device *dev)
758 {
759 	int i;
760 
761 	struct r8192_priv *priv = rtllib_priv(dev);
762 
763 	for (i = 1; i <= 11; i++)
764 		(priv->rtllib->active_channel_map)[i] = 1;
765 	(priv->rtllib->active_channel_map)[12] = 2;
766 	(priv->rtllib->active_channel_map)[13] = 2;
767 
768 	return 0;
769 }
770 
_rtl92e_init(struct net_device * dev)771 static short _rtl92e_init(struct net_device *dev)
772 {
773 	struct r8192_priv *priv = rtllib_priv(dev);
774 
775 	memset(&priv->stats, 0, sizeof(struct rt_stats));
776 
777 	_rtl92e_init_priv_handler(dev);
778 	_rtl92e_init_priv_variable(dev);
779 	_rtl92e_init_priv_lock(priv);
780 	_rtl92e_init_priv_task(dev);
781 	rtl92e_get_eeprom_size(dev);
782 	rtl92e_init_variables(dev);
783 	_rtl92e_get_channel_map(dev);
784 
785 	rtl92e_dm_init(dev);
786 
787 	timer_setup(&priv->watch_dog_timer, _rtl92e_watchdog_timer_cb, 0);
788 
789 	timer_setup(&priv->gpio_polling_timer, rtl92e_check_rfctrl_gpio_timer,
790 		    0);
791 
792 	rtl92e_irq_disable(dev);
793 	if (request_irq(dev->irq, _rtl92e_irq, IRQF_SHARED, dev->name, dev)) {
794 		netdev_err(dev, "Error allocating IRQ %d", dev->irq);
795 		return -1;
796 	}
797 
798 	priv->irq = dev->irq;
799 
800 	if (_rtl92e_pci_initdescring(dev) != 0) {
801 		netdev_err(dev, "Endopoints initialization failed");
802 		free_irq(dev->irq, dev);
803 		return -1;
804 	}
805 
806 	return 0;
807 }
808 
809 /***************************************************************************
810  * -------------------------------WATCHDOG STUFF---------------------------
811  **************************************************************************/
_rtl92e_is_tx_queue_empty(struct net_device * dev)812 static short _rtl92e_is_tx_queue_empty(struct net_device *dev)
813 {
814 	int i = 0;
815 	struct r8192_priv *priv = rtllib_priv(dev);
816 
817 	for (i = 0; i <= MGNT_QUEUE; i++) {
818 		if ((i == TXCMD_QUEUE) || (i == HCCA_QUEUE))
819 			continue;
820 		if (skb_queue_len(&(&priv->tx_ring[i])->queue) > 0) {
821 			netdev_info(dev, "===>tx queue is not empty:%d, %d\n",
822 			       i, skb_queue_len(&(&priv->tx_ring[i])->queue));
823 			return 0;
824 		}
825 	}
826 	return 1;
827 }
828 
_rtl92e_tx_check_stuck(struct net_device * dev)829 static enum reset_type _rtl92e_tx_check_stuck(struct net_device *dev)
830 {
831 	struct r8192_priv *priv = rtllib_priv(dev);
832 	u8	QueueID;
833 	bool	bCheckFwTxCnt = false;
834 	struct rtl8192_tx_ring  *ring = NULL;
835 	struct sk_buff *skb = NULL;
836 	struct cb_desc *tcb_desc = NULL;
837 	unsigned long flags = 0;
838 
839 	switch (priv->rtllib->ps) {
840 	case RTLLIB_PS_DISABLED:
841 		break;
842 	case (RTLLIB_PS_MBCAST | RTLLIB_PS_UNICAST):
843 		break;
844 	default:
845 		break;
846 	}
847 	spin_lock_irqsave(&priv->irq_th_lock, flags);
848 	for (QueueID = 0; QueueID < MAX_TX_QUEUE; QueueID++) {
849 		if (QueueID == TXCMD_QUEUE)
850 			continue;
851 
852 		if (QueueID == BEACON_QUEUE)
853 			continue;
854 
855 		ring = &priv->tx_ring[QueueID];
856 
857 		if (skb_queue_len(&ring->queue) == 0) {
858 			continue;
859 		} else {
860 			skb = __skb_peek(&ring->queue);
861 			tcb_desc = (struct cb_desc *)(skb->cb +
862 				    MAX_DEV_ADDR_SIZE);
863 			tcb_desc->stuck_count++;
864 			bCheckFwTxCnt = true;
865 			if (tcb_desc->stuck_count > 1)
866 				netdev_info(dev,
867 					    "%s: QueueID=%d tcb_desc->stuck_count=%d\n",
868 					    __func__, QueueID,
869 					    tcb_desc->stuck_count);
870 		}
871 	}
872 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
873 
874 	if (bCheckFwTxCnt) {
875 		if (rtl92e_is_tx_stuck(dev))
876 			return RESET_TYPE_SILENT;
877 	}
878 
879 	return RESET_TYPE_NORESET;
880 }
881 
_rtl92e_rx_check_stuck(struct net_device * dev)882 static enum reset_type _rtl92e_rx_check_stuck(struct net_device *dev)
883 {
884 	if (rtl92e_is_rx_stuck(dev))
885 		return RESET_TYPE_SILENT;
886 
887 	return RESET_TYPE_NORESET;
888 }
889 
_rtl92e_if_check_reset(struct net_device * dev)890 static void _rtl92e_if_check_reset(struct net_device *dev)
891 {
892 	struct r8192_priv *priv = rtllib_priv(dev);
893 	enum reset_type TxResetType = RESET_TYPE_NORESET;
894 	enum reset_type RxResetType = RESET_TYPE_NORESET;
895 	enum rt_rf_power_state rfState;
896 
897 	rfState = priv->rtllib->rf_power_state;
898 
899 	if (rfState == rf_on)
900 		TxResetType = _rtl92e_tx_check_stuck(dev);
901 
902 	if (rfState == rf_on &&
903 	    (priv->rtllib->iw_mode == IW_MODE_INFRA) &&
904 	    (priv->rtllib->link_state == MAC80211_LINKED))
905 		RxResetType = _rtl92e_rx_check_stuck(dev);
906 
907 	if (TxResetType == RESET_TYPE_SILENT ||
908 		   RxResetType == RESET_TYPE_SILENT) {
909 		netdev_info(dev, "%s(): TxResetType is %d, RxResetType is %d\n",
910 			    __func__, TxResetType, RxResetType);
911 	}
912 }
913 
_rtl92e_update_rxcounts(struct r8192_priv * priv,u32 * TotalRxBcnNum,u32 * TotalRxDataNum)914 static void _rtl92e_update_rxcounts(struct r8192_priv *priv, u32 *TotalRxBcnNum,
915 				    u32 *TotalRxDataNum)
916 {
917 	u16	slot_index;
918 	u8	i;
919 
920 	*TotalRxBcnNum = 0;
921 	*TotalRxDataNum = 0;
922 
923 	slot_index = (priv->rtllib->link_detect_info.slot_index++) %
924 			(priv->rtllib->link_detect_info.slot_num);
925 	priv->rtllib->link_detect_info.RxBcnNum[slot_index] =
926 			priv->rtllib->link_detect_info.num_recv_bcn_in_period;
927 	priv->rtllib->link_detect_info.RxDataNum[slot_index] =
928 			priv->rtllib->link_detect_info.num_recv_data_in_period;
929 	for (i = 0; i < priv->rtllib->link_detect_info.slot_num; i++) {
930 		*TotalRxBcnNum += priv->rtllib->link_detect_info.RxBcnNum[i];
931 		*TotalRxDataNum += priv->rtllib->link_detect_info.RxDataNum[i];
932 	}
933 }
934 
_rtl92e_watchdog_wq_cb(void * data)935 static void _rtl92e_watchdog_wq_cb(void *data)
936 {
937 	struct r8192_priv *priv = container_of_dwork_rsl(data,
938 				  struct r8192_priv, watch_dog_wq);
939 	struct net_device *dev = priv->rtllib->dev;
940 	struct rtllib_device *ieee = priv->rtllib;
941 	static u8 check_reset_cnt;
942 	unsigned long flags;
943 	struct rt_pwr_save_ctrl *psc = (struct rt_pwr_save_ctrl *)
944 					(&priv->rtllib->pwr_save_ctrl);
945 	bool busy_traffic = false;
946 	bool	bHigherBusyTraffic = false;
947 	bool	bHigherBusyRxTraffic = false;
948 	bool bEnterPS = false;
949 
950 	if (!priv->up || priv->hw_radio_off)
951 		return;
952 
953 	if (priv->rtllib->link_state >= MAC80211_LINKED) {
954 		if (priv->rtllib->cnt_after_link < 2)
955 			priv->rtllib->cnt_after_link++;
956 	} else {
957 		priv->rtllib->cnt_after_link = 0;
958 	}
959 
960 	rtl92e_dm_watchdog(dev);
961 
962 	if (!rtllib_act_scanning(priv->rtllib, false)) {
963 		if ((ieee->iw_mode == IW_MODE_INFRA) && (ieee->link_state ==
964 		     MAC80211_NOLINK) &&
965 		     (ieee->rf_power_state == rf_on) && !ieee->is_set_key &&
966 		     (!ieee->proto_stoppping) && !ieee->wx_set_enc) {
967 			if (ieee->pwr_save_ctrl.return_point == IPS_CALLBACK_NONE)
968 				rtl92e_ips_enter(dev);
969 		}
970 	}
971 	if ((ieee->link_state == MAC80211_LINKED) && (ieee->iw_mode == IW_MODE_INFRA)) {
972 		if (ieee->link_detect_info.num_rx_ok_in_period > 100 ||
973 		ieee->link_detect_info.num_tx_ok_in_period > 100)
974 			busy_traffic = true;
975 
976 		if (ieee->link_detect_info.num_rx_ok_in_period > 4000 ||
977 		    ieee->link_detect_info.num_tx_ok_in_period > 4000) {
978 			bHigherBusyTraffic = true;
979 			if (ieee->link_detect_info.num_rx_ok_in_period > 5000)
980 				bHigherBusyRxTraffic = true;
981 			else
982 				bHigherBusyRxTraffic = false;
983 		}
984 
985 		if (((ieee->link_detect_info.num_rx_unicast_ok_in_period +
986 		    ieee->link_detect_info.num_tx_ok_in_period) > 8) ||
987 		    (ieee->link_detect_info.num_rx_unicast_ok_in_period > 2))
988 			bEnterPS = false;
989 		else
990 			bEnterPS = true;
991 
992 		if (ieee->current_network.beacon_interval < 95)
993 			bEnterPS = false;
994 
995 		if (bEnterPS)
996 			rtl92e_leisure_ps_enter(dev);
997 		else
998 			rtl92e_leisure_ps_leave(dev);
999 
1000 	} else {
1001 		rtl92e_leisure_ps_leave(dev);
1002 	}
1003 
1004 	ieee->link_detect_info.num_rx_ok_in_period = 0;
1005 	ieee->link_detect_info.num_tx_ok_in_period = 0;
1006 	ieee->link_detect_info.num_rx_unicast_ok_in_period = 0;
1007 	ieee->link_detect_info.busy_traffic = busy_traffic;
1008 
1009 	ieee->link_detect_info.bHigherBusyTraffic = bHigherBusyTraffic;
1010 	ieee->link_detect_info.bHigherBusyRxTraffic = bHigherBusyRxTraffic;
1011 
1012 	if (ieee->link_state == MAC80211_LINKED && ieee->iw_mode == IW_MODE_INFRA) {
1013 		u32	TotalRxBcnNum = 0;
1014 		u32	TotalRxDataNum = 0;
1015 
1016 		_rtl92e_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
1017 
1018 		if ((TotalRxBcnNum + TotalRxDataNum) == 0)
1019 			priv->check_roaming_cnt++;
1020 		else
1021 			priv->check_roaming_cnt = 0;
1022 
1023 		if (priv->check_roaming_cnt > 0) {
1024 			if (ieee->rf_power_state == rf_off)
1025 				netdev_info(dev, "%s(): RF is off\n", __func__);
1026 
1027 			netdev_info(dev,
1028 				    "===>%s(): AP is power off, chan:%d, connect another one\n",
1029 				    __func__, priv->chan);
1030 
1031 			ieee->link_state = RTLLIB_ASSOCIATING;
1032 
1033 			remove_peer_ts(priv->rtllib,
1034 				     priv->rtllib->current_network.bssid);
1035 			ieee->is_roaming = true;
1036 			ieee->is_set_key = false;
1037 			ieee->link_change(dev);
1038 			notify_wx_assoc_event(ieee);
1039 
1040 			if (!(ieee->rtllib_ap_sec_type(ieee) &
1041 			     (SEC_ALG_CCMP | SEC_ALG_TKIP)))
1042 				schedule_delayed_work(
1043 					&ieee->associate_procedure_wq, 0);
1044 
1045 			priv->check_roaming_cnt = 0;
1046 		}
1047 		ieee->link_detect_info.num_recv_bcn_in_period = 0;
1048 		ieee->link_detect_info.num_recv_data_in_period = 0;
1049 	}
1050 
1051 	spin_lock_irqsave(&priv->tx_lock, flags);
1052 	if ((check_reset_cnt++ >= 3) && (!ieee->is_roaming) &&
1053 	    (!priv->rf_change_in_progress) && (!psc->bSwRfProcessing)) {
1054 		_rtl92e_if_check_reset(dev);
1055 		check_reset_cnt = 3;
1056 	}
1057 	spin_unlock_irqrestore(&priv->tx_lock, flags);
1058 }
1059 
_rtl92e_watchdog_timer_cb(struct timer_list * t)1060 static void _rtl92e_watchdog_timer_cb(struct timer_list *t)
1061 {
1062 	struct r8192_priv *priv = from_timer(priv, t, watch_dog_timer);
1063 
1064 	schedule_delayed_work(&priv->watch_dog_wq, 0);
1065 	mod_timer(&priv->watch_dog_timer, jiffies +
1066 		  msecs_to_jiffies(RTLLIB_WATCH_DOG_TIME));
1067 }
1068 
1069 /****************************************************************************
1070  * ---------------------------- NIC TX/RX STUFF---------------------------
1071  ****************************************************************************/
rtl92e_rx_enable(struct net_device * dev)1072 void rtl92e_rx_enable(struct net_device *dev)
1073 {
1074 	rtl92e_enable_rx(dev);
1075 }
1076 
rtl92e_tx_enable(struct net_device * dev)1077 void rtl92e_tx_enable(struct net_device *dev)
1078 {
1079 	struct r8192_priv *priv = rtllib_priv(dev);
1080 
1081 	rtl92e_enable_tx(dev);
1082 
1083 	rtllib_reset_queue(priv->rtllib);
1084 }
1085 
_rtl92e_free_rx_ring(struct net_device * dev)1086 static void _rtl92e_free_rx_ring(struct net_device *dev)
1087 {
1088 	struct r8192_priv *priv = rtllib_priv(dev);
1089 	int i;
1090 
1091 	for (i = 0; i < priv->rxringcount; i++) {
1092 		struct sk_buff *skb = priv->rx_buf[i];
1093 
1094 		if (!skb)
1095 			continue;
1096 
1097 		dma_unmap_single(&priv->pdev->dev,
1098 				 *((dma_addr_t *)skb->cb),
1099 				 priv->rxbuffersize, DMA_FROM_DEVICE);
1100 		kfree_skb(skb);
1101 	}
1102 
1103 	dma_free_coherent(&priv->pdev->dev,
1104 			  sizeof(*priv->rx_ring) * priv->rxringcount,
1105 			  priv->rx_ring,
1106 			  priv->rx_ring_dma);
1107 	priv->rx_ring = NULL;
1108 }
1109 
_rtl92e_free_tx_ring(struct net_device * dev,unsigned int prio)1110 static void _rtl92e_free_tx_ring(struct net_device *dev, unsigned int prio)
1111 {
1112 	struct r8192_priv *priv = rtllib_priv(dev);
1113 	struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1114 
1115 	while (skb_queue_len(&ring->queue)) {
1116 		struct tx_desc *entry = &ring->desc[ring->idx];
1117 		struct sk_buff *skb = __skb_dequeue(&ring->queue);
1118 
1119 		dma_unmap_single(&priv->pdev->dev, entry->TxBuffAddr,
1120 				 skb->len, DMA_TO_DEVICE);
1121 		kfree_skb(skb);
1122 		ring->idx = (ring->idx + 1) % ring->entries;
1123 	}
1124 
1125 	dma_free_coherent(&priv->pdev->dev,
1126 			  sizeof(*ring->desc) * ring->entries, ring->desc,
1127 			  ring->dma);
1128 	ring->desc = NULL;
1129 }
1130 
_rtl92e_hard_data_xmit(struct sk_buff * skb,struct net_device * dev,int rate)1131 static void _rtl92e_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
1132 				   int rate)
1133 {
1134 	struct r8192_priv *priv = rtllib_priv(dev);
1135 	int ret;
1136 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1137 				    MAX_DEV_ADDR_SIZE);
1138 	u8 queue_index = tcb_desc->queue_index;
1139 
1140 	if ((priv->rtllib->rf_power_state == rf_off) || !priv->up) {
1141 		kfree_skb(skb);
1142 		return;
1143 	}
1144 
1145 	if (queue_index == TXCMD_QUEUE)
1146 		netdev_warn(dev, "%s(): queue index == TXCMD_QUEUE\n",
1147 			    __func__);
1148 
1149 	memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1150 	skb_push(skb, priv->rtllib->tx_headroom);
1151 	ret = _rtl92e_tx(dev, skb);
1152 
1153 	if (queue_index != MGNT_QUEUE) {
1154 		priv->rtllib->stats.tx_bytes += (skb->len -
1155 						 priv->rtllib->tx_headroom);
1156 		priv->rtllib->stats.tx_packets++;
1157 	}
1158 
1159 	if (ret != 0)
1160 		kfree_skb(skb);
1161 }
1162 
_rtl92e_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)1163 static int _rtl92e_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1164 {
1165 	struct r8192_priv *priv = rtllib_priv(dev);
1166 	int ret;
1167 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1168 				    MAX_DEV_ADDR_SIZE);
1169 	u8 queue_index = tcb_desc->queue_index;
1170 
1171 	if (queue_index != TXCMD_QUEUE) {
1172 		if ((priv->rtllib->rf_power_state == rf_off) ||
1173 		     !priv->up) {
1174 			kfree_skb(skb);
1175 			return 0;
1176 		}
1177 	}
1178 
1179 	memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1180 	if (queue_index == TXCMD_QUEUE) {
1181 		_rtl92e_tx_cmd(dev, skb);
1182 		return 0;
1183 	}
1184 
1185 	tcb_desc->ratr_index = 7;
1186 	tcb_desc->tx_dis_rate_fallback = 1;
1187 	tcb_desc->tx_use_drv_assinged_rate = 1;
1188 	tcb_desc->tx_enable_fw_calc_dur = 1;
1189 	skb_push(skb, priv->rtllib->tx_headroom);
1190 	ret = _rtl92e_tx(dev, skb);
1191 	if (ret != 0)
1192 		kfree_skb(skb);
1193 	return ret;
1194 }
1195 
_rtl92e_tx_isr(struct net_device * dev,int prio)1196 static void _rtl92e_tx_isr(struct net_device *dev, int prio)
1197 {
1198 	struct r8192_priv *priv = rtllib_priv(dev);
1199 
1200 	struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1201 
1202 	while (skb_queue_len(&ring->queue)) {
1203 		struct tx_desc *entry = &ring->desc[ring->idx];
1204 		struct sk_buff *skb;
1205 
1206 		if (prio != BEACON_QUEUE) {
1207 			if (entry->OWN)
1208 				return;
1209 			ring->idx = (ring->idx + 1) % ring->entries;
1210 		}
1211 
1212 		skb = __skb_dequeue(&ring->queue);
1213 		dma_unmap_single(&priv->pdev->dev, entry->TxBuffAddr,
1214 				 skb->len, DMA_TO_DEVICE);
1215 
1216 		kfree_skb(skb);
1217 	}
1218 	if (prio != BEACON_QUEUE)
1219 		tasklet_schedule(&priv->irq_tx_tasklet);
1220 }
1221 
_rtl92e_tx_cmd(struct net_device * dev,struct sk_buff * skb)1222 static void _rtl92e_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1223 {
1224 	struct r8192_priv *priv = rtllib_priv(dev);
1225 	struct rtl8192_tx_ring *ring;
1226 	struct tx_desc_cmd *entry;
1227 	unsigned int idx;
1228 	struct cb_desc *tcb_desc;
1229 	unsigned long flags;
1230 
1231 	spin_lock_irqsave(&priv->irq_th_lock, flags);
1232 	ring = &priv->tx_ring[TXCMD_QUEUE];
1233 
1234 	idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1235 	entry = (struct tx_desc_cmd *)&ring->desc[idx];
1236 
1237 	tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1238 
1239 	rtl92e_fill_tx_cmd_desc(dev, entry, tcb_desc, skb);
1240 
1241 	__skb_queue_tail(&ring->queue, skb);
1242 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1243 }
1244 
_rtl92e_tx(struct net_device * dev,struct sk_buff * skb)1245 static short _rtl92e_tx(struct net_device *dev, struct sk_buff *skb)
1246 {
1247 	struct r8192_priv *priv = rtllib_priv(dev);
1248 	struct rtl8192_tx_ring  *ring;
1249 	unsigned long flags;
1250 	struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1251 				    MAX_DEV_ADDR_SIZE);
1252 	struct tx_desc *pdesc = NULL;
1253 	struct ieee80211_hdr *header = NULL;
1254 	u8 *pda_addr = NULL;
1255 	int   idx;
1256 	u32 fwinfo_size = 0;
1257 
1258 	priv->rtllib->awake_pkt_sent = true;
1259 
1260 	fwinfo_size = sizeof(struct tx_fwinfo_8190pci);
1261 
1262 	header = (struct ieee80211_hdr *)(((u8 *)skb->data) + fwinfo_size);
1263 	pda_addr = header->addr1;
1264 
1265 	if (!is_broadcast_ether_addr(pda_addr) && !is_multicast_ether_addr(pda_addr))
1266 		priv->stats.txbytesunicast += skb->len - fwinfo_size;
1267 
1268 	spin_lock_irqsave(&priv->irq_th_lock, flags);
1269 	ring = &priv->tx_ring[tcb_desc->queue_index];
1270 	if (tcb_desc->queue_index != BEACON_QUEUE)
1271 		idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1272 	else
1273 		idx = 0;
1274 
1275 	pdesc = &ring->desc[idx];
1276 	if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
1277 		netdev_warn(dev,
1278 			    "No more TX desc@%d, ring->idx = %d, idx = %d, skblen = 0x%x queuelen=%d",
1279 			    tcb_desc->queue_index, ring->idx, idx, skb->len,
1280 			    skb_queue_len(&ring->queue));
1281 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1282 		return skb->len;
1283 	}
1284 	rtl92e_fill_tx_desc(dev, pdesc, tcb_desc, skb);
1285 	__skb_queue_tail(&ring->queue, skb);
1286 	pdesc->OWN = 1;
1287 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1288 	netif_trans_update(dev);
1289 
1290 	rtl92e_writew(dev, TP_POLL, 0x01 << tcb_desc->queue_index);
1291 	return 0;
1292 }
1293 
_rtl92e_alloc_rx_ring(struct net_device * dev)1294 static short _rtl92e_alloc_rx_ring(struct net_device *dev)
1295 {
1296 	struct r8192_priv *priv = rtllib_priv(dev);
1297 	struct rx_desc *entry = NULL;
1298 	int i;
1299 
1300 	priv->rx_ring = dma_alloc_coherent(&priv->pdev->dev,
1301 					   sizeof(*priv->rx_ring) * priv->rxringcount,
1302 					   &priv->rx_ring_dma,
1303 					   GFP_ATOMIC);
1304 	if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
1305 		netdev_warn(dev, "Cannot allocate RX ring\n");
1306 		return -ENOMEM;
1307 	}
1308 
1309 	priv->rx_idx = 0;
1310 
1311 	for (i = 0; i < priv->rxringcount; i++) {
1312 		struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
1313 		dma_addr_t *mapping;
1314 
1315 		entry = &priv->rx_ring[i];
1316 		if (!skb)
1317 			return 0;
1318 		skb->dev = dev;
1319 		priv->rx_buf[i] = skb;
1320 		mapping = (dma_addr_t *)skb->cb;
1321 		*mapping = dma_map_single(&priv->pdev->dev,
1322 					  skb_tail_pointer(skb),
1323 					  priv->rxbuffersize, DMA_FROM_DEVICE);
1324 		if (dma_mapping_error(&priv->pdev->dev, *mapping)) {
1325 			dev_kfree_skb_any(skb);
1326 			return -1;
1327 		}
1328 		entry->BufferAddress = *mapping;
1329 
1330 		entry->Length = priv->rxbuffersize;
1331 		entry->OWN = 1;
1332 	}
1333 
1334 	if (entry)
1335 		entry->EOR = 1;
1336 	return 0;
1337 }
1338 
_rtl92e_alloc_tx_ring(struct net_device * dev,unsigned int prio,unsigned int entries)1339 static int _rtl92e_alloc_tx_ring(struct net_device *dev, unsigned int prio,
1340 				 unsigned int entries)
1341 {
1342 	struct r8192_priv *priv = rtllib_priv(dev);
1343 	struct tx_desc *ring;
1344 	dma_addr_t dma;
1345 	int i;
1346 
1347 	ring = dma_alloc_coherent(&priv->pdev->dev, sizeof(*ring) * entries,
1348 				  &dma, GFP_ATOMIC);
1349 	if (!ring || (unsigned long)ring & 0xFF) {
1350 		netdev_warn(dev, "Cannot allocate TX ring (prio = %d)\n", prio);
1351 		return -ENOMEM;
1352 	}
1353 
1354 	priv->tx_ring[prio].desc = ring;
1355 	priv->tx_ring[prio].dma = dma;
1356 	priv->tx_ring[prio].idx = 0;
1357 	priv->tx_ring[prio].entries = entries;
1358 	skb_queue_head_init(&priv->tx_ring[prio].queue);
1359 
1360 	for (i = 0; i < entries; i++)
1361 		ring[i].NextDescAddress =
1362 			(u32)dma + ((i + 1) % entries) *
1363 			sizeof(*ring);
1364 
1365 	return 0;
1366 }
1367 
_rtl92e_pci_initdescring(struct net_device * dev)1368 static short _rtl92e_pci_initdescring(struct net_device *dev)
1369 {
1370 	u32 ret;
1371 	int i;
1372 	struct r8192_priv *priv = rtllib_priv(dev);
1373 
1374 	ret = _rtl92e_alloc_rx_ring(dev);
1375 	if (ret)
1376 		return ret;
1377 
1378 	for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1379 		ret = _rtl92e_alloc_tx_ring(dev, i, priv->txringcount);
1380 		if (ret)
1381 			goto err_free_rings;
1382 	}
1383 
1384 	return 0;
1385 
1386 err_free_rings:
1387 	_rtl92e_free_rx_ring(dev);
1388 	for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
1389 		if (priv->tx_ring[i].desc)
1390 			_rtl92e_free_tx_ring(dev, i);
1391 	return 1;
1392 }
1393 
rtl92e_reset_desc_ring(struct net_device * dev)1394 void rtl92e_reset_desc_ring(struct net_device *dev)
1395 {
1396 	struct r8192_priv *priv = rtllib_priv(dev);
1397 	int i;
1398 	unsigned long flags = 0;
1399 
1400 	if (priv->rx_ring) {
1401 		struct rx_desc *entry = NULL;
1402 
1403 		for (i = 0; i < priv->rxringcount; i++) {
1404 			entry = &priv->rx_ring[i];
1405 			entry->OWN = 1;
1406 		}
1407 		priv->rx_idx = 0;
1408 	}
1409 
1410 	spin_lock_irqsave(&priv->irq_th_lock, flags);
1411 	for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1412 		if (priv->tx_ring[i].desc) {
1413 			struct rtl8192_tx_ring *ring = &priv->tx_ring[i];
1414 
1415 			while (skb_queue_len(&ring->queue)) {
1416 				struct tx_desc *entry = &ring->desc[ring->idx];
1417 				struct sk_buff *skb =
1418 						 __skb_dequeue(&ring->queue);
1419 
1420 				dma_unmap_single(&priv->pdev->dev,
1421 						 entry->TxBuffAddr, skb->len,
1422 						 DMA_TO_DEVICE);
1423 				kfree_skb(skb);
1424 				ring->idx = (ring->idx + 1) % ring->entries;
1425 			}
1426 			ring->idx = 0;
1427 		}
1428 	}
1429 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1430 }
1431 
rtl92e_translate_to_dbm(struct r8192_priv * priv,u8 signal_strength_index)1432 long rtl92e_translate_to_dbm(struct r8192_priv *priv, u8 signal_strength_index)
1433 {
1434 	long	signal_power;
1435 
1436 	signal_power = (long)((signal_strength_index + 1) >> 1);
1437 	signal_power -= 95;
1438 
1439 	return signal_power;
1440 }
1441 
rtl92e_update_rx_statistics(struct r8192_priv * priv,struct rtllib_rx_stats * pprevious_stats)1442 void rtl92e_update_rx_statistics(struct r8192_priv *priv,
1443 				 struct rtllib_rx_stats *pprevious_stats)
1444 {
1445 	int weighting = 0;
1446 
1447 	if (priv->stats.recv_signal_power == 0)
1448 		priv->stats.recv_signal_power =
1449 					 pprevious_stats->RecvSignalPower;
1450 
1451 	if (pprevious_stats->RecvSignalPower > priv->stats.recv_signal_power)
1452 		weighting = 5;
1453 	else if (pprevious_stats->RecvSignalPower <
1454 		 priv->stats.recv_signal_power)
1455 		weighting = (-5);
1456 	priv->stats.recv_signal_power = (priv->stats.recv_signal_power * 5 +
1457 					pprevious_stats->RecvSignalPower +
1458 					weighting) / 6;
1459 }
1460 
rtl92e_rx_db_to_percent(s8 antpower)1461 u8 rtl92e_rx_db_to_percent(s8 antpower)
1462 {
1463 	if ((antpower <= -100) || (antpower >= 20))
1464 		return	0;
1465 	else if (antpower >= 0)
1466 		return	100;
1467 	else
1468 		return	100 + antpower;
1469 
1470 }	/* QueryRxPwrPercentage */
1471 
rtl92e_evm_db_to_percent(s8 value)1472 u8 rtl92e_evm_db_to_percent(s8 value)
1473 {
1474 	s8 ret_val = clamp(-value, 0, 33) * 3;
1475 
1476 	if (ret_val == 99)
1477 		ret_val = 100;
1478 
1479 	return ret_val;
1480 }
1481 
rtl92e_copy_mpdu_stats(struct rtllib_rx_stats * psrc_stats,struct rtllib_rx_stats * ptarget_stats)1482 void rtl92e_copy_mpdu_stats(struct rtllib_rx_stats *psrc_stats,
1483 			    struct rtllib_rx_stats *ptarget_stats)
1484 {
1485 	ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
1486 	ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
1487 }
1488 
_rtl92e_rx_normal(struct net_device * dev)1489 static void _rtl92e_rx_normal(struct net_device *dev)
1490 {
1491 	struct r8192_priv *priv = rtllib_priv(dev);
1492 	struct ieee80211_hdr *rtllib_hdr = NULL;
1493 	bool unicast_packet = false;
1494 	u32 skb_len = 0;
1495 
1496 	struct rtllib_rx_stats stats = {
1497 		.signal = 0,
1498 		.noise = (u8)-98,
1499 		.rate = 0,
1500 	};
1501 	unsigned int count = priv->rxringcount;
1502 
1503 	while (count--) {
1504 		struct rx_desc *pdesc = &priv->rx_ring
1505 					[priv->rx_idx];
1506 		struct sk_buff *skb = priv->rx_buf
1507 				      [priv->rx_idx];
1508 		struct sk_buff *new_skb;
1509 
1510 		if (pdesc->OWN)
1511 			return;
1512 		if (!rtl92e_get_rx_stats(dev, &stats, pdesc, skb))
1513 			goto done;
1514 		new_skb = dev_alloc_skb(priv->rxbuffersize);
1515 		/* if allocation of new skb failed - drop current packet
1516 		 * and reuse skb
1517 		 */
1518 		if (unlikely(!new_skb))
1519 			goto done;
1520 
1521 		dma_unmap_single(&priv->pdev->dev, *((dma_addr_t *)skb->cb),
1522 				 priv->rxbuffersize, DMA_FROM_DEVICE);
1523 
1524 		skb_put(skb, pdesc->Length);
1525 		skb_reserve(skb, stats.rx_drv_info_size +
1526 			stats.rx_buf_shift);
1527 		skb_trim(skb, skb->len - S_CRC_LEN);
1528 		rtllib_hdr = (struct ieee80211_hdr *)skb->data;
1529 		if (!is_multicast_ether_addr(rtllib_hdr->addr1)) {
1530 			/* unicast packet */
1531 			unicast_packet = true;
1532 		}
1533 		skb_len = skb->len;
1534 
1535 		if (!rtllib_rx(priv->rtllib, skb, &stats)) {
1536 			dev_kfree_skb_any(skb);
1537 		} else {
1538 			if (unicast_packet)
1539 				priv->stats.rxbytesunicast += skb_len;
1540 		}
1541 
1542 		skb = new_skb;
1543 		skb->dev = dev;
1544 
1545 		priv->rx_buf[priv->rx_idx] = skb;
1546 		*((dma_addr_t *)skb->cb) = dma_map_single(&priv->pdev->dev,
1547 							  skb_tail_pointer(skb),
1548 							  priv->rxbuffersize, DMA_FROM_DEVICE);
1549 		if (dma_mapping_error(&priv->pdev->dev, *((dma_addr_t *)skb->cb))) {
1550 			dev_kfree_skb_any(skb);
1551 			return;
1552 		}
1553 done:
1554 		pdesc->BufferAddress = *((dma_addr_t *)skb->cb);
1555 		pdesc->OWN = 1;
1556 		pdesc->Length = priv->rxbuffersize;
1557 		if (priv->rx_idx == priv->rxringcount - 1)
1558 			pdesc->EOR = 1;
1559 		priv->rx_idx = (priv->rx_idx + 1) %
1560 					      priv->rxringcount;
1561 	}
1562 }
1563 
_rtl92e_tx_resume(struct net_device * dev)1564 static void _rtl92e_tx_resume(struct net_device *dev)
1565 {
1566 	struct r8192_priv *priv = rtllib_priv(dev);
1567 	struct rtllib_device *ieee = priv->rtllib;
1568 	struct sk_buff *skb;
1569 	int queue_index;
1570 
1571 	for (queue_index = BK_QUEUE;
1572 	     queue_index < MAX_QUEUE_SIZE; queue_index++) {
1573 		while ((!skb_queue_empty(&ieee->skb_waitq[queue_index])) &&
1574 		(priv->rtllib->check_nic_enough_desc(dev, queue_index) > 0)) {
1575 			skb = skb_dequeue(&ieee->skb_waitq[queue_index]);
1576 			ieee->softmac_data_hard_start_xmit(skb, dev, 0);
1577 		}
1578 	}
1579 }
1580 
_rtl92e_irq_tx_tasklet(struct tasklet_struct * t)1581 static void _rtl92e_irq_tx_tasklet(struct tasklet_struct *t)
1582 {
1583 	struct r8192_priv *priv = from_tasklet(priv, t, irq_tx_tasklet);
1584 
1585 	_rtl92e_tx_resume(priv->rtllib->dev);
1586 }
1587 
_rtl92e_irq_rx_tasklet(struct tasklet_struct * t)1588 static void _rtl92e_irq_rx_tasklet(struct tasklet_struct *t)
1589 {
1590 	struct r8192_priv *priv = from_tasklet(priv, t, irq_rx_tasklet);
1591 
1592 	_rtl92e_rx_normal(priv->rtllib->dev);
1593 
1594 	rtl92e_writel(priv->rtllib->dev, INTA_MASK,
1595 		      rtl92e_readl(priv->rtllib->dev, INTA_MASK) | IMR_RDU);
1596 }
1597 
1598 /****************************************************************************
1599  * ---------------------------- NIC START/CLOSE STUFF---------------------------
1600  ****************************************************************************/
_rtl92e_cancel_deferred_work(struct r8192_priv * priv)1601 static void _rtl92e_cancel_deferred_work(struct r8192_priv *priv)
1602 {
1603 	cancel_delayed_work_sync(&priv->watch_dog_wq);
1604 	cancel_delayed_work_sync(&priv->update_beacon_wq);
1605 	cancel_delayed_work(&priv->rtllib->hw_sleep_wq);
1606 	cancel_work_sync(&priv->reset_wq);
1607 	cancel_work_sync(&priv->qos_activate);
1608 }
1609 
_rtl92e_up(struct net_device * dev)1610 static int _rtl92e_up(struct net_device *dev)
1611 {
1612 	if (_rtl92e_sta_up(dev) == -1)
1613 		return -1;
1614 	return 0;
1615 }
1616 
_rtl92e_open(struct net_device * dev)1617 static int _rtl92e_open(struct net_device *dev)
1618 {
1619 	struct r8192_priv *priv = rtllib_priv(dev);
1620 	int ret;
1621 
1622 	mutex_lock(&priv->wx_mutex);
1623 	ret = _rtl92e_try_up(dev);
1624 	mutex_unlock(&priv->wx_mutex);
1625 	return ret;
1626 }
1627 
_rtl92e_try_up(struct net_device * dev)1628 static int _rtl92e_try_up(struct net_device *dev)
1629 {
1630 	struct r8192_priv *priv = rtllib_priv(dev);
1631 
1632 	if (priv->up == 1)
1633 		return -1;
1634 	return _rtl92e_up(dev);
1635 }
1636 
_rtl92e_close(struct net_device * dev)1637 static int _rtl92e_close(struct net_device *dev)
1638 {
1639 	struct r8192_priv *priv = rtllib_priv(dev);
1640 	int ret;
1641 
1642 	if ((rtllib_act_scanning(priv->rtllib, false)) &&
1643 		!(priv->rtllib->softmac_features & IEEE_SOFTMAC_SCAN)) {
1644 		rtllib_stop_scan(priv->rtllib);
1645 	}
1646 
1647 	mutex_lock(&priv->wx_mutex);
1648 
1649 	ret = _rtl92e_down(dev, true);
1650 
1651 	mutex_unlock(&priv->wx_mutex);
1652 
1653 	return ret;
1654 }
1655 
_rtl92e_down(struct net_device * dev,bool shutdownrf)1656 static int _rtl92e_down(struct net_device *dev, bool shutdownrf)
1657 {
1658 	if (_rtl92e_sta_down(dev, shutdownrf) == -1)
1659 		return -1;
1660 
1661 	return 0;
1662 }
1663 
rtl92e_commit(struct net_device * dev)1664 void rtl92e_commit(struct net_device *dev)
1665 {
1666 	struct r8192_priv *priv = rtllib_priv(dev);
1667 
1668 	if (priv->up == 0)
1669 		return;
1670 	rtllib_softmac_stop_protocol(priv->rtllib);
1671 	rtl92e_irq_disable(dev);
1672 	rtl92e_stop_adapter(dev, true);
1673 	_rtl92e_up(dev);
1674 }
1675 
_rtl92e_restart(void * data)1676 static void _rtl92e_restart(void *data)
1677 {
1678 	struct r8192_priv *priv = container_of(data, struct r8192_priv, reset_wq);
1679 	struct net_device *dev = priv->rtllib->dev;
1680 
1681 	mutex_lock(&priv->wx_mutex);
1682 
1683 	rtl92e_commit(dev);
1684 
1685 	mutex_unlock(&priv->wx_mutex);
1686 }
1687 
_rtl92e_set_multicast(struct net_device * dev)1688 static void _rtl92e_set_multicast(struct net_device *dev)
1689 {
1690 	struct r8192_priv *priv = rtllib_priv(dev);
1691 	short promisc;
1692 
1693 	promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
1694 	priv->promisc = promisc;
1695 }
1696 
_rtl92e_set_mac_adr(struct net_device * dev,void * mac)1697 static int _rtl92e_set_mac_adr(struct net_device *dev, void *mac)
1698 {
1699 	struct r8192_priv *priv = rtllib_priv(dev);
1700 	struct sockaddr *addr = mac;
1701 
1702 	mutex_lock(&priv->wx_mutex);
1703 
1704 	eth_hw_addr_set(dev, addr->sa_data);
1705 
1706 	schedule_work(&priv->reset_wq);
1707 	mutex_unlock(&priv->wx_mutex);
1708 
1709 	return 0;
1710 }
1711 
_rtl92e_irq(int irq,void * netdev)1712 static irqreturn_t _rtl92e_irq(int irq, void *netdev)
1713 {
1714 	struct net_device *dev = netdev;
1715 	struct r8192_priv *priv = rtllib_priv(dev);
1716 	unsigned long flags;
1717 	u32 inta;
1718 
1719 	if (priv->irq_enabled == 0)
1720 		goto done;
1721 
1722 	spin_lock_irqsave(&priv->irq_th_lock, flags);
1723 
1724 	rtl92e_ack_irq(dev, &inta);
1725 
1726 	if (!inta) {
1727 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1728 		goto done;
1729 	}
1730 
1731 	if (inta == 0xffff) {
1732 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1733 		goto done;
1734 	}
1735 
1736 	if (!netif_running(dev)) {
1737 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1738 		goto done;
1739 	}
1740 
1741 	if (inta  & IMR_MGNTDOK) {
1742 		_rtl92e_tx_isr(dev, MGNT_QUEUE);
1743 		spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1744 		if (priv->rtllib->ack_tx_to_ieee) {
1745 			if (_rtl92e_is_tx_queue_empty(dev)) {
1746 				priv->rtllib->ack_tx_to_ieee = 0;
1747 				rtllib_ps_tx_ack(priv->rtllib, 1);
1748 			}
1749 		}
1750 		spin_lock_irqsave(&priv->irq_th_lock, flags);
1751 	}
1752 
1753 	if (inta & IMR_COMDOK)
1754 		_rtl92e_tx_isr(dev, TXCMD_QUEUE);
1755 
1756 	if (inta & IMR_HIGHDOK)
1757 		_rtl92e_tx_isr(dev, HIGH_QUEUE);
1758 
1759 	if (inta & IMR_ROK)
1760 		tasklet_schedule(&priv->irq_rx_tasklet);
1761 
1762 	if (inta & IMR_RDU) {
1763 		rtl92e_writel(dev, INTA_MASK,
1764 			      rtl92e_readl(dev, INTA_MASK) & ~IMR_RDU);
1765 		tasklet_schedule(&priv->irq_rx_tasklet);
1766 	}
1767 
1768 	if (inta & IMR_RXFOVW)
1769 		tasklet_schedule(&priv->irq_rx_tasklet);
1770 
1771 	if (inta & IMR_BKDOK) {
1772 		priv->rtllib->link_detect_info.num_tx_ok_in_period++;
1773 		_rtl92e_tx_isr(dev, BK_QUEUE);
1774 	}
1775 
1776 	if (inta & IMR_BEDOK) {
1777 		priv->rtllib->link_detect_info.num_tx_ok_in_period++;
1778 		_rtl92e_tx_isr(dev, BE_QUEUE);
1779 	}
1780 
1781 	if (inta & IMR_VIDOK) {
1782 		priv->rtllib->link_detect_info.num_tx_ok_in_period++;
1783 		_rtl92e_tx_isr(dev, VI_QUEUE);
1784 	}
1785 
1786 	if (inta & IMR_VODOK) {
1787 		priv->rtllib->link_detect_info.num_tx_ok_in_period++;
1788 		_rtl92e_tx_isr(dev, VO_QUEUE);
1789 	}
1790 
1791 	spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1792 
1793 done:
1794 
1795 	return IRQ_HANDLED;
1796 }
1797 
1798 /****************************************************************************
1799  * ---------------------------- PCI_STUFF---------------------------
1800  ****************************************************************************/
1801 static const struct net_device_ops rtl8192_netdev_ops = {
1802 	.ndo_open = _rtl92e_open,
1803 	.ndo_stop = _rtl92e_close,
1804 	.ndo_tx_timeout = _rtl92e_tx_timeout,
1805 	.ndo_set_rx_mode = _rtl92e_set_multicast,
1806 	.ndo_set_mac_address = _rtl92e_set_mac_adr,
1807 	.ndo_validate_addr = eth_validate_addr,
1808 	.ndo_start_xmit = rtllib_xmit,
1809 };
1810 
_rtl92e_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)1811 static int _rtl92e_pci_probe(struct pci_dev *pdev,
1812 			     const struct pci_device_id *id)
1813 {
1814 	unsigned long ioaddr = 0;
1815 	struct net_device *dev = NULL;
1816 	struct r8192_priv *priv = NULL;
1817 	unsigned long pmem_start, pmem_len, pmem_flags;
1818 	int err = -ENOMEM;
1819 
1820 	if (pci_enable_device(pdev)) {
1821 		dev_err(&pdev->dev, "Failed to enable PCI device");
1822 		return -EIO;
1823 	}
1824 
1825 	pci_set_master(pdev);
1826 
1827 	if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
1828 		if (dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32))) {
1829 			dev_info(&pdev->dev,
1830 				 "Unable to obtain 32bit DMA for consistent allocations\n");
1831 			goto err_pci_disable;
1832 		}
1833 	}
1834 	dev = alloc_rtllib(sizeof(struct r8192_priv));
1835 	if (!dev)
1836 		goto err_pci_disable;
1837 
1838 	err = -ENODEV;
1839 
1840 	pci_set_drvdata(pdev, dev);
1841 	SET_NETDEV_DEV(dev, &pdev->dev);
1842 	priv = rtllib_priv(dev);
1843 	priv->rtllib = (struct rtllib_device *)netdev_priv_rsl(dev);
1844 	priv->pdev = pdev;
1845 	priv->rtllib->pdev = pdev;
1846 	if ((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK) &&
1847 	    (pdev->subsystem_device == 0x3304))
1848 		priv->rtllib->bSupportRemoteWakeUp = 1;
1849 	else
1850 		priv->rtllib->bSupportRemoteWakeUp = 0;
1851 
1852 	pmem_start = pci_resource_start(pdev, 1);
1853 	pmem_len = pci_resource_len(pdev, 1);
1854 	pmem_flags = pci_resource_flags(pdev, 1);
1855 
1856 	if (!(pmem_flags & IORESOURCE_MEM)) {
1857 		netdev_err(dev, "region #1 not a MMIO resource, aborting");
1858 		goto err_rel_rtllib;
1859 	}
1860 
1861 	dev_info(&pdev->dev, "Memory mapped space start: 0x%08lx\n",
1862 		 pmem_start);
1863 	if (!request_mem_region(pmem_start, pmem_len, DRV_NAME)) {
1864 		netdev_err(dev, "request_mem_region failed!");
1865 		goto err_rel_rtllib;
1866 	}
1867 
1868 	ioaddr = (unsigned long)ioremap(pmem_start, pmem_len);
1869 	if (ioaddr == (unsigned long)NULL) {
1870 		netdev_err(dev, "ioremap failed!");
1871 		goto err_rel_mem;
1872 	}
1873 
1874 	dev->mem_start = ioaddr;
1875 	dev->mem_end = ioaddr + pci_resource_len(pdev, 0);
1876 
1877 	if (!rtl92e_check_adapter(pdev, dev))
1878 		goto err_unmap;
1879 
1880 	dev->irq = pdev->irq;
1881 	priv->irq = 0;
1882 
1883 	dev->netdev_ops = &rtl8192_netdev_ops;
1884 
1885 	dev->wireless_handlers = &r8192_wx_handlers_def;
1886 	dev->ethtool_ops = &rtl819x_ethtool_ops;
1887 
1888 	dev->type = ARPHRD_ETHER;
1889 	dev->watchdog_timeo = HZ * 3;
1890 
1891 	if (dev_alloc_name(dev, ifname) < 0)
1892 		dev_alloc_name(dev, ifname);
1893 
1894 	if (_rtl92e_init(dev) != 0) {
1895 		netdev_warn(dev, "Initialization failed");
1896 		goto err_free_irq;
1897 	}
1898 
1899 	netif_carrier_off(dev);
1900 	netif_stop_queue(dev);
1901 
1902 	if (register_netdev(dev))
1903 		goto err_free_irq;
1904 
1905 	if (priv->polling_timer_on == 0)
1906 		rtl92e_check_rfctrl_gpio_timer(&priv->gpio_polling_timer);
1907 
1908 	return 0;
1909 
1910 err_free_irq:
1911 	free_irq(dev->irq, dev);
1912 	priv->irq = 0;
1913 err_unmap:
1914 	iounmap((void __iomem *)ioaddr);
1915 err_rel_mem:
1916 	release_mem_region(pmem_start, pmem_len);
1917 err_rel_rtllib:
1918 	free_rtllib(dev);
1919 err_pci_disable:
1920 	pci_disable_device(pdev);
1921 	return err;
1922 }
1923 
_rtl92e_pci_disconnect(struct pci_dev * pdev)1924 static void _rtl92e_pci_disconnect(struct pci_dev *pdev)
1925 {
1926 	struct net_device *dev = pci_get_drvdata(pdev);
1927 	struct r8192_priv *priv;
1928 	u32 i;
1929 
1930 	if (dev) {
1931 		unregister_netdev(dev);
1932 
1933 		priv = rtllib_priv(dev);
1934 
1935 		del_timer_sync(&priv->gpio_polling_timer);
1936 		cancel_delayed_work_sync(&priv->gpio_change_rf_wq);
1937 		priv->polling_timer_on = 0;
1938 		_rtl92e_down(dev, true);
1939 		rtl92e_dm_deinit(dev);
1940 		vfree(priv->fw_info);
1941 		priv->fw_info = NULL;
1942 		_rtl92e_free_rx_ring(dev);
1943 		for (i = 0; i < MAX_TX_QUEUE_COUNT; i++)
1944 			_rtl92e_free_tx_ring(dev, i);
1945 
1946 		if (priv->irq) {
1947 			dev_info(&pdev->dev, "Freeing irq %d\n", dev->irq);
1948 			free_irq(dev->irq, dev);
1949 			priv->irq = 0;
1950 		}
1951 
1952 		if (dev->mem_start != 0) {
1953 			iounmap((void __iomem *)dev->mem_start);
1954 			release_mem_region(pci_resource_start(pdev, 1),
1955 					pci_resource_len(pdev, 1));
1956 		}
1957 
1958 		free_rtllib(dev);
1959 	}
1960 
1961 	pci_disable_device(pdev);
1962 }
1963 
rtl92e_enable_nic(struct net_device * dev)1964 bool rtl92e_enable_nic(struct net_device *dev)
1965 {
1966 	bool init_status = true;
1967 	struct r8192_priv *priv = rtllib_priv(dev);
1968 	struct rt_pwr_save_ctrl *psc = (struct rt_pwr_save_ctrl *)
1969 					(&priv->rtllib->pwr_save_ctrl);
1970 
1971 	if (!priv->up) {
1972 		netdev_warn(dev, "%s(): Driver is already down!\n", __func__);
1973 		return false;
1974 	}
1975 
1976 	init_status = rtl92e_start_adapter(dev);
1977 	if (!init_status) {
1978 		netdev_warn(dev, "%s(): Initialization failed!\n", __func__);
1979 		return false;
1980 	}
1981 	RT_CLEAR_PS_LEVEL(psc, RT_RF_OFF_LEVL_HALT_NIC);
1982 
1983 	rtl92e_irq_enable(dev);
1984 	return init_status;
1985 }
1986 
1987 module_pci_driver(rtl8192_pci_driver);
1988 
rtl92e_check_rfctrl_gpio_timer(struct timer_list * t)1989 void rtl92e_check_rfctrl_gpio_timer(struct timer_list *t)
1990 {
1991 	struct r8192_priv *priv = from_timer(priv, t, gpio_polling_timer);
1992 
1993 	priv->polling_timer_on = 1;
1994 
1995 	schedule_delayed_work(&priv->gpio_change_rf_wq, 0);
1996 
1997 	mod_timer(&priv->gpio_polling_timer, jiffies +
1998 		  msecs_to_jiffies(RTLLIB_WATCH_DOG_TIME));
1999 }
2000 
2001 /***************************************************************************
2002  * ------------------- module init / exit stubs ----------------
2003  ***************************************************************************/
2004 MODULE_DESCRIPTION("Linux driver for Realtek RTL819x WiFi cards");
2005 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
2006 MODULE_VERSION(DRV_VERSION);
2007 MODULE_LICENSE("GPL");
2008 MODULE_FIRMWARE(RTL8192E_BOOT_IMG_FW);
2009 MODULE_FIRMWARE(RTL8192E_MAIN_IMG_FW);
2010 MODULE_FIRMWARE(RTL8192E_DATA_IMG_FW);
2011 
2012 module_param(ifname, charp, 0644);
2013 module_param(hwwep, int, 0644);
2014 
2015 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
2016 MODULE_PARM_DESC(hwwep, " Try to use hardware WEP support(default use hw. set 0 to use software security)");
2017