1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #include <drv_types.h>
8 #include <rtw_debug.h>
9 #include <hal_data.h>
10 
11 MODULE_LICENSE("GPL");
12 MODULE_DESCRIPTION("Realtek Wireless Lan Driver");
13 MODULE_AUTHOR("Realtek Semiconductor Corp.");
14 MODULE_VERSION(DRIVERVERSION);
15 
16 /* module param defaults */
17 static int rtw_chip_version;
18 static int rtw_rfintfs = HWPI;
19 static int rtw_lbkmode;/* RTL8712_AIR_TRX; */
20 
21 
22 static int rtw_network_mode = Ndis802_11IBSS;/* Ndis802_11Infrastructure;infra, ad-hoc, auto */
23 /* struct ndis_802_11_ssid	ssid; */
24 static int rtw_channel = 1;/* ad-hoc support requirement */
25 static int rtw_wireless_mode = WIRELESS_11BG_24N;
26 static int rtw_vrtl_carrier_sense = AUTO_VCS;
27 static int rtw_vcs_type = RTS_CTS;/*  */
28 static int rtw_rts_thresh = 2347;/*  */
29 static int rtw_frag_thresh = 2346;/*  */
30 static int rtw_preamble = PREAMBLE_LONG;/* long, short, auto */
31 static int rtw_scan_mode = 1;/* active, passive */
32 static int rtw_adhoc_tx_pwr = 1;
33 static int rtw_soft_ap;
34 /* int smart_ps = 1; */
35 static int rtw_power_mgnt = 1;
36 static int rtw_ips_mode = IPS_NORMAL;
37 module_param(rtw_ips_mode, int, 0644);
38 MODULE_PARM_DESC(rtw_ips_mode, "The default IPS mode");
39 
40 static int rtw_smart_ps = 2;
41 
42 static int rtw_check_fw_ps = 1;
43 
44 static int rtw_usb_rxagg_mode = 2;/* USB_RX_AGG_DMA = 1, USB_RX_AGG_USB =2 */
45 module_param(rtw_usb_rxagg_mode, int, 0644);
46 
47 static int rtw_radio_enable = 1;
48 static int rtw_long_retry_lmt = 7;
49 static int rtw_short_retry_lmt = 7;
50 static int rtw_busy_thresh = 40;
51 /* int qos_enable = 0; */
52 static int rtw_ack_policy = NORMAL_ACK;
53 
54 static int rtw_software_encrypt;
55 static int rtw_software_decrypt;
56 
57 static int rtw_acm_method;/*  0:By SW 1:By HW. */
58 
59 static int rtw_wmm_enable = 1;/*  default is set to enable the wmm. */
60 static int rtw_uapsd_enable;
61 static int rtw_uapsd_max_sp = NO_LIMIT;
62 static int rtw_uapsd_acbk_en;
63 static int rtw_uapsd_acbe_en;
64 static int rtw_uapsd_acvi_en;
65 static int rtw_uapsd_acvo_en;
66 
67 int rtw_ht_enable = 1;
68 /*
69  * 0: 20 MHz, 1: 40 MHz
70  * 2.4G use bit 0 ~ 3
71  * 0x01 means enable 2.4G 40MHz
72  */
73 static int rtw_bw_mode = 0x01;
74 static int rtw_ampdu_enable = 1;/* for enable tx_ampdu ,0: disable, 0x1:enable (but wifi_spec should be 0), 0x2: force enable (don't care wifi_spec) */
75 static int rtw_rx_stbc = 1;/*  0: disable, 1:enable 2.4g */
76 static int rtw_ampdu_amsdu;/*  0: disabled, 1:enabled, 2:auto . There is an IOT issu with DLINK DIR-629 when the flag turn on */
77 /*  Short GI support Bit Map */
78 /*  BIT0 - 20MHz, 0: non-support, 1: support */
79 /*  BIT1 - 40MHz, 0: non-support, 1: support */
80 /*  BIT2 - 80MHz, 0: non-support, 1: support */
81 /*  BIT3 - 160MHz, 0: non-support, 1: support */
82 static int rtw_short_gi = 0xf;
83 /*  BIT0: Enable VHT LDPC Rx, BIT1: Enable VHT LDPC Tx, BIT4: Enable HT LDPC Rx, BIT5: Enable HT LDPC Tx */
84 static int rtw_ldpc_cap = 0x33;
85 /*  BIT0: Enable VHT STBC Rx, BIT1: Enable VHT STBC Tx, BIT4: Enable HT STBC Rx, BIT5: Enable HT STBC Tx */
86 static int rtw_stbc_cap = 0x13;
87 /*  BIT0: Enable VHT Beamformer, BIT1: Enable VHT Beamformee, BIT4: Enable HT Beamformer, BIT5: Enable HT Beamformee */
88 static int rtw_beamform_cap = 0x2;
89 
90 static int rtw_lowrate_two_xmit = 1;/* Use 2 path Tx to transmit MCS0~7 and legacy mode */
91 
92 static int rtw_low_power;
93 static int rtw_wifi_spec;
94 static int rtw_channel_plan = RT_CHANNEL_DOMAIN_MAX;
95 
96 static int rtw_ant_num = -1; /*  <0: undefined, >0: Antenna number */
97 module_param(rtw_ant_num, int, 0644);
98 MODULE_PARM_DESC(rtw_ant_num, "Antenna number setting");
99 
100 static int rtw_antdiv_cfg = 1; /*  0:OFF , 1:ON, 2:decide by Efuse config */
101 static int rtw_antdiv_type; /* 0:decide by efuse  1: for 88EE, 1Tx and 1RxCG are diversity.(2 Ant with SPDT), 2:  for 88EE, 1Tx and 2Rx are diversity.(2 Ant, Tx and RxCG are both on aux port, RxCS is on main port), 3: for 88EE, 1Tx and 1RxCG are fixed.(1Ant, Tx and RxCG are both on aux port) */
102 
103 
104 
105 static int rtw_hw_wps_pbc;
106 
107 int rtw_mc2u_disable;
108 
109 static int rtw_80211d;
110 
111 static int rtw_qos_opt_enable;/* 0: disable, 1:enable */
112 module_param(rtw_qos_opt_enable, int, 0644);
113 
114 static char *ifname = "wlan%d";
115 module_param(ifname, charp, 0644);
116 MODULE_PARM_DESC(ifname, "The default name to allocate for first interface");
117 
118 char *rtw_initmac;  /*  temp mac address if users want to use instead of the mac address in Efuse */
119 
120 module_param(rtw_initmac, charp, 0644);
121 module_param(rtw_channel_plan, int, 0644);
122 module_param(rtw_chip_version, int, 0644);
123 module_param(rtw_rfintfs, int, 0644);
124 module_param(rtw_lbkmode, int, 0644);
125 module_param(rtw_network_mode, int, 0644);
126 module_param(rtw_channel, int, 0644);
127 module_param(rtw_wmm_enable, int, 0644);
128 module_param(rtw_vrtl_carrier_sense, int, 0644);
129 module_param(rtw_vcs_type, int, 0644);
130 module_param(rtw_busy_thresh, int, 0644);
131 
132 module_param(rtw_ht_enable, int, 0644);
133 module_param(rtw_bw_mode, int, 0644);
134 module_param(rtw_ampdu_enable, int, 0644);
135 module_param(rtw_rx_stbc, int, 0644);
136 module_param(rtw_ampdu_amsdu, int, 0644);
137 
138 module_param(rtw_lowrate_two_xmit, int, 0644);
139 
140 module_param(rtw_power_mgnt, int, 0644);
141 module_param(rtw_smart_ps, int, 0644);
142 module_param(rtw_low_power, int, 0644);
143 module_param(rtw_wifi_spec, int, 0644);
144 
145 module_param(rtw_antdiv_cfg, int, 0644);
146 module_param(rtw_antdiv_type, int, 0644);
147 
148 
149 module_param(rtw_hw_wps_pbc, int, 0644);
150 
151 static uint rtw_max_roaming_times = 2;
152 module_param(rtw_max_roaming_times, uint, 0644);
153 MODULE_PARM_DESC(rtw_max_roaming_times, "The max roaming times to try");
154 
155 module_param(rtw_mc2u_disable, int, 0644);
156 
157 module_param(rtw_80211d, int, 0644);
158 MODULE_PARM_DESC(rtw_80211d, "Enable 802.11d mechanism");
159 
160 static uint rtw_notch_filter;
161 module_param(rtw_notch_filter, uint, 0644);
162 MODULE_PARM_DESC(rtw_notch_filter, "0:Disable, 1:Enable, 2:Enable only for P2P");
163 
164 #define CONFIG_RTW_HIQ_FILTER 1
165 
166 static uint rtw_hiq_filter = CONFIG_RTW_HIQ_FILTER;
167 module_param(rtw_hiq_filter, uint, 0644);
168 MODULE_PARM_DESC(rtw_hiq_filter, "0:allow all, 1:allow special, 2:deny all");
169 
170 static int rtw_tx_pwr_lmt_enable;
171 static int rtw_tx_pwr_by_rate;
172 
173 module_param(rtw_tx_pwr_lmt_enable, int, 0644);
174 MODULE_PARM_DESC(rtw_tx_pwr_lmt_enable, "0:Disable, 1:Enable, 2: Depend on efuse");
175 
176 module_param(rtw_tx_pwr_by_rate, int, 0644);
177 MODULE_PARM_DESC(rtw_tx_pwr_by_rate, "0:Disable, 1:Enable, 2: Depend on efuse");
178 
179 static int netdev_close(struct net_device *pnetdev);
180 
loadparam(struct adapter * padapter,struct net_device * pnetdev)181 static void loadparam(struct adapter *padapter, struct net_device *pnetdev)
182 {
183 	struct registry_priv  *registry_par = &padapter->registrypriv;
184 
185 	registry_par->chip_version = (u8)rtw_chip_version;
186 	registry_par->rfintfs = (u8)rtw_rfintfs;
187 	registry_par->lbkmode = (u8)rtw_lbkmode;
188 	/* registry_par->hci = (u8)hci; */
189 	registry_par->network_mode  = (u8)rtw_network_mode;
190 
191 	memcpy(registry_par->ssid.ssid, "ANY", 3);
192 	registry_par->ssid.ssid_length = 3;
193 
194 	registry_par->channel = (u8)rtw_channel;
195 	registry_par->wireless_mode = (u8)rtw_wireless_mode;
196 
197 	registry_par->vrtl_carrier_sense = (u8)rtw_vrtl_carrier_sense;
198 	registry_par->vcs_type = (u8)rtw_vcs_type;
199 	registry_par->rts_thresh = (u16)rtw_rts_thresh;
200 	registry_par->frag_thresh = (u16)rtw_frag_thresh;
201 	registry_par->preamble = (u8)rtw_preamble;
202 	registry_par->scan_mode = (u8)rtw_scan_mode;
203 	registry_par->adhoc_tx_pwr = (u8)rtw_adhoc_tx_pwr;
204 	registry_par->soft_ap =  (u8)rtw_soft_ap;
205 	registry_par->smart_ps =  (u8)rtw_smart_ps;
206 	registry_par->check_fw_ps = (u8)rtw_check_fw_ps;
207 	registry_par->power_mgnt = (u8)rtw_power_mgnt;
208 	registry_par->ips_mode = (u8)rtw_ips_mode;
209 	registry_par->radio_enable = (u8)rtw_radio_enable;
210 	registry_par->long_retry_lmt = (u8)rtw_long_retry_lmt;
211 	registry_par->short_retry_lmt = (u8)rtw_short_retry_lmt;
212 	registry_par->busy_thresh = (u16)rtw_busy_thresh;
213 	/* registry_par->qos_enable = (u8)rtw_qos_enable; */
214 	registry_par->ack_policy = (u8)rtw_ack_policy;
215 	registry_par->software_encrypt = (u8)rtw_software_encrypt;
216 	registry_par->software_decrypt = (u8)rtw_software_decrypt;
217 
218 	registry_par->acm_method = (u8)rtw_acm_method;
219 	registry_par->usb_rxagg_mode = (u8)rtw_usb_rxagg_mode;
220 
221 	 /* UAPSD */
222 	registry_par->wmm_enable = (u8)rtw_wmm_enable;
223 	registry_par->uapsd_enable = (u8)rtw_uapsd_enable;
224 	registry_par->uapsd_max_sp = (u8)rtw_uapsd_max_sp;
225 	registry_par->uapsd_acbk_en = (u8)rtw_uapsd_acbk_en;
226 	registry_par->uapsd_acbe_en = (u8)rtw_uapsd_acbe_en;
227 	registry_par->uapsd_acvi_en = (u8)rtw_uapsd_acvi_en;
228 	registry_par->uapsd_acvo_en = (u8)rtw_uapsd_acvo_en;
229 
230 	registry_par->ht_enable = (u8)rtw_ht_enable;
231 	registry_par->bw_mode = (u8)rtw_bw_mode;
232 	registry_par->ampdu_enable = (u8)rtw_ampdu_enable;
233 	registry_par->rx_stbc = (u8)rtw_rx_stbc;
234 	registry_par->ampdu_amsdu = (u8)rtw_ampdu_amsdu;
235 	registry_par->short_gi = (u8)rtw_short_gi;
236 	registry_par->ldpc_cap = (u8)rtw_ldpc_cap;
237 	registry_par->stbc_cap = (u8)rtw_stbc_cap;
238 	registry_par->beamform_cap = (u8)rtw_beamform_cap;
239 
240 	registry_par->lowrate_two_xmit = (u8)rtw_lowrate_two_xmit;
241 	registry_par->low_power = (u8)rtw_low_power;
242 
243 
244 	registry_par->wifi_spec = (u8)rtw_wifi_spec;
245 
246 	registry_par->channel_plan = (u8)rtw_channel_plan;
247 
248 	registry_par->ant_num = (s8)rtw_ant_num;
249 
250 	registry_par->accept_addba_req = true;
251 
252 	registry_par->antdiv_cfg = (u8)rtw_antdiv_cfg;
253 	registry_par->antdiv_type = (u8)rtw_antdiv_type;
254 
255 	registry_par->hw_wps_pbc = (u8)rtw_hw_wps_pbc;
256 
257 	registry_par->max_roaming_times = (u8)rtw_max_roaming_times;
258 
259 	registry_par->enable80211d = (u8)rtw_80211d;
260 
261 	snprintf(registry_par->ifname, 16, "%s", ifname);
262 
263 	registry_par->notch_filter = (u8)rtw_notch_filter;
264 
265 	registry_par->RegEnableTxPowerLimit = (u8)rtw_tx_pwr_lmt_enable;
266 	registry_par->RegEnableTxPowerByRate = (u8)rtw_tx_pwr_by_rate;
267 
268 	registry_par->RegPowerBase = 14;
269 	registry_par->TxBBSwing_2G = 0xFF;
270 	registry_par->bEn_RFE = 1;
271 	registry_par->RFE_Type = 64;
272 
273 	registry_par->qos_opt_enable = (u8)rtw_qos_opt_enable;
274 
275 	registry_par->hiq_filter = (u8)rtw_hiq_filter;
276 }
277 
rtw_net_set_mac_address(struct net_device * pnetdev,void * p)278 static int rtw_net_set_mac_address(struct net_device *pnetdev, void *p)
279 {
280 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
281 	struct sockaddr *addr = p;
282 
283 	if (!padapter->bup) {
284 		/* addr->sa_data[4], addr->sa_data[5]); */
285 		memcpy(padapter->eeprompriv.mac_addr, addr->sa_data, ETH_ALEN);
286 		/* eth_hw_addr_set(pnetdev, addr->sa_data); */
287 		/* padapter->bset_hwaddr = true; */
288 	}
289 
290 	return 0;
291 }
292 
rtw_net_get_stats(struct net_device * pnetdev)293 static struct net_device_stats *rtw_net_get_stats(struct net_device *pnetdev)
294 {
295 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
296 	struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
297 	struct recv_priv *precvpriv = &(padapter->recvpriv);
298 
299 	padapter->stats.tx_packets = pxmitpriv->tx_pkts;/* pxmitpriv->tx_pkts++; */
300 	padapter->stats.rx_packets = precvpriv->rx_pkts;/* precvpriv->rx_pkts++; */
301 	padapter->stats.tx_dropped = pxmitpriv->tx_drop;
302 	padapter->stats.rx_dropped = precvpriv->rx_drop;
303 	padapter->stats.tx_bytes = pxmitpriv->tx_bytes;
304 	padapter->stats.rx_bytes = precvpriv->rx_bytes;
305 
306 	return &padapter->stats;
307 }
308 
309 /*
310  * AC to queue mapping
311  *
312  * AC_VO -> queue 0
313  * AC_VI -> queue 1
314  * AC_BE -> queue 2
315  * AC_BK -> queue 3
316  */
317 static const u16 rtw_1d_to_queue[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
318 
319 /* Given a data frame determine the 802.1p/1d tag to use. */
rtw_classify8021d(struct sk_buff * skb)320 static unsigned int rtw_classify8021d(struct sk_buff *skb)
321 {
322 	unsigned int dscp;
323 
324 	/* skb->priority values from 256->263 are magic values to
325 	 * directly indicate a specific 802.1d priority.  This is used
326 	 * to allow 802.1d priority to be passed directly in from VLAN
327 	 * tags, etc.
328 	 */
329 	if (skb->priority >= 256 && skb->priority <= 263)
330 		return skb->priority - 256;
331 
332 	switch (skb->protocol) {
333 	case htons(ETH_P_IP):
334 		dscp = ip_hdr(skb)->tos & 0xfc;
335 		break;
336 	default:
337 		return 0;
338 	}
339 
340 	return dscp >> 5;
341 }
342 
343 
rtw_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)344 static u16 rtw_select_queue(struct net_device *dev, struct sk_buff *skb,
345 			    struct net_device *sb_dev)
346 {
347 	struct adapter	*padapter = rtw_netdev_priv(dev);
348 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
349 
350 	skb->priority = rtw_classify8021d(skb);
351 
352 	if (pmlmepriv->acm_mask != 0)
353 		skb->priority = qos_acm(pmlmepriv->acm_mask, skb->priority);
354 
355 	return rtw_1d_to_queue[skb->priority];
356 }
357 
rtw_recv_select_queue(struct sk_buff * skb)358 u16 rtw_recv_select_queue(struct sk_buff *skb)
359 {
360 	struct iphdr *piphdr;
361 	unsigned int dscp;
362 	__be16	eth_type;
363 	u32 priority;
364 	u8 *pdata = skb->data;
365 
366 	memcpy(&eth_type, pdata + (ETH_ALEN << 1), 2);
367 
368 	switch (be16_to_cpu(eth_type)) {
369 	case ETH_P_IP:
370 
371 		piphdr = (struct iphdr *)(pdata + ETH_HLEN);
372 
373 		dscp = piphdr->tos & 0xfc;
374 
375 		priority = dscp >> 5;
376 
377 		break;
378 	default:
379 		priority = 0;
380 	}
381 
382 	return rtw_1d_to_queue[priority];
383 }
384 
rtw_ndev_notifier_call(struct notifier_block * nb,unsigned long state,void * ptr)385 static int rtw_ndev_notifier_call(struct notifier_block *nb, unsigned long state, void *ptr)
386 {
387 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
388 
389 	if (dev->netdev_ops->ndo_do_ioctl != rtw_ioctl)
390 		return NOTIFY_DONE;
391 
392 	netdev_dbg(dev, FUNC_NDEV_FMT " state:%lu\n", FUNC_NDEV_ARG(dev),
393 		    state);
394 
395 	return NOTIFY_DONE;
396 }
397 
398 static struct notifier_block rtw_ndev_notifier = {
399 	.notifier_call = rtw_ndev_notifier_call,
400 };
401 
rtw_ndev_notifier_register(void)402 int rtw_ndev_notifier_register(void)
403 {
404 	return register_netdevice_notifier(&rtw_ndev_notifier);
405 }
406 
rtw_ndev_notifier_unregister(void)407 void rtw_ndev_notifier_unregister(void)
408 {
409 	unregister_netdevice_notifier(&rtw_ndev_notifier);
410 }
411 
412 
rtw_ndev_init(struct net_device * dev)413 static int rtw_ndev_init(struct net_device *dev)
414 {
415 	struct adapter *adapter = rtw_netdev_priv(dev);
416 
417 	netdev_dbg(dev, FUNC_ADPT_FMT "\n", FUNC_ADPT_ARG(adapter));
418 	strncpy(adapter->old_ifname, dev->name, IFNAMSIZ);
419 
420 	return 0;
421 }
422 
rtw_ndev_uninit(struct net_device * dev)423 static void rtw_ndev_uninit(struct net_device *dev)
424 {
425 	struct adapter *adapter = rtw_netdev_priv(dev);
426 
427 	netdev_dbg(dev, FUNC_ADPT_FMT "\n", FUNC_ADPT_ARG(adapter));
428 }
429 
430 static const struct net_device_ops rtw_netdev_ops = {
431 	.ndo_init = rtw_ndev_init,
432 	.ndo_uninit = rtw_ndev_uninit,
433 	.ndo_open = netdev_open,
434 	.ndo_stop = netdev_close,
435 	.ndo_start_xmit = rtw_xmit_entry,
436 	.ndo_select_queue	= rtw_select_queue,
437 	.ndo_set_mac_address = rtw_net_set_mac_address,
438 	.ndo_get_stats = rtw_net_get_stats,
439 	.ndo_do_ioctl = rtw_ioctl,
440 };
441 
rtw_init_netdev_name(struct net_device * pnetdev,const char * ifname)442 int rtw_init_netdev_name(struct net_device *pnetdev, const char *ifname)
443 {
444 	if (dev_alloc_name(pnetdev, ifname) < 0) {
445 		pr_err("dev_alloc_name, fail for %s\n", ifname);
446 		return 1;
447 	}
448 	netif_carrier_off(pnetdev);
449 	/* rtw_netif_stop_queue(pnetdev); */
450 
451 	return 0;
452 }
453 
rtw_init_netdev(struct adapter * old_padapter)454 struct net_device *rtw_init_netdev(struct adapter *old_padapter)
455 {
456 	struct adapter *padapter;
457 	struct net_device *pnetdev;
458 
459 	if (old_padapter)
460 		pnetdev = rtw_alloc_etherdev_with_old_priv(sizeof(struct adapter), (void *)old_padapter);
461 	else
462 		pnetdev = rtw_alloc_etherdev(sizeof(struct adapter));
463 
464 	pr_info("pnetdev = %p\n", pnetdev);
465 	if (!pnetdev)
466 		return NULL;
467 
468 	padapter = rtw_netdev_priv(pnetdev);
469 	padapter->pnetdev = pnetdev;
470 
471 	/* pnetdev->init = NULL; */
472 
473 	pnetdev->netdev_ops = &rtw_netdev_ops;
474 
475 	/* pnetdev->tx_timeout = NULL; */
476 	pnetdev->watchdog_timeo = HZ * 3; /* 3 second timeout */
477 
478 	/* step 2. */
479 	loadparam(padapter, pnetdev);
480 
481 	return pnetdev;
482 }
483 
rtw_unregister_netdevs(struct dvobj_priv * dvobj)484 void rtw_unregister_netdevs(struct dvobj_priv *dvobj)
485 {
486 	struct adapter *padapter = NULL;
487 	struct net_device *pnetdev = NULL;
488 
489 	padapter = dvobj->padapters;
490 
491 	if (!padapter)
492 		return;
493 
494 	pnetdev = padapter->pnetdev;
495 
496 	if ((padapter->DriverState != DRIVER_DISAPPEAR) && pnetdev)
497 		unregister_netdev(pnetdev); /* will call netdev_close() */
498 	rtw_wdev_unregister(padapter->rtw_wdev);
499 }
500 
rtw_start_drv_threads(struct adapter * padapter)501 u32 rtw_start_drv_threads(struct adapter *padapter)
502 {
503 	u32 _status = _SUCCESS;
504 
505 	padapter->xmitThread = kthread_run(rtw_xmit_thread, padapter, "RTW_XMIT_THREAD");
506 	if (IS_ERR(padapter->xmitThread))
507 		_status = _FAIL;
508 
509 	padapter->cmdThread = kthread_run(rtw_cmd_thread, padapter, "RTW_CMD_THREAD");
510 	if (IS_ERR(padapter->cmdThread))
511 		_status = _FAIL;
512 	else
513 		wait_for_completion(&padapter->cmdpriv.terminate_cmdthread_comp); /* wait for cmd_thread to run */
514 
515 	rtw_hal_start_thread(padapter);
516 	return _status;
517 }
518 
rtw_stop_drv_threads(struct adapter * padapter)519 void rtw_stop_drv_threads(struct adapter *padapter)
520 {
521 	rtw_stop_cmd_thread(padapter);
522 
523 	/*  Below is to termindate tx_thread... */
524 	complete(&padapter->xmitpriv.xmit_comp);
525 	wait_for_completion(&padapter->xmitpriv.terminate_xmitthread_comp);
526 
527 	rtw_hal_stop_thread(padapter);
528 }
529 
rtw_init_default_value(struct adapter * padapter)530 static void rtw_init_default_value(struct adapter *padapter)
531 {
532 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
533 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
534 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
535 	struct security_priv *psecuritypriv = &padapter->securitypriv;
536 
537 	/* xmit_priv */
538 	pxmitpriv->vcs_setting = pregistrypriv->vrtl_carrier_sense;
539 	pxmitpriv->vcs = pregistrypriv->vcs_type;
540 	pxmitpriv->vcs_type = pregistrypriv->vcs_type;
541 	/* pxmitpriv->rts_thresh = pregistrypriv->rts_thresh; */
542 	pxmitpriv->frag_len = pregistrypriv->frag_thresh;
543 
544 	/* recv_priv */
545 
546 	/* mlme_priv */
547 	pmlmepriv->scan_mode = SCAN_ACTIVE;
548 
549 	/* qos_priv */
550 	/* pmlmepriv->qospriv.qos_option = pregistrypriv->wmm_enable; */
551 
552 	/* ht_priv */
553 	pmlmepriv->htpriv.ampdu_enable = false;/* set to disabled */
554 
555 	/* security_priv */
556 	/* rtw_get_encrypt_decrypt_from_registrypriv(padapter); */
557 	psecuritypriv->binstallGrpkey = _FAIL;
558 	psecuritypriv->sw_encrypt = pregistrypriv->software_encrypt;
559 	psecuritypriv->sw_decrypt = pregistrypriv->software_decrypt;
560 
561 	psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
562 	psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_;
563 
564 	psecuritypriv->dot11PrivacyKeyIndex = 0;
565 
566 	psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
567 	psecuritypriv->dot118021XGrpKeyid = 1;
568 
569 	psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
570 	psecuritypriv->ndisencryptstatus = Ndis802_11WEPDisabled;
571 
572 	/* registry_priv */
573 	rtw_init_registrypriv_dev_network(padapter);
574 	rtw_update_registrypriv_dev_network(padapter);
575 
576 	/* hal_priv */
577 	rtw_hal_def_value_init(padapter);
578 
579 	/* misc. */
580 	RTW_ENABLE_FUNC(padapter, DF_RX_BIT);
581 	RTW_ENABLE_FUNC(padapter, DF_TX_BIT);
582 	padapter->bLinkInfoDump = 0;
583 	padapter->bNotifyChannelChange = 0;
584 
585 	/* for debug purpose */
586 	padapter->fix_rate = 0xFF;
587 	padapter->driver_ampdu_spacing = 0xFF;
588 	padapter->driver_rx_ampdu_factor =  0xFF;
589 
590 }
591 
devobj_init(void)592 struct dvobj_priv *devobj_init(void)
593 {
594 	struct dvobj_priv *pdvobj = NULL;
595 
596 	pdvobj = rtw_zmalloc(sizeof(*pdvobj));
597 	if (!pdvobj)
598 		return NULL;
599 
600 	mutex_init(&pdvobj->hw_init_mutex);
601 	mutex_init(&pdvobj->h2c_fwcmd_mutex);
602 	mutex_init(&pdvobj->setch_mutex);
603 	mutex_init(&pdvobj->setbw_mutex);
604 
605 	spin_lock_init(&pdvobj->lock);
606 
607 	pdvobj->macid[1] = true; /* macid = 1 for bc/mc stainfo */
608 
609 	pdvobj->processing_dev_remove = false;
610 
611 	atomic_set(&pdvobj->disable_func, 0);
612 
613 	spin_lock_init(&pdvobj->cam_ctl.lock);
614 
615 	return pdvobj;
616 }
617 
devobj_deinit(struct dvobj_priv * pdvobj)618 void devobj_deinit(struct dvobj_priv *pdvobj)
619 {
620 	if (!pdvobj)
621 		return;
622 
623 	mutex_destroy(&pdvobj->hw_init_mutex);
624 	mutex_destroy(&pdvobj->h2c_fwcmd_mutex);
625 	mutex_destroy(&pdvobj->setch_mutex);
626 	mutex_destroy(&pdvobj->setbw_mutex);
627 
628 	kfree(pdvobj);
629 }
630 
rtw_reset_drv_sw(struct adapter * padapter)631 void rtw_reset_drv_sw(struct adapter *padapter)
632 {
633 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
634 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
635 
636 	/* hal_priv */
637 	if (is_primary_adapter(padapter))
638 		rtw_hal_def_value_init(padapter);
639 
640 	RTW_ENABLE_FUNC(padapter, DF_RX_BIT);
641 	RTW_ENABLE_FUNC(padapter, DF_TX_BIT);
642 	padapter->bLinkInfoDump = 0;
643 
644 	padapter->xmitpriv.tx_pkts = 0;
645 	padapter->recvpriv.rx_pkts = 0;
646 
647 	pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
648 
649 	/* pmlmepriv->LinkDetectInfo.TrafficBusyState = false; */
650 	pmlmepriv->LinkDetectInfo.TrafficTransitionCount = 0;
651 	pmlmepriv->LinkDetectInfo.LowPowerTransitionCount = 0;
652 
653 	_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY | _FW_UNDER_LINKING);
654 
655 	pwrctrlpriv->pwr_state_check_cnts = 0;
656 
657 	/* mlmeextpriv */
658 	padapter->mlmeextpriv.sitesurvey_res.state = SCAN_DISABLE;
659 
660 	rtw_set_signal_stat_timer(&padapter->recvpriv);
661 
662 }
663 
664 
rtw_init_drv_sw(struct adapter * padapter)665 u8 rtw_init_drv_sw(struct adapter *padapter)
666 {
667 	u8 ret8 = _SUCCESS;
668 
669 	rtw_init_default_value(padapter);
670 
671 	rtw_init_hal_com_default_value(padapter);
672 
673 	if (rtw_init_cmd_priv(&padapter->cmdpriv)) {
674 		ret8 = _FAIL;
675 		goto exit;
676 	}
677 
678 	padapter->cmdpriv.padapter = padapter;
679 
680 	if (rtw_init_evt_priv(&padapter->evtpriv)) {
681 		ret8 = _FAIL;
682 		goto exit;
683 	}
684 
685 
686 	if (rtw_init_mlme_priv(padapter) == _FAIL) {
687 		ret8 = _FAIL;
688 		goto exit;
689 	}
690 
691 	init_mlme_ext_priv(padapter);
692 
693 	if (_rtw_init_xmit_priv(&padapter->xmitpriv, padapter) == _FAIL) {
694 		ret8 = _FAIL;
695 		goto exit;
696 	}
697 
698 	if (_rtw_init_recv_priv(&padapter->recvpriv, padapter) == _FAIL) {
699 		ret8 = _FAIL;
700 		goto exit;
701 	}
702 	/*  add for CONFIG_IEEE80211W, none 11w also can use */
703 	spin_lock_init(&padapter->security_key_mutex);
704 
705 	/*  We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
706 	/* memset((unsigned char *)&padapter->securitypriv, 0, sizeof (struct security_priv)); */
707 
708 	if (_rtw_init_sta_priv(&padapter->stapriv) == _FAIL) {
709 		ret8 = _FAIL;
710 		goto exit;
711 	}
712 
713 	padapter->stapriv.padapter = padapter;
714 	padapter->setband = GHZ24_50;
715 	padapter->fix_rate = 0xFF;
716 	rtw_init_bcmc_stainfo(padapter);
717 
718 	rtw_init_pwrctrl_priv(padapter);
719 
720 	rtw_hal_dm_init(padapter);
721 
722 exit:
723 
724 	return ret8;
725 }
726 
rtw_cancel_all_timer(struct adapter * padapter)727 void rtw_cancel_all_timer(struct adapter *padapter)
728 {
729 	del_timer_sync(&padapter->mlmepriv.assoc_timer);
730 
731 	del_timer_sync(&padapter->mlmepriv.scan_to_timer);
732 
733 	del_timer_sync(&padapter->mlmepriv.dynamic_chk_timer);
734 
735 	del_timer_sync(&(adapter_to_pwrctl(padapter)->pwr_state_check_timer));
736 
737 	del_timer_sync(&padapter->mlmepriv.set_scan_deny_timer);
738 	rtw_clear_scan_deny(padapter);
739 
740 	del_timer_sync(&padapter->recvpriv.signal_stat_timer);
741 
742 	/* cancel dm timer */
743 	rtw_hal_dm_deinit(padapter);
744 }
745 
rtw_free_drv_sw(struct adapter * padapter)746 u8 rtw_free_drv_sw(struct adapter *padapter)
747 {
748 	free_mlme_ext_priv(&padapter->mlmeextpriv);
749 
750 	rtw_free_cmd_priv(&padapter->cmdpriv);
751 
752 	rtw_free_evt_priv(&padapter->evtpriv);
753 
754 	rtw_free_mlme_priv(&padapter->mlmepriv);
755 
756 	/* free_io_queue(padapter); */
757 
758 	_rtw_free_xmit_priv(&padapter->xmitpriv);
759 
760 	_rtw_free_sta_priv(&padapter->stapriv); /* will free bcmc_stainfo here */
761 
762 	_rtw_free_recv_priv(&padapter->recvpriv);
763 
764 	rtw_free_pwrctrl_priv(padapter);
765 
766 	/* kfree((void *)padapter); */
767 
768 	rtw_hal_free_data(padapter);
769 
770 	/* free the old_pnetdev */
771 	if (padapter->rereg_nd_name_priv.old_pnetdev) {
772 		free_netdev(padapter->rereg_nd_name_priv.old_pnetdev);
773 		padapter->rereg_nd_name_priv.old_pnetdev = NULL;
774 	}
775 
776 	/*  clear pbuddystruct adapter to avoid access wrong pointer. */
777 	if (padapter->pbuddy_adapter)
778 		padapter->pbuddy_adapter->pbuddy_adapter = NULL;
779 
780 	return _SUCCESS;
781 }
782 
_rtw_drv_register_netdev(struct adapter * padapter,char * name)783 static int _rtw_drv_register_netdev(struct adapter *padapter, char *name)
784 {
785 	int ret = _SUCCESS;
786 	struct net_device *pnetdev = padapter->pnetdev;
787 
788 	/* alloc netdev name */
789 	if (rtw_init_netdev_name(pnetdev, name))
790 		return _FAIL;
791 
792 	eth_hw_addr_set(pnetdev, padapter->eeprompriv.mac_addr);
793 
794 	/* Tell the network stack we exist */
795 	if (register_netdev(pnetdev) != 0) {
796 		ret = _FAIL;
797 		goto error_register_netdev;
798 	}
799 
800 	return ret;
801 
802 error_register_netdev:
803 
804 	rtw_free_drv_sw(padapter);
805 
806 	rtw_free_netdev(pnetdev);
807 
808 	return ret;
809 }
810 
rtw_drv_register_netdev(struct adapter * if1)811 int rtw_drv_register_netdev(struct adapter *if1)
812 {
813 	struct dvobj_priv *dvobj = if1->dvobj;
814 	struct adapter *padapter = dvobj->padapters;
815 	char *name = if1->registrypriv.ifname;
816 
817 	return _rtw_drv_register_netdev(padapter, name);
818 }
819 
_netdev_open(struct net_device * pnetdev)820 static int _netdev_open(struct net_device *pnetdev)
821 {
822 	uint status;
823 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
824 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
825 
826 	padapter->netif_up = true;
827 
828 	if (pwrctrlpriv->ps_flag) {
829 		padapter->net_closed = false;
830 		goto netdev_open_normal_process;
831 	}
832 
833 	if (!padapter->bup) {
834 		padapter->bDriverStopped = false;
835 		padapter->bSurpriseRemoved = false;
836 		padapter->bCardDisableWOHSM = false;
837 
838 		status = rtw_hal_init(padapter);
839 		if (status == _FAIL)
840 			goto netdev_open_error;
841 
842 		status = rtw_start_drv_threads(padapter);
843 		if (status == _FAIL)
844 			goto netdev_open_error;
845 
846 		if (padapter->intf_start)
847 			padapter->intf_start(padapter);
848 
849 		rtw_cfg80211_init_wiphy(padapter);
850 
851 		padapter->bup = true;
852 		pwrctrlpriv->bips_processing = false;
853 	}
854 	padapter->net_closed = false;
855 
856 	_set_timer(&padapter->mlmepriv.dynamic_chk_timer, 2000);
857 
858 	if (!rtw_netif_queue_stopped(pnetdev))
859 		rtw_netif_start_queue(pnetdev);
860 	else
861 		rtw_netif_wake_queue(pnetdev);
862 
863 netdev_open_normal_process:
864 
865 	return 0;
866 
867 netdev_open_error:
868 
869 	padapter->bup = false;
870 
871 	netif_carrier_off(pnetdev);
872 	rtw_netif_stop_queue(pnetdev);
873 
874 	return (-1);
875 }
876 
netdev_open(struct net_device * pnetdev)877 int netdev_open(struct net_device *pnetdev)
878 {
879 	int ret;
880 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
881 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
882 
883 	if (pwrctrlpriv->bInSuspend)
884 		return 0;
885 
886 	if (mutex_lock_interruptible(&(adapter_to_dvobj(padapter)->hw_init_mutex)))
887 		return -1;
888 
889 	ret = _netdev_open(pnetdev);
890 	mutex_unlock(&(adapter_to_dvobj(padapter)->hw_init_mutex));
891 
892 	return ret;
893 }
894 
ips_netdrv_open(struct adapter * padapter)895 static int  ips_netdrv_open(struct adapter *padapter)
896 {
897 	int status = _SUCCESS;
898 	/* struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); */
899 
900 	padapter->net_closed = false;
901 
902 	padapter->bDriverStopped = false;
903 	padapter->bCardDisableWOHSM = false;
904 	/* padapter->bup = true; */
905 
906 	status = rtw_hal_init(padapter);
907 	if (status == _FAIL)
908 		goto netdev_open_error;
909 
910 	if (padapter->intf_start)
911 		padapter->intf_start(padapter);
912 
913 	_set_timer(&padapter->mlmepriv.dynamic_chk_timer, 2000);
914 
915 	return _SUCCESS;
916 
917 netdev_open_error:
918 
919 	return _FAIL;
920 }
921 
922 
rtw_ips_pwr_up(struct adapter * padapter)923 int rtw_ips_pwr_up(struct adapter *padapter)
924 {
925 	return ips_netdrv_open(padapter);
926 }
927 
rtw_ips_pwr_down(struct adapter * padapter)928 void rtw_ips_pwr_down(struct adapter *padapter)
929 {
930 	padapter->bCardDisableWOHSM = true;
931 	padapter->net_closed = true;
932 
933 	rtw_ips_dev_unload(padapter);
934 	padapter->bCardDisableWOHSM = false;
935 }
936 
rtw_ips_dev_unload(struct adapter * padapter)937 void rtw_ips_dev_unload(struct adapter *padapter)
938 {
939 
940 	if (!padapter->bSurpriseRemoved)
941 		rtw_hal_deinit(padapter);
942 }
943 
pm_netdev_open(struct net_device * pnetdev,u8 bnormal)944 static int pm_netdev_open(struct net_device *pnetdev, u8 bnormal)
945 {
946 	int status = -1;
947 
948 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
949 
950 	if (bnormal) {
951 		if (mutex_lock_interruptible(&(adapter_to_dvobj(padapter)->hw_init_mutex)) == 0) {
952 			status = _netdev_open(pnetdev);
953 			mutex_unlock(&(adapter_to_dvobj(padapter)->hw_init_mutex));
954 		}
955 	} else {
956 		status =  (_SUCCESS == ips_netdrv_open(padapter)) ? (0) : (-1);
957 	}
958 
959 	return status;
960 }
961 
netdev_close(struct net_device * pnetdev)962 static int netdev_close(struct net_device *pnetdev)
963 {
964 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
965 	struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter);
966 
967 	if (pwrctl->bInternalAutoSuspend) {
968 		/* rtw_pwr_wakeup(padapter); */
969 		if (pwrctl->rf_pwrstate == rf_off)
970 			pwrctl->ps_flag = true;
971 	}
972 	padapter->net_closed = true;
973 	padapter->netif_up = false;
974 
975 /*if (!padapter->hw_init_completed)
976 	{
977 
978 		padapter->bDriverStopped = true;
979 
980 		rtw_dev_unload(padapter);
981 	}
982 	else*/
983 	if (pwrctl->rf_pwrstate == rf_on) {
984 		/* s1. */
985 		if (pnetdev) {
986 			if (!rtw_netif_queue_stopped(pnetdev))
987 				rtw_netif_stop_queue(pnetdev);
988 		}
989 
990 		/* s2. */
991 		LeaveAllPowerSaveMode(padapter);
992 		rtw_disassoc_cmd(padapter, 500, false);
993 		/* s2-2.  indicate disconnect to os */
994 		rtw_indicate_disconnect(padapter);
995 		/* s2-3. */
996 		rtw_free_assoc_resources(padapter, 1);
997 		/* s2-4. */
998 		rtw_free_network_queue(padapter, true);
999 	}
1000 
1001 	rtw_scan_abort(padapter);
1002 	adapter_wdev_data(padapter)->bandroid_scan = false;
1003 
1004 	return 0;
1005 }
1006 
rtw_ndev_destructor(struct net_device * ndev)1007 void rtw_ndev_destructor(struct net_device *ndev)
1008 {
1009 	kfree(ndev->ieee80211_ptr);
1010 }
1011 
rtw_dev_unload(struct adapter * padapter)1012 void rtw_dev_unload(struct adapter *padapter)
1013 {
1014 	struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter);
1015 	struct dvobj_priv *pobjpriv = padapter->dvobj;
1016 	struct debug_priv *pdbgpriv = &pobjpriv->drv_dbg;
1017 	struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
1018 	u8 cnt = 0;
1019 
1020 	if (padapter->bup) {
1021 
1022 		padapter->bDriverStopped = true;
1023 		if (padapter->xmitpriv.ack_tx)
1024 			rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_DRV_STOP);
1025 
1026 		if (padapter->intf_stop)
1027 			padapter->intf_stop(padapter);
1028 
1029 		if (!pwrctl->bInternalAutoSuspend)
1030 			rtw_stop_drv_threads(padapter);
1031 
1032 		while (atomic_read(&pcmdpriv->cmdthd_running)) {
1033 			if (cnt > 5) {
1034 				break;
1035 			} else {
1036 				cnt++;
1037 				msleep(10);
1038 			}
1039 		}
1040 
1041 		/* check the status of IPS */
1042 		if (rtw_hal_check_ips_status(padapter) || pwrctl->rf_pwrstate == rf_off) {
1043 			/* check HW status and SW state */
1044 			netdev_dbg(padapter->pnetdev,
1045 				   "%s: driver in IPS-FWLPS\n", __func__);
1046 			pdbgpriv->dbg_dev_unload_inIPS_cnt++;
1047 			LeaveAllPowerSaveMode(padapter);
1048 		} else {
1049 			netdev_dbg(padapter->pnetdev,
1050 				   "%s: driver not in IPS\n", __func__);
1051 		}
1052 
1053 		if (!padapter->bSurpriseRemoved) {
1054 			hal_btcoex_IpsNotify(padapter, pwrctl->ips_mode_req);
1055 
1056 			/* amy modify 20120221 for power seq is different between driver open and ips */
1057 			rtw_hal_deinit(padapter);
1058 
1059 			padapter->bSurpriseRemoved = true;
1060 		}
1061 
1062 		padapter->bup = false;
1063 
1064 	}
1065 }
1066 
rtw_suspend_free_assoc_resource(struct adapter * padapter)1067 static int rtw_suspend_free_assoc_resource(struct adapter *padapter)
1068 {
1069 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1070 
1071 	if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME)) {
1072 		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)
1073 			&& check_fwstate(pmlmepriv, _FW_LINKED)) {
1074 			rtw_set_to_roam(padapter, 1);
1075 		}
1076 	}
1077 
1078 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) && check_fwstate(pmlmepriv, _FW_LINKED)) {
1079 		rtw_disassoc_cmd(padapter, 0, false);
1080 		/* s2-2.  indicate disconnect to os */
1081 		rtw_indicate_disconnect(padapter);
1082 	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1083 		rtw_sta_flush(padapter);
1084 	}
1085 
1086 	/* s2-3. */
1087 	rtw_free_assoc_resources(padapter, 1);
1088 
1089 	/* s2-4. */
1090 	rtw_free_network_queue(padapter, true);
1091 
1092 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY))
1093 		rtw_indicate_scan_done(padapter, 1);
1094 
1095 	if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
1096 		netdev_dbg(padapter->pnetdev, "%s: fw_under_linking\n",
1097 			   __func__);
1098 		rtw_indicate_disconnect(padapter);
1099 	}
1100 
1101 	return _SUCCESS;
1102 }
1103 
rtw_suspend_normal(struct adapter * padapter)1104 static void rtw_suspend_normal(struct adapter *padapter)
1105 {
1106 	struct net_device *pnetdev = padapter->pnetdev;
1107 
1108 	if (pnetdev) {
1109 		netif_carrier_off(pnetdev);
1110 		rtw_netif_stop_queue(pnetdev);
1111 	}
1112 
1113 	rtw_suspend_free_assoc_resource(padapter);
1114 
1115 	if ((rtw_hal_check_ips_status(padapter)) || (adapter_to_pwrctl(padapter)->rf_pwrstate == rf_off))
1116 		netdev_dbg(padapter->pnetdev,
1117 			   "%s: ### ERROR #### driver in IPS ####ERROR###!!!\n",
1118 			   __func__);
1119 
1120 	rtw_dev_unload(padapter);
1121 
1122 	/* sdio_deinit(adapter_to_dvobj(padapter)); */
1123 	if (padapter->intf_deinit)
1124 		padapter->intf_deinit(adapter_to_dvobj(padapter));
1125 }
1126 
rtw_suspend_common(struct adapter * padapter)1127 void rtw_suspend_common(struct adapter *padapter)
1128 {
1129 	struct dvobj_priv *psdpriv = padapter->dvobj;
1130 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1131 	struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(psdpriv);
1132 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1133 
1134 	unsigned long start_time = jiffies;
1135 
1136 	netdev_dbg(padapter->pnetdev, " suspend start\n");
1137 	pdbgpriv->dbg_suspend_cnt++;
1138 
1139 	pwrpriv->bInSuspend = true;
1140 
1141 	while (pwrpriv->bips_processing)
1142 		msleep(1);
1143 
1144 	if ((!padapter->bup) || (padapter->bDriverStopped) || (padapter->bSurpriseRemoved)) {
1145 		pdbgpriv->dbg_suspend_error_cnt++;
1146 		goto exit;
1147 	}
1148 	rtw_ps_deny(padapter, PS_DENY_SUSPEND);
1149 
1150 	rtw_cancel_all_timer(padapter);
1151 
1152 	LeaveAllPowerSaveModeDirect(padapter);
1153 
1154 	rtw_stop_cmd_thread(padapter);
1155 
1156 	/*  wait for the latest FW to remove this condition. */
1157 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
1158 		hal_btcoex_SuspendNotify(padapter, 0);
1159 	else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
1160 		hal_btcoex_SuspendNotify(padapter, 1);
1161 
1162 	rtw_ps_deny_cancel(padapter, PS_DENY_SUSPEND);
1163 
1164 	rtw_suspend_normal(padapter);
1165 
1166 	netdev_dbg(padapter->pnetdev, "rtw suspend success in %d ms\n",
1167 		   jiffies_to_msecs(jiffies - start_time));
1168 
1169 exit:
1170 
1171 	return;
1172 }
1173 
rtw_resume_process_normal(struct adapter * padapter)1174 static int rtw_resume_process_normal(struct adapter *padapter)
1175 {
1176 	struct net_device *pnetdev;
1177 	struct pwrctrl_priv *pwrpriv;
1178 	struct mlme_priv *pmlmepriv;
1179 	struct dvobj_priv *psdpriv;
1180 	struct debug_priv *pdbgpriv;
1181 
1182 	int ret = _SUCCESS;
1183 
1184 	if (!padapter) {
1185 		ret = -1;
1186 		goto exit;
1187 	}
1188 
1189 	pnetdev = padapter->pnetdev;
1190 	pwrpriv = adapter_to_pwrctl(padapter);
1191 	pmlmepriv = &padapter->mlmepriv;
1192 	psdpriv = padapter->dvobj;
1193 	pdbgpriv = &psdpriv->drv_dbg;
1194 	/*  interface init */
1195 	/* if (sdio_init(adapter_to_dvobj(padapter)) != _SUCCESS) */
1196 	if ((padapter->intf_init) && (padapter->intf_init(adapter_to_dvobj(padapter)) != _SUCCESS)) {
1197 		ret = -1;
1198 		goto exit;
1199 	}
1200 	rtw_hal_disable_interrupt(padapter);
1201 	/* if (sdio_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS) */
1202 	if ((padapter->intf_alloc_irq) && (padapter->intf_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS)) {
1203 		ret = -1;
1204 		goto exit;
1205 	}
1206 
1207 	rtw_reset_drv_sw(padapter);
1208 	pwrpriv->bkeepfwalive = false;
1209 
1210 	if (pm_netdev_open(pnetdev, true) != 0) {
1211 		ret = -1;
1212 		pdbgpriv->dbg_resume_error_cnt++;
1213 		goto exit;
1214 	}
1215 
1216 	netif_device_attach(pnetdev);
1217 	netif_carrier_on(pnetdev);
1218 
1219 	if (padapter->pid[1] != 0)
1220 		rtw_signal_process(padapter->pid[1], SIGUSR2);
1221 
1222 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1223 		if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME))
1224 			rtw_roaming(padapter, NULL);
1225 	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1226 		rtw_ap_restore_network(padapter);
1227 	}
1228 
1229 exit:
1230 	return ret;
1231 }
1232 
rtw_resume_common(struct adapter * padapter)1233 int rtw_resume_common(struct adapter *padapter)
1234 {
1235 	int ret = 0;
1236 	unsigned long start_time = jiffies;
1237 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
1238 
1239 	netdev_dbg(padapter->pnetdev, "resume start\n");
1240 
1241 	rtw_resume_process_normal(padapter);
1242 
1243 	hal_btcoex_SuspendNotify(padapter, 0);
1244 
1245 	if (pwrpriv) {
1246 		pwrpriv->bInSuspend = false;
1247 	}
1248 	netdev_dbg(padapter->pnetdev, "%s:%d in %d ms\n", __func__, ret,
1249 		   jiffies_to_msecs(jiffies - start_time));
1250 
1251 	return ret;
1252 }
1253