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