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