]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/rtl8712/os_intfs.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[mv-sheeva.git] / drivers / staging / rtl8712 / os_intfs.c
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  * 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>
34 #include "osdep_service.h"
35 #include "drv_types.h"
36 #include "xmit_osdep.h"
37 #include "recv_osdep.h"
38 #include "rtl871x_ioctl.h"
39 #include "usb_osintf.h"
40
41 MODULE_LICENSE("GPL");
42 MODULE_DESCRIPTION("rtl871x wireless lan driver");
43 MODULE_AUTHOR("Larry Finger");
44
45 static char ifname[IFNAMSIZ] = "wlan%d";
46
47 /* module param defaults */
48 static int chip_version = RTL8712_2ndCUT;
49 static int rfintfs = HWPI;
50 static int lbkmode = RTL8712_AIR_TRX;
51 static int hci = RTL8712_USB;
52 static int ampdu_enable = 1;/*for enable tx_ampdu*/
53
54 /* The video_mode variable is for vedio mode.*/
55 /* It may be specify when inserting module with video_mode=1 parameter.*/
56 static int video_mode = 1;   /* enable video mode*/
57
58 /*Ndis802_11Infrastructure; infra, ad-hoc, auto*/
59 static int network_mode = Ndis802_11IBSS;
60 static int channel = 1;/*ad-hoc support requirement*/
61 static int wireless_mode = WIRELESS_11BG;
62 static int vrtl_carrier_sense = AUTO_VCS;
63 static int vcs_type = RTS_CTS;
64 static int frag_thresh = 2346;
65 static int preamble = PREAMBLE_LONG;/*long, short, auto*/
66 static int scan_mode = 1;/*active, passive*/
67 static int adhoc_tx_pwr = 1;
68 static int soft_ap;
69 static int smart_ps = 1;
70 static int power_mgnt = PS_MODE_ACTIVE;
71 static int radio_enable = 1;
72 static int long_retry_lmt = 7;
73 static int short_retry_lmt = 7;
74 static int busy_thresh = 40;
75 static int ack_policy = NORMAL_ACK;
76 static int mp_mode;
77 static int software_encrypt;
78 static int software_decrypt;
79
80 static int wmm_enable;/* default is set to disable the wmm.*/
81 static int uapsd_enable;
82 static int uapsd_max_sp = NO_LIMIT;
83 static int uapsd_acbk_en;
84 static int uapsd_acbe_en;
85 static int uapsd_acvi_en;
86 static int uapsd_acvo_en;
87
88 static int ht_enable = 1;
89 static int cbw40_enable = 1;
90 static int rf_config = RTL8712_RF_1T2R;  /* 1T2R*/
91 static int low_power;
92 /* mac address to use instead of the one stored in Efuse */
93 char *r8712_initmac;
94 static char *initmac;
95
96 module_param_string(ifname, ifname, sizeof(ifname), S_IRUGO|S_IWUSR);
97 module_param(initmac, charp, 0644);
98 module_param(video_mode, int, 0644);
99 module_param(chip_version, int, 0644);
100 module_param(rfintfs, int, 0644);
101 module_param(lbkmode, int, 0644);
102 module_param(hci, int, 0644);
103 module_param(network_mode, int, 0644);
104 module_param(channel, int, 0644);
105 module_param(mp_mode, int, 0644);
106 module_param(wmm_enable, int, 0644);
107 module_param(vrtl_carrier_sense, int, 0644);
108 module_param(vcs_type, int, 0644);
109 module_param(busy_thresh, int, 0644);
110 module_param(ht_enable, int, 0644);
111 module_param(cbw40_enable, int, 0644);
112 module_param(ampdu_enable, int, 0644);
113 module_param(rf_config, int, 0644);
114 module_param(power_mgnt, int, 0644);
115 module_param(low_power, int, 0644);
116
117 MODULE_PARM_DESC(ifname, " Net interface name, wlan%d=default");
118 MODULE_PARM_DESC(initmac, "MAC-Address, default: use FUSE");
119
120 static uint loadparam(struct _adapter *padapter, struct  net_device *pnetdev);
121 static int netdev_open(struct net_device *pnetdev);
122 static int netdev_close(struct net_device *pnetdev);
123
124 static uint loadparam(struct _adapter *padapter, struct  net_device *pnetdev)
125 {
126         uint status = _SUCCESS;
127         struct registry_priv  *registry_par = &padapter->registrypriv;
128
129         registry_par->chip_version = (u8)chip_version;
130         registry_par->rfintfs = (u8)rfintfs;
131         registry_par->lbkmode = (u8)lbkmode;
132         registry_par->hci = (u8)hci;
133         registry_par->network_mode  = (u8)network_mode;
134         memcpy(registry_par->ssid.Ssid, "ANY", 3);
135         registry_par->ssid.SsidLength = 3;
136         registry_par->channel = (u8)channel;
137         registry_par->wireless_mode = (u8)wireless_mode;
138         registry_par->vrtl_carrier_sense = (u8)vrtl_carrier_sense ;
139         registry_par->vcs_type = (u8)vcs_type;
140         registry_par->frag_thresh = (u16)frag_thresh;
141         registry_par->preamble = (u8)preamble;
142         registry_par->scan_mode = (u8)scan_mode;
143         registry_par->adhoc_tx_pwr = (u8)adhoc_tx_pwr;
144         registry_par->soft_ap = (u8)soft_ap;
145         registry_par->smart_ps = (u8)smart_ps;
146         registry_par->power_mgnt = (u8)power_mgnt;
147         registry_par->radio_enable = (u8)radio_enable;
148         registry_par->long_retry_lmt = (u8)long_retry_lmt;
149         registry_par->short_retry_lmt = (u8)short_retry_lmt;
150         registry_par->busy_thresh = (u16)busy_thresh;
151         registry_par->ack_policy = (u8)ack_policy;
152         registry_par->mp_mode = (u8)mp_mode;
153         registry_par->software_encrypt = (u8)software_encrypt;
154         registry_par->software_decrypt = (u8)software_decrypt;
155         /*UAPSD*/
156         registry_par->wmm_enable = (u8)wmm_enable;
157         registry_par->uapsd_enable = (u8)uapsd_enable;
158         registry_par->uapsd_max_sp = (u8)uapsd_max_sp;
159         registry_par->uapsd_acbk_en = (u8)uapsd_acbk_en;
160         registry_par->uapsd_acbe_en = (u8)uapsd_acbe_en;
161         registry_par->uapsd_acvi_en = (u8)uapsd_acvi_en;
162         registry_par->uapsd_acvo_en = (u8)uapsd_acvo_en;
163         registry_par->ht_enable = (u8)ht_enable;
164         registry_par->cbw40_enable = (u8)cbw40_enable;
165         registry_par->ampdu_enable = (u8)ampdu_enable;
166         registry_par->rf_config = (u8)rf_config;
167         registry_par->low_power = (u8)low_power;
168         r8712_initmac = initmac;
169         return status;
170 }
171
172 static int r871x_net_set_mac_address(struct net_device *pnetdev, void *p)
173 {
174         struct _adapter *padapter = (struct _adapter *)_netdev_priv(pnetdev);
175         struct sockaddr *addr = p;
176
177         if (padapter->bup == false)
178                 memcpy(pnetdev->dev_addr, addr->sa_data, ETH_ALEN);
179         return 0;
180 }
181
182 static struct net_device_stats *r871x_net_get_stats(struct net_device *pnetdev)
183 {
184         struct _adapter *padapter = (struct _adapter *) _netdev_priv(pnetdev);
185         struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
186         struct recv_priv *precvpriv = &(padapter->recvpriv);
187
188         padapter->stats.tx_packets = pxmitpriv->tx_pkts;
189         padapter->stats.rx_packets = precvpriv->rx_pkts;
190         padapter->stats.tx_dropped = pxmitpriv->tx_drop;
191         padapter->stats.rx_dropped = precvpriv->rx_drop;
192         padapter->stats.tx_bytes = pxmitpriv->tx_bytes;
193         padapter->stats.rx_bytes = precvpriv->rx_bytes;
194         return &padapter->stats;
195 }
196
197 static const struct net_device_ops rtl8712_netdev_ops = {
198         .ndo_open = netdev_open,
199         .ndo_stop = netdev_close,
200         .ndo_start_xmit = r8712_xmit_entry,
201         .ndo_set_mac_address = r871x_net_set_mac_address,
202         .ndo_get_stats = r871x_net_get_stats,
203         .ndo_do_ioctl = r871x_ioctl,
204 };
205
206 struct net_device *r8712_init_netdev(void)
207 {
208         struct _adapter *padapter;
209         struct net_device *pnetdev;
210
211         pnetdev = alloc_etherdev(sizeof(struct _adapter));
212         if (!pnetdev)
213                 return NULL;
214         if (dev_alloc_name(pnetdev, ifname) < 0) {
215                 strcpy(ifname, "wlan%d");
216                 dev_alloc_name(pnetdev, ifname);
217         }
218         padapter = (struct _adapter *) _netdev_priv(pnetdev);
219         padapter->pnetdev = pnetdev;
220         printk(KERN_INFO "r8712u: register rtl8712_netdev_ops to"
221                " netdev_ops\n");
222         pnetdev->netdev_ops = &rtl8712_netdev_ops;
223         pnetdev->watchdog_timeo = HZ; /* 1 second timeout */
224         pnetdev->wireless_handlers = (struct iw_handler_def *)
225                                      &r871x_handlers_def;
226         /*step 2.*/
227         loadparam(padapter, pnetdev);
228         netif_carrier_off(pnetdev);
229         padapter->pid = 0;  /* Initial the PID value used for HW PBC.*/
230         return pnetdev;
231 }
232
233 static u32 start_drv_threads(struct _adapter *padapter)
234 {
235         padapter->cmdThread = kthread_run(r8712_cmd_thread, padapter,
236                               padapter->pnetdev->name);
237         if (IS_ERR(padapter->cmdThread) < 0)
238                 return _FAIL;
239         return _SUCCESS;
240 }
241
242 void r8712_stop_drv_threads(struct _adapter *padapter)
243 {
244         /*Below is to termindate r8712_cmd_thread & event_thread...*/
245         up(&padapter->cmdpriv.cmd_queue_sema);
246         if (padapter->cmdThread)
247                 _down_sema(&padapter->cmdpriv.terminate_cmdthread_sema);
248         padapter->cmdpriv.cmd_seq = 1;
249 }
250
251 static void start_drv_timers(struct _adapter *padapter)
252 {
253         _set_timer(&padapter->mlmepriv.sitesurveyctrl.sitesurvey_ctrl_timer,
254                    5000);
255         _set_timer(&padapter->mlmepriv.wdg_timer, 2000);
256 }
257
258 static void stop_drv_timers(struct _adapter *padapter)
259 {
260         _cancel_timer_ex(&padapter->mlmepriv.assoc_timer);
261         _cancel_timer_ex(&padapter->mlmepriv.sitesurveyctrl.
262                          sitesurvey_ctrl_timer);
263         _cancel_timer_ex(&padapter->securitypriv.tkip_timer);
264         _cancel_timer_ex(&padapter->mlmepriv.scan_to_timer);
265         _cancel_timer_ex(&padapter->mlmepriv.dhcp_timer);
266         _cancel_timer_ex(&padapter->mlmepriv.wdg_timer);
267 }
268
269 static u8 init_default_value(struct _adapter *padapter)
270 {
271         u8 ret  = _SUCCESS;
272         struct registry_priv *pregistrypriv = &padapter->registrypriv;
273         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
274         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
275         struct security_priv *psecuritypriv = &padapter->securitypriv;
276
277         /*xmit_priv*/
278         pxmitpriv->vcs_setting = pregistrypriv->vrtl_carrier_sense;
279         pxmitpriv->vcs = pregistrypriv->vcs_type;
280         pxmitpriv->vcs_type = pregistrypriv->vcs_type;
281         pxmitpriv->rts_thresh = pregistrypriv->rts_thresh;
282         pxmitpriv->frag_len = pregistrypriv->frag_thresh;
283         /*ht_priv*/
284         {
285                 int i;
286                 struct ht_priv   *phtpriv = &pmlmepriv->htpriv;
287
288                 phtpriv->ampdu_enable = false;/*set to disabled*/
289                 for (i = 0; i < 16; i++)
290                         phtpriv->baddbareq_issued[i] = false;
291         }
292         /*security_priv*/
293         psecuritypriv->sw_encrypt = pregistrypriv->software_encrypt;
294         psecuritypriv->sw_decrypt = pregistrypriv->software_decrypt;
295         psecuritypriv->binstallGrpkey = _FAIL;
296         /*pwrctrl_priv*/
297         /*registry_priv*/
298         r8712_init_registrypriv_dev_network(padapter);
299         r8712_update_registrypriv_dev_network(padapter);
300         /*misc.*/
301         return ret;
302 }
303
304 u8 r8712_init_drv_sw(struct _adapter *padapter)
305 {
306         if ((r8712_init_cmd_priv(&padapter->cmdpriv)) == _FAIL)
307                 return _FAIL;
308         padapter->cmdpriv.padapter = padapter;
309         if ((r8712_init_evt_priv(&padapter->evtpriv)) == _FAIL)
310                 return _FAIL;
311         if (r8712_init_mlme_priv(padapter) == _FAIL)
312                 return _FAIL;
313         _r8712_init_xmit_priv(&padapter->xmitpriv, padapter);
314         _r8712_init_recv_priv(&padapter->recvpriv, padapter);
315         memset((unsigned char *)&padapter->securitypriv, 0,
316                sizeof(struct security_priv));
317         _init_timer(&(padapter->securitypriv.tkip_timer), padapter->pnetdev,
318                     r8712_use_tkipkey_handler, padapter);
319         _r8712_init_sta_priv(&padapter->stapriv);
320         padapter->stapriv.padapter = padapter;
321         r8712_init_bcmc_stainfo(padapter);
322         r8712_init_pwrctrl_priv(padapter);
323         sema_init(&(padapter->pwrctrlpriv.pnp_pwr_mgnt_sema), 0);
324         mp871xinit(padapter);
325         if (init_default_value(padapter) != _SUCCESS)
326                 return _FAIL;
327         r8712_InitSwLeds(padapter);
328         return _SUCCESS;
329 }
330
331 u8 r8712_free_drv_sw(struct _adapter *padapter)
332 {
333         struct net_device *pnetdev = (struct net_device *)padapter->pnetdev;
334
335         r8712_free_cmd_priv(&padapter->cmdpriv);
336         r8712_free_evt_priv(&padapter->evtpriv);
337         r8712_DeInitSwLeds(padapter);
338         r8712_free_mlme_priv(&padapter->mlmepriv);
339         r8712_free_io_queue(padapter);
340         _free_xmit_priv(&padapter->xmitpriv);
341         _r8712_free_sta_priv(&padapter->stapriv);
342         _r8712_free_recv_priv(&padapter->recvpriv);
343         mp871xdeinit(padapter);
344         if (pnetdev)
345                 os_free_netdev(pnetdev);
346         return _SUCCESS;
347 }
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                 intcmd |= 0x200;
368         }
369         r8712_fw_cmd(padapter, intcmd);
370 }
371
372 static int netdev_open(struct net_device *pnetdev)
373 {
374         struct _adapter *padapter = (struct _adapter *)_netdev_priv(pnetdev);
375
376         if (padapter->bup == false) {
377                 padapter->bDriverStopped = false;
378                 padapter->bSurpriseRemoved = false;
379                 padapter->bup = true;
380                 if (rtl871x_hal_init(padapter) != _SUCCESS)
381                         goto netdev_open_error;
382                 if (r8712_initmac == NULL)
383                         /* Use the mac address stored in the Efuse */
384                         memcpy(pnetdev->dev_addr,
385                                 padapter->eeprompriv.mac_addr, ETH_ALEN);
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, (u8 *)pnetdev->dev_addr);
392                         /*
393                          * The "myid" function will get the wifi mac address
394                          * from eeprompriv structure instead of netdev
395                          * structure. So, we have to overwrite the mac_addr
396                          * stored in the eeprompriv structure. In this case,
397                          * the real mac address won't be used anymore. So that,
398                          * the eeprompriv.mac_addr should store the mac which
399                          * users specify.
400                          */
401                         memcpy(padapter->eeprompriv.mac_addr,
402                                 pnetdev->dev_addr, ETH_ALEN);
403                 }
404                 if (start_drv_threads(padapter) != _SUCCESS)
405                         goto netdev_open_error;
406                 if (padapter->dvobjpriv.inirp_init == NULL)
407                         goto netdev_open_error;
408                 else
409                         padapter->dvobjpriv.inirp_init(padapter);
410                 r8712_set_ps_mode(padapter, padapter->registrypriv.power_mgnt,
411                                   padapter->registrypriv.smart_ps);
412         }
413         if (!netif_queue_stopped(pnetdev))
414                 netif_start_queue(pnetdev);
415         else
416                 netif_wake_queue(pnetdev);
417
418          if (video_mode)
419                 enable_video_mode(padapter, cbw40_enable);
420         /* start driver mlme relation timer */
421         start_drv_timers(padapter);
422         padapter->ledpriv.LedControlHandler(padapter, LED_CTL_NO_LINK);
423         return 0;
424 netdev_open_error:
425         padapter->bup = false;
426         netif_carrier_off(pnetdev);
427         netif_stop_queue(pnetdev);
428         return -1;
429 }
430
431 static int netdev_close(struct net_device *pnetdev)
432 {
433         struct _adapter *padapter = (struct _adapter *) _netdev_priv(pnetdev);
434
435         /* Close LED*/
436         padapter->ledpriv.LedControlHandler(padapter, LED_CTL_POWER_OFF);
437         msleep(200);
438
439         /*s1.*/
440         if (pnetdev) {
441                 if (!netif_queue_stopped(pnetdev))
442                         netif_stop_queue(pnetdev);
443         }
444         /*s2.*/
445         /*s2-1.  issue disassoc_cmd to fw*/
446         r8712_disassoc_cmd(padapter);
447         /*s2-2.  indicate disconnect to os*/
448         r8712_ind_disconnect(padapter);
449         /*s2-3.*/
450         r8712_free_assoc_resources(padapter);
451         /*s2-4.*/
452         r8712_free_network_queue(padapter);
453         /*Stop driver mlme relation timer*/
454         stop_drv_timers(padapter);
455         return 0;
456 }
457
458 #include "mlme_osdep.h"