• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	rtw_init_default_value(padapter);
668 
669 	rtw_init_hal_com_default_value(padapter);
670 
671 	if (rtw_init_cmd_priv(&padapter->cmdpriv))
672 		return _FAIL;
673 
674 	padapter->cmdpriv.padapter = padapter;
675 
676 	if (rtw_init_evt_priv(&padapter->evtpriv))
677 		goto free_cmd_priv;
678 
679 	if (rtw_init_mlme_priv(padapter) == _FAIL)
680 		goto free_evt_priv;
681 
682 	init_mlme_ext_priv(padapter);
683 
684 	if (_rtw_init_xmit_priv(&padapter->xmitpriv, padapter) == _FAIL)
685 		goto free_mlme_ext;
686 
687 	if (_rtw_init_recv_priv(&padapter->recvpriv, padapter) == _FAIL)
688 		goto free_xmit_priv;
689 	/*  add for CONFIG_IEEE80211W, none 11w also can use */
690 	spin_lock_init(&padapter->security_key_mutex);
691 
692 	/*  We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
693 	/* memset((unsigned char *)&padapter->securitypriv, 0, sizeof (struct security_priv)); */
694 
695 	if (_rtw_init_sta_priv(&padapter->stapriv) == _FAIL)
696 		goto free_recv_priv;
697 
698 	padapter->stapriv.padapter = padapter;
699 	padapter->setband = GHZ24_50;
700 	padapter->fix_rate = 0xFF;
701 	rtw_init_bcmc_stainfo(padapter);
702 
703 	rtw_init_pwrctrl_priv(padapter);
704 
705 	rtw_hal_dm_init(padapter);
706 
707 	return _SUCCESS;
708 
709 free_recv_priv:
710 	_rtw_free_recv_priv(&padapter->recvpriv);
711 
712 free_xmit_priv:
713 	_rtw_free_xmit_priv(&padapter->xmitpriv);
714 
715 free_mlme_ext:
716 	free_mlme_ext_priv(&padapter->mlmeextpriv);
717 
718 	rtw_free_mlme_priv(&padapter->mlmepriv);
719 
720 free_evt_priv:
721 	rtw_free_evt_priv(&padapter->evtpriv);
722 
723 free_cmd_priv:
724 	rtw_free_cmd_priv(&padapter->cmdpriv);
725 
726 	return _FAIL;
727 }
728 
rtw_cancel_all_timer(struct adapter * padapter)729 void rtw_cancel_all_timer(struct adapter *padapter)
730 {
731 	del_timer_sync(&padapter->mlmepriv.assoc_timer);
732 
733 	del_timer_sync(&padapter->mlmepriv.scan_to_timer);
734 
735 	del_timer_sync(&padapter->mlmepriv.dynamic_chk_timer);
736 
737 	del_timer_sync(&(adapter_to_pwrctl(padapter)->pwr_state_check_timer));
738 
739 	del_timer_sync(&padapter->mlmepriv.set_scan_deny_timer);
740 	rtw_clear_scan_deny(padapter);
741 
742 	del_timer_sync(&padapter->recvpriv.signal_stat_timer);
743 
744 	/* cancel dm timer */
745 	rtw_hal_dm_deinit(padapter);
746 }
747 
rtw_free_drv_sw(struct adapter * padapter)748 u8 rtw_free_drv_sw(struct adapter *padapter)
749 {
750 	free_mlme_ext_priv(&padapter->mlmeextpriv);
751 
752 	rtw_free_cmd_priv(&padapter->cmdpriv);
753 
754 	rtw_free_evt_priv(&padapter->evtpriv);
755 
756 	rtw_free_mlme_priv(&padapter->mlmepriv);
757 
758 	/* free_io_queue(padapter); */
759 
760 	_rtw_free_xmit_priv(&padapter->xmitpriv);
761 
762 	_rtw_free_sta_priv(&padapter->stapriv); /* will free bcmc_stainfo here */
763 
764 	_rtw_free_recv_priv(&padapter->recvpriv);
765 
766 	rtw_free_pwrctrl_priv(padapter);
767 
768 	/* kfree((void *)padapter); */
769 
770 	rtw_hal_free_data(padapter);
771 
772 	/* free the old_pnetdev */
773 	if (padapter->rereg_nd_name_priv.old_pnetdev) {
774 		free_netdev(padapter->rereg_nd_name_priv.old_pnetdev);
775 		padapter->rereg_nd_name_priv.old_pnetdev = NULL;
776 	}
777 
778 	/*  clear pbuddystruct adapter to avoid access wrong pointer. */
779 	if (padapter->pbuddy_adapter)
780 		padapter->pbuddy_adapter->pbuddy_adapter = NULL;
781 
782 	return _SUCCESS;
783 }
784 
_rtw_drv_register_netdev(struct adapter * padapter,char * name)785 static int _rtw_drv_register_netdev(struct adapter *padapter, char *name)
786 {
787 	int ret = _SUCCESS;
788 	struct net_device *pnetdev = padapter->pnetdev;
789 
790 	/* alloc netdev name */
791 	if (rtw_init_netdev_name(pnetdev, name))
792 		return _FAIL;
793 
794 	eth_hw_addr_set(pnetdev, padapter->eeprompriv.mac_addr);
795 
796 	/* Tell the network stack we exist */
797 	if (register_netdev(pnetdev) != 0) {
798 		ret = _FAIL;
799 		goto error_register_netdev;
800 	}
801 
802 	return ret;
803 
804 error_register_netdev:
805 
806 	rtw_free_drv_sw(padapter);
807 
808 	rtw_free_netdev(pnetdev);
809 
810 	return ret;
811 }
812 
rtw_drv_register_netdev(struct adapter * if1)813 int rtw_drv_register_netdev(struct adapter *if1)
814 {
815 	struct dvobj_priv *dvobj = if1->dvobj;
816 	struct adapter *padapter = dvobj->padapters;
817 	char *name = if1->registrypriv.ifname;
818 
819 	return _rtw_drv_register_netdev(padapter, name);
820 }
821 
_netdev_open(struct net_device * pnetdev)822 static int _netdev_open(struct net_device *pnetdev)
823 {
824 	uint status;
825 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
826 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
827 
828 	padapter->netif_up = true;
829 
830 	if (pwrctrlpriv->ps_flag) {
831 		padapter->net_closed = false;
832 		goto netdev_open_normal_process;
833 	}
834 
835 	if (!padapter->bup) {
836 		padapter->bDriverStopped = false;
837 		padapter->bSurpriseRemoved = false;
838 		padapter->bCardDisableWOHSM = false;
839 
840 		status = rtw_hal_init(padapter);
841 		if (status == _FAIL)
842 			goto netdev_open_error;
843 
844 		status = rtw_start_drv_threads(padapter);
845 		if (status == _FAIL)
846 			goto netdev_open_error;
847 
848 		if (padapter->intf_start)
849 			padapter->intf_start(padapter);
850 
851 		rtw_cfg80211_init_wiphy(padapter);
852 
853 		padapter->bup = true;
854 		pwrctrlpriv->bips_processing = false;
855 	}
856 	padapter->net_closed = false;
857 
858 	_set_timer(&padapter->mlmepriv.dynamic_chk_timer, 2000);
859 
860 	if (!rtw_netif_queue_stopped(pnetdev))
861 		rtw_netif_start_queue(pnetdev);
862 	else
863 		rtw_netif_wake_queue(pnetdev);
864 
865 netdev_open_normal_process:
866 
867 	return 0;
868 
869 netdev_open_error:
870 
871 	padapter->bup = false;
872 
873 	netif_carrier_off(pnetdev);
874 	rtw_netif_stop_queue(pnetdev);
875 
876 	return (-1);
877 }
878 
netdev_open(struct net_device * pnetdev)879 int netdev_open(struct net_device *pnetdev)
880 {
881 	int ret;
882 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
883 	struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter);
884 
885 	if (pwrctrlpriv->bInSuspend)
886 		return 0;
887 
888 	if (mutex_lock_interruptible(&(adapter_to_dvobj(padapter)->hw_init_mutex)))
889 		return -1;
890 
891 	ret = _netdev_open(pnetdev);
892 	mutex_unlock(&(adapter_to_dvobj(padapter)->hw_init_mutex));
893 
894 	return ret;
895 }
896 
ips_netdrv_open(struct adapter * padapter)897 static int  ips_netdrv_open(struct adapter *padapter)
898 {
899 	int status = _SUCCESS;
900 	/* struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); */
901 
902 	padapter->net_closed = false;
903 
904 	padapter->bDriverStopped = false;
905 	padapter->bCardDisableWOHSM = false;
906 	/* padapter->bup = true; */
907 
908 	status = rtw_hal_init(padapter);
909 	if (status == _FAIL)
910 		goto netdev_open_error;
911 
912 	if (padapter->intf_start)
913 		padapter->intf_start(padapter);
914 
915 	_set_timer(&padapter->mlmepriv.dynamic_chk_timer, 2000);
916 
917 	return _SUCCESS;
918 
919 netdev_open_error:
920 
921 	return _FAIL;
922 }
923 
924 
rtw_ips_pwr_up(struct adapter * padapter)925 int rtw_ips_pwr_up(struct adapter *padapter)
926 {
927 	return ips_netdrv_open(padapter);
928 }
929 
rtw_ips_pwr_down(struct adapter * padapter)930 void rtw_ips_pwr_down(struct adapter *padapter)
931 {
932 	padapter->bCardDisableWOHSM = true;
933 	padapter->net_closed = true;
934 
935 	rtw_ips_dev_unload(padapter);
936 	padapter->bCardDisableWOHSM = false;
937 }
938 
rtw_ips_dev_unload(struct adapter * padapter)939 void rtw_ips_dev_unload(struct adapter *padapter)
940 {
941 
942 	if (!padapter->bSurpriseRemoved)
943 		rtw_hal_deinit(padapter);
944 }
945 
pm_netdev_open(struct net_device * pnetdev,u8 bnormal)946 static int pm_netdev_open(struct net_device *pnetdev, u8 bnormal)
947 {
948 	int status = -1;
949 
950 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
951 
952 	if (bnormal) {
953 		if (mutex_lock_interruptible(&(adapter_to_dvobj(padapter)->hw_init_mutex)) == 0) {
954 			status = _netdev_open(pnetdev);
955 			mutex_unlock(&(adapter_to_dvobj(padapter)->hw_init_mutex));
956 		}
957 	} else {
958 		status =  (_SUCCESS == ips_netdrv_open(padapter)) ? (0) : (-1);
959 	}
960 
961 	return status;
962 }
963 
netdev_close(struct net_device * pnetdev)964 static int netdev_close(struct net_device *pnetdev)
965 {
966 	struct adapter *padapter = rtw_netdev_priv(pnetdev);
967 	struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter);
968 
969 	if (pwrctl->bInternalAutoSuspend) {
970 		/* rtw_pwr_wakeup(padapter); */
971 		if (pwrctl->rf_pwrstate == rf_off)
972 			pwrctl->ps_flag = true;
973 	}
974 	padapter->net_closed = true;
975 	padapter->netif_up = false;
976 
977 /*if (!padapter->hw_init_completed)
978 	{
979 
980 		padapter->bDriverStopped = true;
981 
982 		rtw_dev_unload(padapter);
983 	}
984 	else*/
985 	if (pwrctl->rf_pwrstate == rf_on) {
986 		/* s1. */
987 		if (pnetdev) {
988 			if (!rtw_netif_queue_stopped(pnetdev))
989 				rtw_netif_stop_queue(pnetdev);
990 		}
991 
992 		/* s2. */
993 		LeaveAllPowerSaveMode(padapter);
994 		rtw_disassoc_cmd(padapter, 500, false);
995 		/* s2-2.  indicate disconnect to os */
996 		rtw_indicate_disconnect(padapter);
997 		/* s2-3. */
998 		rtw_free_assoc_resources(padapter, 1);
999 		/* s2-4. */
1000 		rtw_free_network_queue(padapter, true);
1001 	}
1002 
1003 	rtw_scan_abort(padapter);
1004 	adapter_wdev_data(padapter)->bandroid_scan = false;
1005 
1006 	return 0;
1007 }
1008 
rtw_ndev_destructor(struct net_device * ndev)1009 void rtw_ndev_destructor(struct net_device *ndev)
1010 {
1011 	kfree(ndev->ieee80211_ptr);
1012 }
1013 
rtw_dev_unload(struct adapter * padapter)1014 void rtw_dev_unload(struct adapter *padapter)
1015 {
1016 	struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter);
1017 	struct dvobj_priv *pobjpriv = padapter->dvobj;
1018 	struct debug_priv *pdbgpriv = &pobjpriv->drv_dbg;
1019 	struct cmd_priv *pcmdpriv = &padapter->cmdpriv;
1020 	u8 cnt = 0;
1021 
1022 	if (padapter->bup) {
1023 
1024 		padapter->bDriverStopped = true;
1025 		if (padapter->xmitpriv.ack_tx)
1026 			rtw_ack_tx_done(&padapter->xmitpriv, RTW_SCTX_DONE_DRV_STOP);
1027 
1028 		if (padapter->intf_stop)
1029 			padapter->intf_stop(padapter);
1030 
1031 		if (!pwrctl->bInternalAutoSuspend)
1032 			rtw_stop_drv_threads(padapter);
1033 
1034 		while (atomic_read(&pcmdpriv->cmdthd_running)) {
1035 			if (cnt > 5) {
1036 				break;
1037 			} else {
1038 				cnt++;
1039 				msleep(10);
1040 			}
1041 		}
1042 
1043 		/* check the status of IPS */
1044 		if (rtw_hal_check_ips_status(padapter) || pwrctl->rf_pwrstate == rf_off) {
1045 			/* check HW status and SW state */
1046 			netdev_dbg(padapter->pnetdev,
1047 				   "%s: driver in IPS-FWLPS\n", __func__);
1048 			pdbgpriv->dbg_dev_unload_inIPS_cnt++;
1049 			LeaveAllPowerSaveMode(padapter);
1050 		} else {
1051 			netdev_dbg(padapter->pnetdev,
1052 				   "%s: driver not in IPS\n", __func__);
1053 		}
1054 
1055 		if (!padapter->bSurpriseRemoved) {
1056 			hal_btcoex_IpsNotify(padapter, pwrctl->ips_mode_req);
1057 
1058 			/* amy modify 20120221 for power seq is different between driver open and ips */
1059 			rtw_hal_deinit(padapter);
1060 
1061 			padapter->bSurpriseRemoved = true;
1062 		}
1063 
1064 		padapter->bup = false;
1065 
1066 	}
1067 }
1068 
rtw_suspend_free_assoc_resource(struct adapter * padapter)1069 static int rtw_suspend_free_assoc_resource(struct adapter *padapter)
1070 {
1071 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1072 
1073 	if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME)) {
1074 		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)
1075 			&& check_fwstate(pmlmepriv, _FW_LINKED)) {
1076 			rtw_set_to_roam(padapter, 1);
1077 		}
1078 	}
1079 
1080 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) && check_fwstate(pmlmepriv, _FW_LINKED)) {
1081 		rtw_disassoc_cmd(padapter, 0, false);
1082 		/* s2-2.  indicate disconnect to os */
1083 		rtw_indicate_disconnect(padapter);
1084 	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1085 		rtw_sta_flush(padapter);
1086 	}
1087 
1088 	/* s2-3. */
1089 	rtw_free_assoc_resources(padapter, 1);
1090 
1091 	/* s2-4. */
1092 	rtw_free_network_queue(padapter, true);
1093 
1094 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY))
1095 		rtw_indicate_scan_done(padapter, 1);
1096 
1097 	if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
1098 		netdev_dbg(padapter->pnetdev, "%s: fw_under_linking\n",
1099 			   __func__);
1100 		rtw_indicate_disconnect(padapter);
1101 	}
1102 
1103 	return _SUCCESS;
1104 }
1105 
rtw_suspend_normal(struct adapter * padapter)1106 static void rtw_suspend_normal(struct adapter *padapter)
1107 {
1108 	struct net_device *pnetdev = padapter->pnetdev;
1109 
1110 	if (pnetdev) {
1111 		netif_carrier_off(pnetdev);
1112 		rtw_netif_stop_queue(pnetdev);
1113 	}
1114 
1115 	rtw_suspend_free_assoc_resource(padapter);
1116 
1117 	if ((rtw_hal_check_ips_status(padapter)) || (adapter_to_pwrctl(padapter)->rf_pwrstate == rf_off))
1118 		netdev_dbg(padapter->pnetdev,
1119 			   "%s: ### ERROR #### driver in IPS ####ERROR###!!!\n",
1120 			   __func__);
1121 
1122 	rtw_dev_unload(padapter);
1123 
1124 	/* sdio_deinit(adapter_to_dvobj(padapter)); */
1125 	if (padapter->intf_deinit)
1126 		padapter->intf_deinit(adapter_to_dvobj(padapter));
1127 }
1128 
rtw_suspend_common(struct adapter * padapter)1129 void rtw_suspend_common(struct adapter *padapter)
1130 {
1131 	struct dvobj_priv *psdpriv = padapter->dvobj;
1132 	struct debug_priv *pdbgpriv = &psdpriv->drv_dbg;
1133 	struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(psdpriv);
1134 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1135 
1136 	unsigned long start_time = jiffies;
1137 
1138 	netdev_dbg(padapter->pnetdev, " suspend start\n");
1139 	pdbgpriv->dbg_suspend_cnt++;
1140 
1141 	pwrpriv->bInSuspend = true;
1142 
1143 	while (pwrpriv->bips_processing)
1144 		msleep(1);
1145 
1146 	if ((!padapter->bup) || (padapter->bDriverStopped) || (padapter->bSurpriseRemoved)) {
1147 		pdbgpriv->dbg_suspend_error_cnt++;
1148 		goto exit;
1149 	}
1150 	rtw_ps_deny(padapter, PS_DENY_SUSPEND);
1151 
1152 	rtw_cancel_all_timer(padapter);
1153 
1154 	LeaveAllPowerSaveModeDirect(padapter);
1155 
1156 	rtw_stop_cmd_thread(padapter);
1157 
1158 	/*  wait for the latest FW to remove this condition. */
1159 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
1160 		hal_btcoex_SuspendNotify(padapter, 0);
1161 	else if (check_fwstate(pmlmepriv, WIFI_STATION_STATE))
1162 		hal_btcoex_SuspendNotify(padapter, 1);
1163 
1164 	rtw_ps_deny_cancel(padapter, PS_DENY_SUSPEND);
1165 
1166 	rtw_suspend_normal(padapter);
1167 
1168 	netdev_dbg(padapter->pnetdev, "rtw suspend success in %d ms\n",
1169 		   jiffies_to_msecs(jiffies - start_time));
1170 
1171 exit:
1172 
1173 	return;
1174 }
1175 
rtw_resume_process_normal(struct adapter * padapter)1176 static int rtw_resume_process_normal(struct adapter *padapter)
1177 {
1178 	struct net_device *pnetdev;
1179 	struct pwrctrl_priv *pwrpriv;
1180 	struct mlme_priv *pmlmepriv;
1181 	struct dvobj_priv *psdpriv;
1182 	struct debug_priv *pdbgpriv;
1183 
1184 	int ret = _SUCCESS;
1185 
1186 	if (!padapter) {
1187 		ret = -1;
1188 		goto exit;
1189 	}
1190 
1191 	pnetdev = padapter->pnetdev;
1192 	pwrpriv = adapter_to_pwrctl(padapter);
1193 	pmlmepriv = &padapter->mlmepriv;
1194 	psdpriv = padapter->dvobj;
1195 	pdbgpriv = &psdpriv->drv_dbg;
1196 	/*  interface init */
1197 	/* if (sdio_init(adapter_to_dvobj(padapter)) != _SUCCESS) */
1198 	if ((padapter->intf_init) && (padapter->intf_init(adapter_to_dvobj(padapter)) != _SUCCESS)) {
1199 		ret = -1;
1200 		goto exit;
1201 	}
1202 	rtw_hal_disable_interrupt(padapter);
1203 	/* if (sdio_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS) */
1204 	if ((padapter->intf_alloc_irq) && (padapter->intf_alloc_irq(adapter_to_dvobj(padapter)) != _SUCCESS)) {
1205 		ret = -1;
1206 		goto exit;
1207 	}
1208 
1209 	rtw_reset_drv_sw(padapter);
1210 	pwrpriv->bkeepfwalive = false;
1211 
1212 	if (pm_netdev_open(pnetdev, true) != 0) {
1213 		ret = -1;
1214 		pdbgpriv->dbg_resume_error_cnt++;
1215 		goto exit;
1216 	}
1217 
1218 	netif_device_attach(pnetdev);
1219 	netif_carrier_on(pnetdev);
1220 
1221 	if (padapter->pid[1] != 0)
1222 		rtw_signal_process(padapter->pid[1], SIGUSR2);
1223 
1224 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1225 		if (rtw_chk_roam_flags(padapter, RTW_ROAM_ON_RESUME))
1226 			rtw_roaming(padapter, NULL);
1227 	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1228 		rtw_ap_restore_network(padapter);
1229 	}
1230 
1231 exit:
1232 	return ret;
1233 }
1234 
rtw_resume_common(struct adapter * padapter)1235 int rtw_resume_common(struct adapter *padapter)
1236 {
1237 	int ret = 0;
1238 	unsigned long start_time = jiffies;
1239 	struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter);
1240 
1241 	netdev_dbg(padapter->pnetdev, "resume start\n");
1242 
1243 	rtw_resume_process_normal(padapter);
1244 
1245 	hal_btcoex_SuspendNotify(padapter, 0);
1246 
1247 	if (pwrpriv) {
1248 		pwrpriv->bInSuspend = false;
1249 	}
1250 	netdev_dbg(padapter->pnetdev, "%s:%d in %d ms\n", __func__, ret,
1251 		   jiffies_to_msecs(jiffies - start_time));
1252 
1253 	return ret;
1254 }
1255