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