1 // SPDX-License-Identifier: GPL-2.0-only
3 * Device probe and register.
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.
13 #include <linux/module.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>
33 #include "hif_tx_mib.h"
34 #include "hif_api_cmd.h"
36 #define WFX_PDS_TLV_TYPE 0x4450 // "PD" (Platform Data) in ascii little-endian
37 #define WFX_PDS_MAX_CHUNK_SIZE 1500
39 MODULE_DESCRIPTION("Silicon Labs 802.11 Wireless LAN driver for WF200");
40 MODULE_AUTHOR("Jérôme Pouiller <jerome.pouiller@silabs.com>");
41 MODULE_LICENSE("GPL");
43 #define RATETAB_ENT(_rate, _rateid, _flags) { \
45 .hw_value = (_rateid), \
49 static 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),
64 #define CHAN2G(_channel, _freq, _flags) { \
65 .band = NL80211_BAND_2GHZ, \
66 .center_freq = (_freq), \
67 .hw_value = (_channel), \
69 .max_antenna_gain = 0, \
73 static struct ieee80211_channel wfx_2ghz_chantable
[] = {
90 static 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
),
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
),
100 .ampdu_factor
= IEEE80211_HT_MAX_AMPDU_16K
,
101 .ampdu_density
= IEEE80211_HT_MPDU_DENSITY_NONE
,
103 .rx_mask
= { 0xFF }, /* MCS0 to MCS7 */
104 .rx_highest
= cpu_to_le16(72),
105 .tx_params
= IEEE80211_HT_MCS_TX_DEFINED
,
110 static const struct ieee80211_iface_limit wdev_iface_limits
[] = {
111 { .max
= 1, .types
= BIT(NL80211_IFTYPE_STATION
) },
112 { .max
= 1, .types
= BIT(NL80211_IFTYPE_AP
) },
115 static const struct ieee80211_iface_combination wfx_iface_combinations
[] = {
117 .num_different_channels
= 2,
119 .limits
= wdev_iface_limits
,
120 .n_limits
= ARRAY_SIZE(wdev_iface_limits
),
124 static const struct ieee80211_ops wfx_ops
= {
127 .add_interface
= wfx_add_interface
,
128 .remove_interface
= wfx_remove_interface
,
129 .config
= wfx_config
,
131 .wake_tx_queue
= ieee80211_handle_wake_tx_queue
,
132 .join_ibss
= wfx_join_ibss
,
133 .leave_ibss
= wfx_leave_ibss
,
134 .conf_tx
= wfx_conf_tx
,
135 .hw_scan
= wfx_hw_scan
,
136 .cancel_hw_scan
= wfx_cancel_hw_scan
,
137 .start_ap
= wfx_start_ap
,
138 .stop_ap
= wfx_stop_ap
,
139 .sta_add
= wfx_sta_add
,
140 .sta_remove
= wfx_sta_remove
,
141 .set_tim
= wfx_set_tim
,
142 .set_key
= wfx_set_key
,
143 .set_rts_threshold
= wfx_set_rts_threshold
,
144 .set_default_unicast_key
= wfx_set_default_unicast_key
,
145 .bss_info_changed
= wfx_bss_info_changed
,
146 .configure_filter
= wfx_configure_filter
,
147 .ampdu_action
= wfx_ampdu_action
,
149 .add_chanctx
= wfx_add_chanctx
,
150 .remove_chanctx
= wfx_remove_chanctx
,
151 .change_chanctx
= wfx_change_chanctx
,
152 .assign_vif_chanctx
= wfx_assign_vif_chanctx
,
153 .unassign_vif_chanctx
= wfx_unassign_vif_chanctx
,
154 .remain_on_channel
= wfx_remain_on_channel
,
155 .cancel_remain_on_channel
= wfx_cancel_remain_on_channel
,
158 bool wfx_api_older_than(struct wfx_dev
*wdev
, int major
, int minor
)
160 if (wdev
->hw_caps
.api_version_major
< major
)
162 if (wdev
->hw_caps
.api_version_major
> major
)
164 if (wdev
->hw_caps
.api_version_minor
< minor
)
169 /* The device needs data about the antenna configuration. This information in provided by PDS
170 * (Platform Data Set, this is the wording used in WF200 documentation) files. For hardware
171 * integrators, the full process to create PDS files is described here:
172 * https://github.com/SiliconLabs/wfx-firmware/blob/master/PDS/README.md
174 * The PDS file is an array of Time-Length-Value structs.
176 int wfx_send_pds(struct wfx_dev
*wdev
, u8
*buf
, size_t len
)
178 int ret
, chunk_type
, chunk_len
, chunk_num
= 0;
181 dev_err(wdev
->dev
, "PDS: malformed file (legacy format?)\n");
185 chunk_type
= get_unaligned_le16(buf
+ 0);
186 chunk_len
= get_unaligned_le16(buf
+ 2);
187 if (chunk_len
< 4 || chunk_len
> len
) {
188 dev_err(wdev
->dev
, "PDS:%d: corrupted file\n", chunk_num
);
191 if (chunk_type
!= WFX_PDS_TLV_TYPE
) {
192 dev_info(wdev
->dev
, "PDS:%d: skip unknown data\n", chunk_num
);
195 if (chunk_len
> WFX_PDS_MAX_CHUNK_SIZE
)
196 dev_warn(wdev
->dev
, "PDS:%d: unexpectedly large chunk\n", chunk_num
);
197 if (buf
[4] != '{' || buf
[chunk_len
- 1] != '}')
198 dev_warn(wdev
->dev
, "PDS:%d: unexpected content\n", chunk_num
);
200 ret
= wfx_hif_configuration(wdev
, buf
+ 4, chunk_len
- 4);
202 dev_err(wdev
->dev
, "PDS:%d: invalid data (unsupported options?)\n", chunk_num
);
205 if (ret
== -ETIMEDOUT
) {
206 dev_err(wdev
->dev
, "PDS:%d: chip didn't reply (corrupted file?)\n", chunk_num
);
210 dev_err(wdev
->dev
, "PDS:%d: chip returned an unknown error\n", chunk_num
);
221 static int wfx_send_pdata_pds(struct wfx_dev
*wdev
)
224 const struct firmware
*pds
;
227 ret
= request_firmware(&pds
, wdev
->pdata
.file_pds
, wdev
->dev
);
229 dev_err(wdev
->dev
, "can't load antenna parameters (PDS file %s). The device may be unstable.\n",
230 wdev
->pdata
.file_pds
);
233 tmp_buf
= kmemdup(pds
->data
, pds
->size
, GFP_KERNEL
);
238 ret
= wfx_send_pds(wdev
, tmp_buf
, pds
->size
);
241 release_firmware(pds
);
245 static void wfx_free_common(void *data
)
247 struct wfx_dev
*wdev
= data
;
249 mutex_destroy(&wdev
->tx_power_loop_info_lock
);
250 mutex_destroy(&wdev
->rx_stats_lock
);
251 mutex_destroy(&wdev
->scan_lock
);
252 mutex_destroy(&wdev
->conf_mutex
);
253 ieee80211_free_hw(wdev
->hw
);
256 struct wfx_dev
*wfx_init_common(struct device
*dev
, const struct wfx_platform_data
*pdata
,
257 const struct wfx_hwbus_ops
*hwbus_ops
, void *hwbus_priv
)
259 struct ieee80211_hw
*hw
;
260 struct wfx_dev
*wdev
;
262 hw
= ieee80211_alloc_hw(sizeof(struct wfx_dev
), &wfx_ops
);
266 SET_IEEE80211_DEV(hw
, dev
);
268 ieee80211_hw_set(hw
, TX_AMPDU_SETUP_IN_HW
);
269 ieee80211_hw_set(hw
, AMPDU_AGGREGATION
);
270 ieee80211_hw_set(hw
, CONNECTION_MONITOR
);
271 ieee80211_hw_set(hw
, REPORTS_TX_ACK_STATUS
);
272 ieee80211_hw_set(hw
, SUPPORTS_DYNAMIC_PS
);
273 ieee80211_hw_set(hw
, SIGNAL_DBM
);
274 ieee80211_hw_set(hw
, SUPPORTS_PS
);
275 ieee80211_hw_set(hw
, MFP_CAPABLE
);
277 hw
->vif_data_size
= sizeof(struct wfx_vif
);
278 hw
->sta_data_size
= sizeof(struct wfx_sta_priv
);
281 hw
->max_rate_tries
= 8;
282 hw
->extra_tx_headroom
= sizeof(struct wfx_hif_msg
) + sizeof(struct wfx_hif_req_tx
) +
283 4 /* alignment */ + 8 /* TKIP IV */;
284 hw
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
) |
285 BIT(NL80211_IFTYPE_ADHOC
) |
286 BIT(NL80211_IFTYPE_AP
);
287 hw
->wiphy
->probe_resp_offload
= NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS
|
288 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2
|
289 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P
|
290 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U
;
291 hw
->wiphy
->features
|= NL80211_FEATURE_AP_SCAN
;
292 hw
->wiphy
->flags
|= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD
;
293 hw
->wiphy
->flags
|= WIPHY_FLAG_AP_UAPSD
;
294 hw
->wiphy
->max_remain_on_channel_duration
= 5000;
295 hw
->wiphy
->max_ap_assoc_sta
= HIF_LINK_ID_MAX
;
296 hw
->wiphy
->max_scan_ssids
= 2;
297 hw
->wiphy
->max_scan_ie_len
= IEEE80211_MAX_DATA_LEN
;
298 hw
->wiphy
->n_iface_combinations
= ARRAY_SIZE(wfx_iface_combinations
);
299 hw
->wiphy
->iface_combinations
= wfx_iface_combinations
;
300 /* FIXME: also copy wfx_rates and wfx_2ghz_chantable */
301 hw
->wiphy
->bands
[NL80211_BAND_2GHZ
] = devm_kmemdup(dev
, &wfx_band_2ghz
,
302 sizeof(wfx_band_2ghz
), GFP_KERNEL
);
303 if (!hw
->wiphy
->bands
[NL80211_BAND_2GHZ
])
309 wdev
->hwbus_ops
= hwbus_ops
;
310 wdev
->hwbus_priv
= hwbus_priv
;
311 memcpy(&wdev
->pdata
, pdata
, sizeof(*pdata
));
312 of_property_read_string(dev
->of_node
, "silabs,antenna-config-file", &wdev
->pdata
.file_pds
);
313 wdev
->pdata
.gpio_wakeup
= devm_gpiod_get_optional(dev
, "wakeup", GPIOD_OUT_LOW
);
314 if (IS_ERR(wdev
->pdata
.gpio_wakeup
))
317 if (wdev
->pdata
.gpio_wakeup
)
318 gpiod_set_consumer_name(wdev
->pdata
.gpio_wakeup
, "wfx wakeup");
320 mutex_init(&wdev
->conf_mutex
);
321 mutex_init(&wdev
->scan_lock
);
322 mutex_init(&wdev
->rx_stats_lock
);
323 mutex_init(&wdev
->tx_power_loop_info_lock
);
324 init_completion(&wdev
->firmware_ready
);
325 INIT_DELAYED_WORK(&wdev
->cooling_timeout_work
, wfx_cooling_timeout_work
);
326 skb_queue_head_init(&wdev
->tx_pending
);
327 init_waitqueue_head(&wdev
->tx_dequeue
);
328 wfx_init_hif_cmd(&wdev
->hif_cmd
);
330 if (devm_add_action_or_reset(dev
, wfx_free_common
, wdev
))
336 ieee80211_free_hw(hw
);
340 int wfx_probe(struct wfx_dev
*wdev
)
344 struct gpio_desc
*gpio_saved
;
346 /* During first part of boot, gpio_wakeup cannot yet been used. So prevent bh() to touch
349 gpio_saved
= wdev
->pdata
.gpio_wakeup
;
350 wdev
->pdata
.gpio_wakeup
= NULL
;
351 wdev
->poll_irq
= true;
353 wdev
->bh_wq
= alloc_workqueue("wfx_bh_wq", WQ_HIGHPRI
, 0);
357 wfx_bh_register(wdev
);
359 err
= wfx_init_device(wdev
);
363 wfx_bh_poll_irq(wdev
);
364 err
= wait_for_completion_timeout(&wdev
->firmware_ready
, 1 * HZ
);
366 dev_err(wdev
->dev
, "timeout while waiting for startup indication\n");
371 /* FIXME: fill wiphy::hw_version */
372 dev_info(wdev
->dev
, "started firmware %d.%d.%d \"%s\" (API: %d.%d, keyset: %02X, caps: 0x%.8X)\n",
373 wdev
->hw_caps
.firmware_major
, wdev
->hw_caps
.firmware_minor
,
374 wdev
->hw_caps
.firmware_build
, wdev
->hw_caps
.firmware_label
,
375 wdev
->hw_caps
.api_version_major
, wdev
->hw_caps
.api_version_minor
,
376 wdev
->keyset
, wdev
->hw_caps
.link_mode
);
377 snprintf(wdev
->hw
->wiphy
->fw_version
,
378 sizeof(wdev
->hw
->wiphy
->fw_version
),
380 wdev
->hw_caps
.firmware_major
,
381 wdev
->hw_caps
.firmware_minor
,
382 wdev
->hw_caps
.firmware_build
);
384 if (wfx_api_older_than(wdev
, 1, 0)) {
385 dev_err(wdev
->dev
, "unsupported firmware API version (expect 1 while firmware returns %d)\n",
386 wdev
->hw_caps
.api_version_major
);
391 if (wdev
->hw_caps
.link_mode
== SEC_LINK_ENFORCED
) {
392 dev_err(wdev
->dev
, "chip require secure_link, but can't negotiate it\n");
396 if (wdev
->hw_caps
.region_sel_mode
) {
397 wdev
->hw
->wiphy
->regulatory_flags
|= REGULATORY_DISABLE_BEACON_HINTS
;
398 wdev
->hw
->wiphy
->bands
[NL80211_BAND_2GHZ
]->channels
[11].flags
|=
399 IEEE80211_CHAN_NO_IR
;
400 wdev
->hw
->wiphy
->bands
[NL80211_BAND_2GHZ
]->channels
[12].flags
|=
401 IEEE80211_CHAN_NO_IR
;
402 wdev
->hw
->wiphy
->bands
[NL80211_BAND_2GHZ
]->channels
[13].flags
|=
403 IEEE80211_CHAN_DISABLED
;
406 dev_dbg(wdev
->dev
, "sending configuration file %s\n", wdev
->pdata
.file_pds
);
407 err
= wfx_send_pdata_pds(wdev
);
408 if (err
< 0 && err
!= -ENOENT
)
411 wdev
->poll_irq
= false;
412 err
= wdev
->hwbus_ops
->irq_subscribe(wdev
->hwbus_priv
);
416 err
= wfx_hif_use_multi_tx_conf(wdev
, true);
418 dev_err(wdev
->dev
, "misconfigured IRQ?\n");
420 wdev
->pdata
.gpio_wakeup
= gpio_saved
;
421 if (wdev
->pdata
.gpio_wakeup
) {
422 dev_dbg(wdev
->dev
, "enable 'quiescent' power mode with wakeup GPIO and PDS file %s\n",
423 wdev
->pdata
.file_pds
);
424 gpiod_set_value_cansleep(wdev
->pdata
.gpio_wakeup
, 1);
425 wfx_control_reg_write(wdev
, 0);
426 wfx_hif_set_operational_mode(wdev
, HIF_OP_POWER_MODE_QUIESCENT
);
428 wfx_hif_set_operational_mode(wdev
, HIF_OP_POWER_MODE_DOZE
);
431 for (i
= 0; i
< ARRAY_SIZE(wdev
->addresses
); i
++) {
432 eth_zero_addr(wdev
->addresses
[i
].addr
);
433 err
= of_get_mac_address(wdev
->dev
->of_node
, wdev
->addresses
[i
].addr
);
435 wdev
->addresses
[i
].addr
[ETH_ALEN
- 1] += i
;
437 ether_addr_copy(wdev
->addresses
[i
].addr
, wdev
->hw_caps
.mac_addr
[i
]);
438 if (!is_valid_ether_addr(wdev
->addresses
[i
].addr
)) {
439 dev_warn(wdev
->dev
, "using random MAC address\n");
440 eth_random_addr(wdev
->addresses
[i
].addr
);
442 dev_info(wdev
->dev
, "MAC address %d: %pM\n", i
, wdev
->addresses
[i
].addr
);
444 wdev
->hw
->wiphy
->n_addresses
= ARRAY_SIZE(wdev
->addresses
);
445 wdev
->hw
->wiphy
->addresses
= wdev
->addresses
;
447 if (!wfx_api_older_than(wdev
, 3, 8))
448 wdev
->hw
->wiphy
->flags
|= WIPHY_FLAG_SUPPORTS_TDLS
;
450 err
= ieee80211_register_hw(wdev
->hw
);
452 goto irq_unsubscribe
;
454 err
= wfx_debug_init(wdev
);
456 goto ieee80211_unregister
;
460 ieee80211_unregister
:
461 ieee80211_unregister_hw(wdev
->hw
);
463 wdev
->hwbus_ops
->irq_unsubscribe(wdev
->hwbus_priv
);
465 wfx_bh_unregister(wdev
);
466 destroy_workqueue(wdev
->bh_wq
);
470 void wfx_release(struct wfx_dev
*wdev
)
472 ieee80211_unregister_hw(wdev
->hw
);
473 wfx_hif_shutdown(wdev
);
474 wdev
->hwbus_ops
->irq_unsubscribe(wdev
->hwbus_priv
);
475 wfx_bh_unregister(wdev
);
476 destroy_workqueue(wdev
->bh_wq
);
479 static int __init
wfx_core_init(void)
483 if (IS_ENABLED(CONFIG_SPI
)) {
484 ret
= spi_register_driver(&wfx_spi_driver
);
488 if (IS_ENABLED(CONFIG_MMC
)) {
489 ret
= sdio_register_driver(&wfx_sdio_driver
);
497 if (IS_ENABLED(CONFIG_SPI
))
498 spi_unregister_driver(&wfx_spi_driver
);
502 module_init(wfx_core_init
);
504 static void __exit
wfx_core_exit(void)
506 if (IS_ENABLED(CONFIG_MMC
))
507 sdio_unregister_driver(&wfx_sdio_driver
);
508 if (IS_ENABLED(CONFIG_SPI
))
509 spi_unregister_driver(&wfx_spi_driver
);
511 module_exit(wfx_core_exit
);