]> Git Repo - esp-hosted.git/blob - esp_hosted_ng/host/esp_cfg80211.c
Changed esp-hosted directory structure to incorporate both FG and NG variant in same...
[esp-hosted.git] / esp_hosted_ng / host / esp_cfg80211.c
1 /*
2  * Espressif Systems Wireless LAN device driver
3  *
4  * Copyright (C) 2015-2021 Espressif Systems (Shanghai) PTE LTD
5  *
6  * This software file (the "File") is distributed by Espressif Systems (Shanghai)
7  * PTE LTD under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19
20 #include "esp.h"
21 #include "esp_api.h"
22 #include "esp_cfg80211.h"
23 #include "esp_cmd.h"
24
25 /**
26   * @brief WiFi PHY rate encodings
27   *
28   */
29 typedef enum {
30     WIFI_PHY_RATE_1M_L      = 0x00, /**< 1 Mbps with long preamble */
31     WIFI_PHY_RATE_2M_L      = 0x01, /**< 2 Mbps with long preamble */
32     WIFI_PHY_RATE_5M_L      = 0x02, /**< 5.5 Mbps with long preamble */
33     WIFI_PHY_RATE_11M_L     = 0x03, /**< 11 Mbps with long preamble */
34     WIFI_PHY_RATE_2M_S      = 0x05, /**< 2 Mbps with short preamble */
35     WIFI_PHY_RATE_5M_S      = 0x06, /**< 5.5 Mbps with short preamble */
36     WIFI_PHY_RATE_11M_S     = 0x07, /**< 11 Mbps with short preamble */
37     WIFI_PHY_RATE_48M       = 0x08, /**< 48 Mbps */
38     WIFI_PHY_RATE_24M       = 0x09, /**< 24 Mbps */
39     WIFI_PHY_RATE_12M       = 0x0A, /**< 12 Mbps */
40     WIFI_PHY_RATE_6M        = 0x0B, /**< 6 Mbps */
41     WIFI_PHY_RATE_54M       = 0x0C, /**< 54 Mbps */
42     WIFI_PHY_RATE_36M       = 0x0D, /**< 36 Mbps */
43     WIFI_PHY_RATE_18M       = 0x0E, /**< 18 Mbps */
44     WIFI_PHY_RATE_9M        = 0x0F, /**< 9 Mbps */
45     WIFI_PHY_RATE_MCS0_LGI  = 0x10, /**< MCS0 with long GI, 6.5 Mbps for 20MHz, 13.5 Mbps for 40MHz */
46     WIFI_PHY_RATE_MCS1_LGI  = 0x11, /**< MCS1 with long GI, 13 Mbps for 20MHz, 27 Mbps for 40MHz */
47     WIFI_PHY_RATE_MCS2_LGI  = 0x12, /**< MCS2 with long GI, 19.5 Mbps for 20MHz, 40.5 Mbps for 40MHz */
48     WIFI_PHY_RATE_MCS3_LGI  = 0x13, /**< MCS3 with long GI, 26 Mbps for 20MHz, 54 Mbps for 40MHz */
49     WIFI_PHY_RATE_MCS4_LGI  = 0x14, /**< MCS4 with long GI, 39 Mbps for 20MHz, 81 Mbps for 40MHz */
50     WIFI_PHY_RATE_MCS5_LGI  = 0x15, /**< MCS5 with long GI, 52 Mbps for 20MHz, 108 Mbps for 40MHz */
51     WIFI_PHY_RATE_MCS6_LGI  = 0x16, /**< MCS6 with long GI, 58.5 Mbps for 20MHz, 121.5 Mbps for 40MHz */
52     WIFI_PHY_RATE_MCS7_LGI  = 0x17, /**< MCS7 with long GI, 65 Mbps for 20MHz, 135 Mbps for 40MHz */
53     WIFI_PHY_RATE_MCS0_SGI  = 0x18, /**< MCS0 with short GI, 7.2 Mbps for 20MHz, 15 Mbps for 40MHz */
54     WIFI_PHY_RATE_MCS1_SGI  = 0x19, /**< MCS1 with short GI, 14.4 Mbps for 20MHz, 30 Mbps for 40MHz */
55     WIFI_PHY_RATE_MCS2_SGI  = 0x1A, /**< MCS2 with short GI, 21.7 Mbps for 20MHz, 45 Mbps for 40MHz */
56     WIFI_PHY_RATE_MCS3_SGI  = 0x1B, /**< MCS3 with short GI, 28.9 Mbps for 20MHz, 60 Mbps for 40MHz */
57     WIFI_PHY_RATE_MCS4_SGI  = 0x1C, /**< MCS4 with short GI, 43.3 Mbps for 20MHz, 90 Mbps for 40MHz */
58     WIFI_PHY_RATE_MCS5_SGI  = 0x1D, /**< MCS5 with short GI, 57.8 Mbps for 20MHz, 120 Mbps for 40MHz */
59     WIFI_PHY_RATE_MCS6_SGI  = 0x1E, /**< MCS6 with short GI, 65 Mbps for 20MHz, 135 Mbps for 40MHz */
60     WIFI_PHY_RATE_MCS7_SGI  = 0x1F, /**< MCS7 with short GI, 72.2 Mbps for 20MHz, 150 Mbps for 40MHz */
61     WIFI_PHY_RATE_LORA_250K = 0x29, /**< 250 Kbps */
62     WIFI_PHY_RATE_LORA_500K = 0x2A, /**< 500 Kbps */
63     WIFI_PHY_RATE_MAX,
64 } wifi_phy_rate_t;
65
66 /* Supported rates to be advertised to the cfg80211 */
67 static struct ieee80211_rate esp_rates[] = {
68     {.bitrate = 10, .hw_value = WIFI_PHY_RATE_1M_L, },
69     {.bitrate = 20, .hw_value = WIFI_PHY_RATE_2M_L, },
70     {.bitrate = 55, .hw_value = WIFI_PHY_RATE_5M_L, .hw_value_short = WIFI_PHY_RATE_5M_S},
71     {.bitrate = 110, .hw_value = WIFI_PHY_RATE_11M_L, .hw_value_short = WIFI_PHY_RATE_11M_S},
72     {.bitrate = 60, .hw_value = WIFI_PHY_RATE_6M, },
73     {.bitrate = 90, .hw_value = WIFI_PHY_RATE_9M, },
74     {.bitrate = 120, .hw_value = WIFI_PHY_RATE_12M, },
75     {.bitrate = 180, .hw_value = WIFI_PHY_RATE_18M, },
76     {.bitrate = 240, .hw_value = WIFI_PHY_RATE_24M, },
77     {.bitrate = 360, .hw_value = WIFI_PHY_RATE_36M, },
78     {.bitrate = 480, .hw_value = WIFI_PHY_RATE_48M, },
79     {.bitrate = 540, .hw_value = WIFI_PHY_RATE_54M, },
80 };
81
82
83 /* Channel definitions to be advertised to cfg80211 */
84 static struct ieee80211_channel esp_channels_2ghz[] = {
85     {.center_freq = 2412, .hw_value = 1, },
86     {.center_freq = 2417, .hw_value = 2, },
87     {.center_freq = 2422, .hw_value = 3, },
88     {.center_freq = 2427, .hw_value = 4, },
89     {.center_freq = 2432, .hw_value = 5, },
90     {.center_freq = 2437, .hw_value = 6, },
91     {.center_freq = 2442, .hw_value = 7, },
92     {.center_freq = 2447, .hw_value = 8, },
93     {.center_freq = 2452, .hw_value = 9, },
94     {.center_freq = 2457, .hw_value = 10, },
95     {.center_freq = 2462, .hw_value = 11, },
96     {.center_freq = 2467, .hw_value = 12, },
97     {.center_freq = 2472, .hw_value = 13, },
98     {.center_freq = 2484, .hw_value = 14, },
99 };
100
101 static struct ieee80211_supported_band esp_wifi_bands = {
102     .channels = esp_channels_2ghz,
103     .n_channels = ARRAY_SIZE(esp_channels_2ghz),
104     .bitrates = esp_rates,
105     .n_bitrates = ARRAY_SIZE(esp_rates),
106 };
107
108 /* Supported crypto cipher suits to be advertised to cfg80211 */
109 static const u32 esp_cipher_suites[] = {
110     WLAN_CIPHER_SUITE_WEP40,
111     WLAN_CIPHER_SUITE_WEP104,
112     WLAN_CIPHER_SUITE_TKIP,
113     WLAN_CIPHER_SUITE_CCMP,
114     WLAN_CIPHER_SUITE_SMS4,
115     WLAN_CIPHER_SUITE_AES_CMAC,
116 };
117
118 struct wireless_dev *esp_cfg80211_add_iface(struct wiphy *wiphy,
119                               const char *name,
120                               unsigned char name_assign_type,
121                               enum nl80211_iftype type,
122                               struct vif_params *params)
123 {
124         struct esp_device *esp_dev = wiphy_priv(wiphy);
125 /*      struct wireless_dev *wdev = NULL;*/
126         struct net_device *ndev;
127         struct esp_wifi_device *esp_wdev;
128         uint8_t esp_nw_if_num = 0;
129
130         if (NL80211_IFTYPE_STATION == type) {
131                 esp_nw_if_num = ESP_STA_NW_IF;
132         } else if (NL80211_IFTYPE_AP == type) {
133                 esp_nw_if_num = ESP_AP_NW_IF;
134         } else {
135                 printk(KERN_INFO "%s:%u network type[%u] is not supported\n",
136                                 __func__, __LINE__, type);
137                 return NULL;
138         }
139
140         ndev = alloc_netdev(sizeof(struct esp_wifi_device), name, name_assign_type,
141                         ether_setup);
142
143         if (!ndev)
144                 return ERR_PTR(-ENOMEM);
145
146         esp_wdev = netdev_priv(ndev);
147
148         ndev->ieee80211_ptr = &esp_wdev->wdev;
149         esp_wdev->wdev.wiphy = wiphy;
150         esp_wdev->esp_dev = esp_dev;
151         esp_wdev->ndev = ndev;
152         esp_wdev->adapter = esp_dev->adapter;
153         esp_wdev->adapter->priv[esp_nw_if_num] = esp_wdev;
154         /*printk(KERN_INFO "Updated priv[%u] to %px\n",
155          * esp_nw_if_num, esp_wdev->adapter->priv[esp_nw_if_num]);*/
156         dev_net_set(ndev, wiphy_net(wiphy));
157         SET_NETDEV_DEV(ndev, wiphy_dev(esp_wdev->wdev.wiphy));
158         esp_wdev->wdev.netdev = ndev;
159         esp_wdev->wdev.iftype = type;
160
161         init_waitqueue_head(&esp_wdev->wait_for_scan_completion);
162         esp_wdev->stop_data = 1;
163         esp_wdev->port_open = 0;
164
165         if (cmd_init_interface(esp_wdev))
166                 goto free_and_return;
167
168         if (cmd_get_mac(esp_wdev))
169                 goto free_and_return;
170
171 /*      memcpy(ndev->dev_addr, esp_wdev->mac_address, ETH_ALEN);*/
172         ether_addr_copy(ndev->dev_addr, esp_wdev->mac_address);
173
174         esp_init_priv(ndev);
175
176         if (register_netdevice(ndev))
177                 goto free_and_return;
178
179
180         set_bit(ESP_NETWORK_UP, &esp_wdev->priv_flags);
181         return &esp_wdev->wdev;
182
183 free_and_return:
184         dev_net_set(ndev, NULL);
185         free_netdev(ndev);
186         esp_wdev->ndev = NULL;
187         esp_wdev->wdev.netdev = NULL;
188         ndev = NULL;
189         return NULL;
190 }
191
192 #if 0
193 static int esp_cfg80211_del_iface(struct wiphy *wiphy,
194                                                           struct wireless_dev *wdev)
195 {
196         return 0;
197 }
198
199 static int esp_cfg80211_change_iface(struct wiphy *wiphy,
200                                                           struct net_device *ndev,
201                                                           enum nl80211_iftype type,
202                                                           struct vif_params *params)
203 {
204         return 0;
205 }
206 #endif
207
208 static int esp_cfg80211_scan(struct wiphy *wiphy,
209                                                           struct cfg80211_scan_request *request)
210 {
211         struct net_device *ndev = request->wdev->netdev;
212         struct esp_wifi_device *priv = netdev_priv(ndev);
213
214         if (!priv) {
215                 printk(KERN_ERR "%s: empty priv\n", __func__);
216                 return -EINVAL;
217         }
218
219         return cmd_scan_request(priv, request);
220 }
221
222 static int esp_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
223                                                           struct cfg80211_connect_params *sme)
224 {
225         struct esp_wifi_device *priv = netdev_priv(dev);
226
227         if (!priv) {
228                 printk(KERN_ERR "%s: empty priv\n", __func__);
229                 return -EINVAL;
230         }
231
232         return cmd_connect_request(priv, sme);
233 }
234
235 static int esp_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *dev,
236                 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
237 {
238         return 0;
239 }
240
241 static int esp_cfg80211_set_default_key(struct wiphy *wiphy,
242                 struct net_device *dev, u8 key_index, bool unicast, bool multicast)
243 {
244         struct esp_wifi_device *priv = netdev_priv(dev);
245
246         if (!priv) {
247                 printk(KERN_ERR "%s: empty priv\n", __func__);
248                 return -EINVAL;
249         }
250
251         return cmd_set_default_key(priv, key_index);
252 }
253
254 static int esp_cfg80211_del_key(struct wiphy *wiphy, struct net_device *dev,
255              u8 key_index, bool pairwise, const u8 *mac_addr)
256 {
257         struct esp_wifi_device *priv = netdev_priv(dev);
258
259         if (!priv) {
260                 printk(KERN_ERR "%s: empty priv\n", __func__);
261                 return -EINVAL;
262         }
263
264         return cmd_del_key(priv, key_index, pairwise, mac_addr);
265 }
266
267 static int esp_cfg80211_add_key(struct wiphy *wiphy, struct net_device *dev,
268                  u8 key_index, bool pairwise, const u8 *mac_addr,
269                  struct key_params *params)
270 {
271         struct esp_wifi_device *priv = netdev_priv(dev);
272
273         if (!priv) {
274                 printk(KERN_ERR "%s: empty priv\n", __func__);
275                 return -EINVAL;
276         }
277
278         if (params->key_len == 0)
279                 return esp_cfg80211_del_key(wiphy, dev, key_index, pairwise, mac_addr);
280
281         return cmd_add_key(priv, key_index, pairwise, mac_addr, params);
282 }
283
284 static int esp_cfg80211_disconnect(struct wiphy *wiphy,
285                 struct net_device *dev, u16 reason_code)
286 {
287         struct esp_wifi_device *priv = netdev_priv(dev);
288
289         if (!priv) {
290                 printk(KERN_ERR "%s: empty priv\n", __func__);
291                 return 0;
292         }
293
294         return cmd_disconnect_request(priv, reason_code);
295 }
296
297 static struct cfg80211_ops esp_cfg80211_ops = {
298 #if 0
299         .add_virtual_intf = esp_cfg80211_add_iface,
300         .del_virtual_intf = esp_cfg80211_del_iface,
301         .change_virtual_intf = esp_cfg80211_change_iface,
302 #endif
303         .scan = esp_cfg80211_scan,
304         .connect = esp_cfg80211_connect,
305         .disconnect = esp_cfg80211_disconnect,
306         .add_key = esp_cfg80211_add_key,
307         .del_key = esp_cfg80211_del_key,
308         .set_default_key = esp_cfg80211_set_default_key,
309         .mgmt_tx = esp_cfg80211_mgmt_tx,
310 };
311
312 int esp_cfg80211_register(struct esp_adapter *adapter)
313 {
314         struct wiphy *wiphy;
315         struct esp_device *esp_dev;
316         int ret = 0;
317
318         wiphy = wiphy_new(&esp_cfg80211_ops, sizeof(struct esp_device));
319
320         if (!wiphy) {
321                 printk(KERN_ERR "Failed to create wiphy\n");
322                 return -EFAULT;
323         }
324
325         adapter->wiphy = wiphy;
326
327         esp_dev = wiphy_priv(wiphy);
328         esp_dev->wiphy = wiphy;
329         esp_dev->adapter = adapter;
330
331         esp_dev->dev = adapter->dev;
332
333         set_wiphy_dev(wiphy, esp_dev->dev);
334
335         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
336         wiphy->bands[NL80211_BAND_2GHZ] = &esp_wifi_bands;
337
338         /* Initialize cipher suits */
339         wiphy->cipher_suites = esp_cipher_suites;
340         wiphy->n_cipher_suites = ARRAY_SIZE(esp_cipher_suites);
341
342         /* TODO: check and finalize the numbers */
343         wiphy->max_scan_ssids = 10;
344         /*      wiphy->max_match_sets = 10;*/
345         wiphy->max_scan_ie_len = 1000;
346         wiphy->max_sched_scan_ssids = 10;
347         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
348
349         ret = wiphy_register(wiphy);
350
351         return ret;
352 }
353
This page took 0.044481 seconds and 4 git commands to generate.