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