File Index
Files and directories
Sign in
FILE

main.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/silabs/wfx/main.c C
  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
 22#include "main.h"
 23#include "wfx.h"
 24#include "fwio.h"
 25#include "hwio.h"
 26#include "bus.h"
 27#include "bh.h"
 28#include "sta.h"
 29#include "key.h"
 30#include "scan.h"
 31#include "debug.h"
 32#include "data_tx.h"
 33#include "hif_tx_mib.h"
 34#include "hif_api_cmd.h"
 35
 36#define WFX_PDS_TLV_TYPE 0x4450 // "PD" (Platform Data) in ascii little-endian
 37#define WFX_PDS_MAX_CHUNK_SIZE 1500
 38
 39MODULE_DESCRIPTION("Silicon Labs 802.11 Wireless LAN driver for WF200");
 40MODULE_AUTHOR("Jérôme Pouiller <jerome.pouiller@silabs.com>");
 41MODULE_LICENSE("GPL");
 42
 43#define RATETAB_ENT(_rate, _rateid, _flags) { \
 44	.bitrate  = (_rate),   \
 45	.hw_value = (_rateid), \
 46	.flags    = (_flags),  \
 47}
 48
 49static struct ieee80211_rate wfx_rates[] = {
 50	RATETAB_ENT(10,  0,  0),
 51	RATETAB_ENT(20,  1,  IEEE80211_RATE_SHORT_PREAMBLE),
 52	RATETAB_ENT(55,  2,  IEEE80211_RATE_SHORT_PREAMBLE),
 53	RATETAB_ENT(110, 3,  IEEE80211_RATE_SHORT_PREAMBLE),
 54	RATETAB_ENT(60,  6,  0),
 55	RATETAB_ENT(90,  7,  0),
 56	RATETAB_ENT(120, 8,  0),
 57	RATETAB_ENT(180, 9,  0),
 58	RATETAB_ENT(240, 10, 0),
 59	RATETAB_ENT(360, 11, 0),
 60	RATETAB_ENT(480, 12, 0),
 61	RATETAB_ENT(540, 13, 0),
 62};
 63
 64#define CHAN2G(_channel, _freq, _flags) { \
 65	.band = NL80211_BAND_2GHZ, \
 66	.center_freq = (_freq),    \
 67	.hw_value = (_channel),    \
 68	.flags = (_flags),         \
 69	.max_antenna_gain = 0,     \
 70	.max_power = 30,           \
 71}
 72
 73static struct ieee80211_channel wfx_2ghz_chantable[] = {
 74	CHAN2G(1,  2412, 0),
 75	CHAN2G(2,  2417, 0),
 76	CHAN2G(3,  2422, 0),
 77	CHAN2G(4,  2427, 0),
 78	CHAN2G(5,  2432, 0),
 79	CHAN2G(6,  2437, 0),
 80	CHAN2G(7,  2442, 0),
 81	CHAN2G(8,  2447, 0),
 82	CHAN2G(9,  2452, 0),
 83	CHAN2G(10, 2457, 0),
 84	CHAN2G(11, 2462, 0),
 85	CHAN2G(12, 2467, 0),
 86	CHAN2G(13, 2472, 0),
 87	CHAN2G(14, 2484, 0),
 88};
 89
 90static const struct ieee80211_supported_band wfx_band_2ghz = {
 91	.channels = wfx_2ghz_chantable,
 92	.n_channels = ARRAY_SIZE(wfx_2ghz_chantable),
 93	.bitrates = wfx_rates,
 94	.n_bitrates = ARRAY_SIZE(wfx_rates),
 95	.ht_cap = {
 96		/* Receive caps */
 97		.cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
 98		       IEEE80211_HT_CAP_MAX_AMSDU | (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT),
 99		.ht_supported = 1,
100		.ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K,
101		.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE,
102		.mcs = {
103			.rx_mask = { 0xFF }, /* MCS0 to MCS7 */
104			.rx_highest = cpu_to_le16(72),
105			.tx_params = IEEE80211_HT_MCS_TX_DEFINED,
106		},
107	},
108};
109
110static const struct ieee80211_iface_limit wdev_iface_limits[] = {
111	{ .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
112	{ .max = 1, .types = BIT(NL80211_IFTYPE_AP) },
113};
114
115static const struct ieee80211_iface_combination wfx_iface_combinations[] = {
116	{
117		.num_different_channels = 2,
118		.max_interfaces = 2,
119		.limits = wdev_iface_limits,
120		.n_limits = ARRAY_SIZE(wdev_iface_limits),
121	}
122};
123
124#ifdef CONFIG_PM
125static const struct wiphy_wowlan_support wfx_wowlan_support = {
126	.flags = WIPHY_WOWLAN_ANY | WIPHY_WOWLAN_DISCONNECT,
127};
128#endif
129
130static const struct ieee80211_ops wfx_ops = {
131	.start                   = wfx_start,
132	.stop                    = wfx_stop,
133	.add_interface           = wfx_add_interface,
134	.remove_interface        = wfx_remove_interface,
135	.config                  = wfx_config,
136	.tx                      = wfx_tx,
137	.wake_tx_queue           = ieee80211_handle_wake_tx_queue,
138	.join_ibss               = wfx_join_ibss,
139	.leave_ibss              = wfx_leave_ibss,
140	.conf_tx                 = wfx_conf_tx,
141	.hw_scan                 = wfx_hw_scan,
142	.cancel_hw_scan          = wfx_cancel_hw_scan,
143	.start_ap                = wfx_start_ap,
144	.stop_ap                 = wfx_stop_ap,
145	.sta_add                 = wfx_sta_add,
146	.sta_remove              = wfx_sta_remove,
147	.set_tim                 = wfx_set_tim,
148	.set_key                 = wfx_set_key,
149	.set_rts_threshold       = wfx_set_rts_threshold,
150	.set_default_unicast_key = wfx_set_default_unicast_key,
151	.bss_info_changed        = wfx_bss_info_changed,
152	.configure_filter        = wfx_configure_filter,
153	.ampdu_action            = wfx_ampdu_action,
154	.flush                   = wfx_flush,
155	.add_chanctx             = wfx_add_chanctx,
156	.remove_chanctx          = wfx_remove_chanctx,
157	.change_chanctx          = wfx_change_chanctx,
158	.assign_vif_chanctx      = wfx_assign_vif_chanctx,
159	.unassign_vif_chanctx    = wfx_unassign_vif_chanctx,
160	.remain_on_channel       = wfx_remain_on_channel,
161	.cancel_remain_on_channel = wfx_cancel_remain_on_channel,
162#ifdef CONFIG_PM
163	.suspend                 = wfx_suspend,
164	.resume                  = wfx_resume,
165	.set_wakeup              = wfx_set_wakeup,
166#endif
167};
168
169bool wfx_api_older_than(struct wfx_dev *wdev, int major, int minor)
170{
171	if (wdev->hw_caps.api_version_major < major)
172		return true;
173	if (wdev->hw_caps.api_version_major > major)
174		return false;
175	if (wdev->hw_caps.api_version_minor < minor)
176		return true;
177	return false;
178}
179
180/* The device needs data about the antenna configuration. This information in provided by PDS
181 * (Platform Data Set, this is the wording used in WF200 documentation) files. For hardware
182 * integrators, the full process to create PDS files is described here:
183 *   https://github.com/SiliconLabs/wfx-firmware/blob/master/PDS/README.md
184 *
185 * The PDS file is an array of Time-Length-Value structs.
186 */
187int wfx_send_pds(struct wfx_dev *wdev, u8 *buf, size_t len)
188{
189	int ret, chunk_type, chunk_len, chunk_num = 0;
190
191	if (*buf == '{') {
192		dev_err(wdev->dev, "PDS: malformed file (legacy format?)\n");
193		return -EINVAL;
194	}
195	while (len > 0) {
196		chunk_type = get_unaligned_le16(buf + 0);
197		chunk_len = get_unaligned_le16(buf + 2);
198		if (chunk_len < 4 || chunk_len > len) {
199			dev_err(wdev->dev, "PDS:%d: corrupted file\n", chunk_num);
200			return -EINVAL;
201		}
202		if (chunk_type != WFX_PDS_TLV_TYPE) {
203			dev_info(wdev->dev, "PDS:%d: skip unknown data\n", chunk_num);
204			goto next;
205		}
206		if (chunk_len > WFX_PDS_MAX_CHUNK_SIZE)
207			dev_warn(wdev->dev, "PDS:%d: unexpectedly large chunk\n", chunk_num);
208		if (buf[4] != '{' || buf[chunk_len - 1] != '}')
209			dev_warn(wdev->dev, "PDS:%d: unexpected content\n", chunk_num);
210
211		ret = wfx_hif_configuration(wdev, buf + 4, chunk_len - 4);
212		if (ret > 0) {
213			dev_err(wdev->dev, "PDS:%d: invalid data (unsupported options?)\n", chunk_num);
214			return -EINVAL;
215		}
216		if (ret == -ETIMEDOUT) {
217			dev_err(wdev->dev, "PDS:%d: chip didn't reply (corrupted file?)\n", chunk_num);
218			return ret;
219		}
220		if (ret) {
221			dev_err(wdev->dev, "PDS:%d: chip returned an unknown error\n", chunk_num);
222			return -EIO;
223		}
224next:
225		chunk_num++;
226		len -= chunk_len;
227		buf += chunk_len;
228	}
229	return 0;
230}
231
232static int wfx_send_pdata_pds(struct wfx_dev *wdev)
233{
234	int ret = 0;
235	const struct firmware *pds;
236	u8 *tmp_buf;
237
238	ret = request_firmware(&pds, wdev->pdata.file_pds, wdev->dev);
239	if (ret) {
240		dev_err(wdev->dev, "can't load antenna parameters (PDS file %s). The device may be unstable.\n",
241			wdev->pdata.file_pds);
242		return ret;
243	}
244	tmp_buf = kmemdup(pds->data, pds->size, GFP_KERNEL);
245	if (!tmp_buf) {
246		ret = -ENOMEM;
247		goto release_fw;
248	}
249	ret = wfx_send_pds(wdev, tmp_buf, pds->size);
250	kfree(tmp_buf);
251release_fw:
252	release_firmware(pds);
253	return ret;
254}
255
256static void wfx_free_common(void *data)
257{
258	struct wfx_dev *wdev = data;
259
260	mutex_destroy(&wdev->tx_power_loop_info_lock);
261	mutex_destroy(&wdev->rx_stats_lock);
262	mutex_destroy(&wdev->scan_lock);
263	mutex_destroy(&wdev->conf_mutex);
264	ieee80211_free_hw(wdev->hw);
265}
266
267struct wfx_dev *wfx_init_common(struct device *dev, const struct wfx_platform_data *pdata,
268				const struct wfx_hwbus_ops *hwbus_ops, void *hwbus_priv)
269{
270	struct ieee80211_hw *hw;
271	struct wfx_dev *wdev;
272
273	hw = ieee80211_alloc_hw(sizeof(struct wfx_dev), &wfx_ops);
274	if (!hw)
275		return NULL;
276
277	SET_IEEE80211_DEV(hw, dev);
278
279	ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
280	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
281	ieee80211_hw_set(hw, CONNECTION_MONITOR);
282	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
283	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
284	ieee80211_hw_set(hw, SIGNAL_DBM);
285	ieee80211_hw_set(hw, SUPPORTS_PS);
286	ieee80211_hw_set(hw, MFP_CAPABLE);
287
288	hw->vif_data_size = sizeof(struct wfx_vif);
289	hw->sta_data_size = sizeof(struct wfx_sta_priv);
290	hw->queues = 4;
291	hw->max_rates = 8;
292	hw->max_rate_tries = 8;
293	hw->extra_tx_headroom = sizeof(struct wfx_hif_msg) + sizeof(struct wfx_hif_req_tx) +
294				4 /* alignment */ + 8 /* TKIP IV */;
295	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
296				     BIT(NL80211_IFTYPE_ADHOC) |
297				     BIT(NL80211_IFTYPE_AP);
298	hw->wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
299					NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
300					NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P |
301					NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U;
302	hw->wiphy->features |= NL80211_FEATURE_AP_SCAN;
303#ifdef CONFIG_PM
304	hw->wiphy->wowlan = &wfx_wowlan_support;
305#endif
306	hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
307	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
308	hw->wiphy->max_remain_on_channel_duration = 5000;
309	hw->wiphy->max_ap_assoc_sta = HIF_LINK_ID_MAX;
310	hw->wiphy->max_scan_ssids = 2;
311	hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
312	hw->wiphy->n_iface_combinations = ARRAY_SIZE(wfx_iface_combinations);
313	hw->wiphy->iface_combinations = wfx_iface_combinations;
314	/* FIXME: also copy wfx_rates and wfx_2ghz_chantable */
315	hw->wiphy->bands[NL80211_BAND_2GHZ] = devm_kmemdup(dev, &wfx_band_2ghz,
316							   sizeof(wfx_band_2ghz), GFP_KERNEL);
317	if (!hw->wiphy->bands[NL80211_BAND_2GHZ])
318		goto err;
319
320	wdev = hw->priv;
321	wdev->hw = hw;
322	wdev->dev = dev;
323	wdev->hwbus_ops = hwbus_ops;
324	wdev->hwbus_priv = hwbus_priv;
325	memcpy(&wdev->pdata, pdata, sizeof(*pdata));
326	of_property_read_string(dev->of_node, "silabs,antenna-config-file", &wdev->pdata.file_pds);
327	wdev->pdata.gpio_wakeup = devm_gpiod_get_optional(dev, "wakeup", GPIOD_OUT_LOW);
328	if (IS_ERR(wdev->pdata.gpio_wakeup))
329		goto err;
330
331	if (wdev->pdata.gpio_wakeup)
332		gpiod_set_consumer_name(wdev->pdata.gpio_wakeup, "wfx wakeup");
333
334	mutex_init(&wdev->conf_mutex);
335	mutex_init(&wdev->scan_lock);
336	mutex_init(&wdev->rx_stats_lock);
337	mutex_init(&wdev->tx_power_loop_info_lock);
338	init_completion(&wdev->firmware_ready);
339	INIT_DELAYED_WORK(&wdev->cooling_timeout_work, wfx_cooling_timeout_work);
340	skb_queue_head_init(&wdev->tx_pending);
341	init_waitqueue_head(&wdev->tx_dequeue);
342	wfx_init_hif_cmd(&wdev->hif_cmd);
343
344	if (devm_add_action_or_reset(dev, wfx_free_common, wdev))
345		return NULL;
346
347	return wdev;
348
349err:
350	ieee80211_free_hw(hw);
351	return NULL;
352}
353
354int wfx_probe(struct wfx_dev *wdev)
355{
356	int i;
357	int err;
358	struct gpio_desc *gpio_saved;
359
360	/* During first part of boot, gpio_wakeup cannot yet been used. So prevent bh() to touch
361	 * it.
362	 */
363	gpio_saved = wdev->pdata.gpio_wakeup;
364	wdev->pdata.gpio_wakeup = NULL;
365	wdev->poll_irq = true;
366
367	wdev->bh_wq = alloc_workqueue("wfx_bh_wq", WQ_HIGHPRI | WQ_PERCPU, 0);
368	if (!wdev->bh_wq)
369		return -ENOMEM;
370
371	wfx_bh_register(wdev);
372
373	err = wfx_init_device(wdev);
374	if (err)
375		goto bh_unregister;
376
377	wfx_bh_poll_irq(wdev);
378	err = wait_for_completion_timeout(&wdev->firmware_ready, 1 * HZ);
379	if (err == 0) {
380		dev_err(wdev->dev, "timeout while waiting for startup indication\n");
381		err = -ETIMEDOUT;
382		goto bh_unregister;
383	}
384
385	/* FIXME: fill wiphy::hw_version */
386	dev_info(wdev->dev, "started firmware %d.%d.%d \"%s\" (API: %d.%d, keyset: %02X, caps: 0x%.8X)\n",
387		 wdev->hw_caps.firmware_major, wdev->hw_caps.firmware_minor,
388		 wdev->hw_caps.firmware_build, wdev->hw_caps.firmware_label,
389		 wdev->hw_caps.api_version_major, wdev->hw_caps.api_version_minor,
390		 wdev->keyset, wdev->hw_caps.link_mode);
391	snprintf(wdev->hw->wiphy->fw_version,
392		 sizeof(wdev->hw->wiphy->fw_version),
393		 "%d.%d.%d",
394		 wdev->hw_caps.firmware_major,
395		 wdev->hw_caps.firmware_minor,
396		 wdev->hw_caps.firmware_build);
397
398	if (wfx_api_older_than(wdev, 1, 0)) {
399		dev_err(wdev->dev, "unsupported firmware API version (expect 1 while firmware returns %d)\n",
400			wdev->hw_caps.api_version_major);
401		err = -EOPNOTSUPP;
402		goto bh_unregister;
403	}
404
405	if (wdev->hw_caps.link_mode == SEC_LINK_ENFORCED) {
406		dev_err(wdev->dev, "chip require secure_link, but can't negotiate it\n");
407		goto bh_unregister;
408	}
409
410	if (wdev->hw_caps.region_sel_mode) {
411		wdev->hw->wiphy->regulatory_flags |= REGULATORY_DISABLE_BEACON_HINTS;
412		wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[11].flags |=
413			IEEE80211_CHAN_NO_IR;
414		wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[12].flags |=
415			IEEE80211_CHAN_NO_IR;
416		wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[13].flags |=
417			IEEE80211_CHAN_DISABLED;
418	}
419
420	dev_dbg(wdev->dev, "sending configuration file %s\n", wdev->pdata.file_pds);
421	err = wfx_send_pdata_pds(wdev);
422	if (err < 0 && err != -ENOENT)
423		goto bh_unregister;
424
425	wdev->poll_irq = false;
426	err = wdev->hwbus_ops->irq_subscribe(wdev->hwbus_priv);
427	if (err)
428		goto bh_unregister;
429
430	err = wfx_hif_use_multi_tx_conf(wdev, true);
431	if (err)
432		dev_err(wdev->dev, "misconfigured IRQ?\n");
433
434	wdev->pdata.gpio_wakeup = gpio_saved;
435	if (wdev->pdata.gpio_wakeup) {
436		dev_dbg(wdev->dev, "enable 'quiescent' power mode with wakeup GPIO and PDS file %s\n",
437			wdev->pdata.file_pds);
438		gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1);
439		wfx_control_reg_write(wdev, 0);
440		wfx_hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_QUIESCENT);
441	} else {
442		wfx_hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_DOZE);
443	}
444
445	for (i = 0; i < ARRAY_SIZE(wdev->addresses); i++) {
446		eth_zero_addr(wdev->addresses[i].addr);
447		err = of_get_mac_address(wdev->dev->of_node, wdev->addresses[i].addr);
448		if (!err)
449			wdev->addresses[i].addr[ETH_ALEN - 1] += i;
450		else
451			ether_addr_copy(wdev->addresses[i].addr, wdev->hw_caps.mac_addr[i]);
452		if (!is_valid_ether_addr(wdev->addresses[i].addr)) {
453			dev_warn(wdev->dev, "using random MAC address\n");
454			eth_random_addr(wdev->addresses[i].addr);
455		}
456		dev_info(wdev->dev, "MAC address %d: %pM\n", i, wdev->addresses[i].addr);
457	}
458	wdev->hw->wiphy->n_addresses = ARRAY_SIZE(wdev->addresses);
459	wdev->hw->wiphy->addresses = wdev->addresses;
460
461	if (!wfx_api_older_than(wdev, 3, 8))
462		wdev->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
463
464	err = ieee80211_register_hw(wdev->hw);
465	if (err)
466		goto irq_unsubscribe;
467
468	err = wfx_debug_init(wdev);
469	if (err)
470		goto ieee80211_unregister;
471
472	return 0;
473
474ieee80211_unregister:
475	ieee80211_unregister_hw(wdev->hw);
476irq_unsubscribe:
477	wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
478bh_unregister:
479	wfx_bh_unregister(wdev);
480	destroy_workqueue(wdev->bh_wq);
481	return err;
482}
483
484void wfx_release(struct wfx_dev *wdev)
485{
486	ieee80211_unregister_hw(wdev->hw);
487	wfx_hif_shutdown(wdev);
488	wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
489	wfx_bh_unregister(wdev);
490	destroy_workqueue(wdev->bh_wq);
491}
492
493static int __init wfx_core_init(void)
494{
495	int ret = 0;
496
497	if (IS_ENABLED(CONFIG_SPI)) {
498		ret = spi_register_driver(&wfx_spi_driver);
499		if (ret)
500			goto out;
501	}
502	if (IS_ENABLED(CONFIG_MMC)) {
503		ret = sdio_register_driver(&wfx_sdio_driver);
504		if (ret)
505			goto unregister_spi;
506	}
507
508	return 0;
509
510unregister_spi:
511	if (IS_ENABLED(CONFIG_SPI))
512		spi_unregister_driver(&wfx_spi_driver);
513out:
514	return ret;
515}
516module_init(wfx_core_init);
517
518static void __exit wfx_core_exit(void)
519{
520	if (IS_ENABLED(CONFIG_MMC))
521		sdio_unregister_driver(&wfx_sdio_driver);
522	if (IS_ENABLED(CONFIG_SPI))
523		spi_unregister_driver(&wfx_spi_driver);
524}
525module_exit(wfx_core_exit);
~/ Read-only file preview.