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