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 		goto free_cmd;
308 	ret = r8712_init_mlme_priv(padapter);
309 	if (ret)
310 		goto free_evt;
311 	ret = _r8712_init_xmit_priv(&padapter->xmitpriv, padapter);
312 	if (ret)
313 		goto free_mlme;
314 	ret = _r8712_init_recv_priv(&padapter->recvpriv, padapter);
315 	if (ret)
316 		goto free_xmit;
317 	memset((unsigned char *)&padapter->securitypriv, 0,
318 	       sizeof(struct security_priv));
319 	timer_setup(&padapter->securitypriv.tkip_timer,
320 		    r8712_use_tkipkey_handler, 0);
321 	ret = _r8712_init_sta_priv(&padapter->stapriv);
322 	if (ret)
323 		goto free_recv;
324 	padapter->stapriv.padapter = padapter;
325 	r8712_init_bcmc_stainfo(padapter);
326 	r8712_init_pwrctrl_priv(padapter);
327 	mp871xinit(padapter);
328 	init_default_value(padapter);
329 	r8712_InitSwLeds(padapter);
330 	mutex_init(&padapter->mutex_start);
331 
332 	return 0;
333 
334 free_recv:
335 	_r8712_free_recv_priv(&padapter->recvpriv);
336 free_xmit:
337 	_free_xmit_priv(&padapter->xmitpriv);
338 free_mlme:
339 	r8712_free_mlme_priv(&padapter->mlmepriv);
340 free_evt:
341 	r8712_free_evt_priv(&padapter->evtpriv);
342 free_cmd:
343 	r8712_free_cmd_priv(&padapter->cmdpriv);
344 	return ret;
345 }
346 
r8712_free_drv_sw(struct _adapter * padapter)347 void r8712_free_drv_sw(struct _adapter *padapter)
348 {
349 	r8712_free_cmd_priv(&padapter->cmdpriv);
350 	r8712_free_evt_priv(&padapter->evtpriv);
351 	r8712_DeInitSwLeds(padapter);
352 	r8712_free_mlme_priv(&padapter->mlmepriv);
353 	_free_xmit_priv(&padapter->xmitpriv);
354 	_r8712_free_sta_priv(&padapter->stapriv);
355 	_r8712_free_recv_priv(&padapter->recvpriv);
356 	mp871xdeinit(padapter);
357 }
358 
enable_video_mode(struct _adapter * padapter,int cbw40_value)359 static void enable_video_mode(struct _adapter *padapter, int cbw40_value)
360 {
361 	/*   bit 8:
362 	 *   1 -> enable video mode to 96B AP
363 	 *   0 -> disable video mode to 96B AP
364 	 *   bit 9:
365 	 *   1 -> enable 40MHz mode
366 	 *   0 -> disable 40MHz mode
367 	 *   bit 10:
368 	 *   1 -> enable STBC
369 	 *   0 -> disable STBC
370 	 */
371 	u32  intcmd = 0xf4000500;   /* enable bit8, bit10*/
372 
373 	if (cbw40_value) {
374 		/* if the driver supports the 40M bandwidth,
375 		 * we can enable the bit 9.
376 		 */
377 		intcmd |= 0x200;
378 	}
379 	r8712_fw_cmd(padapter, intcmd);
380 }
381 
382 /*
383  *
384  * This function intends to handle the activation of an interface
385  * i.e. when it is brought Up/Active from a Down state.
386  *
387  */
netdev_open(struct net_device * pnetdev)388 static int netdev_open(struct net_device *pnetdev)
389 {
390 	struct _adapter *padapter = netdev_priv(pnetdev);
391 
392 	mutex_lock(&padapter->mutex_start);
393 	if (!padapter->bup) {
394 		padapter->driver_stopped = false;
395 		padapter->surprise_removed = false;
396 		padapter->bup = true;
397 		if (rtl871x_hal_init(padapter) != _SUCCESS)
398 			goto netdev_open_error;
399 		if (!r8712_initmac) {
400 			/* Use the mac address stored in the Efuse */
401 			eth_hw_addr_set(pnetdev,
402 					padapter->eeprompriv.mac_addr);
403 		} else {
404 			/* We have to inform f/w to use user-supplied MAC
405 			 * address.
406 			 */
407 			msleep(200);
408 			r8712_setMacAddr_cmd(padapter,
409 					     (const u8 *)pnetdev->dev_addr);
410 			/*
411 			 * The "myid" function will get the wifi mac address
412 			 * from eeprompriv structure instead of netdev
413 			 * structure. So, we have to overwrite the mac_addr
414 			 * stored in the eeprompriv structure. In this case,
415 			 * the real mac address won't be used anymore. So that,
416 			 * the eeprompriv.mac_addr should store the mac which
417 			 * users specify.
418 			 */
419 			memcpy(padapter->eeprompriv.mac_addr,
420 			       pnetdev->dev_addr, ETH_ALEN);
421 		}
422 		if (start_drv_threads(padapter) != _SUCCESS)
423 			goto netdev_open_error;
424 		if (!padapter->dvobjpriv.inirp_init)
425 			goto netdev_open_error;
426 		else
427 			padapter->dvobjpriv.inirp_init(padapter);
428 		r8712_set_ps_mode(padapter, padapter->registrypriv.power_mgnt,
429 				  padapter->registrypriv.smart_ps);
430 	}
431 	if (!netif_queue_stopped(pnetdev))
432 		netif_start_queue(pnetdev);
433 	else
434 		netif_wake_queue(pnetdev);
435 
436 	if (video_mode)
437 		enable_video_mode(padapter, cbw40_enable);
438 	/* start driver mlme relation timer */
439 	start_drv_timers(padapter);
440 	padapter->ledpriv.LedControlHandler(padapter, LED_CTL_NO_LINK);
441 	mutex_unlock(&padapter->mutex_start);
442 	return 0;
443 netdev_open_error:
444 	padapter->bup = false;
445 	netif_carrier_off(pnetdev);
446 	netif_stop_queue(pnetdev);
447 	mutex_unlock(&padapter->mutex_start);
448 	return -1;
449 }
450 
451 /*
452  *
453  * This function intends to handle the shutdown of an interface
454  * i.e. when it is brought Down from an Up/Active state.
455  *
456  */
netdev_close(struct net_device * pnetdev)457 static int netdev_close(struct net_device *pnetdev)
458 {
459 	struct _adapter *padapter = netdev_priv(pnetdev);
460 
461 	/* Close LED*/
462 	padapter->ledpriv.LedControlHandler(padapter, LED_CTL_POWER_OFF);
463 	msleep(200);
464 
465 	/*s1.*/
466 	if (pnetdev) {
467 		if (!netif_queue_stopped(pnetdev))
468 			netif_stop_queue(pnetdev);
469 	}
470 	/*s2.*/
471 	/*s2-1.  issue disassoc_cmd to fw*/
472 	r8712_disassoc_cmd(padapter);
473 	/*s2-2.  indicate disconnect to os*/
474 	r8712_ind_disconnect(padapter);
475 	/*s2-3.*/
476 	r8712_free_assoc_resources(padapter);
477 	/*s2-4.*/
478 	r8712_free_network_queue(padapter);
479 	return 0;
480 }
481 
482 #include "mlme_osdep.h"
483