GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / rtl8188eu / os_dep / os_intfs.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15 #define _OS_INTFS_C_
16
17 #include <osdep_service.h>
18 #include <osdep_intf.h>
19 #include <drv_types.h>
20 #include <xmit_osdep.h>
21 #include <recv_osdep.h>
22 #include <hal_intf.h>
23 #include <rtw_ioctl.h>
24 #include <rtl8188e_hal.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_DESCRIPTION("Realtek Wireless Lan Driver");
28 MODULE_AUTHOR("Realtek Semiconductor Corp.");
29 MODULE_VERSION(DRIVERVERSION);
30
31 #define RTW_NOTCH_FILTER 0 /* 0:Disable, 1:Enable, */
32
33 /* module param defaults */
34 /* Ndis802_11Infrastructure; infra, ad-hoc, auto */
35 static int rtw_channel = 1;/* ad-hoc support requirement */
36 static int rtw_wireless_mode = WIRELESS_11BG_24N;
37 static int rtw_vrtl_carrier_sense = AUTO_VCS;
38 static int rtw_vcs_type = RTS_CTS;/*  */
39 static int rtw_rts_thresh = 2347;/*  */
40 static int rtw_frag_thresh = 2346;/*  */
41 static int rtw_preamble = PREAMBLE_LONG;/* long, short, auto */
42 static int rtw_power_mgnt = 1;
43 static int rtw_ips_mode = IPS_NORMAL;
44
45 static int rtw_smart_ps = 2;
46
47 module_param(rtw_ips_mode, int, 0644);
48 MODULE_PARM_DESC(rtw_ips_mode, "The default IPS mode");
49
50 static int rtw_debug = 1;
51
52 static int rtw_acm_method;/*  0:By SW 1:By HW. */
53
54 static int rtw_wmm_enable = 1;/*  default is set to enable the wmm. */
55 static int rtw_uapsd_enable;
56
57 static int rtw_ht_enable = 1;
58 /* 0 :disable, bit(0): enable 2.4g, bit(1): enable 5g */
59 static int rtw_cbw40_enable = 3;
60 static int rtw_ampdu_enable = 1;/* for enable tx_ampdu */
61
62 /* 0: disable
63  * bit(0):enable 2.4g
64  * bit(1):enable 5g
65  * default is set to enable 2.4GHZ for IOT issue with bufflao's AP at 5GHZ
66  */
67 static int rtw_rx_stbc = 1;
68 static int rtw_ampdu_amsdu;/*  0: disabled, 1:enabled, 2:auto */
69
70 static int rtw_wifi_spec;
71 static int rtw_channel_plan = RT_CHANNEL_DOMAIN_MAX;
72
73 static int rtw_antdiv_cfg = 2; /*  0:OFF , 1:ON, 2:decide by Efuse config */
74
75 /* 0: decide by efuse
76  * 1: for 88EE, 1Tx and 1RxCG are diversity (2 Ant with SPDT)
77  * 2: for 88EE, 1Tx and 2Rx are diversity (2 Ant, Tx and RxCG are both on aux
78  *    port, RxCS is on main port)
79  * 3: for 88EE, 1Tx and 1RxCG are fixed (1Ant, Tx and RxCG are both on aux port)
80  */
81 static int rtw_antdiv_type;
82
83 static int rtw_enusbss;/* 0:disable, 1:enable */
84
85 static int rtw_hwpdn_mode = 2;/* 0:disable, 1:enable, 2: by EFUSE config */
86
87 int rtw_mc2u_disable;
88
89 static int rtw_80211d;
90
91 static char *ifname = "wlan%d";
92 module_param(ifname, charp, 0644);
93 MODULE_PARM_DESC(ifname, "The default name to allocate for first interface");
94
95 static char *if2name = "wlan%d";
96 module_param(if2name, charp, 0644);
97 MODULE_PARM_DESC(if2name, "The default name to allocate for second interface");
98
99 /* temp mac address if users want to use instead of the mac address in Efuse */
100 char *rtw_initmac;
101
102 module_param(rtw_initmac, charp, 0644);
103 module_param(rtw_channel_plan, int, 0644);
104 module_param(rtw_channel, int, 0644);
105 module_param(rtw_wmm_enable, int, 0644);
106 module_param(rtw_vrtl_carrier_sense, int, 0644);
107 module_param(rtw_vcs_type, int, 0644);
108 module_param(rtw_ht_enable, int, 0644);
109 module_param(rtw_cbw40_enable, int, 0644);
110 module_param(rtw_ampdu_enable, int, 0644);
111 module_param(rtw_rx_stbc, int, 0644);
112 module_param(rtw_ampdu_amsdu, int, 0644);
113 module_param(rtw_power_mgnt, int, 0644);
114 module_param(rtw_smart_ps, int, 0644);
115 module_param(rtw_wifi_spec, int, 0644);
116 module_param(rtw_antdiv_cfg, int, 0644);
117 module_param(rtw_antdiv_type, int, 0644);
118 module_param(rtw_enusbss, int, 0644);
119 module_param(rtw_hwpdn_mode, int, 0644);
120
121 static uint rtw_max_roaming_times = 2;
122 module_param(rtw_max_roaming_times, uint, 0644);
123 MODULE_PARM_DESC(rtw_max_roaming_times, "The max roaming times to try");
124
125 static int rtw_fw_iol = 1;/*  0:Disable, 1:enable, 2:by usb speed */
126 module_param(rtw_fw_iol, int, 0644);
127 MODULE_PARM_DESC(rtw_fw_iol, "FW IOL");
128
129 module_param(rtw_mc2u_disable, int, 0644);
130
131 module_param(rtw_80211d, int, 0644);
132 MODULE_PARM_DESC(rtw_80211d, "Enable 802.11d mechanism");
133
134 static uint rtw_notch_filter = RTW_NOTCH_FILTER;
135 module_param(rtw_notch_filter, uint, 0644);
136 MODULE_PARM_DESC(rtw_notch_filter, "0:Disable, 1:Enable, 2:Enable only for P2P");
137 module_param_named(debug, rtw_debug, int, 0444);
138 MODULE_PARM_DESC(debug, "Set debug level (1-9) (default 1)");
139
140 static bool rtw_monitor_enable;
141 module_param_named(monitor_enable, rtw_monitor_enable, bool, 0444);
142 MODULE_PARM_DESC(monitor_enable, "Enable monitor inferface (default: false)");
143
144 static int netdev_close(struct net_device *pnetdev);
145
146 static void loadparam(struct adapter *padapter, struct net_device *pnetdev)
147 {
148         struct registry_priv  *registry_par = &padapter->registrypriv;
149
150         GlobalDebugLevel = rtw_debug;
151
152         memcpy(registry_par->ssid.Ssid, "ANY", 3);
153         registry_par->ssid.SsidLength = 3;
154
155         registry_par->channel = (u8)rtw_channel;
156         registry_par->wireless_mode = (u8)rtw_wireless_mode;
157         registry_par->vrtl_carrier_sense = (u8)rtw_vrtl_carrier_sense;
158         registry_par->vcs_type = (u8)rtw_vcs_type;
159         registry_par->rts_thresh = (u16)rtw_rts_thresh;
160         registry_par->frag_thresh = (u16)rtw_frag_thresh;
161         registry_par->preamble = (u8)rtw_preamble;
162         registry_par->smart_ps =  (u8)rtw_smart_ps;
163         registry_par->power_mgnt = (u8)rtw_power_mgnt;
164         registry_par->ips_mode = (u8)rtw_ips_mode;
165         registry_par->mp_mode = 0;
166         registry_par->acm_method = (u8)rtw_acm_method;
167
168          /* UAPSD */
169         registry_par->wmm_enable = (u8)rtw_wmm_enable;
170         registry_par->uapsd_enable = (u8)rtw_uapsd_enable;
171
172         registry_par->ht_enable = (u8)rtw_ht_enable;
173         registry_par->cbw40_enable = (u8)rtw_cbw40_enable;
174         registry_par->ampdu_enable = (u8)rtw_ampdu_enable;
175         registry_par->rx_stbc = (u8)rtw_rx_stbc;
176         registry_par->ampdu_amsdu = (u8)rtw_ampdu_amsdu;
177         registry_par->wifi_spec = (u8)rtw_wifi_spec;
178         registry_par->channel_plan = (u8)rtw_channel_plan;
179         registry_par->accept_addba_req = true;
180         registry_par->antdiv_cfg = (u8)rtw_antdiv_cfg;
181         registry_par->antdiv_type = (u8)rtw_antdiv_type;
182         registry_par->hwpdn_mode = (u8)rtw_hwpdn_mode;
183
184         registry_par->max_roaming_times = (u8)rtw_max_roaming_times;
185
186         registry_par->fw_iol = rtw_fw_iol;
187
188         registry_par->enable80211d = (u8)rtw_80211d;
189         snprintf(registry_par->ifname, 16, "%s", ifname);
190         snprintf(registry_par->if2name, 16, "%s", if2name);
191         registry_par->notch_filter = (u8)rtw_notch_filter;
192         registry_par->monitor_enable = rtw_monitor_enable;
193 }
194
195 static int rtw_net_set_mac_address(struct net_device *pnetdev, void *p)
196 {
197         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
198         struct sockaddr *addr = p;
199
200         if (!padapter->bup)
201                 memcpy(padapter->eeprompriv.mac_addr, addr->sa_data, ETH_ALEN);
202
203         return 0;
204 }
205
206 static struct net_device_stats *rtw_net_get_stats(struct net_device *pnetdev)
207 {
208         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
209         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
210         struct recv_priv *precvpriv = &(padapter->recvpriv);
211
212         padapter->stats.tx_packets = pxmitpriv->tx_pkts;
213         padapter->stats.rx_packets = precvpriv->rx_pkts;
214         padapter->stats.tx_dropped = pxmitpriv->tx_drop;
215         padapter->stats.rx_dropped = precvpriv->rx_drop;
216         padapter->stats.tx_bytes = pxmitpriv->tx_bytes;
217         padapter->stats.rx_bytes = precvpriv->rx_bytes;
218         return &padapter->stats;
219 }
220
221 /*
222  * AC to queue mapping
223  *
224  * AC_VO -> queue 0
225  * AC_VI -> queue 1
226  * AC_BE -> queue 2
227  * AC_BK -> queue 3
228  */
229 static const u16 rtw_1d_to_queue[8] = { 2, 3, 3, 2, 1, 1, 0, 0 };
230
231 /* Given a data frame determine the 802.1p/1d tag to use. */
232 static unsigned int rtw_classify8021d(struct sk_buff *skb)
233 {
234         unsigned int dscp;
235
236         /* skb->priority values from 256->263 are magic values to
237          * directly indicate a specific 802.1d priority.  This is used
238          * to allow 802.1d priority to be passed directly in from VLAN
239          * tags, etc.
240          */
241         if (skb->priority >= 256 && skb->priority <= 263)
242                 return skb->priority - 256;
243
244         switch (skb->protocol) {
245         case htons(ETH_P_IP):
246                 dscp = ip_hdr(skb)->tos & 0xfc;
247                 break;
248         default:
249                 return 0;
250         }
251
252         return dscp >> 5;
253 }
254
255 static u16 rtw_select_queue(struct net_device *dev, struct sk_buff *skb,
256                             void *accel_priv, select_queue_fallback_t fallback)
257 {
258         struct adapter  *padapter = rtw_netdev_priv(dev);
259         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
260
261         skb->priority = rtw_classify8021d(skb);
262
263         if (pmlmepriv->acm_mask != 0)
264                 skb->priority = qos_acm(pmlmepriv->acm_mask, skb->priority);
265
266         return rtw_1d_to_queue[skb->priority];
267 }
268
269 u16 rtw_recv_select_queue(struct sk_buff *skb)
270 {
271         struct iphdr *piphdr;
272         unsigned int dscp;
273         __be16  eth_type;
274         u32 priority;
275         u8 *pdata = skb->data;
276
277         memcpy(&eth_type, pdata+(ETH_ALEN<<1), 2);
278
279         switch (eth_type) {
280         case htons(ETH_P_IP):
281                 piphdr = (struct iphdr *)(pdata+ETH_HLEN);
282                 dscp = piphdr->tos & 0xfc;
283                 priority = dscp >> 5;
284                 break;
285         default:
286                 priority = 0;
287         }
288
289         return rtw_1d_to_queue[priority];
290 }
291
292 static const struct net_device_ops rtw_netdev_ops = {
293         .ndo_open = netdev_open,
294         .ndo_stop = netdev_close,
295         .ndo_start_xmit = rtw_xmit_entry,
296         .ndo_select_queue       = rtw_select_queue,
297         .ndo_set_mac_address = rtw_net_set_mac_address,
298         .ndo_get_stats = rtw_net_get_stats,
299         .ndo_do_ioctl = rtw_ioctl,
300 };
301
302 int rtw_init_netdev_name(struct net_device *pnetdev, const char *ifname)
303 {
304         if (dev_alloc_name(pnetdev, ifname) < 0)
305                 RT_TRACE(_module_os_intfs_c_, _drv_err_, ("dev_alloc_name, fail!\n"));
306
307         netif_carrier_off(pnetdev);
308         return 0;
309 }
310
311 static const struct device_type wlan_type = {
312         .name = "wlan",
313 };
314
315 struct net_device *rtw_init_netdev(struct adapter *old_padapter)
316 {
317         struct adapter *padapter;
318         struct net_device *pnetdev = NULL;
319
320         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("+init_net_dev\n"));
321
322         if (old_padapter != NULL)
323                 pnetdev = rtw_alloc_etherdev_with_old_priv((void *)old_padapter);
324
325         if (!pnetdev)
326                 return NULL;
327
328         pnetdev->dev.type = &wlan_type;
329         padapter = rtw_netdev_priv(pnetdev);
330         padapter->pnetdev = pnetdev;
331         DBG_88E("register rtw_netdev_ops to netdev_ops\n");
332         pnetdev->netdev_ops = &rtw_netdev_ops;
333         pnetdev->watchdog_timeo = HZ*3; /* 3 second timeout */
334         pnetdev->wireless_handlers = (struct iw_handler_def *)&rtw_handlers_def;
335
336         loadparam(padapter, pnetdev);
337
338         return pnetdev;
339 }
340
341 static int rtw_start_drv_threads(struct adapter *padapter)
342 {
343         int err = 0;
344
345         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("+rtw_start_drv_threads\n"));
346
347         padapter->cmdThread = kthread_run(rtw_cmd_thread, padapter,
348                                           "RTW_CMD_THREAD");
349         if (IS_ERR(padapter->cmdThread))
350                 err = PTR_ERR(padapter->cmdThread);
351         else
352                 /* wait for cmd_thread to run */
353                 wait_for_completion_interruptible(&padapter->cmdpriv.terminate_cmdthread_comp);
354
355         return err;
356 }
357
358 void rtw_stop_drv_threads(struct adapter *padapter)
359 {
360         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("+rtw_stop_drv_threads\n"));
361
362         /* Below is to terminate rtw_cmd_thread & event_thread... */
363         complete(&padapter->cmdpriv.cmd_queue_comp);
364         if (padapter->cmdThread)
365                 wait_for_completion_interruptible(&padapter->cmdpriv.terminate_cmdthread_comp);
366
367 }
368
369 static u8 rtw_init_default_value(struct adapter *padapter)
370 {
371         struct registry_priv *pregistrypriv = &padapter->registrypriv;
372         struct xmit_priv        *pxmitpriv = &padapter->xmitpriv;
373         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
374         struct security_priv *psecuritypriv = &padapter->securitypriv;
375
376         /* xmit_priv */
377         pxmitpriv->vcs_setting = pregistrypriv->vrtl_carrier_sense;
378         pxmitpriv->vcs = pregistrypriv->vcs_type;
379         pxmitpriv->vcs_type = pregistrypriv->vcs_type;
380         pxmitpriv->frag_len = pregistrypriv->frag_thresh;
381
382         /* mlme_priv */
383         pmlmepriv->scan_interval = SCAN_INTERVAL;/*  30*2 sec = 60sec */
384         pmlmepriv->scan_mode = SCAN_ACTIVE;
385
386         /* ht_priv */
387         pmlmepriv->htpriv.ampdu_enable = false;/* set to disabled */
388
389         /* security_priv */
390         psecuritypriv->binstallGrpkey = _FAIL;
391         psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
392         psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_;
393         psecuritypriv->dot11PrivacyKeyIndex = 0;
394         psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
395         psecuritypriv->dot118021XGrpKeyid = 1;
396         psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
397         psecuritypriv->ndisencryptstatus = Ndis802_11WEPDisabled;
398
399         /* registry_priv */
400         rtw_init_registrypriv_dev_network(padapter);
401         rtw_update_registrypriv_dev_network(padapter);
402
403         /* hal_priv */
404         rtw_hal_def_value_init(padapter);
405
406         /* misc. */
407         padapter->bReadPortCancel = false;
408         padapter->bWritePortCancel = false;
409         return _SUCCESS;
410 }
411
412 u8 rtw_reset_drv_sw(struct adapter *padapter)
413 {
414         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
415         struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
416
417         /* hal_priv */
418         rtw_hal_def_value_init(padapter);
419         padapter->bReadPortCancel = false;
420         padapter->bWritePortCancel = false;
421         pmlmepriv->scan_interval = SCAN_INTERVAL;/*  30*2 sec = 60sec */
422
423         padapter->xmitpriv.tx_pkts = 0;
424         padapter->recvpriv.rx_pkts = 0;
425
426         pmlmepriv->LinkDetectInfo.bBusyTraffic = false;
427
428         _clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY | _FW_UNDER_LINKING);
429         rtw_hal_sreset_init(padapter);
430         pwrctrlpriv->pwr_state_check_cnts = 0;
431
432         /* mlmeextpriv */
433         padapter->mlmeextpriv.sitesurvey_res.state = SCAN_DISABLE;
434
435         rtw_set_signal_stat_timer(&padapter->recvpriv);
436
437         return _SUCCESS;
438 }
439
440 u8 rtw_init_drv_sw(struct adapter *padapter)
441 {
442         u8      ret8 = _SUCCESS;
443
444
445         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("+rtw_init_drv_sw\n"));
446
447         if ((rtw_init_cmd_priv(&padapter->cmdpriv)) == _FAIL) {
448                 RT_TRACE(_module_os_intfs_c_, _drv_err_, ("\n Can't init cmd_priv\n"));
449                 ret8 = _FAIL;
450                 goto exit;
451         }
452
453         padapter->cmdpriv.padapter = padapter;
454
455         if (rtw_init_mlme_priv(padapter) == _FAIL) {
456                 RT_TRACE(_module_os_intfs_c_, _drv_err_, ("\n Can't init mlme_priv\n"));
457                 ret8 = _FAIL;
458                 goto exit;
459         }
460
461         if (init_mlme_ext_priv(padapter) == _FAIL) {
462                 RT_TRACE(_module_os_intfs_c_, _drv_err_, ("\n Can't init mlme_ext_priv\n"));
463                 ret8 = _FAIL;
464                 goto exit;
465         }
466
467         if (_rtw_init_xmit_priv(&padapter->xmitpriv, padapter) == _FAIL) {
468                 DBG_88E("Can't _rtw_init_xmit_priv\n");
469                 ret8 = _FAIL;
470                 goto exit;
471         }
472
473         if (_rtw_init_recv_priv(&padapter->recvpriv, padapter) == _FAIL) {
474                 DBG_88E("Can't _rtw_init_recv_priv\n");
475                 ret8 = _FAIL;
476                 goto exit;
477         }
478
479         if (_rtw_init_sta_priv(&padapter->stapriv) == _FAIL) {
480                 DBG_88E("Can't _rtw_init_sta_priv\n");
481                 ret8 = _FAIL;
482                 goto exit;
483         }
484
485         padapter->stapriv.padapter = padapter;
486
487         rtw_init_bcmc_stainfo(padapter);
488
489         rtw_init_pwrctrl_priv(padapter);
490
491         ret8 = rtw_init_default_value(padapter);
492
493         rtw_hal_dm_init(padapter);
494         rtw_hal_sw_led_init(padapter);
495
496         rtw_hal_sreset_init(padapter);
497
498 exit:
499         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("-rtw_init_drv_sw\n"));
500
501
502         return ret8;
503 }
504
505 void rtw_cancel_all_timer(struct adapter *padapter)
506 {
507         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("+rtw_cancel_all_timer\n"));
508
509         del_timer_sync(&padapter->mlmepriv.assoc_timer);
510         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("rtw_cancel_all_timer:cancel association timer complete!\n"));
511
512         del_timer_sync(&padapter->mlmepriv.scan_to_timer);
513         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("rtw_cancel_all_timer:cancel scan_to_timer!\n"));
514
515         del_timer_sync(&padapter->mlmepriv.dynamic_chk_timer);
516         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("rtw_cancel_all_timer:cancel dynamic_chk_timer!\n"));
517
518         /*  cancel sw led timer */
519         rtw_hal_sw_led_deinit(padapter);
520         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("rtw_cancel_all_timer:cancel DeInitSwLeds!\n"));
521
522         del_timer_sync(&padapter->pwrctrlpriv.pwr_state_check_timer);
523
524         del_timer_sync(&padapter->recvpriv.signal_stat_timer);
525 }
526
527 u8 rtw_free_drv_sw(struct adapter *padapter)
528 {
529         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("==>rtw_free_drv_sw"));
530
531         free_mlme_ext_priv(&padapter->mlmeextpriv);
532
533         rtw_free_mlme_priv(&padapter->mlmepriv);
534         _rtw_free_xmit_priv(&padapter->xmitpriv);
535
536         /* will free bcmc_stainfo here */
537         _rtw_free_sta_priv(&padapter->stapriv);
538
539         _rtw_free_recv_priv(&padapter->recvpriv);
540
541         rtw_hal_free_data(padapter);
542
543         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("<== rtw_free_drv_sw\n"));
544
545         mutex_destroy(&padapter->hw_init_mutex);
546
547         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("-rtw_free_drv_sw\n"));
548
549         return _SUCCESS;
550 }
551
552 static int _netdev_open(struct net_device *pnetdev)
553 {
554         uint status;
555         int err;
556         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
557         struct pwrctrl_priv *pwrctrlpriv = &padapter->pwrctrlpriv;
558
559         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("+88eu_drv - dev_open\n"));
560         DBG_88E("+88eu_drv - drv_open, bup =%d\n", padapter->bup);
561
562         if (pwrctrlpriv->ps_flag) {
563                 padapter->net_closed = false;
564                 goto netdev_open_normal_process;
565         }
566
567         if (!padapter->bup) {
568                 padapter->bDriverStopped = false;
569                 padapter->bSurpriseRemoved = false;
570
571                 status = rtw_hal_init(padapter);
572                 if (status == _FAIL) {
573                         RT_TRACE(_module_os_intfs_c_, _drv_err_, ("rtl88eu_hal_init(): Can't init h/w!\n"));
574                         goto netdev_open_error;
575                 }
576
577                 pr_info("MAC Address = %pM\n", pnetdev->dev_addr);
578
579                 err = rtw_start_drv_threads(padapter);
580                 if (err) {
581                         pr_info("Initialize driver software resource Failed!\n");
582                         goto netdev_open_error;
583                 }
584
585                 if (init_hw_mlme_ext(padapter) == _FAIL) {
586                         pr_info("can't init mlme_ext_priv\n");
587                         goto netdev_open_error;
588                 }
589                 rtw_hal_inirp_init(padapter);
590
591                 LedControl8188eu(padapter, LED_CTL_NO_LINK);
592
593                 padapter->bup = true;
594         }
595         padapter->net_closed = false;
596
597         mod_timer(&padapter->mlmepriv.dynamic_chk_timer,
598                   jiffies + msecs_to_jiffies(2000));
599
600         padapter->pwrctrlpriv.bips_processing = false;
601         rtw_set_pwr_state_check_timer(&padapter->pwrctrlpriv);
602
603         if (!rtw_netif_queue_stopped(pnetdev))
604                 netif_tx_start_all_queues(pnetdev);
605         else
606                 netif_tx_wake_all_queues(pnetdev);
607
608 netdev_open_normal_process:
609         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("-88eu_drv - dev_open\n"));
610         DBG_88E("-88eu_drv - drv_open, bup =%d\n", padapter->bup);
611         return 0;
612
613 netdev_open_error:
614         padapter->bup = false;
615         netif_carrier_off(pnetdev);
616         netif_tx_stop_all_queues(pnetdev);
617         RT_TRACE(_module_os_intfs_c_, _drv_err_, ("-88eu_drv - dev_open, fail!\n"));
618         DBG_88E("-88eu_drv - drv_open fail, bup =%d\n", padapter->bup);
619         return -1;
620 }
621
622 int netdev_open(struct net_device *pnetdev)
623 {
624         int ret;
625         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
626
627         if (mutex_lock_interruptible(&padapter->hw_init_mutex))
628                 return -ERESTARTSYS;
629         ret = _netdev_open(pnetdev);
630         mutex_unlock(&padapter->hw_init_mutex);
631         return ret;
632 }
633
634 int  ips_netdrv_open(struct adapter *padapter)
635 {
636         int status = _SUCCESS;
637
638         padapter->net_closed = false;
639         DBG_88E("===> %s.........\n", __func__);
640
641         padapter->bDriverStopped = false;
642         padapter->bSurpriseRemoved = false;
643
644         status = rtw_hal_init(padapter);
645         if (status == _FAIL) {
646                 RT_TRACE(_module_os_intfs_c_, _drv_err_, ("ips_netdrv_open(): Can't init h/w!\n"));
647                 goto netdev_open_error;
648         }
649
650         rtw_hal_inirp_init(padapter);
651
652         rtw_set_pwr_state_check_timer(&padapter->pwrctrlpriv);
653         mod_timer(&padapter->mlmepriv.dynamic_chk_timer,
654                   jiffies + msecs_to_jiffies(5000));
655
656          return _SUCCESS;
657
658 netdev_open_error:
659         DBG_88E("-ips_netdrv_open - drv_open failure, bup =%d\n", padapter->bup);
660
661         return _FAIL;
662 }
663
664
665 int rtw_ips_pwr_up(struct adapter *padapter)
666 {
667         int result;
668         unsigned long start_time = jiffies;
669
670         DBG_88E("===>  rtw_ips_pwr_up..............\n");
671         rtw_reset_drv_sw(padapter);
672
673         result = ips_netdrv_open(padapter);
674
675         LedControl8188eu(padapter, LED_CTL_NO_LINK);
676
677         DBG_88E("<===  rtw_ips_pwr_up.............. in %dms\n",
678                 jiffies_to_msecs(jiffies - start_time));
679         return result;
680 }
681
682 void rtw_ips_pwr_down(struct adapter *padapter)
683 {
684         unsigned long start_time = jiffies;
685
686         DBG_88E("===> rtw_ips_pwr_down...................\n");
687
688         padapter->net_closed = true;
689
690         LedControl8188eu(padapter, LED_CTL_POWER_OFF);
691
692         rtw_ips_dev_unload(padapter);
693         DBG_88E("<=== rtw_ips_pwr_down..................... in %dms\n",
694                 jiffies_to_msecs(jiffies - start_time));
695 }
696
697 void rtw_ips_dev_unload(struct adapter *padapter)
698 {
699         DBG_88E("====> %s...\n", __func__);
700
701         rtw_hal_set_hwreg(padapter, HW_VAR_FIFO_CLEARN_UP, NULL);
702
703         usb_intf_stop(padapter);
704
705         /* s5. */
706         if (!padapter->bSurpriseRemoved)
707                 rtw_hal_deinit(padapter);
708 }
709
710 static int netdev_close(struct net_device *pnetdev)
711 {
712         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
713
714         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("+88eu_drv - drv_close\n"));
715
716         if (padapter->pwrctrlpriv.bInternalAutoSuspend) {
717                 if (padapter->pwrctrlpriv.rf_pwrstate == rf_off)
718                         padapter->pwrctrlpriv.ps_flag = true;
719         }
720         padapter->net_closed = true;
721
722         if (padapter->pwrctrlpriv.rf_pwrstate == rf_on) {
723                 DBG_88E("(2)88eu_drv - drv_close, bup =%d, hw_init_completed =%d\n",
724                         padapter->bup, padapter->hw_init_completed);
725
726                 /* s1. */
727                 if (pnetdev) {
728                         if (!rtw_netif_queue_stopped(pnetdev))
729                                 netif_tx_stop_all_queues(pnetdev);
730                 }
731
732                 /* s2. */
733                 LeaveAllPowerSaveMode(padapter);
734                 rtw_disassoc_cmd(padapter, 500, false);
735                 /* s2-2.  indicate disconnect to os */
736                 rtw_indicate_disconnect(padapter);
737                 /* s2-3. */
738                 rtw_free_assoc_resources(padapter);
739                 /* s2-4. */
740                 rtw_free_network_queue(padapter, true);
741                 /*  Close LED */
742                 LedControl8188eu(padapter, LED_CTL_POWER_OFF);
743         }
744
745         RT_TRACE(_module_os_intfs_c_, _drv_info_, ("-88eu_drv - drv_close\n"));
746         DBG_88E("-88eu_drv - drv_close, bup =%d\n", padapter->bup);
747         return 0;
748 }