Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /******************************************************************************
0003  * os_intfs.c
0004  *
0005  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
0006  * Linux device driver for RTL8192SU
0007  *
0008  * Modifications for inclusion into the Linux staging tree are
0009  * Copyright(c) 2010 Larry Finger. All rights reserved.
0010  *
0011  * Contact information:
0012  * WLAN FAE <wlanfae@realtek.com>.
0013  * Larry Finger <Larry.Finger@lwfinger.net>
0014  *
0015  ******************************************************************************/
0016 
0017 #define _OS_INTFS_C_
0018 
0019 #include <linux/module.h>
0020 #include <linux/kthread.h>
0021 #include <linux/firmware.h>
0022 #include "osdep_service.h"
0023 #include "drv_types.h"
0024 #include "xmit_osdep.h"
0025 #include "recv_osdep.h"
0026 #include "rtl871x_ioctl.h"
0027 #include "usb_osintf.h"
0028 
0029 MODULE_LICENSE("GPL");
0030 MODULE_DESCRIPTION("rtl871x wireless lan driver");
0031 MODULE_AUTHOR("Larry Finger");
0032 
0033 static char ifname[IFNAMSIZ] = "wlan%d";
0034 
0035 /* module param defaults */
0036 static int chip_version = RTL8712_2ndCUT;
0037 static int rfintfs = HWPI;
0038 static int lbkmode = RTL8712_AIR_TRX;
0039 static int hci = RTL8712_USB;
0040 static int ampdu_enable = 1;/*for enable tx_ampdu*/
0041 
0042 /* The video_mode variable is for video mode.*/
0043 /* It may be specify when inserting module with video_mode=1 parameter.*/
0044 static int video_mode = 1;   /* enable video mode*/
0045 
0046 /*Ndis802_11Infrastructure; infra, ad-hoc, auto*/
0047 static int network_mode = Ndis802_11IBSS;
0048 static int channel = 1;/*ad-hoc support requirement*/
0049 static int wireless_mode = WIRELESS_11BG;
0050 static int vrtl_carrier_sense = AUTO_VCS;
0051 static int vcs_type = RTS_CTS;
0052 static int frag_thresh = 2346;
0053 static int preamble = PREAMBLE_LONG;/*long, short, auto*/
0054 static int scan_mode = 1;/*active, passive*/
0055 static int adhoc_tx_pwr = 1;
0056 static int soft_ap;
0057 static int smart_ps = 1;
0058 static int power_mgnt = PS_MODE_ACTIVE;
0059 static int radio_enable = 1;
0060 static int long_retry_lmt = 7;
0061 static int short_retry_lmt = 7;
0062 static int busy_thresh = 40;
0063 static int ack_policy = NORMAL_ACK;
0064 static int mp_mode;
0065 static int software_encrypt;
0066 static int software_decrypt;
0067 
0068 static int wmm_enable;/* default is set to disable the wmm.*/
0069 static int uapsd_enable;
0070 static int uapsd_max_sp = NO_LIMIT;
0071 static int uapsd_acbk_en;
0072 static int uapsd_acbe_en;
0073 static int uapsd_acvi_en;
0074 static int uapsd_acvo_en;
0075 
0076 static int ht_enable = 1;
0077 static int cbw40_enable = 1;
0078 static int rf_config = RTL8712_RF_1T2R;  /* 1T2R*/
0079 static int low_power;
0080 /* mac address to use instead of the one stored in Efuse */
0081 char *r8712_initmac;
0082 static char *initmac;
0083 /* if wifi_test = 1, driver will disable the turbo mode and pass it to
0084  * firmware private.
0085  */
0086 static int wifi_test;
0087 
0088 module_param_string(ifname, ifname, sizeof(ifname), 0644);
0089 module_param(wifi_test, int, 0644);
0090 module_param(initmac, charp, 0644);
0091 module_param(video_mode, int, 0644);
0092 module_param(chip_version, int, 0644);
0093 module_param(rfintfs, int, 0644);
0094 module_param(lbkmode, int, 0644);
0095 module_param(hci, int, 0644);
0096 module_param(network_mode, int, 0644);
0097 module_param(channel, int, 0644);
0098 module_param(mp_mode, int, 0644);
0099 module_param(wmm_enable, int, 0644);
0100 module_param(vrtl_carrier_sense, int, 0644);
0101 module_param(vcs_type, int, 0644);
0102 module_param(busy_thresh, int, 0644);
0103 module_param(ht_enable, int, 0644);
0104 module_param(cbw40_enable, int, 0644);
0105 module_param(ampdu_enable, int, 0644);
0106 module_param(rf_config, int, 0644);
0107 module_param(power_mgnt, int, 0644);
0108 module_param(low_power, int, 0644);
0109 
0110 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
0111 MODULE_PARM_DESC(initmac, "MAC-Address, default: use FUSE");
0112 
0113 static int netdev_open(struct net_device *pnetdev);
0114 static int netdev_close(struct net_device *pnetdev);
0115 
0116 static void loadparam(struct _adapter *padapter, struct  net_device *pnetdev)
0117 {
0118     struct registry_priv  *registry_par = &padapter->registrypriv;
0119 
0120     registry_par->chip_version = (u8)chip_version;
0121     registry_par->rfintfs = (u8)rfintfs;
0122     registry_par->lbkmode = (u8)lbkmode;
0123     registry_par->hci = (u8)hci;
0124     registry_par->network_mode  = (u8)network_mode;
0125     memcpy(registry_par->ssid.Ssid, "ANY", 3);
0126     registry_par->ssid.SsidLength = 3;
0127     registry_par->channel = (u8)channel;
0128     registry_par->wireless_mode = (u8)wireless_mode;
0129     registry_par->vrtl_carrier_sense = (u8)vrtl_carrier_sense;
0130     registry_par->vcs_type = (u8)vcs_type;
0131     registry_par->frag_thresh = (u16)frag_thresh;
0132     registry_par->preamble = (u8)preamble;
0133     registry_par->scan_mode = (u8)scan_mode;
0134     registry_par->adhoc_tx_pwr = (u8)adhoc_tx_pwr;
0135     registry_par->soft_ap = (u8)soft_ap;
0136     registry_par->smart_ps = (u8)smart_ps;
0137     registry_par->power_mgnt = (u8)power_mgnt;
0138     registry_par->radio_enable = (u8)radio_enable;
0139     registry_par->long_retry_lmt = (u8)long_retry_lmt;
0140     registry_par->short_retry_lmt = (u8)short_retry_lmt;
0141     registry_par->busy_thresh = (u16)busy_thresh;
0142     registry_par->ack_policy = (u8)ack_policy;
0143     registry_par->mp_mode = (u8)mp_mode;
0144     registry_par->software_encrypt = (u8)software_encrypt;
0145     registry_par->software_decrypt = (u8)software_decrypt;
0146     /*UAPSD*/
0147     registry_par->wmm_enable = (u8)wmm_enable;
0148     registry_par->uapsd_enable = (u8)uapsd_enable;
0149     registry_par->uapsd_max_sp = (u8)uapsd_max_sp;
0150     registry_par->uapsd_acbk_en = (u8)uapsd_acbk_en;
0151     registry_par->uapsd_acbe_en = (u8)uapsd_acbe_en;
0152     registry_par->uapsd_acvi_en = (u8)uapsd_acvi_en;
0153     registry_par->uapsd_acvo_en = (u8)uapsd_acvo_en;
0154     registry_par->ht_enable = (u8)ht_enable;
0155     registry_par->cbw40_enable = (u8)cbw40_enable;
0156     registry_par->ampdu_enable = (u8)ampdu_enable;
0157     registry_par->rf_config = (u8)rf_config;
0158     registry_par->low_power = (u8)low_power;
0159     registry_par->wifi_test = (u8)wifi_test;
0160     r8712_initmac = initmac;
0161 }
0162 
0163 static int r871x_net_set_mac_address(struct net_device *pnetdev, void *p)
0164 {
0165     struct _adapter *padapter = netdev_priv(pnetdev);
0166     struct sockaddr *addr = p;
0167 
0168     if (!padapter->bup)
0169         eth_hw_addr_set(pnetdev, addr->sa_data);
0170     return 0;
0171 }
0172 
0173 static struct net_device_stats *r871x_net_get_stats(struct net_device *pnetdev)
0174 {
0175     struct _adapter *padapter = netdev_priv(pnetdev);
0176     struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
0177     struct recv_priv *precvpriv = &padapter->recvpriv;
0178 
0179     padapter->stats.tx_packets = pxmitpriv->tx_pkts;
0180     padapter->stats.rx_packets = precvpriv->rx_pkts;
0181     padapter->stats.tx_dropped = pxmitpriv->tx_drop;
0182     padapter->stats.rx_dropped = precvpriv->rx_drop;
0183     padapter->stats.tx_bytes = pxmitpriv->tx_bytes;
0184     padapter->stats.rx_bytes = precvpriv->rx_bytes;
0185     return &padapter->stats;
0186 }
0187 
0188 static const struct net_device_ops rtl8712_netdev_ops = {
0189     .ndo_open = netdev_open,
0190     .ndo_stop = netdev_close,
0191     .ndo_start_xmit = r8712_xmit_entry,
0192     .ndo_set_mac_address = r871x_net_set_mac_address,
0193     .ndo_get_stats = r871x_net_get_stats,
0194     .ndo_do_ioctl = r871x_ioctl,
0195 };
0196 
0197 struct net_device *r8712_init_netdev(void)
0198 {
0199     struct _adapter *padapter;
0200     struct net_device *pnetdev;
0201 
0202     pnetdev = alloc_etherdev(sizeof(struct _adapter));
0203     if (!pnetdev)
0204         return NULL;
0205     if (dev_alloc_name(pnetdev, ifname) < 0) {
0206         strscpy(ifname, "wlan%d", sizeof(ifname));
0207         dev_alloc_name(pnetdev, ifname);
0208     }
0209     padapter = netdev_priv(pnetdev);
0210     padapter->pnetdev = pnetdev;
0211     pr_info("r8712u: register rtl8712_netdev_ops to netdev_ops\n");
0212     pnetdev->netdev_ops = &rtl8712_netdev_ops;
0213     pnetdev->watchdog_timeo = HZ; /* 1 second timeout */
0214     pnetdev->wireless_handlers = (struct iw_handler_def *)
0215                      &r871x_handlers_def;
0216     loadparam(padapter, pnetdev);
0217     netif_carrier_off(pnetdev);
0218     padapter->pid = 0;  /* Initial the PID value used for HW PBC.*/
0219     return pnetdev;
0220 }
0221 
0222 static u32 start_drv_threads(struct _adapter *padapter)
0223 {
0224     padapter->cmd_thread = kthread_run(r8712_cmd_thread, padapter, "%s",
0225                       padapter->pnetdev->name);
0226     if (IS_ERR(padapter->cmd_thread))
0227         return _FAIL;
0228     return _SUCCESS;
0229 }
0230 
0231 void r8712_stop_drv_threads(struct _adapter *padapter)
0232 {
0233     struct completion *completion =
0234         &padapter->cmdpriv.terminate_cmdthread_comp;
0235 
0236     /*Below is to terminate r8712_cmd_thread & event_thread...*/
0237     complete(&padapter->cmdpriv.cmd_queue_comp);
0238     if (padapter->cmd_thread)
0239         wait_for_completion_interruptible(completion);
0240     padapter->cmdpriv.cmd_seq = 1;
0241 }
0242 
0243 static void start_drv_timers(struct _adapter *padapter)
0244 {
0245     mod_timer(&padapter->mlmepriv.sitesurveyctrl.sitesurvey_ctrl_timer,
0246           jiffies + msecs_to_jiffies(5000));
0247     mod_timer(&padapter->mlmepriv.wdg_timer,
0248           jiffies + msecs_to_jiffies(2000));
0249 }
0250 
0251 void r8712_stop_drv_timers(struct _adapter *padapter)
0252 {
0253     del_timer_sync(&padapter->mlmepriv.assoc_timer);
0254     del_timer_sync(&padapter->securitypriv.tkip_timer);
0255     del_timer_sync(&padapter->mlmepriv.scan_to_timer);
0256     del_timer_sync(&padapter->mlmepriv.dhcp_timer);
0257     del_timer_sync(&padapter->mlmepriv.wdg_timer);
0258     del_timer_sync(&padapter->mlmepriv.sitesurveyctrl.sitesurvey_ctrl_timer);
0259 }
0260 
0261 static void init_default_value(struct _adapter *padapter)
0262 {
0263     struct registry_priv *pregistrypriv = &padapter->registrypriv;
0264     struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
0265     struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
0266     struct security_priv *psecuritypriv = &padapter->securitypriv;
0267 
0268     /*xmit_priv*/
0269     pxmitpriv->vcs_setting = pregistrypriv->vrtl_carrier_sense;
0270     pxmitpriv->vcs = pregistrypriv->vcs_type;
0271     pxmitpriv->vcs_type = pregistrypriv->vcs_type;
0272     pxmitpriv->rts_thresh = pregistrypriv->rts_thresh;
0273     pxmitpriv->frag_len = pregistrypriv->frag_thresh;
0274     /* mlme_priv */
0275     /* Maybe someday we should rename this variable to "active_mode"(Jeff)*/
0276     pmlmepriv->passive_mode = 1; /* 1: active, 0: passive. */
0277     /*ht_priv*/
0278     {
0279         int i;
0280         struct ht_priv   *phtpriv = &pmlmepriv->htpriv;
0281 
0282         phtpriv->ampdu_enable = false;/*set to disabled*/
0283         for (i = 0; i < 16; i++)
0284             phtpriv->baddbareq_issued[i] = false;
0285     }
0286     /*security_priv*/
0287     psecuritypriv->sw_encrypt = pregistrypriv->software_encrypt;
0288     psecuritypriv->sw_decrypt = pregistrypriv->software_decrypt;
0289     psecuritypriv->binstallGrpkey = _FAIL;
0290     /*pwrctrl_priv*/
0291     /*registry_priv*/
0292     r8712_init_registrypriv_dev_network(padapter);
0293     r8712_update_registrypriv_dev_network(padapter);
0294     /*misc.*/
0295 }
0296 
0297 int r8712_init_drv_sw(struct _adapter *padapter)
0298 {
0299     int ret;
0300 
0301     ret = r8712_init_cmd_priv(&padapter->cmdpriv);
0302     if (ret)
0303         return ret;
0304     padapter->cmdpriv.padapter = padapter;
0305     ret = r8712_init_evt_priv(&padapter->evtpriv);
0306     if (ret)
0307         return ret;
0308     ret = r8712_init_mlme_priv(padapter);
0309     if (ret)
0310         return ret;
0311     _r8712_init_xmit_priv(&padapter->xmitpriv, padapter);
0312     _r8712_init_recv_priv(&padapter->recvpriv, padapter);
0313     memset((unsigned char *)&padapter->securitypriv, 0,
0314            sizeof(struct security_priv));
0315     timer_setup(&padapter->securitypriv.tkip_timer,
0316             r8712_use_tkipkey_handler, 0);
0317     ret = _r8712_init_sta_priv(&padapter->stapriv);
0318     if (ret)
0319         return ret;
0320     padapter->stapriv.padapter = padapter;
0321     r8712_init_bcmc_stainfo(padapter);
0322     r8712_init_pwrctrl_priv(padapter);
0323     mp871xinit(padapter);
0324     init_default_value(padapter);
0325     r8712_InitSwLeds(padapter);
0326     return ret;
0327 }
0328 
0329 void r8712_free_drv_sw(struct _adapter *padapter)
0330 {
0331     r8712_free_cmd_priv(&padapter->cmdpriv);
0332     r8712_free_evt_priv(&padapter->evtpriv);
0333     r8712_DeInitSwLeds(padapter);
0334     r8712_free_mlme_priv(&padapter->mlmepriv);
0335     _free_xmit_priv(&padapter->xmitpriv);
0336     _r8712_free_sta_priv(&padapter->stapriv);
0337     _r8712_free_recv_priv(&padapter->recvpriv);
0338     mp871xdeinit(padapter);
0339 }
0340 
0341 static void enable_video_mode(struct _adapter *padapter, int cbw40_value)
0342 {
0343     /*   bit 8:
0344      *   1 -> enable video mode to 96B AP
0345      *   0 -> disable video mode to 96B AP
0346      *   bit 9:
0347      *   1 -> enable 40MHz mode
0348      *   0 -> disable 40MHz mode
0349      *   bit 10:
0350      *   1 -> enable STBC
0351      *   0 -> disable STBC
0352      */
0353     u32  intcmd = 0xf4000500;   /* enable bit8, bit10*/
0354 
0355     if (cbw40_value) {
0356         /* if the driver supports the 40M bandwidth,
0357          * we can enable the bit 9.
0358          */
0359         intcmd |= 0x200;
0360     }
0361     r8712_fw_cmd(padapter, intcmd);
0362 }
0363 
0364 /*
0365  *
0366  * This function intends to handle the activation of an interface
0367  * i.e. when it is brought Up/Active from a Down state.
0368  *
0369  */
0370 static int netdev_open(struct net_device *pnetdev)
0371 {
0372     struct _adapter *padapter = netdev_priv(pnetdev);
0373 
0374     mutex_lock(&padapter->mutex_start);
0375     if (!padapter->bup) {
0376         padapter->driver_stopped = false;
0377         padapter->surprise_removed = false;
0378         padapter->bup = true;
0379         if (rtl871x_hal_init(padapter) != _SUCCESS)
0380             goto netdev_open_error;
0381         if (!r8712_initmac) {
0382             /* Use the mac address stored in the Efuse */
0383             eth_hw_addr_set(pnetdev,
0384                     padapter->eeprompriv.mac_addr);
0385         } else {
0386             /* We have to inform f/w to use user-supplied MAC
0387              * address.
0388              */
0389             msleep(200);
0390             r8712_setMacAddr_cmd(padapter,
0391                          (const u8 *)pnetdev->dev_addr);
0392             /*
0393              * The "myid" function will get the wifi mac address
0394              * from eeprompriv structure instead of netdev
0395              * structure. So, we have to overwrite the mac_addr
0396              * stored in the eeprompriv structure. In this case,
0397              * the real mac address won't be used anymore. So that,
0398              * the eeprompriv.mac_addr should store the mac which
0399              * users specify.
0400              */
0401             memcpy(padapter->eeprompriv.mac_addr,
0402                    pnetdev->dev_addr, ETH_ALEN);
0403         }
0404         if (start_drv_threads(padapter) != _SUCCESS)
0405             goto netdev_open_error;
0406         if (!padapter->dvobjpriv.inirp_init)
0407             goto netdev_open_error;
0408         else
0409             padapter->dvobjpriv.inirp_init(padapter);
0410         r8712_set_ps_mode(padapter, padapter->registrypriv.power_mgnt,
0411                   padapter->registrypriv.smart_ps);
0412     }
0413     if (!netif_queue_stopped(pnetdev))
0414         netif_start_queue(pnetdev);
0415     else
0416         netif_wake_queue(pnetdev);
0417 
0418     if (video_mode)
0419         enable_video_mode(padapter, cbw40_enable);
0420     /* start driver mlme relation timer */
0421     start_drv_timers(padapter);
0422     padapter->ledpriv.LedControlHandler(padapter, LED_CTL_NO_LINK);
0423     mutex_unlock(&padapter->mutex_start);
0424     return 0;
0425 netdev_open_error:
0426     padapter->bup = false;
0427     netif_carrier_off(pnetdev);
0428     netif_stop_queue(pnetdev);
0429     mutex_unlock(&padapter->mutex_start);
0430     return -1;
0431 }
0432 
0433 /*
0434  *
0435  * This function intends to handle the shutdown of an interface
0436  * i.e. when it is brought Down from an Up/Active state.
0437  *
0438  */
0439 static int netdev_close(struct net_device *pnetdev)
0440 {
0441     struct _adapter *padapter = netdev_priv(pnetdev);
0442 
0443     /* Close LED*/
0444     padapter->ledpriv.LedControlHandler(padapter, LED_CTL_POWER_OFF);
0445     msleep(200);
0446 
0447     /*s1.*/
0448     if (pnetdev) {
0449         if (!netif_queue_stopped(pnetdev))
0450             netif_stop_queue(pnetdev);
0451     }
0452     /*s2.*/
0453     /*s2-1.  issue disassoc_cmd to fw*/
0454     r8712_disassoc_cmd(padapter);
0455     /*s2-2.  indicate disconnect to os*/
0456     r8712_ind_disconnect(padapter);
0457     /*s2-3.*/
0458     r8712_free_assoc_resources(padapter);
0459     /*s2-4.*/
0460     r8712_free_network_queue(padapter);
0461     return 0;
0462 }
0463 
0464 #include "mlme_osdep.h"