Merge remote-tracking branches 'asoc/topic/rt5640', 'asoc/topic/rt5659', 'asoc/topic...
[cascardo/linux.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / common.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/netdevice.h>
20 #include <linux/module.h>
21 #include <brcmu_wifi.h>
22 #include <brcmu_utils.h>
23 #include "core.h"
24 #include "bus.h"
25 #include "debug.h"
26 #include "fwil.h"
27 #include "fwil_types.h"
28 #include "tracepoint.h"
29 #include "common.h"
30
31 const u8 ALLFFMAC[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
32
33 #define BRCMF_DEFAULT_SCAN_CHANNEL_TIME 40
34 #define BRCMF_DEFAULT_SCAN_UNASSOC_TIME 40
35
36 /* boost value for RSSI_DELTA in preferred join selection */
37 #define BRCMF_JOIN_PREF_RSSI_BOOST      8
38
39 #define BRCMF_DEFAULT_TXGLOM_SIZE       32  /* max tx frames in glom chain */
40
41 static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
42 module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
43 MODULE_PARM_DESC(txglomsz, "Maximum tx packet chain size [SDIO]");
44
45 /* Debug level configuration. See debug.h for bits, sysfs modifiable */
46 int brcmf_msg_level;
47 module_param_named(debug, brcmf_msg_level, int, S_IRUSR | S_IWUSR);
48 MODULE_PARM_DESC(debug, "Level of debug output");
49
50 static int brcmf_p2p_enable;
51 module_param_named(p2pon, brcmf_p2p_enable, int, 0);
52 MODULE_PARM_DESC(p2pon, "Enable legacy p2p management functionality");
53
54 static int brcmf_feature_disable;
55 module_param_named(feature_disable, brcmf_feature_disable, int, 0);
56 MODULE_PARM_DESC(feature_disable, "Disable features");
57
58 static char brcmf_firmware_path[BRCMF_FW_ALTPATH_LEN];
59 module_param_string(alternative_fw_path, brcmf_firmware_path,
60                     BRCMF_FW_ALTPATH_LEN, S_IRUSR);
61 MODULE_PARM_DESC(alternative_fw_path, "Alternative firmware path");
62
63 static int brcmf_fcmode;
64 module_param_named(fcmode, brcmf_fcmode, int, 0);
65 MODULE_PARM_DESC(fcmode, "Mode of firmware signalled flow control");
66
67 static int brcmf_roamoff;
68 module_param_named(roamoff, brcmf_roamoff, int, S_IRUSR);
69 MODULE_PARM_DESC(roamoff, "Do not use internal roaming engine");
70
71 #ifdef DEBUG
72 /* always succeed brcmf_bus_start() */
73 static int brcmf_ignore_probe_fail;
74 module_param_named(ignore_probe_fail, brcmf_ignore_probe_fail, int, 0);
75 MODULE_PARM_DESC(ignore_probe_fail, "always succeed probe for debugging");
76 #endif
77
78 struct brcmf_mp_global_t brcmf_mp_global;
79
80 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
81 {
82         s8 eventmask[BRCMF_EVENTING_MASK_LEN];
83         u8 buf[BRCMF_DCMD_SMLEN];
84         struct brcmf_join_pref_params join_pref_params[2];
85         struct brcmf_rev_info_le revinfo;
86         struct brcmf_rev_info *ri;
87         char *ptr;
88         s32 err;
89
90         /* retreive mac address */
91         err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
92                                        sizeof(ifp->mac_addr));
93         if (err < 0) {
94                 brcmf_err("Retreiving cur_etheraddr failed, %d\n", err);
95                 goto done;
96         }
97         memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
98
99         err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_REVINFO,
100                                      &revinfo, sizeof(revinfo));
101         ri = &ifp->drvr->revinfo;
102         if (err < 0) {
103                 brcmf_err("retrieving revision info failed, %d\n", err);
104         } else {
105                 ri->vendorid = le32_to_cpu(revinfo.vendorid);
106                 ri->deviceid = le32_to_cpu(revinfo.deviceid);
107                 ri->radiorev = le32_to_cpu(revinfo.radiorev);
108                 ri->chiprev = le32_to_cpu(revinfo.chiprev);
109                 ri->corerev = le32_to_cpu(revinfo.corerev);
110                 ri->boardid = le32_to_cpu(revinfo.boardid);
111                 ri->boardvendor = le32_to_cpu(revinfo.boardvendor);
112                 ri->boardrev = le32_to_cpu(revinfo.boardrev);
113                 ri->driverrev = le32_to_cpu(revinfo.driverrev);
114                 ri->ucoderev = le32_to_cpu(revinfo.ucoderev);
115                 ri->bus = le32_to_cpu(revinfo.bus);
116                 ri->chipnum = le32_to_cpu(revinfo.chipnum);
117                 ri->phytype = le32_to_cpu(revinfo.phytype);
118                 ri->phyrev = le32_to_cpu(revinfo.phyrev);
119                 ri->anarev = le32_to_cpu(revinfo.anarev);
120                 ri->chippkg = le32_to_cpu(revinfo.chippkg);
121                 ri->nvramrev = le32_to_cpu(revinfo.nvramrev);
122         }
123         ri->result = err;
124
125         /* query for 'ver' to get version info from firmware */
126         memset(buf, 0, sizeof(buf));
127         strcpy(buf, "ver");
128         err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
129         if (err < 0) {
130                 brcmf_err("Retreiving version information failed, %d\n",
131                           err);
132                 goto done;
133         }
134         ptr = (char *)buf;
135         strsep(&ptr, "\n");
136
137         /* Print fw version info */
138         brcmf_err("Firmware version = %s\n", buf);
139
140         /* locate firmware version number for ethtool */
141         ptr = strrchr(buf, ' ') + 1;
142         strlcpy(ifp->drvr->fwver, ptr, sizeof(ifp->drvr->fwver));
143
144         /* set mpc */
145         err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
146         if (err) {
147                 brcmf_err("failed setting mpc\n");
148                 goto done;
149         }
150
151         /* Setup join_pref to select target by RSSI(with boost on 5GHz) */
152         join_pref_params[0].type = BRCMF_JOIN_PREF_RSSI_DELTA;
153         join_pref_params[0].len = 2;
154         join_pref_params[0].rssi_gain = BRCMF_JOIN_PREF_RSSI_BOOST;
155         join_pref_params[0].band = WLC_BAND_5G;
156         join_pref_params[1].type = BRCMF_JOIN_PREF_RSSI;
157         join_pref_params[1].len = 2;
158         join_pref_params[1].rssi_gain = 0;
159         join_pref_params[1].band = 0;
160         err = brcmf_fil_iovar_data_set(ifp, "join_pref", join_pref_params,
161                                        sizeof(join_pref_params));
162         if (err)
163                 brcmf_err("Set join_pref error (%d)\n", err);
164
165         /* Setup event_msgs, enable E_IF */
166         err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
167                                        BRCMF_EVENTING_MASK_LEN);
168         if (err) {
169                 brcmf_err("Get event_msgs error (%d)\n", err);
170                 goto done;
171         }
172         setbit(eventmask, BRCMF_E_IF);
173         err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
174                                        BRCMF_EVENTING_MASK_LEN);
175         if (err) {
176                 brcmf_err("Set event_msgs error (%d)\n", err);
177                 goto done;
178         }
179
180         /* Setup default scan channel time */
181         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
182                                     BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
183         if (err) {
184                 brcmf_err("BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
185                           err);
186                 goto done;
187         }
188
189         /* Setup default scan unassoc time */
190         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
191                                     BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
192         if (err) {
193                 brcmf_err("BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
194                           err);
195                 goto done;
196         }
197
198         /* Enable tx beamforming, errors can be ignored (not supported) */
199         (void)brcmf_fil_iovar_int_set(ifp, "txbf", 1);
200
201         /* do bus specific preinit here */
202         err = brcmf_bus_preinit(ifp->drvr->bus_if);
203 done:
204         return err;
205 }
206
207 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
208 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
209 {
210         struct va_format vaf = {
211                 .fmt = fmt,
212         };
213         va_list args;
214
215         va_start(args, fmt);
216         vaf.va = &args;
217         if (brcmf_msg_level & level)
218                 pr_debug("%s %pV", func, &vaf);
219         trace_brcmf_dbg(level, func, &vaf);
220         va_end(args);
221 }
222 #endif
223
224 void brcmf_mp_attach(void)
225 {
226         strlcpy(brcmf_mp_global.firmware_path, brcmf_firmware_path,
227                 BRCMF_FW_ALTPATH_LEN);
228 }
229
230 int brcmf_mp_device_attach(struct brcmf_pub *drvr)
231 {
232         drvr->settings = kzalloc(sizeof(*drvr->settings), GFP_ATOMIC);
233         if (!drvr->settings) {
234                 brcmf_err("Failed to alloca storage space for settings\n");
235                 return -ENOMEM;
236         }
237
238         drvr->settings->sdiod_txglomsz = brcmf_sdiod_txglomsz;
239         drvr->settings->p2p_enable = !!brcmf_p2p_enable;
240         drvr->settings->feature_disable = brcmf_feature_disable;
241         drvr->settings->fcmode = brcmf_fcmode;
242         drvr->settings->roamoff = !!brcmf_roamoff;
243 #ifdef DEBUG
244         drvr->settings->ignore_probe_fail = !!brcmf_ignore_probe_fail;
245 #endif
246         return 0;
247 }
248
249 void brcmf_mp_device_detach(struct brcmf_pub *drvr)
250 {
251         kfree(drvr->settings);
252 }
253