common.c 15 KB

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