Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Device probe and register.
4 *
5 * Copyright (c) 2017-2019, 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.h>
17#include <linux/gpio/consumer.h>
18#include <linux/mmc/sdio_func.h>
19#include <linux/spi/spi.h>
20#include <linux/etherdevice.h>
21#include <linux/firmware.h>
22
23#include "main.h"
24#include "wfx.h"
25#include "fwio.h"
26#include "hwio.h"
27#include "bus.h"
28#include "bh.h"
29#include "sta.h"
30#include "key.h"
31#include "scan.h"
32#include "debug.h"
33#include "data_tx.h"
34#include "secure_link.h"
35#include "hif_tx_mib.h"
36#include "hif_api_cmd.h"
37
38#define WFX_PDS_MAX_SIZE 1500
39
40MODULE_DESCRIPTION("Silicon Labs 802.11 Wireless LAN driver for WFx");
41MODULE_AUTHOR("Jérôme Pouiller <jerome.pouiller@silabs.com>");
42MODULE_LICENSE("GPL");
43
44static int gpio_wakeup = -2;
45module_param(gpio_wakeup, int, 0644);
46MODULE_PARM_DESC(gpio_wakeup, "gpio number for wakeup. -1 for none.");
47
48#define RATETAB_ENT(_rate, _rateid, _flags) { \
49 .bitrate = (_rate), \
50 .hw_value = (_rateid), \
51 .flags = (_flags), \
52}
53
54static struct ieee80211_rate wfx_rates[] = {
55 RATETAB_ENT(10, 0, 0),
56 RATETAB_ENT(20, 1, IEEE80211_RATE_SHORT_PREAMBLE),
57 RATETAB_ENT(55, 2, IEEE80211_RATE_SHORT_PREAMBLE),
58 RATETAB_ENT(110, 3, IEEE80211_RATE_SHORT_PREAMBLE),
59 RATETAB_ENT(60, 6, 0),
60 RATETAB_ENT(90, 7, 0),
61 RATETAB_ENT(120, 8, 0),
62 RATETAB_ENT(180, 9, 0),
63 RATETAB_ENT(240, 10, 0),
64 RATETAB_ENT(360, 11, 0),
65 RATETAB_ENT(480, 12, 0),
66 RATETAB_ENT(540, 13, 0),
67};
68
69#define CHAN2G(_channel, _freq, _flags) { \
70 .band = NL80211_BAND_2GHZ, \
71 .center_freq = (_freq), \
72 .hw_value = (_channel), \
73 .flags = (_flags), \
74 .max_antenna_gain = 0, \
75 .max_power = 30, \
76}
77
78static struct ieee80211_channel wfx_2ghz_chantable[] = {
79 CHAN2G(1, 2412, 0),
80 CHAN2G(2, 2417, 0),
81 CHAN2G(3, 2422, 0),
82 CHAN2G(4, 2427, 0),
83 CHAN2G(5, 2432, 0),
84 CHAN2G(6, 2437, 0),
85 CHAN2G(7, 2442, 0),
86 CHAN2G(8, 2447, 0),
87 CHAN2G(9, 2452, 0),
88 CHAN2G(10, 2457, 0),
89 CHAN2G(11, 2462, 0),
90 CHAN2G(12, 2467, 0),
91 CHAN2G(13, 2472, 0),
92 CHAN2G(14, 2484, 0),
93};
94
95static const struct ieee80211_supported_band wfx_band_2ghz = {
96 .channels = wfx_2ghz_chantable,
97 .n_channels = ARRAY_SIZE(wfx_2ghz_chantable),
98 .bitrates = wfx_rates,
99 .n_bitrates = ARRAY_SIZE(wfx_rates),
100 .ht_cap = {
101 // Receive caps
102 .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
103 IEEE80211_HT_CAP_MAX_AMSDU |
104 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT),
105 .ht_supported = 1,
106 .ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K,
107 .ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE,
108 .mcs = {
109 .rx_mask = { 0xFF }, // MCS0 to MCS7
110 .rx_highest = cpu_to_le16(72),
111 .tx_params = IEEE80211_HT_MCS_TX_DEFINED,
112 },
113 },
114};
115
116static const struct ieee80211_iface_limit wdev_iface_limits[] = {
117 { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
118 { .max = 1, .types = BIT(NL80211_IFTYPE_AP) },
119};
120
121static const struct ieee80211_iface_combination wfx_iface_combinations[] = {
122 {
123 .num_different_channels = 2,
124 .max_interfaces = 2,
125 .limits = wdev_iface_limits,
126 .n_limits = ARRAY_SIZE(wdev_iface_limits),
127 }
128};
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 .join_ibss = wfx_join_ibss,
138 .leave_ibss = wfx_leave_ibss,
139 .conf_tx = wfx_conf_tx,
140 .hw_scan = wfx_hw_scan,
141 .cancel_hw_scan = wfx_cancel_hw_scan,
142 .start_ap = wfx_start_ap,
143 .stop_ap = wfx_stop_ap,
144 .sta_add = wfx_sta_add,
145 .sta_remove = wfx_sta_remove,
146 .set_tim = wfx_set_tim,
147 .set_key = wfx_set_key,
148 .set_rts_threshold = wfx_set_rts_threshold,
149 .set_default_unicast_key = wfx_set_default_unicast_key,
150 .bss_info_changed = wfx_bss_info_changed,
151 .prepare_multicast = wfx_prepare_multicast,
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};
161
162bool wfx_api_older_than(struct wfx_dev *wdev, int major, int minor)
163{
164 if (wdev->hw_caps.api_version_major < major)
165 return true;
166 if (wdev->hw_caps.api_version_major > major)
167 return false;
168 if (wdev->hw_caps.api_version_minor < minor)
169 return true;
170 return false;
171}
172
173struct gpio_desc *wfx_get_gpio(struct device *dev,
174 int override, const char *label)
175{
176 struct gpio_desc *ret;
177 char label_buf[256];
178
179 if (override >= 0) {
180 snprintf(label_buf, sizeof(label_buf), "wfx_%s", label);
181 ret = ERR_PTR(devm_gpio_request_one(dev, override,
182 GPIOF_OUT_INIT_LOW,
183 label_buf));
184 if (!ret)
185 ret = gpio_to_desc(override);
186 } else if (override == -1) {
187 ret = NULL;
188 } else {
189 ret = devm_gpiod_get(dev, label, GPIOD_OUT_LOW);
190 }
191 if (IS_ERR_OR_NULL(ret)) {
192 if (!ret || PTR_ERR(ret) == -ENOENT)
193 dev_warn(dev, "gpio %s is not defined\n", label);
194 else
195 dev_warn(dev, "error while requesting gpio %s\n",
196 label);
197 ret = NULL;
198 } else {
199 dev_dbg(dev, "using gpio %d for %s\n",
200 desc_to_gpio(ret), label);
201 }
202 return ret;
203}
204
205/* NOTE: wfx_send_pds() destroy buf */
206int wfx_send_pds(struct wfx_dev *wdev, u8 *buf, size_t len)
207{
208 int ret;
209 int start, brace_level, i;
210
211 start = 0;
212 brace_level = 0;
213 if (buf[0] != '{') {
214 dev_err(wdev->dev, "valid PDS start with '{'. Did you forget to compress it?\n");
215 return -EINVAL;
216 }
217 for (i = 1; i < len - 1; i++) {
218 if (buf[i] == '{')
219 brace_level++;
220 if (buf[i] == '}')
221 brace_level--;
222 if (buf[i] == '}' && !brace_level) {
223 i++;
224 if (i - start + 1 > WFX_PDS_MAX_SIZE)
225 return -EFBIG;
226 buf[start] = '{';
227 buf[i] = 0;
228 dev_dbg(wdev->dev, "send PDS '%s}'\n", buf + start);
229 buf[i] = '}';
230 ret = hif_configuration(wdev, buf + start,
231 i - start + 1);
232 if (ret > 0) {
233 dev_err(wdev->dev, "PDS bytes %d to %d: invalid data (unsupported options?)\n",
234 start, i);
235 return -EINVAL;
236 }
237 if (ret == -ETIMEDOUT) {
238 dev_err(wdev->dev, "PDS bytes %d to %d: chip didn't reply (corrupted file?)\n",
239 start, i);
240 return ret;
241 }
242 if (ret) {
243 dev_err(wdev->dev, "PDS bytes %d to %d: chip returned an unknown error\n",
244 start, i);
245 return -EIO;
246 }
247 buf[i] = ',';
248 start = i;
249 }
250 }
251 return 0;
252}
253
254static int wfx_send_pdata_pds(struct wfx_dev *wdev)
255{
256 int ret = 0;
257 const struct firmware *pds;
258 u8 *tmp_buf;
259
260 ret = request_firmware(&pds, wdev->pdata.file_pds, wdev->dev);
261 if (ret) {
262 dev_err(wdev->dev, "can't load PDS file %s\n",
263 wdev->pdata.file_pds);
264 return ret;
265 }
266 tmp_buf = kmemdup(pds->data, pds->size, GFP_KERNEL);
267 ret = wfx_send_pds(wdev, tmp_buf, pds->size);
268 kfree(tmp_buf);
269 release_firmware(pds);
270 return ret;
271}
272
273static void wfx_free_common(void *data)
274{
275 struct wfx_dev *wdev = data;
276
277 mutex_destroy(&wdev->tx_power_loop_info_lock);
278 mutex_destroy(&wdev->rx_stats_lock);
279 mutex_destroy(&wdev->conf_mutex);
280 ieee80211_free_hw(wdev->hw);
281}
282
283struct wfx_dev *wfx_init_common(struct device *dev,
284 const struct wfx_platform_data *pdata,
285 const struct hwbus_ops *hwbus_ops,
286 void *hwbus_priv)
287{
288 struct ieee80211_hw *hw;
289 struct wfx_dev *wdev;
290
291 hw = ieee80211_alloc_hw(sizeof(struct wfx_dev), &wfx_ops);
292 if (!hw)
293 return NULL;
294
295 SET_IEEE80211_DEV(hw, dev);
296
297 ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
298 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
299 ieee80211_hw_set(hw, CONNECTION_MONITOR);
300 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
301 ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
302 ieee80211_hw_set(hw, SIGNAL_DBM);
303 ieee80211_hw_set(hw, SUPPORTS_PS);
304 ieee80211_hw_set(hw, MFP_CAPABLE);
305
306 hw->vif_data_size = sizeof(struct wfx_vif);
307 hw->sta_data_size = sizeof(struct wfx_sta_priv);
308 hw->queues = 4;
309 hw->max_rates = 8;
310 hw->max_rate_tries = 8;
311 hw->extra_tx_headroom = sizeof(struct hif_sl_msg_hdr) +
312 sizeof(struct hif_msg)
313 + sizeof(struct hif_req_tx)
314 + 4 /* alignment */ + 8 /* TKIP IV */;
315 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
316 BIT(NL80211_IFTYPE_ADHOC) |
317 BIT(NL80211_IFTYPE_AP);
318 hw->wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
319 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
320 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P |
321 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U;
322 hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
323 hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
324 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
325 hw->wiphy->max_ap_assoc_sta = HIF_LINK_ID_MAX;
326 hw->wiphy->max_scan_ssids = 2;
327 hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
328 hw->wiphy->n_iface_combinations = ARRAY_SIZE(wfx_iface_combinations);
329 hw->wiphy->iface_combinations = wfx_iface_combinations;
330 hw->wiphy->bands[NL80211_BAND_2GHZ] = devm_kmalloc(dev, sizeof(wfx_band_2ghz), GFP_KERNEL);
331 // FIXME: also copy wfx_rates and wfx_2ghz_chantable
332 memcpy(hw->wiphy->bands[NL80211_BAND_2GHZ], &wfx_band_2ghz,
333 sizeof(wfx_band_2ghz));
334
335 wdev = hw->priv;
336 wdev->hw = hw;
337 wdev->dev = dev;
338 wdev->hwbus_ops = hwbus_ops;
339 wdev->hwbus_priv = hwbus_priv;
340 memcpy(&wdev->pdata, pdata, sizeof(*pdata));
341 of_property_read_string(dev->of_node, "config-file",
342 &wdev->pdata.file_pds);
343 wdev->pdata.gpio_wakeup = wfx_get_gpio(dev, gpio_wakeup, "wakeup");
344 wfx_sl_fill_pdata(dev, &wdev->pdata);
345
346 mutex_init(&wdev->conf_mutex);
347 mutex_init(&wdev->rx_stats_lock);
348 mutex_init(&wdev->tx_power_loop_info_lock);
349 init_completion(&wdev->firmware_ready);
350 INIT_DELAYED_WORK(&wdev->cooling_timeout_work,
351 wfx_cooling_timeout_work);
352 wfx_init_hif_cmd(&wdev->hif_cmd);
353 wfx_tx_queues_init(wdev);
354
355 if (devm_add_action_or_reset(dev, wfx_free_common, wdev))
356 return NULL;
357
358 return wdev;
359}
360
361int wfx_probe(struct wfx_dev *wdev)
362{
363 int i;
364 int err;
365 const void *macaddr;
366 struct gpio_desc *gpio_saved;
367
368 // During first part of boot, gpio_wakeup cannot yet been used. So
369 // prevent bh() to touch it.
370 gpio_saved = wdev->pdata.gpio_wakeup;
371 wdev->pdata.gpio_wakeup = NULL;
372 wdev->poll_irq = true;
373
374 wfx_bh_register(wdev);
375
376 err = wfx_init_device(wdev);
377 if (err)
378 goto err0;
379
380 wfx_bh_poll_irq(wdev);
381 err = wait_for_completion_timeout(&wdev->firmware_ready, 1 * HZ);
382 if (err <= 0) {
383 if (err == 0) {
384 dev_err(wdev->dev, "timeout while waiting for startup indication\n");
385 err = -ETIMEDOUT;
386 } else if (err == -ERESTARTSYS) {
387 dev_info(wdev->dev, "probe interrupted by user\n");
388 }
389 goto err0;
390 }
391
392 // FIXME: fill wiphy::hw_version
393 dev_info(wdev->dev, "started firmware %d.%d.%d \"%s\" (API: %d.%d, keyset: %02X, caps: 0x%.8X)\n",
394 wdev->hw_caps.firmware_major, wdev->hw_caps.firmware_minor,
395 wdev->hw_caps.firmware_build, wdev->hw_caps.firmware_label,
396 wdev->hw_caps.api_version_major,
397 wdev->hw_caps.api_version_minor,
398 wdev->keyset, *((u32 *)&wdev->hw_caps.capabilities));
399 snprintf(wdev->hw->wiphy->fw_version,
400 sizeof(wdev->hw->wiphy->fw_version),
401 "%d.%d.%d",
402 wdev->hw_caps.firmware_major,
403 wdev->hw_caps.firmware_minor,
404 wdev->hw_caps.firmware_build);
405
406 if (wfx_api_older_than(wdev, 1, 0)) {
407 dev_err(wdev->dev,
408 "unsupported firmware API version (expect 1 while firmware returns %d)\n",
409 wdev->hw_caps.api_version_major);
410 err = -ENOTSUPP;
411 goto err0;
412 }
413
414 err = wfx_sl_init(wdev);
415 if (err && wdev->hw_caps.capabilities.link_mode == SEC_LINK_ENFORCED) {
416 dev_err(wdev->dev,
417 "chip require secure_link, but can't negociate it\n");
418 goto err0;
419 }
420
421 if (wdev->hw_caps.regul_sel_mode_info.region_sel_mode) {
422 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[11].flags |= IEEE80211_CHAN_NO_IR;
423 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[12].flags |= IEEE80211_CHAN_NO_IR;
424 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[13].flags |= IEEE80211_CHAN_DISABLED;
425 }
426
427 dev_dbg(wdev->dev, "sending configuration file %s\n",
428 wdev->pdata.file_pds);
429 err = wfx_send_pdata_pds(wdev);
430 if (err < 0)
431 goto err0;
432
433 wdev->poll_irq = false;
434 err = wdev->hwbus_ops->irq_subscribe(wdev->hwbus_priv);
435 if (err)
436 goto err0;
437
438 err = hif_use_multi_tx_conf(wdev, true);
439 if (err)
440 dev_err(wdev->dev, "misconfigured IRQ?\n");
441
442 wdev->pdata.gpio_wakeup = gpio_saved;
443 if (wdev->pdata.gpio_wakeup) {
444 dev_dbg(wdev->dev,
445 "enable 'quiescent' power mode with gpio %d and PDS file %s\n",
446 desc_to_gpio(wdev->pdata.gpio_wakeup),
447 wdev->pdata.file_pds);
448 gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1);
449 control_reg_write(wdev, 0);
450 hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_QUIESCENT);
451 } else {
452 hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_DOZE);
453 }
454
455 for (i = 0; i < ARRAY_SIZE(wdev->addresses); i++) {
456 eth_zero_addr(wdev->addresses[i].addr);
457 macaddr = of_get_mac_address(wdev->dev->of_node);
458 if (!IS_ERR_OR_NULL(macaddr)) {
459 ether_addr_copy(wdev->addresses[i].addr, macaddr);
460 wdev->addresses[i].addr[ETH_ALEN - 1] += i;
461 } else {
462 ether_addr_copy(wdev->addresses[i].addr,
463 wdev->hw_caps.mac_addr[i]);
464 }
465 if (!is_valid_ether_addr(wdev->addresses[i].addr)) {
466 dev_warn(wdev->dev, "using random MAC address\n");
467 eth_random_addr(wdev->addresses[i].addr);
468 }
469 dev_info(wdev->dev, "MAC address %d: %pM\n", i,
470 wdev->addresses[i].addr);
471 }
472 wdev->hw->wiphy->n_addresses = ARRAY_SIZE(wdev->addresses);
473 wdev->hw->wiphy->addresses = wdev->addresses;
474
475 err = ieee80211_register_hw(wdev->hw);
476 if (err)
477 goto err1;
478
479 err = wfx_debug_init(wdev);
480 if (err)
481 goto err2;
482
483 return 0;
484
485err2:
486 ieee80211_unregister_hw(wdev->hw);
487err1:
488 wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
489err0:
490 wfx_bh_unregister(wdev);
491 return err;
492}
493
494void wfx_release(struct wfx_dev *wdev)
495{
496 ieee80211_unregister_hw(wdev->hw);
497 hif_shutdown(wdev);
498 wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
499 wfx_bh_unregister(wdev);
500 wfx_sl_deinit(wdev);
501}
502
503static int __init wfx_core_init(void)
504{
505 int ret = 0;
506
507 if (IS_ENABLED(CONFIG_SPI))
508 ret = spi_register_driver(&wfx_spi_driver);
509 if (IS_ENABLED(CONFIG_MMC) && !ret)
510 ret = sdio_register_driver(&wfx_sdio_driver);
511 return ret;
512}
513module_init(wfx_core_init);
514
515static void __exit wfx_core_exit(void)
516{
517 if (IS_ENABLED(CONFIG_MMC))
518 sdio_unregister_driver(&wfx_sdio_driver);
519 if (IS_ENABLED(CONFIG_SPI))
520 spi_unregister_driver(&wfx_spi_driver);
521}
522module_exit(wfx_core_exit);