common.c 14 KB

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