1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  * os_intfs.c
4  *
5  * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
6  * Linux device driver for RTL8192SU
7  *
8  * Modifications for inclusion into the Linux staging tree are
9  * Copyright(c) 2010 Larry Finger. All rights reserved.
10  *
11  * Contact information:
12  * WLAN FAE <wlanfae@realtek.com>.
13  * Larry Finger <Larry.Finger@lwfinger.net>
14  *
15  ******************************************************************************/
16 
17 #define _OS_INTFS_C_
18 
19 #include <linux/module.h>
20 #include <linux/kthread.h>
21 #include <linux/firmware.h>
22 #include "osdep_service.h"
23 #include "drv_types.h"
24 #include "xmit_osdep.h"
25 #include "recv_osdep.h"
26 #include "rtl871x_ioctl.h"
27 #include "usb_osintf.h"
28 
29 MODULE_LICENSE("GPL");
30 MODULE_DESCRIPTION("rtl871x wireless lan driver");
31 MODULE_AUTHOR("Larry Finger");
32 
33 static char ifname[IFNAMSIZ] = "wlan%d";
34 
35 /* module param defaults */
36 static int chip_version = RTL8712_2ndCUT;
37 static int rfintfs = HWPI;
38 static int lbkmode = RTL8712_AIR_TRX;
39 static int hci = RTL8712_USB;
40 static int ampdu_enable = 1;/*for enable tx_ampdu*/
41 
42 /* The video_mode variable is for video mode.*/
43 /* It may be specify when inserting module with video_mode=1 parameter.*/
44 static int video_mode = 1;   /* enable video mode*/
45 
46 /*Ndis802_11Infrastructure; infra, ad-hoc, auto*/
47 static int network_mode = Ndis802_11IBSS;
48 static int channel = 1;/*ad-hoc support requirement*/
49 static int wireless_mode = WIRELESS_11BG;
50 static int vrtl_carrier_sense = AUTO_VCS;
51 static int vcs_type = RTS_CTS;
52 static int frag_thresh = 2346;
53 static int preamble = PREAMBLE_LONG;/*long, short, auto*/
54 static int scan_mode = 1;/*active, passive*/
55 static int adhoc_tx_pwr = 1;
56 static int soft_ap;
57 static int smart_ps = 1;
58 static int power_mgnt = PS_MODE_ACTIVE;
59 static int radio_enable = 1;
60 static int long_retry_lmt = 7;
61 static int short_retry_lmt = 7;
62 static int busy_thresh = 40;
63 static int ack_policy = NORMAL_ACK;
64 static int mp_mode;
65 static int software_encrypt;
66 static int software_decrypt;
67 
68 static int wmm_enable;/* default is set to disable the wmm.*/
69 static int uapsd_enable;
70 static int uapsd_max_sp = NO_LIMIT;
71 static int uapsd_acbk_en;
72 static int uapsd_acbe_en;
73 static int uapsd_acvi_en;
74 static int uapsd_acvo_en;
75 
76 static int ht_enable = 1;
77 static int cbw40_enable = 1;
78 static int rf_config = RTL8712_RF_1T2R;  /* 1T2R*/
79 static int low_power;
80 /* mac address to use instead of the one stored in Efuse */
81 char *r8712_initmac;
82 static char *initmac;
83 /* if wifi_test = 1, driver will disable the turbo mode and pass it to
84  * firmware private.
85  */
86 static int wifi_test;
87 
88 module_param_string(ifname, ifname, sizeof(ifname), 0644);
89 module_param(wifi_test, int, 0644);
90 module_param(initmac, charp, 0644);
91 module_param(video_mode, int, 0644);
92 module_param(chip_version, int, 0644);
93 module_param(rfintfs, int, 0644);
94 module_param(lbkmode, int, 0644);
95 module_param(hci, int, 0644);
96 module_param(network_mode, int, 0644);
97 module_param(channel, int, 0644);
98 module_param(mp_mode, int, 0644);
99 module_param(wmm_enable, int, 0644);
100 module_param(vrtl_carrier_sense, int, 0644);
101 module_param(vcs_type, int, 0644);
102 module_param(busy_thresh, int, 0644);
103 module_param(ht_enable, int, 0644);
104 module_param(cbw40_enable, int, 0644);
105 module_param(ampdu_enable, int, 0644);
106 module_param(rf_config, int, 0644);
107 module_param(power_mgnt, int, 0644);
108 module_param(low_power, int, 0644);
109 
110 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
111 MODULE_PARM_DESC(initmac, "MAC-Address, default: use FUSE");
112 
113 static int netdev_open(struct net_device *pnetdev);
114 static int netdev_close(struct net_device *pnetdev);
115 
loadparam(struct _adapter * padapter,struct net_device * pnetdev)116 static void loadparam(struct _adapter *padapter, struct  net_device *pnetdev)
117 {
118 	struct registry_priv  *registry_par = &padapter->registrypriv;
119 
120 	registry_par->chip_version = (u8)chip_version;
121 	registry_par->rfintfs = (u8)rfintfs;
122 	registry_par->lbkmode = (u8)lbkmode;
123 	registry_par->hci = (u8)hci;
124 	registry_par->network_mode  = (u8)network_mode;
125 	memcpy(registry_par->ssid.Ssid, "ANY", 3);
126 	registry_par->ssid.SsidLength = 3;
127 	registry_par->channel = (u8)channel;
128 	registry_par->wireless_mode = (u8)wireless_mode;
129 	registry_par->vrtl_carrier_sense = (u8)vrtl_carrier_sense;
130 	registry_par->vcs_type = (u8)vcs_type;
131 	registry_par->frag_thresh = (u16)frag_thresh;
132 	registry_par->preamble = (u8)preamble;
133 	registry_par->scan_mode = (u8)scan_mode;
134 	registry_par->adhoc_tx_pwr = (u8)adhoc_tx_pwr;
135 	registry_par->soft_ap = (u8)soft_ap;
136 	registry_par->smart_ps = (u8)smart_ps;
137 	registry_par->power_mgnt = (u8)power_mgnt;
138 	registry_par->radio_enable = (u8)radio_enable;
139 	registry_par->long_retry_lmt = (u8)long_retry_lmt;
140 	registry_par->short_retry_lmt = (u8)short_retry_lmt;
141 	registry_par->busy_thresh = (u16)busy_thresh;
142 	registry_par->ack_policy = (u8)ack_policy;
143 	registry_par->mp_mode = (u8)mp_mode;
144 	registry_par->software_encrypt = (u8)software_encrypt;
145 	registry_par->software_decrypt = (u8)software_decrypt;
146 	/*UAPSD*/
147 	registry_par->wmm_enable = (u8)wmm_enable;
148 	registry_par->uapsd_enable = (u8)uapsd_enable;
149 	registry_par->uapsd_max_sp = (u8)uapsd_max_sp;
150 	registry_par->uapsd_acbk_en = (u8)uapsd_acbk_en;
151 	registry_par->uapsd_acbe_en = (u8)uapsd_acbe_en;
152 	registry_par->uapsd_acvi_en = (u8)uapsd_acvi_en;
153 	registry_par->uapsd_acvo_en = (u8)uapsd_acvo_en;
154 	registry_par->ht_enable = (u8)ht_enable;
155 	registry_par->cbw40_enable = (u8)cbw40_enable;
156 	registry_par->ampdu_enable = (u8)ampdu_enable;
157 	registry_par->rf_config = (u8)rf_config;
158 	registry_par->low_power = (u8)low_power;
159 	registry_par->wifi_test = (u8)wifi_test;
160 	r8712_initmac = initmac;
161 }
162 
r871x_net_set_mac_address(struct net_device * pnetdev,void * p)163 static int r871x_net_set_mac_address(struct net_device *pnetdev, void *p)
164 {
165 	struct _adapter *padapter = netdev_priv(pnetdev);
166 	struct sockaddr *addr = p;
167 
168 	if (!padapter->bup)
169 		eth_hw_addr_set(pnetdev, addr->sa_data);
170 	return 0;
171 }
172 
r871x_net_get_stats(struct net_device * pnetdev)173 static struct net_device_stats *r871x_net_get_stats(struct net_device *pnetdev)
174 {
175 	struct _adapter *padapter = netdev_priv(pnetdev);
176 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
177 	struct recv_priv *precvpriv = &padapter->recvpriv;
178 
179 	padapter->stats.tx_packets = pxmitpriv->tx_pkts;
180 	padapter->stats.rx_packets = precvpriv->rx_pkts;
181 	padapter->stats.tx_dropped = pxmitpriv->tx_drop;
182 	padapter->stats.rx_dropped = precvpriv->rx_drop;
183 	padapter->stats.tx_bytes = pxmitpriv->tx_bytes;
184 	padapter->stats.rx_bytes = precvpriv->rx_bytes;
185 	return &padapter->stats;
186 }
187 
188 static const struct net_device_ops rtl8712_netdev_ops = {
189 	.ndo_open = netdev_open,
190 	.ndo_stop = netdev_close,
191 	.ndo_start_xmit = r8712_xmit_entry,
192 	.ndo_set_mac_address = r871x_net_set_mac_address,
193 	.ndo_get_stats = r871x_net_get_stats,
194 	.ndo_do_ioctl = r871x_ioctl,
195 };
196 
r8712_init_netdev(void)197 struct net_device *r8712_init_netdev(void)
198 {
199 	struct _adapter *padapter;
200 	struct net_device *pnetdev;
201 
202 	pnetdev = alloc_etherdev(sizeof(struct _adapter));
203 	if (!pnetdev)
204 		return NULL;
205 	if (dev_alloc_name(pnetdev, ifname) < 0) {
206 		strscpy(ifname, "wlan%d", sizeof(ifname));
207 		dev_alloc_name(pnetdev, ifname);
208 	}
209 	padapter = netdev_priv(pnetdev);
210 	padapter->pnetdev = pnetdev;
211 	pr_info("r8712u: register rtl8712_netdev_ops to netdev_ops\n");
212 	pnetdev->netdev_ops = &rtl8712_netdev_ops;
213 	pnetdev->watchdog_timeo = HZ; /* 1 second timeout */
214 	pnetdev->wireless_handlers = (struct iw_handler_def *)
215 				     &r871x_handlers_def;
216 	loadparam(padapter, pnetdev);
217 	netif_carrier_off(pnetdev);
218 	padapter->pid = 0;  /* Initial the PID value used for HW PBC.*/
219 	return pnetdev;
220 }
221 
start_drv_threads(struct _adapter * padapter)222 static u32 start_drv_threads(struct _adapter *padapter)
223 {
224 	padapter->cmd_thread = kthread_run(r8712_cmd_thread, padapter, "%s",
225 					  padapter->pnetdev->name);
226 	if (IS_ERR(padapter->cmd_thread))
227 		return _FAIL;
228 	return _SUCCESS;
229 }
230 
r8712_stop_drv_threads(struct _adapter * padapter)231 void r8712_stop_drv_threads(struct _adapter *padapter)
232 {
233 	struct completion *completion =
234 		&padapter->cmdpriv.terminate_cmdthread_comp;
235 
236 	/*Below is to terminate r8712_cmd_thread & event_thread...*/
237 	complete(&padapter->cmdpriv.cmd_queue_comp);
238 	if (padapter->cmd_thread)
239 		wait_for_completion_interruptible(completion);
240 	padapter->cmdpriv.cmd_seq = 1;
241 }
242 
start_drv_timers(struct _adapter * padapter)243 static void start_drv_timers(struct _adapter *padapter)
244 {
245 	mod_timer(&padapter->mlmepriv.sitesurveyctrl.sitesurvey_ctrl_timer,
246 		  jiffies + msecs_to_jiffies(5000));
247 	mod_timer(&padapter->mlmepriv.wdg_timer,
248 		  jiffies + msecs_to_jiffies(2000));
249 }
250 
r8712_stop_drv_timers(struct _adapter * padapter)251 void r8712_stop_drv_timers(struct _adapter *padapter)
252 {
253 	del_timer_sync(&padapter->mlmepriv.assoc_timer);
254 	del_timer_sync(&padapter->securitypriv.tkip_timer);
255 	del_timer_sync(&padapter->mlmepriv.scan_to_timer);
256 	del_timer_sync(&padapter->mlmepriv.dhcp_timer);
257 	del_timer_sync(&padapter->mlmepriv.wdg_timer);
258 	del_timer_sync(&padapter->mlmepriv.sitesurveyctrl.sitesurvey_ctrl_timer);
259 }
260 
init_default_value(struct _adapter * padapter)261 static void init_default_value(struct _adapter *padapter)
262 {
263 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
264 	struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
265 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
266 	struct security_priv *psecuritypriv = &padapter->securitypriv;
267 
268 	/*xmit_priv*/
269 	pxmitpriv->vcs_setting = pregistrypriv->vrtl_carrier_sense;
270 	pxmitpriv->vcs = pregistrypriv->vcs_type;
271 	pxmitpriv->vcs_type = pregistrypriv->vcs_type;
272 	pxmitpriv->rts_thresh = pregistrypriv->rts_thresh;
273 	pxmitpriv->frag_len = pregistrypriv->frag_thresh;
274 	/* mlme_priv */
275 	/* Maybe someday we should rename this variable to "active_mode"(Jeff)*/
276 	pmlmepriv->passive_mode = 1; /* 1: active, 0: passive. */
277 	/*ht_priv*/
278 	{
279 		int i;
280 		struct ht_priv	 *phtpriv = &pmlmepriv->htpriv;
281 
282 		phtpriv->ampdu_enable = false;/*set to disabled*/
283 		for (i = 0; i < 16; i++)
284 			phtpriv->baddbareq_issued[i] = false;
285 	}
286 	/*security_priv*/
287 	psecuritypriv->sw_encrypt = pregistrypriv->software_encrypt;
288 	psecuritypriv->sw_decrypt = pregistrypriv->software_decrypt;
289 	psecuritypriv->binstallGrpkey = _FAIL;
290 	/*pwrctrl_priv*/
291 	/*registry_priv*/
292 	r8712_init_registrypriv_dev_network(padapter);
293 	r8712_update_registrypriv_dev_network(padapter);
294 	/*misc.*/
295 }
296 
r8712_init_drv_sw(struct _adapter * padapter)297 int r8712_init_drv_sw(struct _adapter *padapter)
298 {
299 	int ret;
300 
301 	ret = r8712_init_cmd_priv(&padapter->cmdpriv);
302 	if (ret)
303 		return ret;
304 	padapter->cmdpriv.padapter = padapter;
305 	ret = r8712_init_evt_priv(&padapter->evtpriv);
306 	if (ret)
307 		return ret;
308 	ret = r8712_init_mlme_priv(padapter);
309 	if (ret)
310 		return ret;
311 	_r8712_init_xmit_priv(&padapter->xmitpriv, padapter);
312 	_r8712_init_recv_priv(&padapter->recvpriv, padapter);
313 	memset((unsigned char *)&padapter->securitypriv, 0,
314 	       sizeof(struct security_priv));
315 	timer_setup(&padapter->securitypriv.tkip_timer,
316 		    r8712_use_tkipkey_handler, 0);
317 	ret = _r8712_init_sta_priv(&padapter->stapriv);
318 	if (ret)
319 		return ret;
320 	padapter->stapriv.padapter = padapter;
321 	r8712_init_bcmc_stainfo(padapter);
322 	r8712_init_pwrctrl_priv(padapter);
323 	mp871xinit(padapter);
324 	init_default_value(padapter);
325 	r8712_InitSwLeds(padapter);
326 	return ret;
327 }
328 
r8712_free_drv_sw(struct _adapter * padapter)329 void r8712_free_drv_sw(struct _adapter *padapter)
330 {
331 	r8712_free_cmd_priv(&padapter->cmdpriv);
332 	r8712_free_evt_priv(&padapter->evtpriv);
333 	r8712_DeInitSwLeds(padapter);
334 	r8712_free_mlme_priv(&padapter->mlmepriv);
335 	r8712_free_io_queue(padapter);
336 	_free_xmit_priv(&padapter->xmitpriv);
337 	_r8712_free_sta_priv(&padapter->stapriv);
338 	_r8712_free_recv_priv(&padapter->recvpriv);
339 	mp871xdeinit(padapter);
340 }
341 
enable_video_mode(struct _adapter * padapter,int cbw40_value)342 static void enable_video_mode(struct _adapter *padapter, int cbw40_value)
343 {
344 	/*   bit 8:
345 	 *   1 -> enable video mode to 96B AP
346 	 *   0 -> disable video mode to 96B AP
347 	 *   bit 9:
348 	 *   1 -> enable 40MHz mode
349 	 *   0 -> disable 40MHz mode
350 	 *   bit 10:
351 	 *   1 -> enable STBC
352 	 *   0 -> disable STBC
353 	 */
354 	u32  intcmd = 0xf4000500;   /* enable bit8, bit10*/
355 
356 	if (cbw40_value) {
357 		/* if the driver supports the 40M bandwidth,
358 		 * we can enable the bit 9.
359 		 */
360 		intcmd |= 0x200;
361 	}
362 	r8712_fw_cmd(padapter, intcmd);
363 }
364 
365 /*
366  *
367  * This function intends to handle the activation of an interface
368  * i.e. when it is brought Up/Active from a Down state.
369  *
370  */
netdev_open(struct net_device * pnetdev)371 static int netdev_open(struct net_device *pnetdev)
372 {
373 	struct _adapter *padapter = netdev_priv(pnetdev);
374 
375 	mutex_lock(&padapter->mutex_start);
376 	if (!padapter->bup) {
377 		padapter->driver_stopped = false;
378 		padapter->surprise_removed = false;
379 		padapter->bup = true;
380 		if (rtl871x_hal_init(padapter) != _SUCCESS)
381 			goto netdev_open_error;
382 		if (!r8712_initmac) {
383 			/* Use the mac address stored in the Efuse */
384 			eth_hw_addr_set(pnetdev,
385 					padapter->eeprompriv.mac_addr);
386 		} else {
387 			/* We have to inform f/w to use user-supplied MAC
388 			 * address.
389 			 */
390 			msleep(200);
391 			r8712_setMacAddr_cmd(padapter,
392 					     (const u8 *)pnetdev->dev_addr);
393 			/*
394 			 * The "myid" function will get the wifi mac address
395 			 * from eeprompriv structure instead of netdev
396 			 * structure. So, we have to overwrite the mac_addr
397 			 * stored in the eeprompriv structure. In this case,
398 			 * the real mac address won't be used anymore. So that,
399 			 * the eeprompriv.mac_addr should store the mac which
400 			 * users specify.
401 			 */
402 			memcpy(padapter->eeprompriv.mac_addr,
403 			       pnetdev->dev_addr, ETH_ALEN);
404 		}
405 		if (start_drv_threads(padapter) != _SUCCESS)
406 			goto netdev_open_error;
407 		if (!padapter->dvobjpriv.inirp_init)
408 			goto netdev_open_error;
409 		else
410 			padapter->dvobjpriv.inirp_init(padapter);
411 		r8712_set_ps_mode(padapter, padapter->registrypriv.power_mgnt,
412 				  padapter->registrypriv.smart_ps);
413 	}
414 	if (!netif_queue_stopped(pnetdev))
415 		netif_start_queue(pnetdev);
416 	else
417 		netif_wake_queue(pnetdev);
418 
419 	if (video_mode)
420 		enable_video_mode(padapter, cbw40_enable);
421 	/* start driver mlme relation timer */
422 	start_drv_timers(padapter);
423 	padapter->ledpriv.LedControlHandler(padapter, LED_CTL_NO_LINK);
424 	mutex_unlock(&padapter->mutex_start);
425 	return 0;
426 netdev_open_error:
427 	padapter->bup = false;
428 	netif_carrier_off(pnetdev);
429 	netif_stop_queue(pnetdev);
430 	mutex_unlock(&padapter->mutex_start);
431 	return -1;
432 }
433 
434 /*
435  *
436  * This function intends to handle the shutdown of an interface
437  * i.e. when it is brought Down from an Up/Active state.
438  *
439  */
netdev_close(struct net_device * pnetdev)440 static int netdev_close(struct net_device *pnetdev)
441 {
442 	struct _adapter *padapter = netdev_priv(pnetdev);
443 
444 	/* Close LED*/
445 	padapter->ledpriv.LedControlHandler(padapter, LED_CTL_POWER_OFF);
446 	msleep(200);
447 
448 	/*s1.*/
449 	if (pnetdev) {
450 		if (!netif_queue_stopped(pnetdev))
451 			netif_stop_queue(pnetdev);
452 	}
453 	/*s2.*/
454 	/*s2-1.  issue disassoc_cmd to fw*/
455 	r8712_disassoc_cmd(padapter);
456 	/*s2-2.  indicate disconnect to os*/
457 	r8712_ind_disconnect(padapter);
458 	/*s2-3.*/
459 	r8712_free_assoc_resources(padapter);
460 	/*s2-4.*/
461 	r8712_free_network_queue(padapter);
462 	return 0;
463 }
464 
465 #include "mlme_osdep.h"
466