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