]> Git Repo - linux.git/blob - drivers/staging/wfx/main.c
slab: remove __alloc_size attribute from __kmalloc_track_caller
[linux.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-2020, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  * Copyright (c) 2008, Johannes Berg <[email protected]>
8  * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
9  * Copyright (c) 2007-2009, Christian Lamparter <[email protected]>
10  * Copyright (c) 2006, Michael Wu <[email protected]>
11  * Copyright (c) 2004-2006 Jean-Baptiste Note <[email protected]>, 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_MAX_SIZE 1500
37
38 MODULE_DESCRIPTION("Silicon Labs 802.11 Wireless LAN driver for WF200");
39 MODULE_AUTHOR("Jérôme Pouiller <[email protected]>");
40 MODULE_LICENSE("GPL");
41
42 #define RATETAB_ENT(_rate, _rateid, _flags) { \
43         .bitrate  = (_rate),   \
44         .hw_value = (_rateid), \
45         .flags    = (_flags),  \
46 }
47
48 static struct ieee80211_rate wfx_rates[] = {
49         RATETAB_ENT(10,  0,  0),
50         RATETAB_ENT(20,  1,  IEEE80211_RATE_SHORT_PREAMBLE),
51         RATETAB_ENT(55,  2,  IEEE80211_RATE_SHORT_PREAMBLE),
52         RATETAB_ENT(110, 3,  IEEE80211_RATE_SHORT_PREAMBLE),
53         RATETAB_ENT(60,  6,  0),
54         RATETAB_ENT(90,  7,  0),
55         RATETAB_ENT(120, 8,  0),
56         RATETAB_ENT(180, 9,  0),
57         RATETAB_ENT(240, 10, 0),
58         RATETAB_ENT(360, 11, 0),
59         RATETAB_ENT(480, 12, 0),
60         RATETAB_ENT(540, 13, 0),
61 };
62
63 #define CHAN2G(_channel, _freq, _flags) { \
64         .band = NL80211_BAND_2GHZ, \
65         .center_freq = (_freq),    \
66         .hw_value = (_channel),    \
67         .flags = (_flags),         \
68         .max_antenna_gain = 0,     \
69         .max_power = 30,           \
70 }
71
72 static struct ieee80211_channel wfx_2ghz_chantable[] = {
73         CHAN2G(1,  2412, 0),
74         CHAN2G(2,  2417, 0),
75         CHAN2G(3,  2422, 0),
76         CHAN2G(4,  2427, 0),
77         CHAN2G(5,  2432, 0),
78         CHAN2G(6,  2437, 0),
79         CHAN2G(7,  2442, 0),
80         CHAN2G(8,  2447, 0),
81         CHAN2G(9,  2452, 0),
82         CHAN2G(10, 2457, 0),
83         CHAN2G(11, 2462, 0),
84         CHAN2G(12, 2467, 0),
85         CHAN2G(13, 2472, 0),
86         CHAN2G(14, 2484, 0),
87 };
88
89 static const struct ieee80211_supported_band wfx_band_2ghz = {
90         .channels = wfx_2ghz_chantable,
91         .n_channels = ARRAY_SIZE(wfx_2ghz_chantable),
92         .bitrates = wfx_rates,
93         .n_bitrates = ARRAY_SIZE(wfx_rates),
94         .ht_cap = {
95                 /* Receive caps */
96                 .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 |
97                        IEEE80211_HT_CAP_MAX_AMSDU |
98                        (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
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) },
113 };
114
115 static 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 static const struct ieee80211_ops wfx_ops = {
125         .start                  = wfx_start,
126         .stop                   = wfx_stop,
127         .add_interface          = wfx_add_interface,
128         .remove_interface       = wfx_remove_interface,
129         .config                 = wfx_config,
130         .tx                     = wfx_tx,
131         .join_ibss              = wfx_join_ibss,
132         .leave_ibss             = wfx_leave_ibss,
133         .conf_tx                = wfx_conf_tx,
134         .hw_scan                = wfx_hw_scan,
135         .cancel_hw_scan         = wfx_cancel_hw_scan,
136         .start_ap               = wfx_start_ap,
137         .stop_ap                = wfx_stop_ap,
138         .sta_add                = wfx_sta_add,
139         .sta_remove             = wfx_sta_remove,
140         .set_tim                = wfx_set_tim,
141         .set_key                = wfx_set_key,
142         .set_rts_threshold      = wfx_set_rts_threshold,
143         .set_default_unicast_key = wfx_set_default_unicast_key,
144         .bss_info_changed       = wfx_bss_info_changed,
145         .configure_filter       = wfx_configure_filter,
146         .ampdu_action           = wfx_ampdu_action,
147         .flush                  = wfx_flush,
148         .add_chanctx            = wfx_add_chanctx,
149         .remove_chanctx         = wfx_remove_chanctx,
150         .change_chanctx         = wfx_change_chanctx,
151         .assign_vif_chanctx     = wfx_assign_vif_chanctx,
152         .unassign_vif_chanctx   = wfx_unassign_vif_chanctx,
153 };
154
155 bool wfx_api_older_than(struct wfx_dev *wdev, int major, int minor)
156 {
157         if (wdev->hw_caps.api_version_major < major)
158                 return true;
159         if (wdev->hw_caps.api_version_major > major)
160                 return false;
161         if (wdev->hw_caps.api_version_minor < minor)
162                 return true;
163         return false;
164 }
165
166 /* The device needs data about the antenna configuration. This information in
167  * provided by PDS (Platform Data Set, this is the wording used in WF200
168  * documentation) files. For hardware integrators, the full process to create
169  * PDS files is described here:
170  *   https:github.com/SiliconLabs/wfx-firmware/blob/master/PDS/README.md
171  *
172  * So this function aims to send PDS to the device. However, the PDS file is
173  * often bigger than Rx buffers of the chip, so it has to be sent in multiple
174  * parts.
175  *
176  * In add, the PDS data cannot be split anywhere. The PDS files contains tree
177  * structures. Braces are used to enter/leave a level of the tree (in a JSON
178  * fashion). PDS files can only been split between root nodes.
179  */
180 int wfx_send_pds(struct wfx_dev *wdev, u8 *buf, size_t len)
181 {
182         int ret;
183         int start, brace_level, i;
184
185         start = 0;
186         brace_level = 0;
187         if (buf[0] != '{') {
188                 dev_err(wdev->dev, "valid PDS start with '{'. Did you forget to compress it?\n");
189                 return -EINVAL;
190         }
191         for (i = 1; i < len - 1; i++) {
192                 if (buf[i] == '{')
193                         brace_level++;
194                 if (buf[i] == '}')
195                         brace_level--;
196                 if (buf[i] == '}' && !brace_level) {
197                         i++;
198                         if (i - start + 1 > WFX_PDS_MAX_SIZE)
199                                 return -EFBIG;
200                         buf[start] = '{';
201                         buf[i] = 0;
202                         dev_dbg(wdev->dev, "send PDS '%s}'\n", buf + start);
203                         buf[i] = '}';
204                         ret = hif_configuration(wdev, buf + start,
205                                                 i - start + 1);
206                         if (ret > 0) {
207                                 dev_err(wdev->dev, "PDS bytes %d to %d: invalid data (unsupported options?)\n",
208                                         start, i);
209                                 return -EINVAL;
210                         }
211                         if (ret == -ETIMEDOUT) {
212                                 dev_err(wdev->dev, "PDS bytes %d to %d: chip didn't reply (corrupted file?)\n",
213                                         start, i);
214                                 return ret;
215                         }
216                         if (ret) {
217                                 dev_err(wdev->dev, "PDS bytes %d to %d: chip returned an unknown error\n",
218                                         start, i);
219                                 return -EIO;
220                         }
221                         buf[i] = ',';
222                         start = i;
223                 }
224         }
225         return 0;
226 }
227
228 static int wfx_send_pdata_pds(struct wfx_dev *wdev)
229 {
230         int ret = 0;
231         const struct firmware *pds;
232         u8 *tmp_buf;
233
234         ret = request_firmware(&pds, wdev->pdata.file_pds, wdev->dev);
235         if (ret) {
236                 dev_err(wdev->dev, "can't load antenna parameters (PDS file %s). The device may be unstable.\n",
237                         wdev->pdata.file_pds);
238                 goto err1;
239         }
240         tmp_buf = kmemdup(pds->data, pds->size, GFP_KERNEL);
241         if (!tmp_buf) {
242                 ret = -ENOMEM;
243                 goto err2;
244         }
245         ret = wfx_send_pds(wdev, tmp_buf, pds->size);
246         kfree(tmp_buf);
247 err2:
248         release_firmware(pds);
249 err1:
250         return ret;
251 }
252
253 static void wfx_free_common(void *data)
254 {
255         struct wfx_dev *wdev = data;
256
257         mutex_destroy(&wdev->tx_power_loop_info_lock);
258         mutex_destroy(&wdev->rx_stats_lock);
259         mutex_destroy(&wdev->conf_mutex);
260         ieee80211_free_hw(wdev->hw);
261 }
262
263 struct wfx_dev *wfx_init_common(struct device *dev,
264                                 const struct wfx_platform_data *pdata,
265                                 const struct hwbus_ops *hwbus_ops,
266                                 void *hwbus_priv)
267 {
268         struct ieee80211_hw *hw;
269         struct wfx_dev *wdev;
270
271         hw = ieee80211_alloc_hw(sizeof(struct wfx_dev), &wfx_ops);
272         if (!hw)
273                 return NULL;
274
275         SET_IEEE80211_DEV(hw, dev);
276
277         ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
278         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
279         ieee80211_hw_set(hw, CONNECTION_MONITOR);
280         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
281         ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
282         ieee80211_hw_set(hw, SIGNAL_DBM);
283         ieee80211_hw_set(hw, SUPPORTS_PS);
284         ieee80211_hw_set(hw, MFP_CAPABLE);
285
286         hw->vif_data_size = sizeof(struct wfx_vif);
287         hw->sta_data_size = sizeof(struct wfx_sta_priv);
288         hw->queues = 4;
289         hw->max_rates = 8;
290         hw->max_rate_tries = 8;
291         hw->extra_tx_headroom = sizeof(struct hif_msg)
292                                 + sizeof(struct hif_req_tx)
293                                 + 4 /* alignment */ + 8 /* TKIP IV */;
294         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
295                                      BIT(NL80211_IFTYPE_ADHOC) |
296                                      BIT(NL80211_IFTYPE_AP);
297         hw->wiphy->probe_resp_offload = NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
298                                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
299                                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P |
300                                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U;
301         hw->wiphy->features |= NL80211_FEATURE_AP_SCAN;
302         hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
303         hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
304         hw->wiphy->max_ap_assoc_sta = HIF_LINK_ID_MAX;
305         hw->wiphy->max_scan_ssids = 2;
306         hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
307         hw->wiphy->n_iface_combinations = ARRAY_SIZE(wfx_iface_combinations);
308         hw->wiphy->iface_combinations = wfx_iface_combinations;
309         hw->wiphy->bands[NL80211_BAND_2GHZ] = devm_kmalloc(dev, sizeof(wfx_band_2ghz), GFP_KERNEL);
310         /* FIXME: also copy wfx_rates and wfx_2ghz_chantable */
311         memcpy(hw->wiphy->bands[NL80211_BAND_2GHZ], &wfx_band_2ghz,
312                sizeof(wfx_band_2ghz));
313
314         wdev = hw->priv;
315         wdev->hw = hw;
316         wdev->dev = dev;
317         wdev->hwbus_ops = hwbus_ops;
318         wdev->hwbus_priv = hwbus_priv;
319         memcpy(&wdev->pdata, pdata, sizeof(*pdata));
320         of_property_read_string(dev->of_node, "config-file",
321                                 &wdev->pdata.file_pds);
322         wdev->pdata.gpio_wakeup = devm_gpiod_get_optional(dev, "wakeup",
323                                                           GPIOD_OUT_LOW);
324         if (IS_ERR(wdev->pdata.gpio_wakeup))
325                 return NULL;
326         if (wdev->pdata.gpio_wakeup)
327                 gpiod_set_consumer_name(wdev->pdata.gpio_wakeup, "wfx wakeup");
328
329         mutex_init(&wdev->conf_mutex);
330         mutex_init(&wdev->rx_stats_lock);
331         mutex_init(&wdev->tx_power_loop_info_lock);
332         init_completion(&wdev->firmware_ready);
333         INIT_DELAYED_WORK(&wdev->cooling_timeout_work,
334                           wfx_cooling_timeout_work);
335         skb_queue_head_init(&wdev->tx_pending);
336         init_waitqueue_head(&wdev->tx_dequeue);
337         wfx_init_hif_cmd(&wdev->hif_cmd);
338         wdev->force_ps_timeout = -1;
339
340         if (devm_add_action_or_reset(dev, wfx_free_common, wdev))
341                 return NULL;
342
343         return wdev;
344 }
345
346 int wfx_probe(struct wfx_dev *wdev)
347 {
348         int i;
349         int err;
350         struct gpio_desc *gpio_saved;
351
352         /* During first part of boot, gpio_wakeup cannot yet been used. So
353          * prevent bh() to touch it.
354          */
355         gpio_saved = wdev->pdata.gpio_wakeup;
356         wdev->pdata.gpio_wakeup = NULL;
357         wdev->poll_irq = true;
358
359         wfx_bh_register(wdev);
360
361         err = wfx_init_device(wdev);
362         if (err)
363                 goto err0;
364
365         wfx_bh_poll_irq(wdev);
366         err = wait_for_completion_timeout(&wdev->firmware_ready, 1 * HZ);
367         if (err <= 0) {
368                 if (err == 0) {
369                         dev_err(wdev->dev, "timeout while waiting for startup indication\n");
370                         err = -ETIMEDOUT;
371                 } else if (err == -ERESTARTSYS) {
372                         dev_info(wdev->dev, "probe interrupted by user\n");
373                 }
374                 goto err0;
375         }
376
377         /* FIXME: fill wiphy::hw_version */
378         dev_info(wdev->dev, "started firmware %d.%d.%d \"%s\" (API: %d.%d, keyset: %02X, caps: 0x%.8X)\n",
379                  wdev->hw_caps.firmware_major, wdev->hw_caps.firmware_minor,
380                  wdev->hw_caps.firmware_build, wdev->hw_caps.firmware_label,
381                  wdev->hw_caps.api_version_major, wdev->hw_caps.api_version_minor,
382                  wdev->keyset, wdev->hw_caps.link_mode);
383         snprintf(wdev->hw->wiphy->fw_version,
384                  sizeof(wdev->hw->wiphy->fw_version),
385                  "%d.%d.%d",
386                  wdev->hw_caps.firmware_major,
387                  wdev->hw_caps.firmware_minor,
388                  wdev->hw_caps.firmware_build);
389
390         if (wfx_api_older_than(wdev, 1, 0)) {
391                 dev_err(wdev->dev,
392                         "unsupported firmware API version (expect 1 while firmware returns %d)\n",
393                         wdev->hw_caps.api_version_major);
394                 err = -ENOTSUPP;
395                 goto err0;
396         }
397
398         if (wdev->hw_caps.link_mode == SEC_LINK_ENFORCED) {
399                 dev_err(wdev->dev,
400                         "chip require secure_link, but can't negotiate it\n");
401                 goto err0;
402         }
403
404         if (wdev->hw_caps.region_sel_mode) {
405                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[11].flags |= IEEE80211_CHAN_NO_IR;
406                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[12].flags |= IEEE80211_CHAN_NO_IR;
407                 wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[13].flags |= IEEE80211_CHAN_DISABLED;
408         }
409
410         dev_dbg(wdev->dev, "sending configuration file %s\n",
411                 wdev->pdata.file_pds);
412         err = wfx_send_pdata_pds(wdev);
413         if (err < 0 && err != -ENOENT)
414                 goto err0;
415
416         wdev->poll_irq = false;
417         err = wdev->hwbus_ops->irq_subscribe(wdev->hwbus_priv);
418         if (err)
419                 goto err0;
420
421         err = hif_use_multi_tx_conf(wdev, true);
422         if (err)
423                 dev_err(wdev->dev, "misconfigured IRQ?\n");
424
425         wdev->pdata.gpio_wakeup = gpio_saved;
426         if (wdev->pdata.gpio_wakeup) {
427                 dev_dbg(wdev->dev,
428                         "enable 'quiescent' power mode with wakeup GPIO and PDS file %s\n",
429                         wdev->pdata.file_pds);
430                 gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1);
431                 control_reg_write(wdev, 0);
432                 hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_QUIESCENT);
433         } else {
434                 hif_set_operational_mode(wdev, HIF_OP_POWER_MODE_DOZE);
435         }
436
437         for (i = 0; i < ARRAY_SIZE(wdev->addresses); i++) {
438                 eth_zero_addr(wdev->addresses[i].addr);
439                 err = of_get_mac_address(wdev->dev->of_node,
440                                          wdev->addresses[i].addr);
441                 if (!err) {
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         if (!wfx_api_older_than(wdev, 3, 8))
458                 wdev->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
459
460         err = ieee80211_register_hw(wdev->hw);
461         if (err)
462                 goto err1;
463
464         err = wfx_debug_init(wdev);
465         if (err)
466                 goto err2;
467
468         return 0;
469
470 err2:
471         ieee80211_unregister_hw(wdev->hw);
472 err1:
473         wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
474 err0:
475         wfx_bh_unregister(wdev);
476         return err;
477 }
478
479 void wfx_release(struct wfx_dev *wdev)
480 {
481         ieee80211_unregister_hw(wdev->hw);
482         hif_shutdown(wdev);
483         wdev->hwbus_ops->irq_unsubscribe(wdev->hwbus_priv);
484         wfx_bh_unregister(wdev);
485 }
486
487 static int __init wfx_core_init(void)
488 {
489         int ret = 0;
490
491         if (IS_ENABLED(CONFIG_SPI))
492                 ret = spi_register_driver(&wfx_spi_driver);
493         if (IS_ENABLED(CONFIG_MMC) && !ret)
494                 ret = sdio_register_driver(&wfx_sdio_driver);
495         return ret;
496 }
497 module_init(wfx_core_init);
498
499 static void __exit wfx_core_exit(void)
500 {
501         if (IS_ENABLED(CONFIG_MMC))
502                 sdio_unregister_driver(&wfx_sdio_driver);
503         if (IS_ENABLED(CONFIG_SPI))
504                 spi_unregister_driver(&wfx_spi_driver);
505 }
506 module_exit(wfx_core_exit);
This page took 0.063743 seconds and 4 git commands to generate.