Merge git://git.kernel.org/pub/scm/linux/kernel/git/kvalo/wireless-drivers.git
[sfrench/cifs-2.6.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 <linux/firmware.h>
22 #include <brcmu_wifi.h>
23 #include <brcmu_utils.h>
24 #include "core.h"
25 #include "bus.h"
26 #include "debug.h"
27 #include "fwil.h"
28 #include "fwil_types.h"
29 #include "tracepoint.h"
30 #include "common.h"
31 #include "of.h"
32 #include "firmware.h"
33 #include "chip.h"
34
35 MODULE_AUTHOR("Broadcom Corporation");
36 MODULE_DESCRIPTION("Broadcom 802.11 wireless LAN fullmac driver.");
37 MODULE_LICENSE("Dual BSD/GPL");
38
39 const u8 ALLFFMAC[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
40
41 #define BRCMF_DEFAULT_SCAN_CHANNEL_TIME 40
42 #define BRCMF_DEFAULT_SCAN_UNASSOC_TIME 40
43
44 /* default boost value for RSSI_DELTA in preferred join selection */
45 #define BRCMF_JOIN_PREF_RSSI_BOOST      8
46
47 #define BRCMF_DEFAULT_TXGLOM_SIZE       32  /* max tx frames in glom chain */
48
49 static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
50 module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
51 MODULE_PARM_DESC(txglomsz, "Maximum tx packet chain size [SDIO]");
52
53 /* Debug level configuration. See debug.h for bits, sysfs modifiable */
54 int brcmf_msg_level;
55 module_param_named(debug, brcmf_msg_level, int, 0600);
56 MODULE_PARM_DESC(debug, "Level of debug output");
57
58 static int brcmf_p2p_enable;
59 module_param_named(p2pon, brcmf_p2p_enable, int, 0);
60 MODULE_PARM_DESC(p2pon, "Enable legacy p2p management functionality");
61
62 static int brcmf_feature_disable;
63 module_param_named(feature_disable, brcmf_feature_disable, int, 0);
64 MODULE_PARM_DESC(feature_disable, "Disable features");
65
66 static char brcmf_firmware_path[BRCMF_FW_ALTPATH_LEN];
67 module_param_string(alternative_fw_path, brcmf_firmware_path,
68                     BRCMF_FW_ALTPATH_LEN, 0400);
69 MODULE_PARM_DESC(alternative_fw_path, "Alternative firmware path");
70
71 static int brcmf_fcmode;
72 module_param_named(fcmode, brcmf_fcmode, int, 0);
73 MODULE_PARM_DESC(fcmode, "Mode of firmware signalled flow control");
74
75 static int brcmf_roamoff;
76 module_param_named(roamoff, brcmf_roamoff, int, 0400);
77 MODULE_PARM_DESC(roamoff, "Do not use internal roaming engine");
78
79 static int brcmf_iapp_enable;
80 module_param_named(iapp, brcmf_iapp_enable, int, 0);
81 MODULE_PARM_DESC(iapp, "Enable partial support for the obsoleted Inter-Access Point Protocol");
82
83 #ifdef DEBUG
84 /* always succeed brcmf_bus_started() */
85 static int brcmf_ignore_probe_fail;
86 module_param_named(ignore_probe_fail, brcmf_ignore_probe_fail, int, 0);
87 MODULE_PARM_DESC(ignore_probe_fail, "always succeed probe for debugging");
88 #endif
89
90 static struct brcmfmac_platform_data *brcmfmac_pdata;
91 struct brcmf_mp_global_t brcmf_mp_global;
92
93 void brcmf_c_set_joinpref_default(struct brcmf_if *ifp)
94 {
95         struct brcmf_join_pref_params join_pref_params[2];
96         int err;
97
98         /* Setup join_pref to select target by RSSI (boost on 5GHz) */
99         join_pref_params[0].type = BRCMF_JOIN_PREF_RSSI_DELTA;
100         join_pref_params[0].len = 2;
101         join_pref_params[0].rssi_gain = BRCMF_JOIN_PREF_RSSI_BOOST;
102         join_pref_params[0].band = WLC_BAND_5G;
103
104         join_pref_params[1].type = BRCMF_JOIN_PREF_RSSI;
105         join_pref_params[1].len = 2;
106         join_pref_params[1].rssi_gain = 0;
107         join_pref_params[1].band = 0;
108         err = brcmf_fil_iovar_data_set(ifp, "join_pref", join_pref_params,
109                                        sizeof(join_pref_params));
110         if (err)
111                 brcmf_err("Set join_pref error (%d)\n", err);
112 }
113
114 static int brcmf_c_download(struct brcmf_if *ifp, u16 flag,
115                             struct brcmf_dload_data_le *dload_buf,
116                             u32 len)
117 {
118         s32 err;
119
120         flag |= (DLOAD_HANDLER_VER << DLOAD_FLAG_VER_SHIFT);
121         dload_buf->flag = cpu_to_le16(flag);
122         dload_buf->dload_type = cpu_to_le16(DL_TYPE_CLM);
123         dload_buf->len = cpu_to_le32(len);
124         dload_buf->crc = cpu_to_le32(0);
125         len = sizeof(*dload_buf) + len - 1;
126
127         err = brcmf_fil_iovar_data_set(ifp, "clmload", dload_buf, len);
128
129         return err;
130 }
131
132 static int brcmf_c_process_clm_blob(struct brcmf_if *ifp)
133 {
134         struct brcmf_bus *bus = ifp->drvr->bus_if;
135         struct brcmf_dload_data_le *chunk_buf;
136         const struct firmware *clm = NULL;
137         u8 clm_name[BRCMF_FW_NAME_LEN];
138         u32 chunk_len;
139         u32 datalen;
140         u32 cumulative_len;
141         u16 dl_flag = DL_BEGIN;
142         u32 status;
143         s32 err;
144
145         brcmf_dbg(TRACE, "Enter\n");
146
147         memset(clm_name, 0, sizeof(clm_name));
148         err = brcmf_bus_get_fwname(bus, ".clm_blob", clm_name);
149         if (err) {
150                 brcmf_err("get CLM blob file name failed (%d)\n", err);
151                 return err;
152         }
153
154         err = request_firmware(&clm, clm_name, bus->dev);
155         if (err) {
156                 brcmf_info("no clm_blob available (err=%d), device may have limited channels available\n",
157                            err);
158                 return 0;
159         }
160
161         chunk_buf = kzalloc(sizeof(*chunk_buf) + MAX_CHUNK_LEN - 1, GFP_KERNEL);
162         if (!chunk_buf) {
163                 err = -ENOMEM;
164                 goto done;
165         }
166
167         datalen = clm->size;
168         cumulative_len = 0;
169         do {
170                 if (datalen > MAX_CHUNK_LEN) {
171                         chunk_len = MAX_CHUNK_LEN;
172                 } else {
173                         chunk_len = datalen;
174                         dl_flag |= DL_END;
175                 }
176                 memcpy(chunk_buf->data, clm->data + cumulative_len, chunk_len);
177
178                 err = brcmf_c_download(ifp, dl_flag, chunk_buf, chunk_len);
179
180                 dl_flag &= ~DL_BEGIN;
181
182                 cumulative_len += chunk_len;
183                 datalen -= chunk_len;
184         } while ((datalen > 0) && (err == 0));
185
186         if (err) {
187                 brcmf_err("clmload (%zu byte file) failed (%d); ",
188                           clm->size, err);
189                 /* Retrieve clmload_status and print */
190                 err = brcmf_fil_iovar_int_get(ifp, "clmload_status", &status);
191                 if (err)
192                         brcmf_err("get clmload_status failed (%d)\n", err);
193                 else
194                         brcmf_dbg(INFO, "clmload_status=%d\n", status);
195                 err = -EIO;
196         }
197
198         kfree(chunk_buf);
199 done:
200         release_firmware(clm);
201         return err;
202 }
203
204 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
205 {
206         s8 eventmask[BRCMF_EVENTING_MASK_LEN];
207         u8 buf[BRCMF_DCMD_SMLEN];
208         struct brcmf_bus *bus;
209         struct brcmf_rev_info_le revinfo;
210         struct brcmf_rev_info *ri;
211         char *clmver;
212         char *ptr;
213         s32 err;
214
215         /* retreive mac address */
216         err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
217                                        sizeof(ifp->mac_addr));
218         if (err < 0) {
219                 brcmf_err("Retreiving cur_etheraddr failed, %d\n", err);
220                 goto done;
221         }
222         memcpy(ifp->drvr->wiphy->perm_addr, ifp->drvr->mac, ETH_ALEN);
223         memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
224
225         bus = ifp->drvr->bus_if;
226         ri = &ifp->drvr->revinfo;
227
228         err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_REVINFO,
229                                      &revinfo, sizeof(revinfo));
230         if (err < 0) {
231                 brcmf_err("retrieving revision info failed, %d\n", err);
232                 strlcpy(ri->chipname, "UNKNOWN", sizeof(ri->chipname));
233         } else {
234                 ri->vendorid = le32_to_cpu(revinfo.vendorid);
235                 ri->deviceid = le32_to_cpu(revinfo.deviceid);
236                 ri->radiorev = le32_to_cpu(revinfo.radiorev);
237                 ri->corerev = le32_to_cpu(revinfo.corerev);
238                 ri->boardid = le32_to_cpu(revinfo.boardid);
239                 ri->boardvendor = le32_to_cpu(revinfo.boardvendor);
240                 ri->boardrev = le32_to_cpu(revinfo.boardrev);
241                 ri->driverrev = le32_to_cpu(revinfo.driverrev);
242                 ri->ucoderev = le32_to_cpu(revinfo.ucoderev);
243                 ri->bus = le32_to_cpu(revinfo.bus);
244                 ri->phytype = le32_to_cpu(revinfo.phytype);
245                 ri->phyrev = le32_to_cpu(revinfo.phyrev);
246                 ri->anarev = le32_to_cpu(revinfo.anarev);
247                 ri->chippkg = le32_to_cpu(revinfo.chippkg);
248                 ri->nvramrev = le32_to_cpu(revinfo.nvramrev);
249
250                 /* use revinfo if not known yet */
251                 if (!bus->chip) {
252                         bus->chip = le32_to_cpu(revinfo.chipnum);
253                         bus->chiprev = le32_to_cpu(revinfo.chiprev);
254                 }
255         }
256         ri->result = err;
257
258         if (bus->chip)
259                 brcmf_chip_name(bus->chip, bus->chiprev,
260                                 ri->chipname, sizeof(ri->chipname));
261
262         /* Do any CLM downloading */
263         err = brcmf_c_process_clm_blob(ifp);
264         if (err < 0) {
265                 brcmf_err("download CLM blob file failed, %d\n", err);
266                 goto done;
267         }
268
269         /* query for 'ver' to get version info from firmware */
270         memset(buf, 0, sizeof(buf));
271         strcpy(buf, "ver");
272         err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
273         if (err < 0) {
274                 brcmf_err("Retreiving version information failed, %d\n",
275                           err);
276                 goto done;
277         }
278         ptr = (char *)buf;
279         strsep(&ptr, "\n");
280
281         /* Print fw version info */
282         brcmf_info("Firmware: %s %s\n", ri->chipname, buf);
283
284         /* locate firmware version number for ethtool */
285         ptr = strrchr(buf, ' ') + 1;
286         strlcpy(ifp->drvr->fwver, ptr, sizeof(ifp->drvr->fwver));
287
288         /* Query for 'clmver' to get CLM version info from firmware */
289         memset(buf, 0, sizeof(buf));
290         err = brcmf_fil_iovar_data_get(ifp, "clmver", buf, sizeof(buf));
291         if (err) {
292                 brcmf_dbg(TRACE, "retrieving clmver failed, %d\n", err);
293         } else {
294                 clmver = (char *)buf;
295                 /* store CLM version for adding it to revinfo debugfs file */
296                 memcpy(ifp->drvr->clmver, clmver, sizeof(ifp->drvr->clmver));
297
298                 /* Replace all newline/linefeed characters with space
299                  * character
300                  */
301                 ptr = clmver;
302                 while ((ptr = strnchr(ptr, '\n', sizeof(buf))) != NULL)
303                         *ptr = ' ';
304
305                 brcmf_dbg(INFO, "CLM version = %s\n", clmver);
306         }
307
308         /* set mpc */
309         err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
310         if (err) {
311                 brcmf_err("failed setting mpc\n");
312                 goto done;
313         }
314
315         brcmf_c_set_joinpref_default(ifp);
316
317         /* Setup event_msgs, enable E_IF */
318         err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
319                                        BRCMF_EVENTING_MASK_LEN);
320         if (err) {
321                 brcmf_err("Get event_msgs error (%d)\n", err);
322                 goto done;
323         }
324         setbit(eventmask, BRCMF_E_IF);
325         err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
326                                        BRCMF_EVENTING_MASK_LEN);
327         if (err) {
328                 brcmf_err("Set event_msgs error (%d)\n", err);
329                 goto done;
330         }
331
332         /* Setup default scan channel time */
333         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
334                                     BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
335         if (err) {
336                 brcmf_err("BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
337                           err);
338                 goto done;
339         }
340
341         /* Setup default scan unassoc time */
342         err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
343                                     BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
344         if (err) {
345                 brcmf_err("BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
346                           err);
347                 goto done;
348         }
349
350         /* Enable tx beamforming, errors can be ignored (not supported) */
351         (void)brcmf_fil_iovar_int_set(ifp, "txbf", 1);
352 done:
353         return err;
354 }
355
356 #ifndef CONFIG_BRCM_TRACING
357 void __brcmf_err(const char *func, const char *fmt, ...)
358 {
359         struct va_format vaf;
360         va_list args;
361
362         va_start(args, fmt);
363
364         vaf.fmt = fmt;
365         vaf.va = &args;
366         pr_err("%s: %pV", func, &vaf);
367
368         va_end(args);
369 }
370 #endif
371
372 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
373 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
374 {
375         struct va_format vaf = {
376                 .fmt = fmt,
377         };
378         va_list args;
379
380         va_start(args, fmt);
381         vaf.va = &args;
382         if (brcmf_msg_level & level)
383                 pr_debug("%s %pV", func, &vaf);
384         trace_brcmf_dbg(level, func, &vaf);
385         va_end(args);
386 }
387 #endif
388
389 static void brcmf_mp_attach(void)
390 {
391         /* If module param firmware path is set then this will always be used,
392          * if not set then if available use the platform data version. To make
393          * sure it gets initialized at all, always copy the module param version
394          */
395         strlcpy(brcmf_mp_global.firmware_path, brcmf_firmware_path,
396                 BRCMF_FW_ALTPATH_LEN);
397         if ((brcmfmac_pdata) && (brcmfmac_pdata->fw_alternative_path) &&
398             (brcmf_mp_global.firmware_path[0] == '\0')) {
399                 strlcpy(brcmf_mp_global.firmware_path,
400                         brcmfmac_pdata->fw_alternative_path,
401                         BRCMF_FW_ALTPATH_LEN);
402         }
403 }
404
405 struct brcmf_mp_device *brcmf_get_module_param(struct device *dev,
406                                                enum brcmf_bus_type bus_type,
407                                                u32 chip, u32 chiprev)
408 {
409         struct brcmf_mp_device *settings;
410         struct brcmfmac_pd_device *device_pd;
411         bool found;
412         int i;
413
414         brcmf_dbg(INFO, "Enter, bus=%d, chip=%d, rev=%d\n", bus_type, chip,
415                   chiprev);
416         settings = kzalloc(sizeof(*settings), GFP_ATOMIC);
417         if (!settings)
418                 return NULL;
419
420         /* start by using the module paramaters */
421         settings->p2p_enable = !!brcmf_p2p_enable;
422         settings->feature_disable = brcmf_feature_disable;
423         settings->fcmode = brcmf_fcmode;
424         settings->roamoff = !!brcmf_roamoff;
425         settings->iapp = !!brcmf_iapp_enable;
426 #ifdef DEBUG
427         settings->ignore_probe_fail = !!brcmf_ignore_probe_fail;
428 #endif
429
430         if (bus_type == BRCMF_BUSTYPE_SDIO)
431                 settings->bus.sdio.txglomsz = brcmf_sdiod_txglomsz;
432
433         /* See if there is any device specific platform data configured */
434         found = false;
435         if (brcmfmac_pdata) {
436                 for (i = 0; i < brcmfmac_pdata->device_count; i++) {
437                         device_pd = &brcmfmac_pdata->devices[i];
438                         if ((device_pd->bus_type == bus_type) &&
439                             (device_pd->id == chip) &&
440                             ((device_pd->rev == chiprev) ||
441                              (device_pd->rev == -1))) {
442                                 brcmf_dbg(INFO, "Platform data for device found\n");
443                                 settings->country_codes =
444                                                 device_pd->country_codes;
445                                 if (device_pd->bus_type == BRCMF_BUSTYPE_SDIO)
446                                         memcpy(&settings->bus.sdio,
447                                                &device_pd->bus.sdio,
448                                                sizeof(settings->bus.sdio));
449                                 found = true;
450                                 break;
451                         }
452                 }
453         }
454         if (!found) {
455                 /* No platform data for this device, try OF (Open Firwmare) */
456                 brcmf_of_probe(dev, bus_type, settings);
457         }
458         return settings;
459 }
460
461 void brcmf_release_module_param(struct brcmf_mp_device *module_param)
462 {
463         kfree(module_param);
464 }
465
466 static int __init brcmf_common_pd_probe(struct platform_device *pdev)
467 {
468         brcmf_dbg(INFO, "Enter\n");
469
470         brcmfmac_pdata = dev_get_platdata(&pdev->dev);
471
472         if (brcmfmac_pdata->power_on)
473                 brcmfmac_pdata->power_on();
474
475         return 0;
476 }
477
478 static int brcmf_common_pd_remove(struct platform_device *pdev)
479 {
480         brcmf_dbg(INFO, "Enter\n");
481
482         if (brcmfmac_pdata->power_off)
483                 brcmfmac_pdata->power_off();
484
485         return 0;
486 }
487
488 static struct platform_driver brcmf_pd = {
489         .remove         = brcmf_common_pd_remove,
490         .driver         = {
491                 .name   = BRCMFMAC_PDATA_NAME,
492         }
493 };
494
495 static int __init brcmfmac_module_init(void)
496 {
497         int err;
498
499         /* Get the platform data (if available) for our devices */
500         err = platform_driver_probe(&brcmf_pd, brcmf_common_pd_probe);
501         if (err == -ENODEV)
502                 brcmf_dbg(INFO, "No platform data available.\n");
503
504         /* Initialize global module paramaters */
505         brcmf_mp_attach();
506
507         /* Continue the initialization by registering the different busses */
508         err = brcmf_core_init();
509         if (err) {
510                 if (brcmfmac_pdata)
511                         platform_driver_unregister(&brcmf_pd);
512         }
513
514         return err;
515 }
516
517 static void __exit brcmfmac_module_exit(void)
518 {
519         brcmf_core_exit();
520         if (brcmfmac_pdata)
521                 platform_driver_unregister(&brcmf_pd);
522 }
523
524 module_init(brcmfmac_module_init);
525 module_exit(brcmfmac_module_exit);
526