]>
Commit | Line | Data |
---|---|---|
e8b168e8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
774e9b2e MM |
2 | /* |
3 | * Espressif Systems Wireless LAN device driver | |
4 | * | |
d7215282 | 5 | * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD |
774e9b2e | 6 | * |
774e9b2e | 7 | */ |
057d3956 | 8 | #include "utils.h" |
774e9b2e MM |
9 | #include <linux/init.h> |
10 | #include <linux/module.h> | |
11 | #include <linux/kernel.h> | |
12 | #include <linux/gpio.h> | |
7406fe82 | 13 | #include <linux/igmp.h> |
774e9b2e MM |
14 | |
15 | #include "esp.h" | |
16 | #include "esp_if.h" | |
17 | #include "esp_bt_api.h" | |
18 | #include "esp_api.h" | |
19 | #include "esp_cmd.h" | |
aad2ae2e | 20 | #include "esp_kernel_port.h" |
774e9b2e MM |
21 | |
22 | #include "esp_cfg80211.h" | |
bf3d6cb6 | 23 | #include "esp_stats.h" |
774e9b2e | 24 | |
7e6de249 | 25 | #define RELEASE_VERSION "1.0.3" |
774e9b2e MM |
26 | #define HOST_GPIO_PIN_INVALID -1 |
27 | static int resetpin = HOST_GPIO_PIN_INVALID; | |
2b40207b | 28 | static u32 clockspeed = 0; |
774e9b2e | 29 | extern u8 ap_bssid[MAC_ADDR_LEN]; |
3fef9acf | 30 | extern volatile u8 host_sleep; |
774e9b2e MM |
31 | |
32 | module_param(resetpin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | |
33 | MODULE_PARM_DESC(resetpin, "Host's GPIO pin number which is connected to ESP32's EN to reset ESP32 device"); | |
34 | ||
2b40207b JT |
35 | module_param(clockspeed, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
36 | MODULE_PARM_DESC(clockspeed, "Hosts clock speed in MHz"); | |
37 | ||
774e9b2e MM |
38 | static void deinit_adapter(void); |
39 | ||
40 | ||
7406fe82 | 41 | struct multicast_list mcast_list = {0}; |
774e9b2e MM |
42 | struct esp_adapter adapter; |
43 | /*struct esp_device esp_dev;*/ | |
44 | ||
a3829bf3 | 45 | struct esp_adapter *esp_get_adapter(void) |
774e9b2e MM |
46 | { |
47 | return &adapter; | |
48 | } | |
49 | ||
50 | void esp_process_new_packet_intr(struct esp_adapter *adapter) | |
51 | { | |
a3829bf3 | 52 | if (adapter) |
774e9b2e MM |
53 | queue_work(adapter->if_rx_workqueue, &adapter->if_rx_work); |
54 | } | |
55 | ||
a3829bf3 | 56 | static int process_tx_packet(struct sk_buff *skb) |
774e9b2e MM |
57 | { |
58 | struct esp_wifi_device *priv = NULL; | |
59 | struct esp_skb_cb *cb = NULL; | |
60 | struct esp_payload_header *payload_header = NULL; | |
61 | struct sk_buff *new_skb = NULL; | |
62 | int ret = 0; | |
63 | u8 pad_len = 0, realloc_skb = 0; | |
64 | u16 len = 0; | |
65 | u16 total_len = 0; | |
a3829bf3 | 66 | static u8 c; |
774e9b2e MM |
67 | u8 *pos = NULL; |
68 | ||
69 | c++; | |
70 | /* Get the priv */ | |
71 | cb = (struct esp_skb_cb *) skb->cb; | |
72 | priv = cb->priv; | |
73 | ||
74 | if (!priv) { | |
75 | dev_kfree_skb(skb); | |
6f8fee57 | 76 | esp_info("No priv\n"); |
774e9b2e MM |
77 | return NETDEV_TX_OK; |
78 | } | |
79 | ||
80 | if (netif_queue_stopped((const struct net_device *) priv->ndev)) { | |
6f8fee57 | 81 | esp_info("Netif queue stopped\n"); |
774e9b2e MM |
82 | return NETDEV_TX_BUSY; |
83 | } | |
84 | ||
3fef9acf MM |
85 | if (host_sleep) { |
86 | return NETDEV_TX_BUSY; | |
87 | } | |
88 | ||
774e9b2e MM |
89 | len = skb->len; |
90 | ||
91 | /* Create space for payload header */ | |
92 | pad_len = sizeof(struct esp_payload_header); | |
93 | ||
94 | total_len = len + pad_len; | |
95 | ||
96 | /* Align buffer length */ | |
97 | pad_len += SKB_DATA_ADDR_ALIGNMENT - (total_len % SKB_DATA_ADDR_ALIGNMENT); | |
98 | ||
99 | if (skb_headroom(skb) < pad_len) { | |
100 | /* Headroom is not sufficient */ | |
101 | realloc_skb = 1; | |
102 | } | |
103 | ||
104 | if (realloc_skb || !IS_ALIGNED((unsigned long) skb->data, SKB_DATA_ADDR_ALIGNMENT)) { | |
105 | /* Realloc SKB */ | |
106 | if (skb_linearize(skb)) { | |
107 | priv->stats.tx_errors++; | |
108 | dev_kfree_skb(skb); | |
6f8fee57 | 109 | esp_err("Failed to linearize SKB"); |
774e9b2e MM |
110 | return NETDEV_TX_OK; |
111 | } | |
112 | ||
113 | new_skb = esp_alloc_skb(skb->len + pad_len); | |
114 | ||
115 | if (!new_skb) { | |
6f8fee57 | 116 | esp_err("Failed to allocate SKB"); |
774e9b2e MM |
117 | priv->stats.tx_errors++; |
118 | dev_kfree_skb(skb); | |
119 | return NETDEV_TX_OK; | |
120 | } | |
121 | ||
122 | pos = new_skb->data; | |
123 | pos += pad_len; | |
124 | ||
125 | /* Populate new SKB */ | |
126 | skb_copy_from_linear_data(skb, pos, skb->len); | |
127 | skb_put(new_skb, skb->len + pad_len); | |
128 | ||
129 | /* Replace old SKB */ | |
130 | dev_kfree_skb_any(skb); | |
131 | skb = new_skb; | |
132 | } else { | |
133 | /* Realloc is not needed, Make space for interface header */ | |
134 | skb_push(skb, pad_len); | |
135 | } | |
136 | ||
137 | /* Set payload header */ | |
138 | payload_header = (struct esp_payload_header *) skb->data; | |
139 | memset(payload_header, 0, pad_len); | |
140 | ||
141 | payload_header->if_type = priv->if_type; | |
142 | payload_header->if_num = priv->if_num; | |
143 | payload_header->len = cpu_to_le16(len); | |
144 | payload_header->offset = cpu_to_le16(pad_len); | |
145 | payload_header->packet_type = PACKET_TYPE_DATA; | |
146 | ||
bf3d6cb6 SR |
147 | if (adapter.capabilities & ESP_CHECKSUM_ENABLED) |
148 | payload_header->checksum = cpu_to_le16(compute_checksum(skb->data, (len + pad_len))); | |
774e9b2e MM |
149 | |
150 | if (!priv->stop_data) { | |
151 | ret = esp_send_packet(priv->adapter, skb); | |
152 | ||
153 | if (ret) { | |
6f8fee57 | 154 | /* esp_err("Failed to send SKB");*/ |
774e9b2e MM |
155 | priv->stats.tx_errors++; |
156 | } else { | |
157 | priv->stats.tx_packets++; | |
158 | priv->stats.tx_bytes += skb->len; | |
159 | } | |
160 | } else { | |
161 | dev_kfree_skb_any(skb); | |
162 | priv->stats.tx_dropped++; | |
163 | } | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
a3829bf3 | 168 | void esp_port_open(struct esp_wifi_device *priv) |
774e9b2e MM |
169 | { |
170 | priv->port_open = 1; | |
171 | priv->stop_data = 0; | |
172 | } | |
173 | ||
a3829bf3 | 174 | void esp_port_close(struct esp_wifi_device *priv) |
774e9b2e MM |
175 | { |
176 | if (!priv) | |
177 | return; | |
178 | ||
179 | priv->port_open = 0; | |
180 | priv->stop_data = 1; | |
181 | } | |
182 | ||
183 | void print_capabilities(u32 cap) | |
184 | { | |
6f8fee57 | 185 | esp_info("Capabilities: 0x%x. Features supported are:\n", cap); |
774e9b2e | 186 | if (cap & ESP_WLAN_SDIO_SUPPORT) |
6f8fee57 | 187 | esp_info("\t * WLAN on SDIO\n"); |
774e9b2e | 188 | else if (cap & ESP_WLAN_SPI_SUPPORT) |
6f8fee57 | 189 | esp_info("\t * WLAN on SPI\n"); |
774e9b2e MM |
190 | |
191 | if ((cap & ESP_BT_UART_SUPPORT) || | |
bf3d6cb6 SR |
192 | (cap & ESP_BT_SDIO_SUPPORT) || |
193 | (cap & ESP_BT_SPI_SUPPORT)) { | |
6f8fee57 | 194 | esp_info("\t * BT/BLE\n"); |
774e9b2e | 195 | if (cap & ESP_BT_UART_SUPPORT) |
6f8fee57 | 196 | esp_info("\t - HCI over UART\n"); |
774e9b2e | 197 | if (cap & ESP_BT_SDIO_SUPPORT) |
6f8fee57 | 198 | esp_info("\t - HCI over SDIO\n"); |
774e9b2e | 199 | if (cap & ESP_BT_SPI_SUPPORT) |
6f8fee57 | 200 | esp_info("\t - HCI over SPI\n"); |
774e9b2e MM |
201 | |
202 | if ((cap & ESP_BLE_ONLY_SUPPORT) && (cap & ESP_BR_EDR_ONLY_SUPPORT)) | |
6f8fee57 | 203 | esp_info("\t - BT/BLE dual mode\n"); |
774e9b2e | 204 | else if (cap & ESP_BLE_ONLY_SUPPORT) |
6f8fee57 | 205 | esp_info("\t - BLE only\n"); |
774e9b2e | 206 | else if (cap & ESP_BR_EDR_ONLY_SUPPORT) |
6f8fee57 | 207 | esp_info("\t - BR EDR only\n"); |
774e9b2e MM |
208 | } |
209 | } | |
210 | ||
211 | void process_capabilities(struct esp_adapter *adapter) | |
212 | { | |
6f8fee57 | 213 | esp_info("ESP peripheral capabilities: 0x%x\n", adapter->capabilities); |
774e9b2e MM |
214 | |
215 | /* Reset BT */ | |
216 | esp_deinit_bt(adapter); | |
217 | ||
218 | if ((adapter->capabilities & ESP_BT_SPI_SUPPORT) || | |
219 | (adapter->capabilities & ESP_BT_SDIO_SUPPORT)) { | |
220 | msleep(200); | |
6f8fee57 | 221 | esp_info("ESP Bluetooth init\n"); |
774e9b2e MM |
222 | esp_init_bt(adapter); |
223 | } | |
224 | } | |
225 | ||
226 | static int check_esp_version(struct fw_version *ver) | |
227 | { | |
6f8fee57 | 228 | esp_info("esp32: ESP Firmware version: %u.%u.%u\n", |
774e9b2e MM |
229 | ver->major1, ver->major2, ver->minor); |
230 | if (!ver->major1) { | |
6f8fee57 | 231 | esp_err("Incompatible ESP firmware release detected, Please use correct ESP-Hosted branch/compatible release\n"); |
774e9b2e MM |
232 | return -1; |
233 | } | |
234 | return 0; | |
235 | } | |
236 | ||
237 | static void print_reset_reason(uint32_t reason) | |
238 | { | |
239 | switch (reason) | |
240 | { | |
a3829bf3 KG |
241 | case 1: esp_info("POWERON_RESET\n"); break; /**<1, Vbat power on reset*/ |
242 | case 3: esp_info("SW_RESET\n"); break; /**<3, Software reset digital core*/ | |
243 | case 4: esp_info("OWDT_RESET\n"); break; /**<4, Legacy watch dog reset digital core*/ | |
244 | case 5: esp_info("DEEPSLEEP_RESET\n"); break; /**<5, Deep Sleep reset digital core*/ | |
245 | case 6: esp_info("SDIO_RESET\n"); break; /**<6, Reset by SLC module, reset digital core*/ | |
246 | case 7: esp_info("TG0WDT_SYS_RESET\n"); break; /**<7, Timer Group0 Watch dog reset digital core*/ | |
247 | case 8: esp_info("TG1WDT_SYS_RESET\n"); break; /**<8, Timer Group1 Watch dog reset digital core*/ | |
248 | case 9: esp_info("RTCWDT_SYS_RESET\n"); break; /**<9, RTC Watch dog Reset digital core*/ | |
249 | case 10: esp_info("INTRUSION_RESET\n"); break; /**<10, Instrusion tested to reset CPU*/ | |
250 | case 11: esp_info("TGWDT_CPU_RESET\n"); break; /**<11, Time Group reset CPU*/ | |
251 | case 12: esp_info("SW_CPU_RESET\n"); break; /**<12, Software reset CPU*/ | |
252 | case 13: esp_info("RTCWDT_CPU_RESET\n"); break; /**<13, RTC Watch dog Reset CPU*/ | |
253 | case 14: esp_info("EXT_CPU_RESET\n"); break; /**<14, for APP CPU, reseted by PRO CPU*/ | |
254 | case 15: esp_info("RTCWDT_BROWN_OUT_RESET\n"); break;/**<15, Reset when the vdd voltage is not stable*/ | |
255 | case 16: esp_info("RTCWDT_RTC_RESET\n"); break; /**<16, RTC Watch dog reset digital core and rtc module*/ | |
256 | default: esp_info("Unknown[%u]\n", reason); break; | |
774e9b2e MM |
257 | } |
258 | } | |
259 | ||
260 | int process_fw_data(struct fw_data *fw_p) | |
261 | { | |
262 | if (!fw_p) { | |
6f8fee57 | 263 | esp_err("Incomplete/incorrect bootup event received\n"); |
774e9b2e MM |
264 | return -1; |
265 | } | |
266 | ||
e58fd496 | 267 | esp_info("ESP chipset's last reset cause:\n"); |
774e9b2e MM |
268 | print_reset_reason(le32_to_cpu(fw_p->last_reset_reason)); |
269 | return check_esp_version(&fw_p->version); | |
270 | } | |
271 | ||
272 | static int esp_open(struct net_device *ndev) | |
273 | { | |
774e9b2e MM |
274 | return 0; |
275 | } | |
276 | ||
277 | static int esp_stop(struct net_device *ndev) | |
278 | { | |
225e14eb | 279 | struct esp_wifi_device *priv = netdev_priv(ndev); |
a3829bf3 | 280 | |
0af9c02e | 281 | ESP_MARK_SCAN_DONE(priv, true); |
774e9b2e MM |
282 | return 0; |
283 | } | |
284 | ||
a3829bf3 | 285 | static struct net_device_stats *esp_get_stats(struct net_device *ndev) |
774e9b2e MM |
286 | { |
287 | struct esp_wifi_device *priv = netdev_priv(ndev); | |
288 | ||
289 | if (!priv) | |
290 | return NULL; | |
291 | ||
292 | return &priv->stats; | |
293 | } | |
294 | ||
295 | static int esp_set_mac_address(struct net_device *ndev, void *data) | |
296 | { | |
297 | struct esp_wifi_device *priv = netdev_priv(ndev); | |
98f8ece9 KG |
298 | struct sockaddr *sa = (struct sockaddr *)data; |
299 | int ret; | |
774e9b2e MM |
300 | |
301 | if (!priv || !priv->adapter) | |
302 | return -EINVAL; | |
303 | ||
98f8ece9 | 304 | esp_info("%u "MACSTR"\n", __LINE__, MAC2STR(sa->sa_data)); |
774e9b2e | 305 | |
98f8ece9 KG |
306 | ret = cmd_set_mac(priv, sa->sa_data); |
307 | ||
308 | if (ret == 0) | |
309 | eth_hw_addr_set(ndev, priv->mac_address/*mac_addr->sa_data*/); | |
310 | ||
311 | return ret; | |
774e9b2e MM |
312 | } |
313 | ||
774e9b2e MM |
314 | static void esp_set_rx_mode(struct net_device *ndev) |
315 | { | |
7406fe82 MM |
316 | struct esp_wifi_device *priv = netdev_priv(ndev); |
317 | struct netdev_hw_addr *mac_addr; | |
318 | u32 count = 0; | |
5a47b073 | 319 | #if 0 |
7406fe82 MM |
320 | struct in_device *in_dev = in_dev_get(ndev); |
321 | struct ip_mc_list *ip_list = in_dev->mc_list; | |
5a47b073 | 322 | #endif |
7406fe82 MM |
323 | netdev_for_each_mc_addr(mac_addr, ndev) { |
324 | if (count < MAX_MULTICAST_ADDR_COUNT) { | |
e58fd496 | 325 | /*esp_info("%d: "MACSTR"\n", count+1, MAC2STR(mac_addr->addr));*/ |
7406fe82 MM |
326 | memcpy(&mcast_list.mcast_addr[count++], mac_addr->addr, ETH_ALEN); |
327 | } | |
328 | } | |
329 | ||
330 | mcast_list.priv = priv; | |
331 | mcast_list.addr_count = count; | |
332 | ||
333 | if (priv->port_open) { | |
6f8fee57 | 334 | /*esp_info("Set Multicast list\n");*/ |
7406fe82 MM |
335 | if (adapter.mac_filter_wq) |
336 | queue_work(adapter.mac_filter_wq, &adapter.mac_flter_work); | |
337 | } | |
338 | #if 0 | |
5a47b073 | 339 | cmd_set_mcast_mac_list(priv, &mcast_list); |
a3829bf3 | 340 | while (ip_list) { |
6f8fee57 | 341 | esp_dbg(" IP MC Address: 0x%x\n", ip_list->multiaddr); |
7406fe82 MM |
342 | ip_list = ip_list->next; |
343 | } | |
344 | #endif | |
345 | ||
774e9b2e MM |
346 | } |
347 | ||
348 | static int esp_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev) | |
349 | { | |
350 | struct esp_wifi_device *priv = NULL; | |
351 | struct esp_skb_cb *cb = NULL; | |
352 | ||
353 | if (!skb || !ndev) | |
354 | return NETDEV_TX_OK; | |
355 | ||
356 | priv = netdev_priv(ndev); | |
357 | if (!priv) { | |
358 | dev_kfree_skb(skb); | |
359 | return NETDEV_TX_OK; | |
360 | } | |
361 | ||
362 | if (!priv->port_open) { | |
363 | priv->stats.tx_dropped++; | |
6f8fee57 | 364 | /*esp_err("Port not yet open\n");*/ |
774e9b2e MM |
365 | dev_kfree_skb(skb); |
366 | return NETDEV_TX_OK; | |
367 | } | |
368 | ||
369 | if (!skb->len || (skb->len > ETH_FRAME_LEN)) { | |
6f8fee57 | 370 | esp_err("Bad len %d\n", skb->len); |
774e9b2e MM |
371 | priv->stats.tx_dropped++; |
372 | dev_kfree_skb(skb); | |
373 | return NETDEV_TX_OK; | |
374 | } | |
375 | ||
376 | cb = (struct esp_skb_cb *) skb->cb; | |
377 | cb->priv = priv; | |
378 | ||
379 | return process_tx_packet(skb); | |
380 | } | |
381 | ||
382 | static const struct net_device_ops esp_netdev_ops = { | |
383 | .ndo_open = esp_open, | |
384 | .ndo_stop = esp_stop, | |
385 | .ndo_start_xmit = esp_hard_start_xmit, | |
98f8ece9 | 386 | .ndo_set_mac_address = esp_set_mac_address, |
774e9b2e | 387 | .ndo_validate_addr = eth_validate_addr, |
774e9b2e MM |
388 | .ndo_get_stats = esp_get_stats, |
389 | .ndo_set_rx_mode = esp_set_rx_mode, | |
390 | }; | |
391 | ||
392 | ||
393 | void esp_init_priv(struct net_device *ndev) | |
394 | { | |
395 | ndev->netdev_ops = &esp_netdev_ops; | |
396 | ndev->needed_headroom = roundup(sizeof(struct esp_payload_header) + | |
397 | INTERFACE_HEADER_PADDING, 4); | |
398 | } | |
399 | ||
774e9b2e MM |
400 | static int add_network_iface(void) |
401 | { | |
402 | int ret = 0; | |
a3829bf3 KG |
403 | struct esp_adapter *adapter = esp_get_adapter(); |
404 | struct wireless_dev *wdev = NULL; | |
774e9b2e MM |
405 | |
406 | if (!adapter) { | |
6f8fee57 | 407 | esp_info("adapter not yet init\n"); |
774e9b2e MM |
408 | return -EINVAL; |
409 | } | |
410 | ||
411 | ret = esp_cfg80211_register(adapter); | |
412 | if (ret) { | |
6f8fee57 | 413 | esp_err("Failed to register with cfg80211 (err code 0x%x)\n", ret); |
774e9b2e MM |
414 | return ret; |
415 | } | |
416 | ||
417 | rtnl_lock(); | |
225e14eb | 418 | wdev = esp_cfg80211_add_iface(adapter->wiphy, "espsta%d", 1, NL80211_IFTYPE_STATION, NULL); |
774e9b2e MM |
419 | rtnl_unlock(); |
420 | ||
225e14eb YM |
421 | /* Return success if network added successfully */ |
422 | if (wdev) | |
423 | return 0; | |
424 | ||
425 | return -1; | |
774e9b2e MM |
426 | } |
427 | ||
428 | int esp_add_card(struct esp_adapter *adapter) | |
429 | { | |
225e14eb | 430 | RET_ON_FAIL(esp_commands_setup(adapter)); |
774e9b2e | 431 | |
225e14eb | 432 | RET_ON_FAIL(add_network_iface()); |
774e9b2e | 433 | |
225e14eb | 434 | return 0; |
774e9b2e MM |
435 | } |
436 | ||
437 | void esp_remove_network_interfaces(struct esp_adapter *adapter) | |
438 | { | |
439 | uint8_t iface_idx = 0; | |
440 | struct net_device *ndev = NULL; | |
441 | struct esp_wifi_device *priv = NULL; | |
442 | ||
a3829bf3 | 443 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { |
774e9b2e MM |
444 | |
445 | priv = adapter->priv[iface_idx]; | |
446 | ||
447 | if (!priv) | |
448 | continue; | |
449 | ||
450 | if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags)) | |
451 | continue; | |
452 | ||
453 | /* stop and unregister network */ | |
454 | ndev = priv->ndev; | |
455 | ||
456 | if (ndev) { | |
457 | ||
458 | if (netif_carrier_ok(ndev)) | |
459 | netif_carrier_off(ndev); | |
460 | ||
461 | netif_device_detach(ndev); | |
462 | ||
463 | if (ndev->reg_state == NETREG_REGISTERED) { | |
7406fe82 | 464 | unregister_inetaddr_notifier(&(adapter->priv[0]->nb)); |
774e9b2e MM |
465 | unregister_netdev(ndev); |
466 | free_netdev(ndev); | |
467 | ndev = NULL; | |
468 | } | |
469 | } | |
470 | clear_bit(ESP_NETWORK_UP, &priv->priv_flags); | |
471 | } | |
472 | ||
473 | if (adapter->wiphy) { | |
474 | ||
475 | wiphy_unregister(adapter->wiphy); | |
476 | wiphy_free(adapter->wiphy); | |
477 | adapter->wiphy = NULL; | |
478 | } | |
479 | } | |
480 | ||
481 | int esp_remove_card(struct esp_adapter *adapter) | |
482 | { | |
483 | uint8_t iface_idx = 0; | |
484 | ||
485 | if (!adapter) { | |
486 | return 0; | |
487 | } | |
488 | ||
225e14eb | 489 | esp_deinit_bt(adapter); |
774e9b2e | 490 | |
225e14eb | 491 | esp_commands_teardown(adapter); |
774e9b2e MM |
492 | |
493 | esp_remove_network_interfaces(adapter); | |
494 | ||
a3829bf3 | 495 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { |
774e9b2e MM |
496 | esp_port_close(adapter->priv[iface_idx]); |
497 | adapter->priv[iface_idx] = NULL; | |
498 | } | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
a3829bf3 | 503 | struct esp_wifi_device *get_priv_from_payload_header( |
774e9b2e MM |
504 | struct esp_payload_header *header) |
505 | { | |
506 | struct esp_wifi_device *priv = NULL; | |
507 | u8 i = 0; | |
508 | ||
509 | if (!header) | |
510 | return NULL; | |
511 | ||
512 | for (i = 0; i < ESP_MAX_INTERFACE; i++) { | |
513 | priv = adapter.priv[i]; | |
514 | ||
515 | if (!priv) | |
516 | continue; | |
517 | ||
518 | if (priv->if_type == header->if_type && | |
519 | priv->if_num == header->if_num) { | |
520 | return priv; | |
521 | } | |
522 | } | |
523 | return NULL; | |
524 | } | |
525 | ||
526 | static void process_esp_bootup_event(struct esp_adapter *adapter, | |
527 | struct esp_internal_bootup_event *evt) | |
528 | { | |
529 | if (!adapter || !evt) { | |
6f8fee57 | 530 | esp_err("Invalid arguments\n"); |
774e9b2e MM |
531 | return; |
532 | } | |
533 | ||
534 | if (evt->header.status) { | |
6f8fee57 | 535 | esp_err("Incorrect ESP bootup event\n"); |
774e9b2e MM |
536 | return; |
537 | } | |
538 | ||
6f8fee57 | 539 | esp_info("\nReceived ESP bootup event\n"); |
774e9b2e MM |
540 | process_event_esp_bootup(adapter, evt->data, evt->len); |
541 | } | |
542 | ||
543 | static int process_internal_event(struct esp_adapter *adapter, | |
544 | struct sk_buff *skb) | |
545 | { | |
546 | struct event_header *header = NULL; | |
547 | ||
548 | if (!skb || !adapter) { | |
6f8fee57 | 549 | esp_err("esp32: Incorrect event data!\n"); |
774e9b2e MM |
550 | return -1; |
551 | } | |
552 | ||
553 | header = (struct event_header *) (skb->data); | |
554 | ||
555 | switch (header->event_code) { | |
556 | ||
557 | case ESP_INTERNAL_BOOTUP_EVENT: | |
558 | process_esp_bootup_event(adapter, | |
559 | (struct esp_internal_bootup_event *)(skb->data)); | |
560 | break; | |
561 | ||
562 | default: | |
6f8fee57 KG |
563 | esp_info("%u unhandled internal event[%u]\n", |
564 | __LINE__, header->event_code); | |
774e9b2e MM |
565 | break; |
566 | } | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
571 | static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb) | |
572 | { | |
573 | struct esp_wifi_device *priv = NULL; | |
574 | struct esp_payload_header *payload_header = NULL; | |
575 | u16 len = 0, offset = 0; | |
bf3d6cb6 | 576 | u16 rx_checksum = 0, checksum = 0; |
774e9b2e MM |
577 | struct hci_dev *hdev = adapter->hcidev; |
578 | u8 *type = NULL; | |
a3829bf3 KG |
579 | struct sk_buff *eap_skb = NULL; |
580 | struct ethhdr *eth = NULL; | |
774e9b2e MM |
581 | |
582 | if (!skb) | |
583 | return; | |
584 | ||
585 | /* get the paload header */ | |
586 | payload_header = (struct esp_payload_header *) skb->data; | |
587 | ||
588 | len = le16_to_cpu(payload_header->len); | |
589 | offset = le16_to_cpu(payload_header->offset); | |
590 | ||
3fef9acf MM |
591 | if (payload_header->reserved2 == 0xFF) { |
592 | print_hex_dump(KERN_INFO, "Wake up packet: ", DUMP_PREFIX_ADDRESS, 16, 1, skb->data, len+offset, 1); | |
593 | } | |
774e9b2e | 594 | |
bf3d6cb6 SR |
595 | if (adapter->capabilities & ESP_CHECKSUM_ENABLED) { |
596 | rx_checksum = le16_to_cpu(payload_header->checksum); | |
597 | payload_header->checksum = 0; | |
598 | ||
599 | checksum = compute_checksum(skb->data, (len + offset)); | |
600 | ||
601 | if (checksum != rx_checksum) { | |
602 | dev_kfree_skb_any(skb); | |
603 | return; | |
604 | } | |
605 | } | |
606 | ||
774e9b2e MM |
607 | /* chop off the header from skb */ |
608 | skb_pull(skb, offset); | |
609 | ||
610 | if (payload_header->if_type == ESP_STA_IF || payload_header->if_type == ESP_AP_IF) { | |
611 | ||
612 | /* retrieve priv based on payload header contents */ | |
613 | priv = get_priv_from_payload_header(payload_header); | |
614 | ||
615 | if (!priv) { | |
6f8fee57 | 616 | esp_err("Empty priv\n"); |
774e9b2e MM |
617 | dev_kfree_skb_any(skb); |
618 | return; | |
619 | } | |
620 | ||
621 | if (payload_header->packet_type == PACKET_TYPE_EAPOL) { | |
6f8fee57 | 622 | esp_info("Rx PACKET_TYPE_EAPOL!!!!\n"); |
774e9b2e MM |
623 | esp_port_open(priv); |
624 | ||
7b6ed49e | 625 | eap_skb = alloc_skb(skb->len + ETH_HLEN, GFP_KERNEL); |
a3829bf3 | 626 | if (!eap_skb) { |
6f8fee57 | 627 | esp_info("%u memory alloc failed\n", __LINE__); |
774e9b2e MM |
628 | return; |
629 | } | |
630 | eap_skb->dev = priv->ndev; | |
631 | ||
632 | if (!IS_ALIGNED((unsigned long) eap_skb->data, SKB_DATA_ADDR_ALIGNMENT)) { | |
6f8fee57 | 633 | esp_info("%u eap skb unaligned\n", __LINE__); |
774e9b2e MM |
634 | } |
635 | ||
aad2ae2e | 636 | eth = (struct ethhdr *) skb_put(eap_skb, ETH_HLEN); |
774e9b2e MM |
637 | ether_addr_copy(eth->h_dest, /*skb->data*/priv->ndev->dev_addr); |
638 | ether_addr_copy(eth->h_source, /*skb->data+6*/ ap_bssid); | |
639 | eth->h_proto = cpu_to_be16(ETH_P_PAE); | |
640 | ||
641 | skb_put_data(eap_skb, skb->data, skb->len); | |
642 | eap_skb->protocol = eth_type_trans(eap_skb, eap_skb->dev); | |
643 | ||
644 | netif_rx(eap_skb); | |
645 | ||
646 | } else if (payload_header->packet_type == PACKET_TYPE_DATA) { | |
647 | ||
648 | skb->dev = priv->ndev; | |
649 | skb->protocol = eth_type_trans(skb, priv->ndev); | |
650 | skb->ip_summed = CHECKSUM_NONE; | |
651 | ||
652 | priv->stats.rx_bytes += skb->len; | |
653 | /* Forward skb to kernel */ | |
1294f8ac | 654 | NETIF_RX_NI(skb); |
774e9b2e MM |
655 | priv->stats.rx_packets++; |
656 | } else if (payload_header->packet_type == PACKET_TYPE_COMMAND_RESPONSE) { | |
225e14eb | 657 | process_cmd_resp(priv->adapter, skb); |
774e9b2e | 658 | } else if (payload_header->packet_type == PACKET_TYPE_EVENT) { |
225e14eb | 659 | process_cmd_event(priv, skb); |
774e9b2e MM |
660 | dev_kfree_skb_any(skb); |
661 | } | |
662 | ||
663 | } else if (payload_header->if_type == ESP_HCI_IF) { | |
664 | if (hdev) { | |
665 | ||
666 | type = skb->data; | |
667 | hci_skb_pkt_type(skb) = *type; | |
668 | skb_pull(skb, 1); | |
669 | ||
670 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) | |
671 | if (hci_recv_frame(hdev, skb)) { | |
672 | #else | |
673 | if (hci_recv_frame(skb)) { | |
674 | #endif | |
675 | hdev->stat.err_rx++; | |
676 | } else { | |
677 | esp_hci_update_rx_counter(hdev, *type, skb->len); | |
678 | } | |
679 | } | |
680 | } else if (payload_header->if_type == ESP_INTERNAL_IF) { | |
681 | ||
682 | /* Queue event skb for processing in events workqueue */ | |
683 | skb_queue_tail(&adapter->events_skb_q, skb); | |
684 | ||
685 | if (adapter->events_wq) | |
686 | queue_work(adapter->events_wq, &adapter->events_work); | |
687 | else | |
688 | dev_kfree_skb_any(skb); | |
689 | ||
bf3d6cb6 SR |
690 | } else if (payload_header->if_type == ESP_TEST_IF) { |
691 | #if TEST_RAW_TP | |
692 | update_test_raw_tp_rx_stats(len); | |
693 | #endif | |
694 | dev_kfree_skb_any(skb); | |
774e9b2e MM |
695 | } else { |
696 | dev_kfree_skb_any(skb); | |
697 | } | |
698 | } | |
699 | ||
bf3d6cb6 SR |
700 | int esp_is_tx_queue_paused(struct esp_wifi_device *priv) |
701 | { | |
702 | if (!priv || !priv->ndev) | |
703 | return 0; | |
704 | ||
705 | if ((priv->ndev && | |
706 | !netif_queue_stopped((const struct net_device *)priv->ndev))) | |
707 | return 1; | |
708 | return 0; | |
709 | } | |
710 | ||
774e9b2e MM |
711 | void esp_tx_pause(struct esp_wifi_device *priv) |
712 | { | |
713 | if (!priv || !priv->ndev) | |
714 | return; | |
715 | ||
716 | if (!netif_queue_stopped((const struct net_device *)priv->ndev)) { | |
717 | netif_stop_queue(priv->ndev); | |
718 | } | |
719 | } | |
720 | ||
721 | void esp_tx_resume(struct esp_wifi_device *priv) | |
722 | { | |
723 | if (!priv || !priv->ndev) | |
724 | return; | |
725 | ||
726 | if (netif_queue_stopped((const struct net_device *)priv->ndev)) { | |
727 | netif_wake_queue(priv->ndev); | |
728 | } | |
729 | } | |
730 | ||
a3829bf3 | 731 | struct sk_buff *esp_alloc_skb(u32 len) |
774e9b2e MM |
732 | { |
733 | struct sk_buff *skb = NULL; | |
734 | ||
735 | u8 offset; | |
736 | ||
737 | skb = netdev_alloc_skb(NULL, len + INTERFACE_HEADER_PADDING); | |
738 | ||
739 | if (skb) { | |
740 | /* Align SKB data pointer */ | |
741 | offset = ((unsigned long)skb->data) & (SKB_DATA_ADDR_ALIGNMENT - 1); | |
742 | ||
743 | if (offset) | |
744 | skb_reserve(skb, INTERFACE_HEADER_PADDING - offset); | |
745 | } | |
746 | ||
747 | return skb; | |
748 | } | |
749 | ||
750 | ||
751 | static int esp_get_packets(struct esp_adapter *adapter) | |
752 | { | |
753 | struct sk_buff *skb = NULL; | |
754 | ||
755 | if (!adapter || !adapter->if_ops || !adapter->if_ops->read) | |
756 | return -EINVAL; | |
757 | ||
758 | skb = adapter->if_ops->read(adapter); | |
759 | ||
760 | if (!skb) | |
761 | return -EFAULT; | |
762 | ||
763 | process_rx_packet(adapter, skb); | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
768 | int esp_send_packet(struct esp_adapter *adapter, struct sk_buff *skb) | |
769 | { | |
225e14eb | 770 | if (!adapter || !adapter->if_ops || !adapter->if_ops->write) { |
6f8fee57 | 771 | esp_err("%u adapter: %p\n", __LINE__, adapter); |
774e9b2e | 772 | return -EINVAL; |
225e14eb | 773 | } |
774e9b2e MM |
774 | |
775 | return adapter->if_ops->write(adapter, skb); | |
776 | } | |
777 | ||
778 | static void esp_if_rx_work(struct work_struct *work) | |
779 | { | |
780 | /* read inbound packet and forward it to network/serial interface */ | |
781 | esp_get_packets(&adapter); | |
782 | } | |
783 | ||
7406fe82 MM |
784 | static void update_mac_filter(struct work_struct *work) |
785 | { | |
5a47b073 | 786 | cmd_set_mcast_mac_list(mcast_list.priv, &mcast_list); |
7406fe82 MM |
787 | } |
788 | ||
774e9b2e MM |
789 | static void esp_events_work(struct work_struct *work) |
790 | { | |
791 | struct sk_buff *skb = NULL; | |
792 | ||
793 | skb = skb_dequeue(&adapter.events_skb_q); | |
794 | if (!skb) | |
795 | return; | |
796 | ||
797 | process_internal_event(&adapter, skb); | |
798 | dev_kfree_skb_any(skb); | |
799 | } | |
800 | ||
a3829bf3 | 801 | static struct esp_adapter *init_adapter(void) |
774e9b2e MM |
802 | { |
803 | memset(&adapter, 0, sizeof(adapter)); | |
804 | ||
805 | /* Prepare interface RX work */ | |
806 | adapter.if_rx_workqueue = alloc_workqueue("ESP_IF_RX_WORK_QUEUE", 0, 0); | |
807 | ||
808 | if (!adapter.if_rx_workqueue) { | |
809 | deinit_adapter(); | |
810 | return NULL; | |
811 | } | |
812 | ||
813 | INIT_WORK(&adapter.if_rx_work, esp_if_rx_work); | |
814 | ||
815 | skb_queue_head_init(&adapter.events_skb_q); | |
816 | ||
817 | adapter.events_wq = alloc_workqueue("ESP_EVENTS_WORKQUEUE", WQ_HIGHPRI, 0); | |
818 | ||
819 | if (!adapter.events_wq) { | |
820 | deinit_adapter(); | |
821 | return NULL; | |
822 | } | |
823 | ||
824 | INIT_WORK(&adapter.events_work, esp_events_work); | |
825 | ||
7406fe82 MM |
826 | adapter.mac_filter_wq = alloc_workqueue("MAC_FILTER", 0, 0); |
827 | if (!adapter.mac_filter_wq) { | |
828 | deinit_adapter(); | |
829 | return NULL; | |
830 | } | |
831 | ||
832 | INIT_WORK(&adapter.mac_flter_work, update_mac_filter); | |
833 | ||
774e9b2e MM |
834 | return &adapter; |
835 | } | |
836 | ||
837 | static void deinit_adapter(void) | |
838 | { | |
839 | skb_queue_purge(&adapter.events_skb_q); | |
840 | ||
841 | if (adapter.events_wq) | |
842 | destroy_workqueue(adapter.events_wq); | |
843 | ||
844 | if (adapter.if_rx_workqueue) | |
845 | destroy_workqueue(adapter.if_rx_workqueue); | |
5a47b073 MM |
846 | |
847 | if (adapter.mac_filter_wq) | |
848 | destroy_workqueue(adapter.mac_filter_wq); | |
774e9b2e MM |
849 | } |
850 | ||
851 | static void esp_reset(void) | |
852 | { | |
853 | if (resetpin != HOST_GPIO_PIN_INVALID) { | |
854 | /* Check valid GPIO or not */ | |
855 | if (!gpio_is_valid(resetpin)) { | |
6f8fee57 | 856 | esp_warn("host resetpin (%d) configured is invalid GPIO\n", resetpin); |
774e9b2e MM |
857 | resetpin = HOST_GPIO_PIN_INVALID; |
858 | } else { | |
859 | gpio_request(resetpin, "sysfs"); | |
860 | ||
861 | /* HOST's resetpin set to OUTPUT, HIGH */ | |
862 | gpio_direction_output(resetpin, true); | |
863 | ||
864 | /* HOST's resetpin set to LOW */ | |
865 | gpio_set_value(resetpin, 0); | |
866 | udelay(200); | |
867 | ||
868 | /* HOST's resetpin set to INPUT */ | |
869 | gpio_direction_input(resetpin); | |
870 | ||
6f8fee57 | 871 | esp_dbg("Triggering ESP reset.\n"); |
774e9b2e MM |
872 | } |
873 | } | |
874 | } | |
875 | ||
876 | ||
877 | static int __init esp_init(void) | |
878 | { | |
879 | int ret = 0; | |
880 | struct esp_adapter *adapter = NULL; | |
881 | ||
882 | /* Reset ESP, Clean start ESP */ | |
883 | esp_reset(); | |
884 | msleep(200); | |
885 | ||
886 | adapter = init_adapter(); | |
887 | ||
888 | if (!adapter) | |
889 | return -EFAULT; | |
890 | ||
891 | /* Init transport layer */ | |
2b40207b | 892 | ret = esp_init_interface_layer(adapter, clockspeed); |
774e9b2e MM |
893 | |
894 | if (ret != 0) { | |
895 | deinit_adapter(); | |
896 | } | |
897 | ||
898 | return ret; | |
899 | } | |
900 | ||
901 | static void __exit esp_exit(void) | |
902 | { | |
903 | uint8_t iface_idx = 0; | |
bf3d6cb6 SR |
904 | #if TEST_RAW_TP |
905 | test_raw_tp_cleanup(); | |
906 | #endif | |
a3829bf3 | 907 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { |
774e9b2e MM |
908 | cmd_deinit_interface(adapter.priv[iface_idx]); |
909 | } | |
225e14eb | 910 | clear_bit(ESP_DRIVER_ACTIVE, &adapter.state_flags); |
774e9b2e MM |
911 | |
912 | esp_deinit_interface_layer(); | |
913 | deinit_adapter(); | |
914 | ||
915 | if (resetpin != HOST_GPIO_PIN_INVALID) { | |
916 | gpio_free(resetpin); | |
917 | } | |
918 | } | |
919 | MODULE_LICENSE("GPL"); | |
920 | MODULE_AUTHOR("Amey Inamdar <[email protected]>"); | |
921 | MODULE_AUTHOR("Mangesh Malusare <[email protected]>"); | |
922 | MODULE_AUTHOR("Yogesh Mantri <[email protected]>"); | |
923 | MODULE_DESCRIPTION("Wifi driver for ESP-Hosted solution"); | |
7e6de249 | 924 | MODULE_VERSION(RELEASE_VERSION); |
774e9b2e MM |
925 | module_init(esp_init); |
926 | module_exit(esp_exit); |