]> Git Repo - esp-hosted.git/blame - esp_hosted_ng/host/main.c
esp_hosted_Xg/host: fix driver build failure with Linux version >= 5.17
[esp-hosted.git] / esp_hosted_ng / host / main.c
CommitLineData
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
37static int resetpin = HOST_GPIO_PIN_INVALID;
38extern u8 ap_bssid[MAC_ADDR_LEN];
3fef9acf 39extern volatile u8 host_sleep;
774e9b2e
MM
40
41module_param(resetpin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
42MODULE_PARM_DESC(resetpin, "Host's GPIO pin number which is connected to ESP32's EN to reset ESP32 device");
43
44static void deinit_adapter(void);
45
46
7406fe82 47struct multicast_list mcast_list = {0};
774e9b2e
MM
48struct esp_adapter adapter;
49/*struct esp_device esp_dev;*/
50
51struct esp_adapter * esp_get_adapter(void)
52{
53 return &adapter;
54}
55
56void 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
62static 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
174void esp_port_open(struct esp_wifi_device * priv)
175{
176 priv->port_open = 1;
177 priv->stop_data = 0;
178}
179
180void 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
189void 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
217void 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
232static 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
243static 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
266int 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
278static int esp_open(struct net_device *ndev)
279{
774e9b2e
MM
280 return 0;
281}
282
283static 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
290static 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
300static int esp_set_mac_address(struct net_device *ndev, void *data)
301{
302 struct esp_wifi_device *priv = netdev_priv(ndev);
303 //struct sockaddr *mac_addr = data;
304
305 if (!priv || !priv->adapter)
306 return -EINVAL;
307
308 printk(KERN_INFO "%s:%u %pM\n", __func__, __LINE__, priv->mac_address);
309 /* TODO Handle in correct way */
310 ether_addr_copy(ndev->dev_addr, priv->mac_address/*mac_addr->sa_data*/);
311
774e9b2e
MM
312 return 0;
313}
314
4c494b4b 315static NDO_TX_TIMEOUT_PROTOTYPE()
774e9b2e
MM
316{
317}
318
319static void esp_set_rx_mode(struct net_device *ndev)
320{
7406fe82
MM
321 struct esp_wifi_device *priv = netdev_priv(ndev);
322 struct netdev_hw_addr *mac_addr;
323 u32 count = 0;
5a47b073 324#if 0
7406fe82
MM
325 struct in_device *in_dev = in_dev_get(ndev);
326 struct ip_mc_list *ip_list = in_dev->mc_list;
5a47b073 327#endif
7406fe82
MM
328 netdev_for_each_mc_addr(mac_addr, ndev) {
329 if (count < MAX_MULTICAST_ADDR_COUNT) {
5a47b073 330 /*printk(KERN_INFO "%d: %pM\n", count+1, mac_addr->addr);*/
7406fe82
MM
331 memcpy(&mcast_list.mcast_addr[count++], mac_addr->addr, ETH_ALEN);
332 }
333 }
334
335 mcast_list.priv = priv;
336 mcast_list.addr_count = count;
337
338 if (priv->port_open) {
5a47b073 339 /*printk (KERN_INFO "Set Multicast list\n");*/
7406fe82
MM
340 if (adapter.mac_filter_wq)
341 queue_work(adapter.mac_filter_wq, &adapter.mac_flter_work);
342 }
343#if 0
5a47b073 344 cmd_set_mcast_mac_list(priv, &mcast_list);
7406fe82
MM
345 while(ip_list) {
346 printk(KERN_DEBUG " IP MC Address: 0x%x\n", ip_list->multiaddr);
347 ip_list = ip_list->next;
348 }
349#endif
350
774e9b2e
MM
351}
352
353static int esp_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
354{
355 struct esp_wifi_device *priv = NULL;
356 struct esp_skb_cb *cb = NULL;
357
358 if (!skb || !ndev)
359 return NETDEV_TX_OK;
360
361 priv = netdev_priv(ndev);
362 if (!priv) {
363 dev_kfree_skb(skb);
364 return NETDEV_TX_OK;
365 }
366
367 if (!priv->port_open) {
368 priv->stats.tx_dropped++;
369 /*printk(KERN_ERR "esp32: %s: port not yet open\n", __func__);*/
370 dev_kfree_skb(skb);
371 return NETDEV_TX_OK;
372 }
373
374 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
375 printk(KERN_ERR "esp32: %s: Bad len %d\n", __func__, skb->len);
376 priv->stats.tx_dropped++;
377 dev_kfree_skb(skb);
378 return NETDEV_TX_OK;
379 }
380
381 cb = (struct esp_skb_cb *) skb->cb;
382 cb->priv = priv;
383
384 return process_tx_packet(skb);
385}
386
387static const struct net_device_ops esp_netdev_ops = {
388 .ndo_open = esp_open,
389 .ndo_stop = esp_stop,
390 .ndo_start_xmit = esp_hard_start_xmit,
391 .ndo_set_mac_address = esp_set_mac_address,
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
399void 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
406static 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
434int 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
443void 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
487int 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
509struct 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
532static 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
549static 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
577static 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 */
660 netif_rx_ni(skb);
661
662 priv->stats.rx_packets++;
663 } else if (payload_header->packet_type == PACKET_TYPE_COMMAND_RESPONSE) {
225e14eb 664 process_cmd_resp(priv->adapter, skb);
774e9b2e 665 } else if (payload_header->packet_type == PACKET_TYPE_EVENT) {
225e14eb 666 process_cmd_event(priv, skb);
774e9b2e
MM
667 dev_kfree_skb_any(skb);
668 }
669
670 } else if (payload_header->if_type == ESP_HCI_IF) {
671 if (hdev) {
672
673 type = skb->data;
674 hci_skb_pkt_type(skb) = *type;
675 skb_pull(skb, 1);
676
677#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
678 if (hci_recv_frame(hdev, skb)) {
679#else
680 if (hci_recv_frame(skb)) {
681#endif
682 hdev->stat.err_rx++;
683 } else {
684 esp_hci_update_rx_counter(hdev, *type, skb->len);
685 }
686 }
687 } else if (payload_header->if_type == ESP_INTERNAL_IF) {
688
689 /* Queue event skb for processing in events workqueue */
690 skb_queue_tail(&adapter->events_skb_q, skb);
691
692 if (adapter->events_wq)
693 queue_work(adapter->events_wq, &adapter->events_work);
694 else
695 dev_kfree_skb_any(skb);
696
bf3d6cb6
SR
697 } else if (payload_header->if_type == ESP_TEST_IF) {
698 #if TEST_RAW_TP
699 update_test_raw_tp_rx_stats(len);
700 #endif
701 dev_kfree_skb_any(skb);
774e9b2e
MM
702 } else {
703 dev_kfree_skb_any(skb);
704 }
705}
706
bf3d6cb6
SR
707int esp_is_tx_queue_paused(struct esp_wifi_device *priv)
708{
709 if (!priv || !priv->ndev)
710 return 0;
711
712 if ((priv->ndev &&
713 !netif_queue_stopped((const struct net_device *)priv->ndev)))
714 return 1;
715 return 0;
716}
717
774e9b2e
MM
718void esp_tx_pause(struct esp_wifi_device *priv)
719{
720 if (!priv || !priv->ndev)
721 return;
722
723 if (!netif_queue_stopped((const struct net_device *)priv->ndev)) {
724 netif_stop_queue(priv->ndev);
725 }
726}
727
728void esp_tx_resume(struct esp_wifi_device *priv)
729{
730 if (!priv || !priv->ndev)
731 return;
732
733 if (netif_queue_stopped((const struct net_device *)priv->ndev)) {
734 netif_wake_queue(priv->ndev);
735 }
736}
737
738struct sk_buff * esp_alloc_skb(u32 len)
739{
740 struct sk_buff *skb = NULL;
741
742 u8 offset;
743
744 skb = netdev_alloc_skb(NULL, len + INTERFACE_HEADER_PADDING);
745
746 if (skb) {
747 /* Align SKB data pointer */
748 offset = ((unsigned long)skb->data) & (SKB_DATA_ADDR_ALIGNMENT - 1);
749
750 if (offset)
751 skb_reserve(skb, INTERFACE_HEADER_PADDING - offset);
752 }
753
754 return skb;
755}
756
757
758static int esp_get_packets(struct esp_adapter *adapter)
759{
760 struct sk_buff *skb = NULL;
761
762 if (!adapter || !adapter->if_ops || !adapter->if_ops->read)
763 return -EINVAL;
764
765 skb = adapter->if_ops->read(adapter);
766
767 if (!skb)
768 return -EFAULT;
769
770 process_rx_packet(adapter, skb);
771
772 return 0;
773}
774
775int esp_send_packet(struct esp_adapter *adapter, struct sk_buff *skb)
776{
225e14eb
YM
777 if (!adapter || !adapter->if_ops || !adapter->if_ops->write) {
778 printk(KERN_ERR "esp32: %s:%u adapter: %p\n", __func__, __LINE__, adapter);
774e9b2e 779 return -EINVAL;
225e14eb 780 }
774e9b2e
MM
781
782 return adapter->if_ops->write(adapter, skb);
783}
784
785static void esp_if_rx_work(struct work_struct *work)
786{
787 /* read inbound packet and forward it to network/serial interface */
788 esp_get_packets(&adapter);
789}
790
7406fe82
MM
791static void update_mac_filter(struct work_struct *work)
792{
5a47b073 793 cmd_set_mcast_mac_list(mcast_list.priv, &mcast_list);
7406fe82
MM
794}
795
774e9b2e
MM
796static void esp_events_work(struct work_struct *work)
797{
798 struct sk_buff *skb = NULL;
799
800 skb = skb_dequeue(&adapter.events_skb_q);
801 if (!skb)
802 return;
803
804 process_internal_event(&adapter, skb);
805 dev_kfree_skb_any(skb);
806}
807
808static struct esp_adapter * init_adapter(void)
809{
810 memset(&adapter, 0, sizeof(adapter));
811
812 /* Prepare interface RX work */
813 adapter.if_rx_workqueue = alloc_workqueue("ESP_IF_RX_WORK_QUEUE", 0, 0);
814
815 if (!adapter.if_rx_workqueue) {
816 deinit_adapter();
817 return NULL;
818 }
819
820 INIT_WORK(&adapter.if_rx_work, esp_if_rx_work);
821
822 skb_queue_head_init(&adapter.events_skb_q);
823
824 adapter.events_wq = alloc_workqueue("ESP_EVENTS_WORKQUEUE", WQ_HIGHPRI, 0);
825
826 if (!adapter.events_wq) {
827 deinit_adapter();
828 return NULL;
829 }
830
831 INIT_WORK(&adapter.events_work, esp_events_work);
832
7406fe82
MM
833 adapter.mac_filter_wq = alloc_workqueue("MAC_FILTER", 0, 0);
834 if (!adapter.mac_filter_wq) {
835 deinit_adapter();
836 return NULL;
837 }
838
839 INIT_WORK(&adapter.mac_flter_work, update_mac_filter);
840
774e9b2e
MM
841 return &adapter;
842}
843
844static void deinit_adapter(void)
845{
846 skb_queue_purge(&adapter.events_skb_q);
847
848 if (adapter.events_wq)
849 destroy_workqueue(adapter.events_wq);
850
851 if (adapter.if_rx_workqueue)
852 destroy_workqueue(adapter.if_rx_workqueue);
5a47b073
MM
853
854 if (adapter.mac_filter_wq)
855 destroy_workqueue(adapter.mac_filter_wq);
774e9b2e
MM
856}
857
858static void esp_reset(void)
859{
860 if (resetpin != HOST_GPIO_PIN_INVALID) {
861 /* Check valid GPIO or not */
862 if (!gpio_is_valid(resetpin)) {
863 printk(KERN_WARNING "%s, ESP32: host resetpin (%d) configured is invalid GPIO\n", __func__, resetpin);
864 resetpin = HOST_GPIO_PIN_INVALID;
865 } else {
866 gpio_request(resetpin, "sysfs");
867
868 /* HOST's resetpin set to OUTPUT, HIGH */
869 gpio_direction_output(resetpin, true);
870
871 /* HOST's resetpin set to LOW */
872 gpio_set_value(resetpin, 0);
873 udelay(200);
874
875 /* HOST's resetpin set to INPUT */
876 gpio_direction_input(resetpin);
877
878 printk(KERN_DEBUG "%s, ESP32: Triggering ESP reset.\n", __func__);
879 }
880 }
881}
882
883
884static int __init esp_init(void)
885{
886 int ret = 0;
887 struct esp_adapter *adapter = NULL;
888
889 /* Reset ESP, Clean start ESP */
890 esp_reset();
891 msleep(200);
892
893 adapter = init_adapter();
894
895 if (!adapter)
896 return -EFAULT;
897
898 /* Init transport layer */
899 ret = esp_init_interface_layer(adapter);
900
901 if (ret != 0) {
902 deinit_adapter();
903 }
904
905 return ret;
906}
907
908static void __exit esp_exit(void)
909{
910 uint8_t iface_idx = 0;
bf3d6cb6
SR
911#if TEST_RAW_TP
912 test_raw_tp_cleanup();
913#endif
774e9b2e
MM
914 for (iface_idx=0; iface_idx<ESP_MAX_INTERFACE; iface_idx++) {
915 cmd_deinit_interface(adapter.priv[iface_idx]);
916 }
225e14eb 917 clear_bit(ESP_DRIVER_ACTIVE, &adapter.state_flags);
774e9b2e
MM
918
919 esp_deinit_interface_layer();
920 deinit_adapter();
921
922 if (resetpin != HOST_GPIO_PIN_INVALID) {
923 gpio_free(resetpin);
924 }
925}
926MODULE_LICENSE("GPL");
927MODULE_AUTHOR("Amey Inamdar <[email protected]>");
928MODULE_AUTHOR("Mangesh Malusare <[email protected]>");
929MODULE_AUTHOR("Yogesh Mantri <[email protected]>");
930MODULE_DESCRIPTION("Wifi driver for ESP-Hosted solution");
931MODULE_VERSION("0.1");
932module_init(esp_init);
933module_exit(esp_exit);
This page took 0.148216 seconds and 4 git commands to generate.