common.c 14 KB

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