main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Device probe and register.
  4. *
  5. * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
  6. * Copyright (c) 2010, ST-Ericsson
  7. * Copyright (c) 2008, Johannes Berg <johannes@sipsolutions.net>
  8. * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
  9. * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
  10. * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
  11. * Copyright (c) 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_net.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/mmc/sdio_func.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/firmware.h>
  21. #include "main.h"
  22. #include "wfx.h"
  23. #include "fwio.h"
  24. #include "hwio.h"
  25. #include "bus.h"
  26. #include "bh.h"
  27. #include "sta.h"
  28. #include "key.h"
  29. #include "scan.h"
  30. #include "debug.h"
  31. #include "data_tx.h"
  32. #include "hif_tx_mib.h"
  33. #include "hif_api_cmd.h"
  34. #define WFX_PDS_MAX_SIZE 1500
  35. MODULE_DESCRIPTION("Silicon Labs 802.11 Wireless LAN driver for WFx");
  36. MODULE_AUTHOR("Jérôme Pouiller <jerome.pouiller@silabs.com>");
  37. MODULE_LICENSE("GPL");
  38. #define RATETAB_ENT(_rate, _rateid, _flags) { \
  39. .bitrate = (_rate), \
  40. .hw_value = (_rateid), \
  41. .flags = (_flags), \
  42. }
  43. static struct ieee80211_rate wfx_rates[] = {
  44. RATETAB_ENT(10, 0, 0),
  45. RATETAB_ENT(20, 1, IEEE80211_RATE_SHORT_PREAMBLE),
  46. RATETAB_ENT(55, 2, IEEE80211_RATE_SHORT_PREAMBLE),
  47. RATETAB_ENT(110, 3, IEEE80211_RATE_SHORT_PREAMBLE),
  48. RATETAB_ENT(60, 6, 0),
  49. RATETAB_ENT(90, 7, 0),
  50. RATETAB_ENT(120, 8, 0),
  51. RATETAB_ENT(180, 9, 0),
  52. RATETAB_ENT(240, 10, 0),
  53. RATETAB_ENT(360, 11, 0),
  54. RATETAB_ENT(480, 12, 0),
  55. RATETAB_ENT(540, 13, 0),
  56. };
  57. #define CHAN2G(_channel, _freq, _flags) { \
  58. .band = NL80211_BAND_2GHZ, \
  59. .center_freq = (_freq), \
  60. .hw_value = (_channel), \
  61. .flags = (_flags), \
  62. .max_antenna_gain = 0, \
  63. .max_power = 30, \
  64. }
  65. static struct ieee80211_channel wfx_2ghz_chantable[] = {
  66. CHAN2G(1, 2412, 0),
  67. CHAN2G(2, 2417, 0),
  68. CHAN2G(3, 2422, 0),
  69. CHAN2G(4, 2427, 0),
  70. CHAN2G(5, 2432, 0),
  71. CHAN2G(6, 2437, 0),
  72. CHAN2G(7, 2442, 0),
  73. CHAN2G(8, 2447, 0),
  74. CHAN2G(9, 2452, 0),
  75. CHAN2G(10, 2457, 0),
  76. CHAN2G(11, 2462, 0),
  77. CHAN2G(12, 2467, 0),
  78. CHAN2G(13, 2472, 0),
  79. CHAN2G(14, 2484, 0),
  80. };
  81. static const struct ieee80211_supported_band wfx_band_2ghz = {
  82. .channels = wfx_2ghz_chantable,
  83. .n_channels = ARRAY_SIZE(wfx_2ghz_chantable),
  84. .bitrates = wfx_rates,
  85. .n_bitrates = ARRAY_SIZE(wfx_rates),
  86. .ht_cap = {
  87. // Receive caps
  88. .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
  89. IEEE80211_HT_CAP_MAX_AMSDU |
  90. (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT),
  91. .ht_supported = 1,
  92. .ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K,
  93. .ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE,
  94. .mcs = {
  95. .rx_mask = { 0xFF }, // MCS0 to MCS7
  96. .rx_highest = cpu_to_le16(72),
  97. .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
  98. },
  99. },
  100. };
  101. static const struct ieee80211_iface_limit wdev_iface_limits[] = {
  102. { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
  103. { .max = 1, .types = BIT(NL80211_IFTYPE_AP) },
  104. };
  105. static const struct ieee80211_iface_combination wfx_iface_combinations[] = {
  106. {
  107. .num_different_channels = 2,
  108. .max_interfaces = 2,
  109. .limits = wdev_iface_limits,
  110. .n_limits = ARRAY_SIZE(wdev_iface_limits),
  111. }
  112. };
  113. static const struct ieee80211_ops wfx_ops = {
  114. .start = wfx_start,
  115. .stop = wfx_stop,
  116. .add_interface = wfx_add_interface,
  117. .remove_interface = wfx_remove_interface,
  118. .config = wfx_config,
  119. .tx = wfx_tx,
  120. .join_ibss = wfx_join_ibss,
  121. .leave_ibss = wfx_leave_ibss,
  122. .conf_tx = wfx_conf_tx,
  123. .hw_scan = wfx_hw_scan,
  124. .cancel_hw_scan = wfx_cancel_hw_scan,
  125. .start_ap = wfx_start_ap,
  126. .stop_ap = wfx_stop_ap,
  127. .sta_add = wfx_sta_add,
  128. .sta_remove = wfx_sta_remove,
  129. .set_tim = wfx_set_tim,
  130. .set_key = wfx_set_key,
  131. .set_rts_threshold = wfx_set_rts_threshold,
  132. .set_default_unicast_key = wfx_set_default_unicast_key,
  133. .bss_info_changed = wfx_bss_info_changed,
  134. .configure_filter = wfx_configure_filter,
  135. .ampdu_action = wfx_ampdu_action,
  136. .flush = wfx_flush,
  137. .add_chanctx = wfx_add_chanctx,
  138. .remove_chanctx = wfx_remove_chanctx,
  139. .change_chanctx = wfx_change_chanctx,
  140. .assign_vif_chanctx = wfx_assign_vif_chanctx,
  141. .unassign_vif_chanctx = wfx_unassign_vif_chanctx,
  142. };
  143. bool wfx_api_older_than(struct wfx_dev *wdev, int major, int minor)
  144. {
  145. if (wdev->hw_caps.api_version_major < major)
  146. return true;
  147. if (wdev->hw_caps.api_version_major > major)
  148. return false;
  149. if (wdev->hw_caps.api_version_minor < minor)
  150. return true;
  151. return false;
  152. }
  153. /* NOTE: wfx_send_pds() destroy buf */
  154. int wfx_send_pds(struct wfx_dev *wdev, u8 *buf, size_t len)
  155. {
  156. int ret;
  157. int start, brace_level, i;
  158. start = 0;
  159. brace_level = 0;
  160. if (buf[0] != '{') {
  161. dev_err(wdev->dev, "valid PDS start with '{'. Did you forget to compress it?\n");
  162. return -EINVAL;
  163. }
  164. for (i = 1; i < len - 1; i++) {
  165. if (buf[i] == '{')
  166. brace_level++;
  167. if (buf[i] == '}')
  168. brace_level--;
  169. if (buf[i] == '}' && !brace_level) {
  170. i++;
  171. if (i - start + 1 > WFX_PDS_MAX_SIZE)
  172. return -EFBIG;
  173. buf[start] = '{';
  174. buf[i] = 0;
  175. dev_dbg(wdev->dev, "send PDS '%s}'\n", buf + start);
  176. buf[i] = '}';
  177. ret = hif_configuration(wdev, buf + start,
  178. i - start + 1);
  179. if (ret > 0) {
  180. dev_err(wdev->dev, "PDS bytes %d to %d: invalid data (unsupported options?)\n",
  181. start, i);
  182. return -EINVAL;
  183. }
  184. if (ret == -ETIMEDOUT) {
  185. dev_err(wdev->dev, "PDS bytes %d to %d: chip didn't reply (corrupted file?)\n",
  186. start, i);
  187. return ret;
  188. }
  189. if (ret) {
  190. dev_err(wdev->dev, "PDS bytes %d to %d: chip returned an unknown error\n",
  191. start, i);
  192. return -EIO;
  193. }
  194. buf[i] = ',';
  195. start = i;
  196. }
  197. }
  198. return 0;
  199. }
  200. static int wfx_send_pdata_pds(struct wfx_dev *wdev)
  201. {
  202. int ret = 0;
  203. const struct firmware *pds;
  204. u8 *tmp_buf;
  205. ret = request_firmware(&pds, wdev->pdata.file_pds, wdev->dev);
  206. if (ret) {
  207. dev_err(wdev->dev, "can't load PDS file %s\n",
  208. wdev->pdata.file_pds);
  209. goto err1;
  210. }
  211. tmp_buf = kmemdup(pds->data, pds->size, GFP_KERNEL);
  212. if (!tmp_buf) {
  213. ret = -ENOMEM;
  214. goto err2;
  215. }
  216. ret = wfx_send_pds(wdev, tmp_buf, pds->size);
  217. kfree(tmp_buf);
  218. err2:
  219. release_firmware(pds);
  220. err1:
  221. return ret;
  222. }
  223. static void wfx_free_common(void *data)
  224. {
  225. struct wfx_dev *wdev = data;
  226. mutex_destroy(&wdev->tx_power_loop_info_lock);
  227. mutex_destroy(&wdev->rx_stats_lock);
  228. mutex_destroy(&wdev->conf_mutex);
  229. ieee80211_free_hw(wdev->hw);
  230. }
  231. struct wfx_dev *wfx_init_common(struct device *dev,
  232. const struct wfx_platform_data *pdata,
  233. const struct hwbus_ops *hwbus_ops,
  234. void *hwbus_priv)
  235. {
  236. struct ieee80211_hw *hw;
  237. struct wfx_dev *wdev;
  238. hw = ieee80211_alloc_hw(sizeof(struct wfx_dev), &wfx_ops);
  239. if (!hw)
  240. return NULL;
  241. SET_IEEE80211_DEV(hw, dev);
  242. ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
  243. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  244. ieee80211_hw_set(hw, CONNECTION_MONITOR);
  245. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  246. ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
  247. ieee80211_hw_set(hw, SIGNAL_DBM);
  248. ieee80211_hw_set(hw, SUPPORTS_PS);
  249. ieee80211_hw_set(hw, MFP_CAPABLE);
  250. hw->vif_data_size = sizeof(struct wfx_vif);
  251. hw->sta_data_size = sizeof(struct wfx_sta_priv);
  252. hw->queues = 4;
  253. hw->max_rates = 8;
  254. hw->max_rate_tries = 8;
  255. hw->extra_tx_headroom = sizeof(struct hif_msg)
  256. + sizeof(struct hif_req_tx)
  257. + 4 /* alignment */ + 8 /* TKIP IV */;
  258. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  259. BIT(NL80211_IFTYPE_ADHOC) |
  260. BIT(NL80211_IFTYPE_AP);
  261. hw->wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
  262. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
  263. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P |
  264. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U;
  265. hw->wiphy->features |= NL80211_FEATURE_AP_SCAN;
  266. hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
  267. hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
  268. hw->wiphy->max_ap_assoc_sta = HIF_LINK_ID_MAX;
  269. hw->wiphy->max_scan_ssids = 2;
  270. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  271. hw->wiphy->n_iface_combinations = ARRAY_SIZE(wfx_iface_combinations);
  272. hw->wiphy->iface_combinations = wfx_iface_combinations;
  273. hw->wiphy->bands[NL80211_BAND_2GHZ] = devm_kmalloc(dev, sizeof(wfx_band_2ghz), GFP_KERNEL);
  274. // FIXME: also copy wfx_rates and wfx_2ghz_chantable
  275. memcpy(hw->wiphy->bands[NL80211_BAND_2GHZ], &wfx_band_2ghz,
  276. sizeof(wfx_band_2ghz));
  277. wdev = hw->priv;
  278. wdev->hw = hw;
  279. wdev->dev = dev;
  280. wdev->hwbus_ops = hwbus_ops;
  281. wdev->hwbus_priv = hwbus_priv;
  282. memcpy(&wdev->pdata, pdata, sizeof(*pdata));
  283. of_property_read_string(dev->of_node, "config-file",
  284. &wdev->pdata.file_pds);
  285. wdev->pdata.gpio_wakeup = devm_gpiod_get_optional(dev, "wakeup",
  286. GPIOD_OUT_LOW);
  287. if (IS_ERR(wdev->pdata.gpio_wakeup))
  288. goto err;
  289. if (wdev->pdata.gpio_wakeup)
  290. gpiod_set_consumer_name(wdev->pdata.gpio_wakeup, "wfx wakeup");
  291. mutex_init(&wdev->conf_mutex);
  292. mutex_init(&wdev->rx_stats_lock);
  293. mutex_init(&wdev->tx_power_loop_info_lock);
  294. init_completion(&wdev->firmware_ready);
  295. INIT_DELAYED_WORK(&wdev->cooling_timeout_work,
  296. wfx_cooling_timeout_work);
  297. skb_queue_head_init(&wdev->tx_pending);
  298. init_waitqueue_head(&wdev->tx_dequeue);
  299. wfx_init_hif_cmd(&wdev->hif_cmd);
  300. wdev->force_ps_timeout = -1;
  301. if (devm_add_action_or_reset(dev, wfx_free_common, wdev))
  302. return NULL;
  303. return wdev;
  304. err:
  305. ieee80211_free_hw(hw);
  306. return NULL;
  307. }
  308. int wfx_probe(struct wfx_dev *wdev)
  309. {
  310. int i;
  311. int err;
  312. const void *macaddr;
  313. struct gpio_desc *gpio_saved;
  314. // During first part of boot, gpio_wakeup cannot yet been used. So
  315. // prevent bh() to touch it.
  316. gpio_saved = wdev->pdata.gpio_wakeup;
  317. wdev->pdata.gpio_wakeup = NULL;
  318. wdev->poll_irq = true;
  319. wfx_bh_register(wdev);
  320. err = wfx_init_device(wdev);
  321. if (err)
  322. goto err0;
  323. wfx_bh_poll_irq(wdev);
  324. err = wait_for_completion_timeout(&wdev->firmware_ready, 1 * HZ);
  325. if (err <= 0) {
  326. if (err == 0) {
  327. dev_err(wdev->dev, "timeout while waiting for startup indication\n");
  328. err = -ETIMEDOUT;
  329. } else if (err == -ERESTARTSYS) {
  330. dev_info(wdev->dev, "probe interrupted by user\n");
  331. }
  332. goto err0;
  333. }
  334. // FIXME: fill wiphy::hw_version
  335. dev_info(wdev->dev, "started firmware %d.%d.%d \"%s\" (API: %d.%d, keyset: %02X, caps: 0x%.8X)\n",
  336. wdev->hw_caps.firmware_major, wdev->hw_caps.firmware_minor,
  337. wdev->hw_caps.firmware_build, wdev->hw_caps.firmware_label,
  338. wdev->hw_caps.api_version_major, wdev->hw_caps.api_version_minor,
  339. wdev->keyset, wdev->hw_caps.link_mode);
  340. snprintf(wdev->hw->wiphy->fw_version,
  341. sizeof(wdev->hw->wiphy->fw_version),
  342. "%d.%d.%d",
  343. wdev->hw_caps.firmware_major,
  344. wdev->hw_caps.firmware_minor,
  345. wdev->hw_caps.firmware_build);
  346. if (wfx_api_older_than(wdev, 1, 0)) {
  347. dev_err(wdev->dev,
  348. "unsupported firmware API version (expect 1 while firmware returns %d)\n",
  349. wdev->hw_caps.api_version_major);
  350. err = -ENOTSUPP;
  351. goto err0;
  352. }
  353. if (wdev->hw_caps.link_mode == SEC_LINK_ENFORCED) {
  354. dev_err(wdev->dev,
  355. "chip require secure_link, but can't negotiate it\n");
  356. goto err0;
  357. }
  358. if (wdev->hw_caps.region_sel_mode) {
  359. wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[11].flags |= IEEE80211_CHAN_NO_IR;
  360. wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[12].flags |= IEEE80211_CHAN_NO_IR;
  361. wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[13].flags |= IEEE80211_CHAN_DISABLED;
  362. }
  363. dev_dbg(wdev->dev, "sending configuration file %s\n",
  364. wdev->pdata.file_pds);
  365. err = wfx_send_pdata_pds(wdev);
  366. if (err < 0)
  367. goto err0;
  368. wdev->poll_irq = false;
  369. err = wdev->hwbus_ops->irq_subscribe(wdev->hwbus_priv);
  370. if (err)
  371. goto err0;
  372. err = hif_use_multi_tx_conf(wdev, true);
  373. if (err)
  374. dev_err(wdev->dev, "misconfigured IRQ?\n");
  375. wdev->pdata.gpio_wakeup = gpio_saved;
  376. if (wdev->pdata.gpio_wakeup) {
  377. dev_dbg(wdev->dev,
  378. "enable 'quiescent' power mode with wakeup GPIO and PDS file %s\n",
  379. wdev->pdata.file_pds);
  380. gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1);
  381. control_reg_write(wdev, 0);
  382. hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_QUIESCENT);
  383. } else {
  384. hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_DOZE);
  385. }
  386. for (i = 0; i < ARRAY_SIZE(wdev->addresses); i++) {
  387. eth_zero_addr(wdev->addresses[i].addr);
  388. macaddr = of_get_mac_address(wdev->dev->of_node);
  389. if (!IS_ERR_OR_NULL(macaddr)) {
  390. ether_addr_copy(wdev->addresses[i].addr, macaddr);
  391. wdev->addresses[i].addr[ETH_ALEN - 1] += i;
  392. } else {
  393. ether_addr_copy(wdev->addresses[i].addr,
  394. wdev->hw_caps.mac_addr[i]);
  395. }
  396. if (!is_valid_ether_addr(wdev->addresses[i].addr)) {
  397. dev_warn(wdev->dev, "using random MAC address\n");
  398. eth_random_addr(wdev->addresses[i].addr);
  399. }
  400. dev_info(wdev->dev, "MAC address %d: %pM\n", i,
  401. wdev->addresses[i].addr);
  402. }
  403. wdev->hw->wiphy->n_addresses = ARRAY_SIZE(wdev->addresses);
  404. wdev->hw->wiphy->addresses = wdev->addresses;
  405. err = ieee80211_register_hw(wdev->hw);
  406. if (err)
  407. goto err1;
  408. err = wfx_debug_init(wdev);
  409. if (err)
  410. goto err2;
  411. return 0;
  412. err2:
  413. ieee80211_unregister_hw(wdev->hw);
  414. err1:
  415. wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
  416. err0:
  417. wfx_bh_unregister(wdev);
  418. return err;
  419. }
  420. void wfx_release(struct wfx_dev *wdev)
  421. {
  422. ieee80211_unregister_hw(wdev->hw);
  423. hif_shutdown(wdev);
  424. wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
  425. wfx_bh_unregister(wdev);
  426. }
  427. static int __init wfx_core_init(void)
  428. {
  429. int ret = 0;
  430. if (IS_ENABLED(CONFIG_SPI))
  431. ret = spi_register_driver(&wfx_spi_driver);
  432. if (IS_ENABLED(CONFIG_MMC) && !ret)
  433. ret = sdio_register_driver(&wfx_sdio_driver);
  434. return ret;
  435. }
  436. module_init(wfx_core_init);
  437. static void __exit wfx_core_exit(void)
  438. {
  439. if (IS_ENABLED(CONFIG_MMC))
  440. sdio_unregister_driver(&wfx_sdio_driver);
  441. if (IS_ENABLED(CONFIG_SPI))
  442. spi_unregister_driver(&wfx_spi_driver);
  443. }
  444. module_exit(wfx_core_exit);