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