1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
5 * Based on the r8180 driver, which is:
6 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
8 * Contact Information: wlanfae <wlanfae@realtek.com>
10 #include <linux/uaccess.h>
11 #include <linux/pci.h>
12 #include <linux/vmalloc.h>
13 #include <linux/ieee80211.h>
15 #include "r8192E_phy.h"
16 #include "r8192E_phyreg.h"
17 #include "r8190P_rtl8256.h"
18 #include "r8192E_cmdpkt.h"
26 static char *ifname = "wlan%d";
28 static struct pci_device_id rtl8192_pci_id_tbl[] = {
29 {PCI_DEVICE(0x10ec, 0x8192)},
30 {PCI_DEVICE(0x07aa, 0x0044)},
31 {PCI_DEVICE(0x07aa, 0x0047)},
35 MODULE_DEVICE_TABLE(pci, rtl8192_pci_id_tbl);
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);
42 static SIMPLE_DEV_PM_OPS(rtl92e_pm_ops, rtl92e_suspend, rtl92e_resume);
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,
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,
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);
69 /****************************************************************************
70 * -----------------------------IO STUFF-------------------------
71 ****************************************************************************/
73 u8 rtl92e_readb(struct net_device *dev, int x)
75 return 0xff & readb((u8 __iomem *)dev->mem_start + x);
78 u32 rtl92e_readl(struct net_device *dev, int x)
80 return readl((u8 __iomem *)dev->mem_start + x);
83 u16 rtl92e_readw(struct net_device *dev, int x)
85 return readw((u8 __iomem *)dev->mem_start + x);
88 void rtl92e_writeb(struct net_device *dev, int x, u8 y)
90 writeb(y, (u8 __iomem *)dev->mem_start + x);
95 void rtl92e_writel(struct net_device *dev, int x, u32 y)
97 writel(y, (u8 __iomem *)dev->mem_start + x);
102 void rtl92e_writew(struct net_device *dev, int x, u16 y)
104 writew(y, (u8 __iomem *)dev->mem_start + x);
109 /****************************************************************************
110 * -----------------------------GENERAL FUNCTION-------------------------
111 ****************************************************************************/
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)
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;
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);
129 while (priv->rf_change_in_progress) {
133 if (rf_wait_counter > 100) {
135 "%s(): Timeout waiting for RF change.\n",
141 priv->rf_change_in_progress = true;
142 spin_unlock_irqrestore(&priv->rf_ps_lock, flag);
147 rt_state = priv->rtllib->rf_power_state;
149 switch (state_to_set) {
151 priv->rtllib->rf_off_reason &= (~change_source);
153 if ((change_source == RF_CHANGE_BY_HW) && priv->hw_radio_off)
154 priv->hw_radio_off = false;
156 if (!priv->rtllib->rf_off_reason) {
157 priv->rtllib->rf_off_reason = 0;
158 action_allowed = true;
160 if (rt_state == rf_off &&
161 change_source >= RF_CHANGE_BY_HW)
162 connect_by_ssid = true;
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;
174 priv->blinked_ingpio = false;
175 rtllib_mgnt_disconnect(priv->rtllib,
176 WLAN_REASON_DISASSOC_STA_HAS_LEFT);
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;
186 priv->rtllib->rf_off_reason |= change_source;
187 action_allowed = true;
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;
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;
211 static short _rtl92e_check_nic_enough_desc(struct net_device *dev, int prio)
213 struct r8192_priv *priv = rtllib_priv(dev);
214 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
216 if (ring->entries - skb_queue_len(&ring->queue) >= 2)
221 static void _rtl92e_tx_timeout(struct net_device *dev, unsigned int txqueue)
223 struct r8192_priv *priv = rtllib_priv(dev);
225 schedule_work(&priv->reset_wq);
226 netdev_info(dev, "TXTIMEOUT");
229 static void _rtl92e_update_cap(struct net_device *dev, u16 cap)
231 struct r8192_priv *priv = rtllib_priv(dev);
232 struct rtllib_network *net = &priv->rtllib->current_network;
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);
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);
251 if (net->mode & (WIRELESS_MODE_G | WIRELESS_MODE_N_24G)) {
253 u8 cur_slot_time = priv->slot_time;
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);
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);
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)},
280 static void _rtl92e_update_beacon(void *data)
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;
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);
293 static void _rtl92e_qos_activate(void *data)
295 struct r8192_priv *priv = container_of(data, struct r8192_priv, qos_activate);
296 struct net_device *dev = priv->rtllib->dev;
299 mutex_lock(&priv->mutex);
300 if (priv->rtllib->link_state != MAC80211_LINKED)
303 for (i = 0; i < QOS_QUEUE_NUM; i++)
304 priv->rtllib->set_hw_reg_handler(dev, HW_VAR_AC_PARAM, (u8 *)(&i));
307 mutex_unlock(&priv->mutex);
310 static int _rtl92e_qos_handle_probe_response(struct r8192_priv *priv,
312 struct rtllib_network *network)
315 u32 size = sizeof(struct rtllib_qos_parameters);
317 if (priv->rtllib->link_state != MAC80211_LINKED)
320 if (priv->rtllib->iw_mode != IW_MODE_INFRA)
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;
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);
338 memcpy(&priv->rtllib->current_network.qos_data.parameters,
339 &def_qos_parameters, size);
341 if ((network->qos_data.active == 1) && (active_network == 1))
342 schedule_work(&priv->qos_activate);
344 network->qos_data.active = 0;
345 network->qos_data.supported = 0;
351 static int _rtl92e_handle_beacon(struct net_device *dev,
352 struct rtllib_beacon *beacon,
353 struct rtllib_network *network)
355 struct r8192_priv *priv = rtllib_priv(dev);
357 _rtl92e_qos_handle_probe_response(priv, 1, network);
359 schedule_delayed_work(&priv->update_beacon_wq, 0);
363 static int _rtl92e_qos_assoc_resp(struct r8192_priv *priv,
364 struct rtllib_network *network)
367 u32 size = sizeof(struct rtllib_qos_parameters);
368 int set_qos_param = 0;
370 if (!priv || !network)
373 if (priv->rtllib->link_state != MAC80211_LINKED)
376 if (priv->rtllib->iw_mode != IW_MODE_INFRA)
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;
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;
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;
399 spin_unlock_irqrestore(&priv->rtllib->lock, flags);
401 if (set_qos_param == 1) {
402 rtl92e_dm_init_edca_turbo(priv->rtllib->dev);
403 schedule_work(&priv->qos_activate);
408 static int _rtl92e_handle_assoc_response(struct net_device *dev,
409 struct rtllib_assoc_response_frame *resp,
410 struct rtllib_network *network)
412 struct r8192_priv *priv = rtllib_priv(dev);
414 _rtl92e_qos_assoc_resp(priv, network);
418 void rtl92e_config_rate(struct net_device *dev, u16 *rate_config)
420 struct r8192_priv *priv = rtllib_priv(dev);
421 struct rtllib_network *net;
422 u8 i = 0, basic_rate = 0;
424 net = &priv->rtllib->current_network;
426 for (i = 0; i < net->rates_len; i++) {
427 basic_rate = net->rates[i] & 0x7f;
428 switch (basic_rate) {
430 *rate_config |= RRSR_1M;
433 *rate_config |= RRSR_2M;
436 *rate_config |= RRSR_5_5M;
439 *rate_config |= RRSR_11M;
442 *rate_config |= RRSR_6M;
445 *rate_config |= RRSR_9M;
448 *rate_config |= RRSR_12M;
451 *rate_config |= RRSR_18M;
454 *rate_config |= RRSR_24M;
457 *rate_config |= RRSR_36M;
460 *rate_config |= RRSR_48M;
463 *rate_config |= RRSR_54M;
468 for (i = 0; i < net->rates_ex_len; i++) {
469 basic_rate = net->rates_ex[i] & 0x7f;
470 switch (basic_rate) {
472 *rate_config |= RRSR_1M;
475 *rate_config |= RRSR_2M;
478 *rate_config |= RRSR_5_5M;
481 *rate_config |= RRSR_11M;
484 *rate_config |= RRSR_6M;
487 *rate_config |= RRSR_9M;
490 *rate_config |= RRSR_12M;
493 *rate_config |= RRSR_18M;
496 *rate_config |= RRSR_24M;
499 *rate_config |= RRSR_36M;
502 *rate_config |= RRSR_48M;
505 *rate_config |= RRSR_54M;
511 static void _rtl92e_refresh_support_rate(struct r8192_priv *priv)
513 struct rtllib_device *ieee = priv->rtllib;
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);
522 memset(ieee->reg_dot11ht_oper_rate_set, 0, 16);
526 void rtl92e_set_wireless_mode(struct net_device *dev, u8 wireless_mode)
528 struct r8192_priv *priv = rtllib_priv(dev);
529 u8 support_mode = (WIRELESS_MODE_N_24G | WIRELESS_MODE_G | WIRELESS_MODE_B);
531 if ((wireless_mode == WIRELESS_MODE_AUTO) || ((wireless_mode & support_mode) == 0))
532 wireless_mode = WIRELESS_MODE_N_24G;
534 if ((wireless_mode & (WIRELESS_MODE_B | WIRELESS_MODE_G)) ==
535 (WIRELESS_MODE_G | WIRELESS_MODE_B))
536 wireless_mode = WIRELESS_MODE_G;
538 priv->rtllib->mode = wireless_mode;
540 if (wireless_mode == WIRELESS_MODE_N_24G)
541 priv->rtllib->ht_info->enable_ht = 1;
543 priv->rtllib->ht_info->enable_ht = 0;
545 _rtl92e_refresh_support_rate(priv);
548 static int _rtl92e_sta_up(struct net_device *dev)
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);
556 priv->rtllib->ieee_up = 1;
558 priv->up_first_time = 0;
559 init_status = rtl92e_start_adapter(dev);
561 netdev_err(dev, "%s(): Initialization failed!\n", __func__);
565 RT_CLEAR_PS_LEVEL(psc, RT_RF_OFF_LEVL_HALT_NIC);
567 if (priv->polling_timer_on == 0)
568 rtl92e_check_rfctrl_gpio_timer(&priv->gpio_polling_timer);
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);
575 if (!netif_queue_stopped(dev))
576 netif_start_queue(dev);
578 netif_wake_queue(dev);
580 priv->bfirst_after_down = false;
584 static int _rtl92e_sta_down(struct net_device *dev, bool shutdownrf)
586 struct r8192_priv *priv = rtllib_priv(dev);
587 unsigned long flags = 0;
588 u8 rf_in_progress_timeout = 0;
593 priv->rtllib->rtllib_ips_leave(dev);
595 if (priv->rtllib->link_state == MAC80211_LINKED)
596 rtl92e_leisure_ps_leave(dev);
599 priv->rtllib->ieee_up = 0;
600 priv->bfirst_after_down = true;
601 if (!netif_queue_stopped(dev))
602 netif_stop_queue(dev);
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);
611 del_timer_sync(&priv->watch_dog_timer);
612 _rtl92e_cancel_deferred_work(priv);
613 cancel_delayed_work(&priv->rtllib->hw_wakeup_wq);
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);
624 rf_in_progress_timeout++;
625 spin_lock_irqsave(&priv->rf_ps_lock, flags);
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);
634 memset(&priv->rtllib->current_network, 0,
635 offsetof(struct rtllib_network, list));
640 static void _rtl92e_init_priv_handler(struct net_device *dev)
642 struct r8192_priv *priv = rtllib_priv(dev);
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;
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;
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;
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->ScanOperationBackupHandler = rtl92e_scan_op_backup;
671 static void _rtl92e_init_priv_variable(struct net_device *dev)
673 struct r8192_priv *priv = rtllib_priv(dev);
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;
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);
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;
712 priv->rtllib->current_network.beacon_interval = DEFAULT_BEACONINTERVAL;
713 priv->rtllib->iw_mode = IW_MODE_INFRA;
714 priv->rtllib->be_scan_inprogress = false;
716 priv->rtllib->fts = DEFAULT_FRAG_THRESHOLD;
718 priv->fw_info = vzalloc(sizeof(struct rt_firmware));
721 "rtl8192e: Unable to allocate space for firmware\n");
723 skb_queue_head_init(&priv->skb_queue);
725 for (i = 0; i < MAX_QUEUE_SIZE; i++)
726 skb_queue_head_init(&priv->rtllib->skb_waitq[i]);
729 static void _rtl92e_init_priv_lock(struct r8192_priv *priv)
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);
740 static void _rtl92e_init_priv_task(struct net_device *dev)
742 struct r8192_priv *priv = rtllib_priv(dev);
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);
757 static short _rtl92e_get_channel_map(struct net_device *dev)
761 struct r8192_priv *priv = rtllib_priv(dev);
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;
771 static short _rtl92e_init(struct net_device *dev)
773 struct r8192_priv *priv = rtllib_priv(dev);
775 memset(&priv->stats, 0, sizeof(struct rt_stats));
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);
787 timer_setup(&priv->watch_dog_timer, _rtl92e_watchdog_timer_cb, 0);
789 timer_setup(&priv->gpio_polling_timer, rtl92e_check_rfctrl_gpio_timer,
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);
798 priv->irq = dev->irq;
800 if (_rtl92e_pci_initdescring(dev) != 0) {
801 netdev_err(dev, "Endopoints initialization failed");
802 free_irq(dev->irq, dev);
809 /***************************************************************************
810 * -------------------------------WATCHDOG STUFF---------------------------
811 **************************************************************************/
812 static short _rtl92e_is_tx_queue_empty(struct net_device *dev)
815 struct r8192_priv *priv = rtllib_priv(dev);
817 for (i = 0; i <= MGNT_QUEUE; i++) {
818 if ((i == TXCMD_QUEUE) || (i == HCCA_QUEUE))
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));
829 static enum reset_type _rtl92e_tx_check_stuck(struct net_device *dev)
831 struct r8192_priv *priv = rtllib_priv(dev);
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;
839 switch (priv->rtllib->ps) {
840 case RTLLIB_PS_DISABLED:
842 case (RTLLIB_PS_MBCAST | RTLLIB_PS_UNICAST):
847 spin_lock_irqsave(&priv->irq_th_lock, flags);
848 for (QueueID = 0; QueueID < MAX_TX_QUEUE; QueueID++) {
849 if (QueueID == TXCMD_QUEUE)
852 if (QueueID == BEACON_QUEUE)
855 ring = &priv->tx_ring[QueueID];
857 if (skb_queue_len(&ring->queue) == 0) {
860 skb = __skb_peek(&ring->queue);
861 tcb_desc = (struct cb_desc *)(skb->cb +
863 tcb_desc->nStuckCount++;
864 bCheckFwTxCnt = true;
865 if (tcb_desc->nStuckCount > 1)
867 "%s: QueueID=%d tcb_desc->nStuckCount=%d\n",
869 tcb_desc->nStuckCount);
872 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
875 if (rtl92e_is_tx_stuck(dev))
876 return RESET_TYPE_SILENT;
879 return RESET_TYPE_NORESET;
882 static enum reset_type _rtl92e_rx_check_stuck(struct net_device *dev)
884 if (rtl92e_is_rx_stuck(dev))
885 return RESET_TYPE_SILENT;
887 return RESET_TYPE_NORESET;
890 static void _rtl92e_if_check_reset(struct net_device *dev)
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;
897 rfState = priv->rtllib->rf_power_state;
899 if (rfState == rf_on)
900 TxResetType = _rtl92e_tx_check_stuck(dev);
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);
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);
914 static void _rtl92e_update_rxcounts(struct r8192_priv *priv, u32 *TotalRxBcnNum,
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];
935 static void _rtl92e_watchdog_wq_cb(void *data)
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;
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;
950 if (!priv->up || priv->hw_radio_off)
953 if (priv->rtllib->link_state >= MAC80211_LINKED) {
954 if (priv->rtllib->CntAfterLink < 2)
955 priv->rtllib->CntAfterLink++;
957 priv->rtllib->CntAfterLink = 0;
960 rtl92e_dm_watchdog(dev);
962 if (!rtllib_act_scanning(priv->rtllib, false)) {
963 if ((ieee->iw_mode == IW_MODE_INFRA) && (ieee->link_state ==
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.ReturnPoint == IPS_CALLBACK_NONE)
968 rtl92e_ips_enter(dev);
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)
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;
982 bHigherBusyRxTraffic = false;
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))
992 if (ieee->current_network.beacon_interval < 95)
996 rtl92e_leisure_ps_enter(dev);
998 rtl92e_leisure_ps_leave(dev);
1001 rtl92e_leisure_ps_leave(dev);
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;
1009 ieee->link_detect_info.bHigherBusyTraffic = bHigherBusyTraffic;
1010 ieee->link_detect_info.bHigherBusyRxTraffic = bHigherBusyRxTraffic;
1012 if (ieee->link_state == MAC80211_LINKED && ieee->iw_mode == IW_MODE_INFRA) {
1013 u32 TotalRxBcnNum = 0;
1014 u32 TotalRxDataNum = 0;
1016 _rtl92e_update_rxcounts(priv, &TotalRxBcnNum, &TotalRxDataNum);
1018 if ((TotalRxBcnNum + TotalRxDataNum) == 0)
1019 priv->check_roaming_cnt++;
1021 priv->check_roaming_cnt = 0;
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__);
1028 "===>%s(): AP is power off, chan:%d, connect another one\n",
1029 __func__, priv->chan);
1031 ieee->link_state = RTLLIB_ASSOCIATING;
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);
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);
1045 priv->check_roaming_cnt = 0;
1047 ieee->link_detect_info.num_recv_bcn_in_period = 0;
1048 ieee->link_detect_info.num_recv_data_in_period = 0;
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;
1057 spin_unlock_irqrestore(&priv->tx_lock, flags);
1060 static void _rtl92e_watchdog_timer_cb(struct timer_list *t)
1062 struct r8192_priv *priv = from_timer(priv, t, watch_dog_timer);
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));
1069 /****************************************************************************
1070 * ---------------------------- NIC TX/RX STUFF---------------------------
1071 ****************************************************************************/
1072 void rtl92e_rx_enable(struct net_device *dev)
1074 rtl92e_enable_rx(dev);
1077 void rtl92e_tx_enable(struct net_device *dev)
1079 struct r8192_priv *priv = rtllib_priv(dev);
1081 rtl92e_enable_tx(dev);
1083 rtllib_reset_queue(priv->rtllib);
1086 static void _rtl92e_free_rx_ring(struct net_device *dev)
1088 struct r8192_priv *priv = rtllib_priv(dev);
1091 for (i = 0; i < priv->rxringcount; i++) {
1092 struct sk_buff *skb = priv->rx_buf[i];
1097 dma_unmap_single(&priv->pdev->dev,
1098 *((dma_addr_t *)skb->cb),
1099 priv->rxbuffersize, DMA_FROM_DEVICE);
1103 dma_free_coherent(&priv->pdev->dev,
1104 sizeof(*priv->rx_ring) * priv->rxringcount,
1107 priv->rx_ring = NULL;
1110 static void _rtl92e_free_tx_ring(struct net_device *dev, unsigned int prio)
1112 struct r8192_priv *priv = rtllib_priv(dev);
1113 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
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);
1119 dma_unmap_single(&priv->pdev->dev, entry->TxBuffAddr,
1120 skb->len, DMA_TO_DEVICE);
1122 ring->idx = (ring->idx + 1) % ring->entries;
1125 dma_free_coherent(&priv->pdev->dev,
1126 sizeof(*ring->desc) * ring->entries, ring->desc,
1131 static void _rtl92e_hard_data_xmit(struct sk_buff *skb, struct net_device *dev,
1134 struct r8192_priv *priv = rtllib_priv(dev);
1136 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1138 u8 queue_index = tcb_desc->queue_index;
1140 if ((priv->rtllib->rf_power_state == rf_off) || !priv->up) {
1145 if (queue_index == TXCMD_QUEUE)
1146 netdev_warn(dev, "%s(): queue index == TXCMD_QUEUE\n",
1149 memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1150 skb_push(skb, priv->rtllib->tx_headroom);
1151 ret = _rtl92e_tx(dev, skb);
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++;
1163 static int _rtl92e_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1165 struct r8192_priv *priv = rtllib_priv(dev);
1167 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb +
1169 u8 queue_index = tcb_desc->queue_index;
1171 if (queue_index != TXCMD_QUEUE) {
1172 if ((priv->rtllib->rf_power_state == rf_off) ||
1179 memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
1180 if (queue_index == TXCMD_QUEUE) {
1181 _rtl92e_tx_cmd(dev, skb);
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);
1196 static void _rtl92e_tx_isr(struct net_device *dev, int prio)
1198 struct r8192_priv *priv = rtllib_priv(dev);
1200 struct rtl8192_tx_ring *ring = &priv->tx_ring[prio];
1202 while (skb_queue_len(&ring->queue)) {
1203 struct tx_desc *entry = &ring->desc[ring->idx];
1204 struct sk_buff *skb;
1206 if (prio != BEACON_QUEUE) {
1209 ring->idx = (ring->idx + 1) % ring->entries;
1212 skb = __skb_dequeue(&ring->queue);
1213 dma_unmap_single(&priv->pdev->dev, entry->TxBuffAddr,
1214 skb->len, DMA_TO_DEVICE);
1218 if (prio != BEACON_QUEUE)
1219 tasklet_schedule(&priv->irq_tx_tasklet);
1222 static void _rtl92e_tx_cmd(struct net_device *dev, struct sk_buff *skb)
1224 struct r8192_priv *priv = rtllib_priv(dev);
1225 struct rtl8192_tx_ring *ring;
1226 struct tx_desc_cmd *entry;
1228 struct cb_desc *tcb_desc;
1229 unsigned long flags;
1231 spin_lock_irqsave(&priv->irq_th_lock, flags);
1232 ring = &priv->tx_ring[TXCMD_QUEUE];
1234 idx = (ring->idx + skb_queue_len(&ring->queue)) % ring->entries;
1235 entry = (struct tx_desc_cmd *)&ring->desc[idx];
1237 tcb_desc = (struct cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
1239 rtl92e_fill_tx_cmd_desc(dev, entry, tcb_desc, skb);
1241 __skb_queue_tail(&ring->queue, skb);
1242 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1245 static short _rtl92e_tx(struct net_device *dev, struct sk_buff *skb)
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 +
1252 struct tx_desc *pdesc = NULL;
1253 struct ieee80211_hdr *header = NULL;
1254 u8 *pda_addr = NULL;
1256 u32 fwinfo_size = 0;
1258 priv->rtllib->awake_pkt_sent = true;
1260 fwinfo_size = sizeof(struct tx_fwinfo_8190pci);
1262 header = (struct ieee80211_hdr *)(((u8 *)skb->data) + fwinfo_size);
1263 pda_addr = header->addr1;
1265 if (!is_broadcast_ether_addr(pda_addr) && !is_multicast_ether_addr(pda_addr))
1266 priv->stats.txbytesunicast += skb->len - fwinfo_size;
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;
1275 pdesc = &ring->desc[idx];
1276 if ((pdesc->OWN == 1) && (tcb_desc->queue_index != BEACON_QUEUE)) {
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);
1284 rtl92e_fill_tx_desc(dev, pdesc, tcb_desc, skb);
1285 __skb_queue_tail(&ring->queue, skb);
1287 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1288 netif_trans_update(dev);
1290 rtl92e_writew(dev, TP_POLL, 0x01 << tcb_desc->queue_index);
1294 static short _rtl92e_alloc_rx_ring(struct net_device *dev)
1296 struct r8192_priv *priv = rtllib_priv(dev);
1297 struct rx_desc *entry = NULL;
1300 priv->rx_ring = dma_alloc_coherent(&priv->pdev->dev,
1301 sizeof(*priv->rx_ring) * priv->rxringcount,
1304 if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
1305 netdev_warn(dev, "Cannot allocate RX ring\n");
1311 for (i = 0; i < priv->rxringcount; i++) {
1312 struct sk_buff *skb = dev_alloc_skb(priv->rxbuffersize);
1313 dma_addr_t *mapping;
1315 entry = &priv->rx_ring[i];
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);
1328 entry->BufferAddress = *mapping;
1330 entry->Length = priv->rxbuffersize;
1339 static int _rtl92e_alloc_tx_ring(struct net_device *dev, unsigned int prio,
1340 unsigned int entries)
1342 struct r8192_priv *priv = rtllib_priv(dev);
1343 struct tx_desc *ring;
1347 ring = dma_alloc_coherent(&priv->pdev->dev, sizeof(*ring) * entries,
1349 if (!ring || (unsigned long)ring & 0xFF) {
1350 netdev_warn(dev, "Cannot allocate TX ring (prio = %d)\n", prio);
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);
1360 for (i = 0; i < entries; i++)
1361 ring[i].NextDescAddress =
1362 (u32)dma + ((i + 1) % entries) *
1368 static short _rtl92e_pci_initdescring(struct net_device *dev)
1372 struct r8192_priv *priv = rtllib_priv(dev);
1374 ret = _rtl92e_alloc_rx_ring(dev);
1378 for (i = 0; i < MAX_TX_QUEUE_COUNT; i++) {
1379 ret = _rtl92e_alloc_tx_ring(dev, i, priv->txringcount);
1381 goto 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);
1394 void rtl92e_reset_desc_ring(struct net_device *dev)
1396 struct r8192_priv *priv = rtllib_priv(dev);
1398 unsigned long flags = 0;
1400 if (priv->rx_ring) {
1401 struct rx_desc *entry = NULL;
1403 for (i = 0; i < priv->rxringcount; i++) {
1404 entry = &priv->rx_ring[i];
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];
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);
1420 dma_unmap_single(&priv->pdev->dev,
1421 entry->TxBuffAddr, skb->len,
1424 ring->idx = (ring->idx + 1) % ring->entries;
1429 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1432 long rtl92e_translate_to_dbm(struct r8192_priv *priv, u8 signal_strength_index)
1436 signal_power = (long)((signal_strength_index + 1) >> 1);
1439 return signal_power;
1442 void rtl92e_update_rx_statistics(struct r8192_priv *priv,
1443 struct rtllib_rx_stats *pprevious_stats)
1447 if (priv->stats.recv_signal_power == 0)
1448 priv->stats.recv_signal_power =
1449 pprevious_stats->RecvSignalPower;
1451 if (pprevious_stats->RecvSignalPower > priv->stats.recv_signal_power)
1453 else if (pprevious_stats->RecvSignalPower <
1454 priv->stats.recv_signal_power)
1456 priv->stats.recv_signal_power = (priv->stats.recv_signal_power * 5 +
1457 pprevious_stats->RecvSignalPower +
1461 u8 rtl92e_rx_db_to_percent(s8 antpower)
1463 if ((antpower <= -100) || (antpower >= 20))
1465 else if (antpower >= 0)
1468 return 100 + antpower;
1470 } /* QueryRxPwrPercentage */
1472 u8 rtl92e_evm_db_to_percent(s8 value)
1474 s8 ret_val = clamp(-value, 0, 33) * 3;
1482 void rtl92e_copy_mpdu_stats(struct rtllib_rx_stats *psrc_stats,
1483 struct rtllib_rx_stats *ptarget_stats)
1485 ptarget_stats->bIsAMPDU = psrc_stats->bIsAMPDU;
1486 ptarget_stats->bFirstMPDU = psrc_stats->bFirstMPDU;
1489 static void _rtl92e_rx_normal(struct net_device *dev)
1491 struct r8192_priv *priv = rtllib_priv(dev);
1492 struct ieee80211_hdr *rtllib_hdr = NULL;
1493 bool unicast_packet = false;
1496 struct rtllib_rx_stats stats = {
1501 unsigned int count = priv->rxringcount;
1504 struct rx_desc *pdesc = &priv->rx_ring
1506 struct sk_buff *skb = priv->rx_buf
1508 struct sk_buff *new_skb;
1512 if (!rtl92e_get_rx_stats(dev, &stats, pdesc, skb))
1514 new_skb = dev_alloc_skb(priv->rxbuffersize);
1515 /* if allocation of new skb failed - drop current packet
1518 if (unlikely(!new_skb))
1521 dma_unmap_single(&priv->pdev->dev, *((dma_addr_t *)skb->cb),
1522 priv->rxbuffersize, DMA_FROM_DEVICE);
1524 skb_put(skb, pdesc->Length);
1525 skb_reserve(skb, stats.RxDrvInfoSize +
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;
1535 if (!rtllib_rx(priv->rtllib, skb, &stats)) {
1536 dev_kfree_skb_any(skb);
1539 priv->stats.rxbytesunicast += skb_len;
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);
1554 pdesc->BufferAddress = *((dma_addr_t *)skb->cb);
1556 pdesc->Length = priv->rxbuffersize;
1557 if (priv->rx_idx == priv->rxringcount - 1)
1559 priv->rx_idx = (priv->rx_idx + 1) %
1564 static void _rtl92e_tx_resume(struct net_device *dev)
1566 struct r8192_priv *priv = rtllib_priv(dev);
1567 struct rtllib_device *ieee = priv->rtllib;
1568 struct sk_buff *skb;
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);
1581 static void _rtl92e_irq_tx_tasklet(struct tasklet_struct *t)
1583 struct r8192_priv *priv = from_tasklet(priv, t, irq_tx_tasklet);
1585 _rtl92e_tx_resume(priv->rtllib->dev);
1588 static void _rtl92e_irq_rx_tasklet(struct tasklet_struct *t)
1590 struct r8192_priv *priv = from_tasklet(priv, t, irq_rx_tasklet);
1592 _rtl92e_rx_normal(priv->rtllib->dev);
1594 rtl92e_writel(priv->rtllib->dev, INTA_MASK,
1595 rtl92e_readl(priv->rtllib->dev, INTA_MASK) | IMR_RDU);
1598 /****************************************************************************
1599 * ---------------------------- NIC START/CLOSE STUFF---------------------------
1600 ****************************************************************************/
1601 static void _rtl92e_cancel_deferred_work(struct r8192_priv *priv)
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);
1610 static int _rtl92e_up(struct net_device *dev)
1612 if (_rtl92e_sta_up(dev) == -1)
1617 static int _rtl92e_open(struct net_device *dev)
1619 struct r8192_priv *priv = rtllib_priv(dev);
1622 mutex_lock(&priv->wx_mutex);
1623 ret = _rtl92e_try_up(dev);
1624 mutex_unlock(&priv->wx_mutex);
1628 static int _rtl92e_try_up(struct net_device *dev)
1630 struct r8192_priv *priv = rtllib_priv(dev);
1634 return _rtl92e_up(dev);
1637 static int _rtl92e_close(struct net_device *dev)
1639 struct r8192_priv *priv = rtllib_priv(dev);
1642 if ((rtllib_act_scanning(priv->rtllib, false)) &&
1643 !(priv->rtllib->softmac_features & IEEE_SOFTMAC_SCAN)) {
1644 rtllib_stop_scan(priv->rtllib);
1647 mutex_lock(&priv->wx_mutex);
1649 ret = _rtl92e_down(dev, true);
1651 mutex_unlock(&priv->wx_mutex);
1656 static int _rtl92e_down(struct net_device *dev, bool shutdownrf)
1658 if (_rtl92e_sta_down(dev, shutdownrf) == -1)
1664 void rtl92e_commit(struct net_device *dev)
1666 struct r8192_priv *priv = rtllib_priv(dev);
1670 rtllib_softmac_stop_protocol(priv->rtllib);
1671 rtl92e_irq_disable(dev);
1672 rtl92e_stop_adapter(dev, true);
1676 static void _rtl92e_restart(void *data)
1678 struct r8192_priv *priv = container_of(data, struct r8192_priv, reset_wq);
1679 struct net_device *dev = priv->rtllib->dev;
1681 mutex_lock(&priv->wx_mutex);
1685 mutex_unlock(&priv->wx_mutex);
1688 static void _rtl92e_set_multicast(struct net_device *dev)
1690 struct r8192_priv *priv = rtllib_priv(dev);
1693 promisc = (dev->flags & IFF_PROMISC) ? 1 : 0;
1694 priv->promisc = promisc;
1697 static int _rtl92e_set_mac_adr(struct net_device *dev, void *mac)
1699 struct r8192_priv *priv = rtllib_priv(dev);
1700 struct sockaddr *addr = mac;
1702 mutex_lock(&priv->wx_mutex);
1704 eth_hw_addr_set(dev, addr->sa_data);
1706 schedule_work(&priv->reset_wq);
1707 mutex_unlock(&priv->wx_mutex);
1712 static irqreturn_t _rtl92e_irq(int irq, void *netdev)
1714 struct net_device *dev = netdev;
1715 struct r8192_priv *priv = rtllib_priv(dev);
1716 unsigned long flags;
1719 if (priv->irq_enabled == 0)
1722 spin_lock_irqsave(&priv->irq_th_lock, flags);
1724 rtl92e_ack_irq(dev, &inta);
1727 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1731 if (inta == 0xffff) {
1732 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
1736 if (!netif_running(dev)) {
1737 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
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);
1750 spin_lock_irqsave(&priv->irq_th_lock, flags);
1753 if (inta & IMR_COMDOK)
1754 _rtl92e_tx_isr(dev, TXCMD_QUEUE);
1756 if (inta & IMR_HIGHDOK)
1757 _rtl92e_tx_isr(dev, HIGH_QUEUE);
1760 tasklet_schedule(&priv->irq_rx_tasklet);
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);
1768 if (inta & IMR_RXFOVW)
1769 tasklet_schedule(&priv->irq_rx_tasklet);
1771 if (inta & IMR_BKDOK) {
1772 priv->rtllib->link_detect_info.num_tx_ok_in_period++;
1773 _rtl92e_tx_isr(dev, BK_QUEUE);
1776 if (inta & IMR_BEDOK) {
1777 priv->rtllib->link_detect_info.num_tx_ok_in_period++;
1778 _rtl92e_tx_isr(dev, BE_QUEUE);
1781 if (inta & IMR_VIDOK) {
1782 priv->rtllib->link_detect_info.num_tx_ok_in_period++;
1783 _rtl92e_tx_isr(dev, VI_QUEUE);
1786 if (inta & IMR_VODOK) {
1787 priv->rtllib->link_detect_info.num_tx_ok_in_period++;
1788 _rtl92e_tx_isr(dev, VO_QUEUE);
1791 spin_unlock_irqrestore(&priv->irq_th_lock, flags);
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,
1811 static int _rtl92e_pci_probe(struct pci_dev *pdev,
1812 const struct pci_device_id *id)
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;
1820 if (pci_enable_device(pdev)) {
1821 dev_err(&pdev->dev, "Failed to enable PCI device");
1825 pci_set_master(pdev);
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;
1834 dev = alloc_rtllib(sizeof(struct r8192_priv));
1836 goto err_pci_disable;
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);
1845 priv->rtllib->pdev = pdev;
1846 if ((pdev->subsystem_vendor == PCI_VENDOR_ID_DLINK) &&
1847 (pdev->subsystem_device == 0x3304))
1848 priv->rtllib->bSupportRemoteWakeUp = 1;
1850 priv->rtllib->bSupportRemoteWakeUp = 0;
1852 pmem_start = pci_resource_start(pdev, 1);
1853 pmem_len = pci_resource_len(pdev, 1);
1854 pmem_flags = pci_resource_flags(pdev, 1);
1856 if (!(pmem_flags & IORESOURCE_MEM)) {
1857 netdev_err(dev, "region #1 not a MMIO resource, aborting");
1858 goto err_rel_rtllib;
1861 dev_info(&pdev->dev, "Memory mapped space start: 0x%08lx\n",
1863 if (!request_mem_region(pmem_start, pmem_len, DRV_NAME)) {
1864 netdev_err(dev, "request_mem_region failed!");
1865 goto err_rel_rtllib;
1868 ioaddr = (unsigned long)ioremap(pmem_start, pmem_len);
1869 if (ioaddr == (unsigned long)NULL) {
1870 netdev_err(dev, "ioremap failed!");
1874 dev->mem_start = ioaddr;
1875 dev->mem_end = ioaddr + pci_resource_len(pdev, 0);
1877 if (!rtl92e_check_adapter(pdev, dev))
1880 dev->irq = pdev->irq;
1883 dev->netdev_ops = &rtl8192_netdev_ops;
1885 dev->wireless_handlers = &r8192_wx_handlers_def;
1886 dev->ethtool_ops = &rtl819x_ethtool_ops;
1888 dev->type = ARPHRD_ETHER;
1889 dev->watchdog_timeo = HZ * 3;
1891 if (dev_alloc_name(dev, ifname) < 0)
1892 dev_alloc_name(dev, ifname);
1894 if (_rtl92e_init(dev) != 0) {
1895 netdev_warn(dev, "Initialization failed");
1899 netif_carrier_off(dev);
1900 netif_stop_queue(dev);
1902 if (register_netdev(dev))
1905 if (priv->polling_timer_on == 0)
1906 rtl92e_check_rfctrl_gpio_timer(&priv->gpio_polling_timer);
1911 free_irq(dev->irq, dev);
1914 iounmap((void __iomem *)ioaddr);
1916 release_mem_region(pmem_start, pmem_len);
1920 pci_disable_device(pdev);
1924 static void _rtl92e_pci_disconnect(struct pci_dev *pdev)
1926 struct net_device *dev = pci_get_drvdata(pdev);
1927 struct r8192_priv *priv;
1931 unregister_netdev(dev);
1933 priv = rtllib_priv(dev);
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);
1947 dev_info(&pdev->dev, "Freeing irq %d\n", dev->irq);
1948 free_irq(dev->irq, dev);
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));
1961 pci_disable_device(pdev);
1964 bool rtl92e_enable_nic(struct net_device *dev)
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);
1972 netdev_warn(dev, "%s(): Driver is already down!\n", __func__);
1976 init_status = rtl92e_start_adapter(dev);
1978 netdev_warn(dev, "%s(): Initialization failed!\n", __func__);
1981 RT_CLEAR_PS_LEVEL(psc, RT_RF_OFF_LEVL_HALT_NIC);
1983 rtl92e_irq_enable(dev);
1987 module_pci_driver(rtl8192_pci_driver);
1989 void rtl92e_check_rfctrl_gpio_timer(struct timer_list *t)
1991 struct r8192_priv *priv = from_timer(priv, t, gpio_polling_timer);
1993 priv->polling_timer_on = 1;
1995 schedule_delayed_work(&priv->gpio_change_rf_wq, 0);
1997 mod_timer(&priv->gpio_polling_timer, jiffies +
1998 msecs_to_jiffies(RTLLIB_WATCH_DOG_TIME));
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);
2012 module_param(ifname, charp, 0644);
2013 module_param(hwwep, int, 0644);
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)");