blob: 6e81ba0eaf1e5542346c7a04121333d4193aedc4 [file] [log] [blame]
Larry Finger2865d422010-08-20 10:15:30 -05001/******************************************************************************
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 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 *
20 * Modifications for inclusion into the Linux staging tree are
21 * Copyright(c) 2010 Larry Finger. All rights reserved.
22 *
23 * Contact information:
24 * WLAN FAE <wlanfae@realtek.com>.
25 * Larry Finger <Larry.Finger@lwfinger.net>
26 *
27 ******************************************************************************/
28
29#define _OS_INTFS_C_
30
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/kthread.h>
Larry Finger8c213fa592012-02-05 21:12:26 -060034#include <linux/firmware.h>
Larry Finger2865d422010-08-20 10:15:30 -050035#include "osdep_service.h"
36#include "drv_types.h"
37#include "xmit_osdep.h"
38#include "recv_osdep.h"
39#include "rtl871x_ioctl.h"
40#include "usb_osintf.h"
41
42MODULE_LICENSE("GPL");
43MODULE_DESCRIPTION("rtl871x wireless lan driver");
44MODULE_AUTHOR("Larry Finger");
45
46static char ifname[IFNAMSIZ] = "wlan%d";
47
48/* module param defaults */
49static int chip_version = RTL8712_2ndCUT;
50static int rfintfs = HWPI;
51static int lbkmode = RTL8712_AIR_TRX;
52static int hci = RTL8712_USB;
53static int ampdu_enable = 1;/*for enable tx_ampdu*/
54
Justin P. Mattockbe10ac22012-05-07 07:38:22 -070055/* The video_mode variable is for video mode.*/
Larry Finger2865d422010-08-20 10:15:30 -050056/* It may be specify when inserting module with video_mode=1 parameter.*/
57static int video_mode = 1; /* enable video mode*/
58
59/*Ndis802_11Infrastructure; infra, ad-hoc, auto*/
60static int network_mode = Ndis802_11IBSS;
61static int channel = 1;/*ad-hoc support requirement*/
62static int wireless_mode = WIRELESS_11BG;
63static int vrtl_carrier_sense = AUTO_VCS;
64static int vcs_type = RTS_CTS;
65static int frag_thresh = 2346;
66static int preamble = PREAMBLE_LONG;/*long, short, auto*/
67static int scan_mode = 1;/*active, passive*/
68static int adhoc_tx_pwr = 1;
69static int soft_ap;
70static int smart_ps = 1;
71static int power_mgnt = PS_MODE_ACTIVE;
72static int radio_enable = 1;
73static int long_retry_lmt = 7;
74static int short_retry_lmt = 7;
75static int busy_thresh = 40;
76static int ack_policy = NORMAL_ACK;
77static int mp_mode;
78static int software_encrypt;
79static int software_decrypt;
80
81static int wmm_enable;/* default is set to disable the wmm.*/
82static int uapsd_enable;
83static int uapsd_max_sp = NO_LIMIT;
84static int uapsd_acbk_en;
85static int uapsd_acbe_en;
86static int uapsd_acvi_en;
87static int uapsd_acvo_en;
88
89static int ht_enable = 1;
90static int cbw40_enable = 1;
91static int rf_config = RTL8712_RF_1T2R; /* 1T2R*/
92static int low_power;
93/* mac address to use instead of the one stored in Efuse */
94char *r8712_initmac;
95static char *initmac;
Larry Fingerd19b8642010-08-30 22:51:58 -050096/* if wifi_test = 1, driver will disable the turbo mode and pass it to
97 * firmware private.
98 */
Devendra Nagae1656642012-09-23 12:57:21 -040099static int wifi_test;
Larry Finger2865d422010-08-20 10:15:30 -0500100
101module_param_string(ifname, ifname, sizeof(ifname), S_IRUGO|S_IWUSR);
Larry Fingerd19b8642010-08-30 22:51:58 -0500102module_param(wifi_test, int, 0644);
Larry Finger2865d422010-08-20 10:15:30 -0500103module_param(initmac, charp, 0644);
104module_param(video_mode, int, 0644);
105module_param(chip_version, int, 0644);
106module_param(rfintfs, int, 0644);
107module_param(lbkmode, int, 0644);
108module_param(hci, int, 0644);
109module_param(network_mode, int, 0644);
110module_param(channel, int, 0644);
111module_param(mp_mode, int, 0644);
112module_param(wmm_enable, int, 0644);
113module_param(vrtl_carrier_sense, int, 0644);
114module_param(vcs_type, int, 0644);
115module_param(busy_thresh, int, 0644);
116module_param(ht_enable, int, 0644);
117module_param(cbw40_enable, int, 0644);
118module_param(ampdu_enable, int, 0644);
119module_param(rf_config, int, 0644);
120module_param(power_mgnt, int, 0644);
121module_param(low_power, int, 0644);
122
123MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
124MODULE_PARM_DESC(initmac, "MAC-Address, default: use FUSE");
125
126static uint loadparam(struct _adapter *padapter, struct net_device *pnetdev);
127static int netdev_open(struct net_device *pnetdev);
128static int netdev_close(struct net_device *pnetdev);
129
130static uint loadparam(struct _adapter *padapter, struct net_device *pnetdev)
131{
132 uint status = _SUCCESS;
133 struct registry_priv *registry_par = &padapter->registrypriv;
134
135 registry_par->chip_version = (u8)chip_version;
136 registry_par->rfintfs = (u8)rfintfs;
137 registry_par->lbkmode = (u8)lbkmode;
138 registry_par->hci = (u8)hci;
139 registry_par->network_mode = (u8)network_mode;
140 memcpy(registry_par->ssid.Ssid, "ANY", 3);
141 registry_par->ssid.SsidLength = 3;
142 registry_par->channel = (u8)channel;
143 registry_par->wireless_mode = (u8)wireless_mode;
144 registry_par->vrtl_carrier_sense = (u8)vrtl_carrier_sense ;
145 registry_par->vcs_type = (u8)vcs_type;
146 registry_par->frag_thresh = (u16)frag_thresh;
147 registry_par->preamble = (u8)preamble;
148 registry_par->scan_mode = (u8)scan_mode;
149 registry_par->adhoc_tx_pwr = (u8)adhoc_tx_pwr;
150 registry_par->soft_ap = (u8)soft_ap;
151 registry_par->smart_ps = (u8)smart_ps;
152 registry_par->power_mgnt = (u8)power_mgnt;
153 registry_par->radio_enable = (u8)radio_enable;
154 registry_par->long_retry_lmt = (u8)long_retry_lmt;
155 registry_par->short_retry_lmt = (u8)short_retry_lmt;
156 registry_par->busy_thresh = (u16)busy_thresh;
157 registry_par->ack_policy = (u8)ack_policy;
158 registry_par->mp_mode = (u8)mp_mode;
159 registry_par->software_encrypt = (u8)software_encrypt;
160 registry_par->software_decrypt = (u8)software_decrypt;
161 /*UAPSD*/
162 registry_par->wmm_enable = (u8)wmm_enable;
163 registry_par->uapsd_enable = (u8)uapsd_enable;
164 registry_par->uapsd_max_sp = (u8)uapsd_max_sp;
165 registry_par->uapsd_acbk_en = (u8)uapsd_acbk_en;
166 registry_par->uapsd_acbe_en = (u8)uapsd_acbe_en;
167 registry_par->uapsd_acvi_en = (u8)uapsd_acvi_en;
168 registry_par->uapsd_acvo_en = (u8)uapsd_acvo_en;
169 registry_par->ht_enable = (u8)ht_enable;
170 registry_par->cbw40_enable = (u8)cbw40_enable;
171 registry_par->ampdu_enable = (u8)ampdu_enable;
172 registry_par->rf_config = (u8)rf_config;
173 registry_par->low_power = (u8)low_power;
Larry Fingerd19b8642010-08-30 22:51:58 -0500174 registry_par->wifi_test = (u8) wifi_test;
Larry Finger2865d422010-08-20 10:15:30 -0500175 r8712_initmac = initmac;
176 return status;
177}
178
179static int r871x_net_set_mac_address(struct net_device *pnetdev, void *p)
180{
Ali Bahar7c1f4202011-09-04 03:14:05 +0800181 struct _adapter *padapter = (struct _adapter *)netdev_priv(pnetdev);
Larry Finger2865d422010-08-20 10:15:30 -0500182 struct sockaddr *addr = p;
183
184 if (padapter->bup == false)
185 memcpy(pnetdev->dev_addr, addr->sa_data, ETH_ALEN);
186 return 0;
187}
188
189static struct net_device_stats *r871x_net_get_stats(struct net_device *pnetdev)
190{
Ali Bahar7c1f4202011-09-04 03:14:05 +0800191 struct _adapter *padapter = (struct _adapter *) netdev_priv(pnetdev);
Larry Finger2865d422010-08-20 10:15:30 -0500192 struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
193 struct recv_priv *precvpriv = &(padapter->recvpriv);
194
195 padapter->stats.tx_packets = pxmitpriv->tx_pkts;
196 padapter->stats.rx_packets = precvpriv->rx_pkts;
197 padapter->stats.tx_dropped = pxmitpriv->tx_drop;
198 padapter->stats.rx_dropped = precvpriv->rx_drop;
199 padapter->stats.tx_bytes = pxmitpriv->tx_bytes;
200 padapter->stats.rx_bytes = precvpriv->rx_bytes;
201 return &padapter->stats;
202}
203
204static const struct net_device_ops rtl8712_netdev_ops = {
205 .ndo_open = netdev_open,
206 .ndo_stop = netdev_close,
207 .ndo_start_xmit = r8712_xmit_entry,
208 .ndo_set_mac_address = r871x_net_set_mac_address,
209 .ndo_get_stats = r871x_net_get_stats,
210 .ndo_do_ioctl = r871x_ioctl,
211};
212
213struct net_device *r8712_init_netdev(void)
214{
215 struct _adapter *padapter;
216 struct net_device *pnetdev;
217
218 pnetdev = alloc_etherdev(sizeof(struct _adapter));
219 if (!pnetdev)
220 return NULL;
221 if (dev_alloc_name(pnetdev, ifname) < 0) {
222 strcpy(ifname, "wlan%d");
223 dev_alloc_name(pnetdev, ifname);
224 }
Ali Bahar7c1f4202011-09-04 03:14:05 +0800225 padapter = (struct _adapter *) netdev_priv(pnetdev);
Larry Finger2865d422010-08-20 10:15:30 -0500226 padapter->pnetdev = pnetdev;
Przemo Firszt87a573a2012-12-10 23:21:21 +0000227 pr_info("r8712u: register rtl8712_netdev_ops to netdev_ops\n");
Larry Finger2865d422010-08-20 10:15:30 -0500228 pnetdev->netdev_ops = &rtl8712_netdev_ops;
229 pnetdev->watchdog_timeo = HZ; /* 1 second timeout */
230 pnetdev->wireless_handlers = (struct iw_handler_def *)
231 &r871x_handlers_def;
232 /*step 2.*/
233 loadparam(padapter, pnetdev);
234 netif_carrier_off(pnetdev);
235 padapter->pid = 0; /* Initial the PID value used for HW PBC.*/
236 return pnetdev;
237}
238
239static u32 start_drv_threads(struct _adapter *padapter)
240{
Kees Cookf1701682013-07-03 15:04:58 -0700241 padapter->cmdThread = kthread_run(r8712_cmd_thread, padapter, "%s",
Larry Finger2865d422010-08-20 10:15:30 -0500242 padapter->pnetdev->name);
243 if (IS_ERR(padapter->cmdThread) < 0)
244 return _FAIL;
245 return _SUCCESS;
246}
247
248void r8712_stop_drv_threads(struct _adapter *padapter)
249{
Justin P. Mattockbe10ac22012-05-07 07:38:22 -0700250 /*Below is to terminate r8712_cmd_thread & event_thread...*/
Larry Finger2865d422010-08-20 10:15:30 -0500251 up(&padapter->cmdpriv.cmd_queue_sema);
252 if (padapter->cmdThread)
253 _down_sema(&padapter->cmdpriv.terminate_cmdthread_sema);
254 padapter->cmdpriv.cmd_seq = 1;
255}
256
257static void start_drv_timers(struct _adapter *padapter)
258{
259 _set_timer(&padapter->mlmepriv.sitesurveyctrl.sitesurvey_ctrl_timer,
260 5000);
261 _set_timer(&padapter->mlmepriv.wdg_timer, 2000);
262}
263
Ali Bahar6c19d862011-09-04 03:14:14 +0800264void r8712_stop_drv_timers(struct _adapter *padapter)
Larry Finger2865d422010-08-20 10:15:30 -0500265{
266 _cancel_timer_ex(&padapter->mlmepriv.assoc_timer);
Larry Finger2865d422010-08-20 10:15:30 -0500267 _cancel_timer_ex(&padapter->securitypriv.tkip_timer);
268 _cancel_timer_ex(&padapter->mlmepriv.scan_to_timer);
269 _cancel_timer_ex(&padapter->mlmepriv.dhcp_timer);
270 _cancel_timer_ex(&padapter->mlmepriv.wdg_timer);
Larry Finger8c213fa592012-02-05 21:12:26 -0600271 _cancel_timer_ex(&padapter->mlmepriv.sitesurveyctrl.
272 sitesurvey_ctrl_timer);
Larry Finger2865d422010-08-20 10:15:30 -0500273}
274
275static u8 init_default_value(struct _adapter *padapter)
276{
277 u8 ret = _SUCCESS;
278 struct registry_priv *pregistrypriv = &padapter->registrypriv;
279 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
280 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
281 struct security_priv *psecuritypriv = &padapter->securitypriv;
282
283 /*xmit_priv*/
284 pxmitpriv->vcs_setting = pregistrypriv->vrtl_carrier_sense;
285 pxmitpriv->vcs = pregistrypriv->vcs_type;
286 pxmitpriv->vcs_type = pregistrypriv->vcs_type;
287 pxmitpriv->rts_thresh = pregistrypriv->rts_thresh;
288 pxmitpriv->frag_len = pregistrypriv->frag_thresh;
Ali Baharee5b1aa2011-09-04 03:14:21 +0800289 /* mlme_priv */
290 /* Maybe someday we should rename this variable to "active_mode"(Jeff)*/
291 pmlmepriv->passive_mode = 1; /* 1: active, 0: passive. */
Larry Finger2865d422010-08-20 10:15:30 -0500292 /*ht_priv*/
293 {
294 int i;
295 struct ht_priv *phtpriv = &pmlmepriv->htpriv;
296
297 phtpriv->ampdu_enable = false;/*set to disabled*/
298 for (i = 0; i < 16; i++)
299 phtpriv->baddbareq_issued[i] = false;
300 }
301 /*security_priv*/
302 psecuritypriv->sw_encrypt = pregistrypriv->software_encrypt;
303 psecuritypriv->sw_decrypt = pregistrypriv->software_decrypt;
304 psecuritypriv->binstallGrpkey = _FAIL;
305 /*pwrctrl_priv*/
306 /*registry_priv*/
307 r8712_init_registrypriv_dev_network(padapter);
308 r8712_update_registrypriv_dev_network(padapter);
309 /*misc.*/
310 return ret;
311}
312
313u8 r8712_init_drv_sw(struct _adapter *padapter)
314{
315 if ((r8712_init_cmd_priv(&padapter->cmdpriv)) == _FAIL)
316 return _FAIL;
317 padapter->cmdpriv.padapter = padapter;
318 if ((r8712_init_evt_priv(&padapter->evtpriv)) == _FAIL)
319 return _FAIL;
320 if (r8712_init_mlme_priv(padapter) == _FAIL)
321 return _FAIL;
322 _r8712_init_xmit_priv(&padapter->xmitpriv, padapter);
323 _r8712_init_recv_priv(&padapter->recvpriv, padapter);
324 memset((unsigned char *)&padapter->securitypriv, 0,
325 sizeof(struct security_priv));
326 _init_timer(&(padapter->securitypriv.tkip_timer), padapter->pnetdev,
327 r8712_use_tkipkey_handler, padapter);
328 _r8712_init_sta_priv(&padapter->stapriv);
329 padapter->stapriv.padapter = padapter;
330 r8712_init_bcmc_stainfo(padapter);
331 r8712_init_pwrctrl_priv(padapter);
Larry Finger2865d422010-08-20 10:15:30 -0500332 mp871xinit(padapter);
333 if (init_default_value(padapter) != _SUCCESS)
334 return _FAIL;
335 r8712_InitSwLeds(padapter);
336 return _SUCCESS;
337}
338
339u8 r8712_free_drv_sw(struct _adapter *padapter)
340{
341 struct net_device *pnetdev = (struct net_device *)padapter->pnetdev;
342
343 r8712_free_cmd_priv(&padapter->cmdpriv);
344 r8712_free_evt_priv(&padapter->evtpriv);
345 r8712_DeInitSwLeds(padapter);
346 r8712_free_mlme_priv(&padapter->mlmepriv);
347 r8712_free_io_queue(padapter);
348 _free_xmit_priv(&padapter->xmitpriv);
Larry Finger8c213fa592012-02-05 21:12:26 -0600349 if (padapter->fw_found)
350 _r8712_free_sta_priv(&padapter->stapriv);
Larry Finger2865d422010-08-20 10:15:30 -0500351 _r8712_free_recv_priv(&padapter->recvpriv);
352 mp871xdeinit(padapter);
353 if (pnetdev)
Ali Bahar7bcd9ce2011-09-04 03:14:06 +0800354 free_netdev(pnetdev);
Larry Finger2865d422010-08-20 10:15:30 -0500355 return _SUCCESS;
356}
357
358
359static 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 intcmd |= 0x200;
377 }
378 r8712_fw_cmd(padapter, intcmd);
379}
380
Ali Bahara5ee6522011-07-12 23:10:56 +0800381/**
382 *
383 * This function intends to handle the activation of an interface
384 * i.e. when it is brought Up/Active from a Down state.
385 *
386 */
Larry Finger2865d422010-08-20 10:15:30 -0500387static int netdev_open(struct net_device *pnetdev)
388{
Ali Bahar7c1f4202011-09-04 03:14:05 +0800389 struct _adapter *padapter = (struct _adapter *)netdev_priv(pnetdev);
Larry Finger2865d422010-08-20 10:15:30 -0500390
Larry Finger8c213fa592012-02-05 21:12:26 -0600391 mutex_lock(&padapter->mutex_start);
Larry Finger2865d422010-08-20 10:15:30 -0500392 if (padapter->bup == false) {
393 padapter->bDriverStopped = false;
394 padapter->bSurpriseRemoved = false;
395 padapter->bup = true;
396 if (rtl871x_hal_init(padapter) != _SUCCESS)
397 goto netdev_open_error;
398 if (r8712_initmac == NULL)
399 /* Use the mac address stored in the Efuse */
400 memcpy(pnetdev->dev_addr,
401 padapter->eeprompriv.mac_addr, ETH_ALEN);
402 else {
403 /* We have to inform f/w to use user-supplied MAC
404 * address.
405 */
406 msleep(200);
407 r8712_setMacAddr_cmd(padapter, (u8 *)pnetdev->dev_addr);
408 /*
409 * The "myid" function will get the wifi mac address
410 * from eeprompriv structure instead of netdev
411 * structure. So, we have to overwrite the mac_addr
412 * stored in the eeprompriv structure. In this case,
413 * the real mac address won't be used anymore. So that,
414 * the eeprompriv.mac_addr should store the mac which
415 * users specify.
416 */
417 memcpy(padapter->eeprompriv.mac_addr,
418 pnetdev->dev_addr, ETH_ALEN);
419 }
420 if (start_drv_threads(padapter) != _SUCCESS)
421 goto netdev_open_error;
422 if (padapter->dvobjpriv.inirp_init == NULL)
423 goto netdev_open_error;
424 else
425 padapter->dvobjpriv.inirp_init(padapter);
426 r8712_set_ps_mode(padapter, padapter->registrypriv.power_mgnt,
427 padapter->registrypriv.smart_ps);
428 }
429 if (!netif_queue_stopped(pnetdev))
430 netif_start_queue(pnetdev);
431 else
432 netif_wake_queue(pnetdev);
433
434 if (video_mode)
435 enable_video_mode(padapter, cbw40_enable);
436 /* start driver mlme relation timer */
437 start_drv_timers(padapter);
438 padapter->ledpriv.LedControlHandler(padapter, LED_CTL_NO_LINK);
Larry Finger8c213fa592012-02-05 21:12:26 -0600439 mutex_unlock(&padapter->mutex_start);
Larry Finger2865d422010-08-20 10:15:30 -0500440 return 0;
441netdev_open_error:
442 padapter->bup = false;
443 netif_carrier_off(pnetdev);
444 netif_stop_queue(pnetdev);
Larry Finger8c213fa592012-02-05 21:12:26 -0600445 mutex_unlock(&padapter->mutex_start);
Larry Finger2865d422010-08-20 10:15:30 -0500446 return -1;
447}
448
Ali Bahara5ee6522011-07-12 23:10:56 +0800449/**
450 *
451 * This function intends to handle the shutdown of an interface
452 * i.e. when it is brought Down from an Up/Active state.
453 *
454 */
Larry Finger2865d422010-08-20 10:15:30 -0500455static int netdev_close(struct net_device *pnetdev)
456{
Ali Bahar7c1f4202011-09-04 03:14:05 +0800457 struct _adapter *padapter = (struct _adapter *) netdev_priv(pnetdev);
Larry Finger2865d422010-08-20 10:15:30 -0500458
459 /* Close LED*/
460 padapter->ledpriv.LedControlHandler(padapter, LED_CTL_POWER_OFF);
461 msleep(200);
462
463 /*s1.*/
464 if (pnetdev) {
465 if (!netif_queue_stopped(pnetdev))
466 netif_stop_queue(pnetdev);
467 }
468 /*s2.*/
469 /*s2-1. issue disassoc_cmd to fw*/
470 r8712_disassoc_cmd(padapter);
471 /*s2-2. indicate disconnect to os*/
472 r8712_ind_disconnect(padapter);
473 /*s2-3.*/
474 r8712_free_assoc_resources(padapter);
475 /*s2-4.*/
476 r8712_free_network_queue(padapter);
Larry Finger2865d422010-08-20 10:15:30 -0500477 return 0;
478}
479
480#include "mlme_osdep.h"