]> Git Repo - esp-hosted.git/blame - esp_hosted_ng/host/main.c
Merge branch 'bugfix/kernel_crash_ym2_6sep' into 'master'
[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>
24
25#include "esp.h"
26#include "esp_if.h"
27#include "esp_bt_api.h"
28#include "esp_api.h"
29#include "esp_cmd.h"
30
31#include "esp_cfg80211.h"
32
33#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0))
34 #define NDO_TX_TIMEOUT_PROTOTYPE() \
35 static void esp_tx_timeout(struct net_device *ndev, unsigned int txqueue)
36#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
37 #define NDO_TX_TIMEOUT_PROTOTYPE() \
38 static void esp_tx_timeout(struct net_device *ndev)
39#else
40 #error "No symbol **ndo_tx_timeout** found in kernel < 2.6.29"
41#endif
42
43#define HOST_GPIO_PIN_INVALID -1
44static int resetpin = HOST_GPIO_PIN_INVALID;
45extern u8 ap_bssid[MAC_ADDR_LEN];
46
47module_param(resetpin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
48MODULE_PARM_DESC(resetpin, "Host's GPIO pin number which is connected to ESP32's EN to reset ESP32 device");
49
50static void deinit_adapter(void);
51
52
53struct esp_adapter adapter;
54/*struct esp_device esp_dev;*/
55
56struct esp_adapter * esp_get_adapter(void)
57{
58 return &adapter;
59}
60
61void esp_process_new_packet_intr(struct esp_adapter *adapter)
62{
63 if(adapter)
64 queue_work(adapter->if_rx_workqueue, &adapter->if_rx_work);
65}
66
67static int process_tx_packet (struct sk_buff *skb)
68{
69 struct esp_wifi_device *priv = NULL;
70 struct esp_skb_cb *cb = NULL;
71 struct esp_payload_header *payload_header = NULL;
72 struct sk_buff *new_skb = NULL;
73 int ret = 0;
74 u8 pad_len = 0, realloc_skb = 0;
75 u16 len = 0;
76 u16 total_len = 0;
77 static u8 c = 0;
78 u8 *pos = NULL;
79
80 c++;
81 /* Get the priv */
82 cb = (struct esp_skb_cb *) skb->cb;
83 priv = cb->priv;
84
85 if (!priv) {
86 dev_kfree_skb(skb);
87 printk(KERN_INFO "%s: no priv\n", __func__);
88 return NETDEV_TX_OK;
89 }
90
91 if (netif_queue_stopped((const struct net_device *) priv->ndev)) {
92 printk(KERN_INFO "%s: Netif queue stopped\n", __func__);
93 return NETDEV_TX_BUSY;
94 }
95
96 len = skb->len;
97
98 /* Create space for payload header */
99 pad_len = sizeof(struct esp_payload_header);
100
101 total_len = len + pad_len;
102
103 /* Align buffer length */
104 pad_len += SKB_DATA_ADDR_ALIGNMENT - (total_len % SKB_DATA_ADDR_ALIGNMENT);
105
106 if (skb_headroom(skb) < pad_len) {
107 /* Headroom is not sufficient */
108 realloc_skb = 1;
109 }
110
111 if (realloc_skb || !IS_ALIGNED((unsigned long) skb->data, SKB_DATA_ADDR_ALIGNMENT)) {
112 /* Realloc SKB */
113 if (skb_linearize(skb)) {
114 priv->stats.tx_errors++;
115 dev_kfree_skb(skb);
116 printk(KERN_ERR "%s: Failed to linearize SKB", __func__);
117 return NETDEV_TX_OK;
118 }
119
120 new_skb = esp_alloc_skb(skb->len + pad_len);
121
122 if (!new_skb) {
123 printk(KERN_ERR "%s: Failed to allocate SKB", __func__);
124 priv->stats.tx_errors++;
125 dev_kfree_skb(skb);
126 return NETDEV_TX_OK;
127 }
128
129 pos = new_skb->data;
130 pos += pad_len;
131
132 /* Populate new SKB */
133 skb_copy_from_linear_data(skb, pos, skb->len);
134 skb_put(new_skb, skb->len + pad_len);
135
136 /* Replace old SKB */
137 dev_kfree_skb_any(skb);
138 skb = new_skb;
139 } else {
140 /* Realloc is not needed, Make space for interface header */
141 skb_push(skb, pad_len);
142 }
143
144 /* Set payload header */
145 payload_header = (struct esp_payload_header *) skb->data;
146 memset(payload_header, 0, pad_len);
147
148 payload_header->if_type = priv->if_type;
149 payload_header->if_num = priv->if_num;
150 payload_header->len = cpu_to_le16(len);
151 payload_header->offset = cpu_to_le16(pad_len);
152 payload_header->packet_type = PACKET_TYPE_DATA;
153
154 payload_header->checksum = cpu_to_le16(compute_checksum(skb->data, (len + pad_len)));
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) ||
198 (cap & ESP_BT_SDIO_SUPPORT) ||
199 (cap & ESP_BT_SPI_SUPPORT)) {
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
YM
285 struct esp_wifi_device *priv = netdev_priv(ndev);
286 esp_mark_scan_done(priv);
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
315NDO_TX_TIMEOUT_PROTOTYPE()
316{
317}
318
319static void esp_set_rx_mode(struct net_device *ndev)
320{
321}
322
323static int esp_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
324{
325 struct esp_wifi_device *priv = NULL;
326 struct esp_skb_cb *cb = NULL;
327
328 if (!skb || !ndev)
329 return NETDEV_TX_OK;
330
331 priv = netdev_priv(ndev);
332 if (!priv) {
333 dev_kfree_skb(skb);
334 return NETDEV_TX_OK;
335 }
336
337 if (!priv->port_open) {
338 priv->stats.tx_dropped++;
339 /*printk(KERN_ERR "esp32: %s: port not yet open\n", __func__);*/
340 dev_kfree_skb(skb);
341 return NETDEV_TX_OK;
342 }
343
344 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
345 printk(KERN_ERR "esp32: %s: Bad len %d\n", __func__, skb->len);
346 priv->stats.tx_dropped++;
347 dev_kfree_skb(skb);
348 return NETDEV_TX_OK;
349 }
350
351 cb = (struct esp_skb_cb *) skb->cb;
352 cb->priv = priv;
353
354 return process_tx_packet(skb);
355}
356
357static const struct net_device_ops esp_netdev_ops = {
358 .ndo_open = esp_open,
359 .ndo_stop = esp_stop,
360 .ndo_start_xmit = esp_hard_start_xmit,
361 .ndo_set_mac_address = esp_set_mac_address,
362 .ndo_validate_addr = eth_validate_addr,
363 .ndo_tx_timeout = esp_tx_timeout,
364 .ndo_get_stats = esp_get_stats,
365 .ndo_set_rx_mode = esp_set_rx_mode,
366};
367
368
369void esp_init_priv(struct net_device *ndev)
370{
371 ndev->netdev_ops = &esp_netdev_ops;
372 ndev->needed_headroom = roundup(sizeof(struct esp_payload_header) +
373 INTERFACE_HEADER_PADDING, 4);
374}
375
774e9b2e
MM
376static int add_network_iface(void)
377{
378 int ret = 0;
379 struct esp_adapter * adapter = esp_get_adapter();
225e14eb 380 struct wireless_dev * wdev = NULL;
774e9b2e
MM
381
382 if (!adapter) {
383 printk(KERN_INFO "%s: adapter not yet init\n", __func__);
384 return -EINVAL;
385 }
386
387 ret = esp_cfg80211_register(adapter);
388 if (ret) {
389 printk(KERN_ERR "Failed to register with cfg80211 (err code 0x%x)\n", ret);
390 return ret;
391 }
392
393 rtnl_lock();
225e14eb 394 wdev = esp_cfg80211_add_iface(adapter->wiphy, "espsta%d", 1, NL80211_IFTYPE_STATION, NULL);
774e9b2e
MM
395 rtnl_unlock();
396
225e14eb
YM
397 /* Return success if network added successfully */
398 if (wdev)
399 return 0;
400
401 return -1;
774e9b2e
MM
402}
403
404int esp_add_card(struct esp_adapter *adapter)
405{
225e14eb 406 RET_ON_FAIL(esp_commands_setup(adapter));
774e9b2e 407
225e14eb 408 RET_ON_FAIL(add_network_iface());
774e9b2e 409
225e14eb 410 return 0;
774e9b2e
MM
411}
412
413void esp_remove_network_interfaces(struct esp_adapter *adapter)
414{
415 uint8_t iface_idx = 0;
416 struct net_device *ndev = NULL;
417 struct esp_wifi_device *priv = NULL;
418
419 for (iface_idx=0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
420
421 priv = adapter->priv[iface_idx];
422
423 if (!priv)
424 continue;
425
426 if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags))
427 continue;
428
429 /* stop and unregister network */
430 ndev = priv->ndev;
431
432 if (ndev) {
433
434 if (netif_carrier_ok(ndev))
435 netif_carrier_off(ndev);
436
437 netif_device_detach(ndev);
438
439 if (ndev->reg_state == NETREG_REGISTERED) {
440 unregister_netdev(ndev);
441 free_netdev(ndev);
442 ndev = NULL;
443 }
444 }
445 clear_bit(ESP_NETWORK_UP, &priv->priv_flags);
446 }
447
448 if (adapter->wiphy) {
449
450 wiphy_unregister(adapter->wiphy);
451 wiphy_free(adapter->wiphy);
452 adapter->wiphy = NULL;
453 }
454}
455
456int esp_remove_card(struct esp_adapter *adapter)
457{
458 uint8_t iface_idx = 0;
459
460 if (!adapter) {
461 return 0;
462 }
463
225e14eb 464 esp_deinit_bt(adapter);
774e9b2e 465
225e14eb 466 esp_commands_teardown(adapter);
774e9b2e
MM
467
468 esp_remove_network_interfaces(adapter);
469
470 for (iface_idx=0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) {
471 esp_port_close(adapter->priv[iface_idx]);
472 adapter->priv[iface_idx] = NULL;
473 }
474
475 return 0;
476}
477
478struct esp_wifi_device * get_priv_from_payload_header(
479 struct esp_payload_header *header)
480{
481 struct esp_wifi_device *priv = NULL;
482 u8 i = 0;
483
484 if (!header)
485 return NULL;
486
487 for (i = 0; i < ESP_MAX_INTERFACE; i++) {
488 priv = adapter.priv[i];
489
490 if (!priv)
491 continue;
492
493 if (priv->if_type == header->if_type &&
494 priv->if_num == header->if_num) {
495 return priv;
496 }
497 }
498 return NULL;
499}
500
501static void process_esp_bootup_event(struct esp_adapter *adapter,
502 struct esp_internal_bootup_event *evt)
503{
504 if (!adapter || !evt) {
505 printk(KERN_ERR "%s: Invalid arguments\n", __func__);
506 return;
507 }
508
509 if (evt->header.status) {
510 printk(KERN_ERR "%s: Incorrect ESP bootup event\n", __func__);
511 return;
512 }
513
514 printk (KERN_INFO "\nReceived ESP bootup event\n");
515 process_event_esp_bootup(adapter, evt->data, evt->len);
516}
517
518static int process_internal_event(struct esp_adapter *adapter,
519 struct sk_buff *skb)
520{
521 struct event_header *header = NULL;
522
523 if (!skb || !adapter) {
524 printk (KERN_ERR "esp32: Incorrect event data!\n");
525 return -1;
526 }
527
528 header = (struct event_header *) (skb->data);
529
530 switch (header->event_code) {
531
532 case ESP_INTERNAL_BOOTUP_EVENT:
533 process_esp_bootup_event(adapter,
534 (struct esp_internal_bootup_event *)(skb->data));
535 break;
536
537 default:
538 printk(KERN_INFO "%s:%u unhandled internal event[%u]\n",
539 __func__, __LINE__, header->event_code);
540 break;
541 }
542
543 return 0;
544}
545
546static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb)
547{
548 struct esp_wifi_device *priv = NULL;
549 struct esp_payload_header *payload_header = NULL;
550 u16 len = 0, offset = 0;
551 struct hci_dev *hdev = adapter->hcidev;
552 u8 *type = NULL;
553 struct sk_buff * eap_skb = NULL;
554 struct ethhdr * eth = NULL;
555
556 if (!skb)
557 return;
558
559 /* get the paload header */
560 payload_header = (struct esp_payload_header *) skb->data;
561
562 len = le16_to_cpu(payload_header->len);
563 offset = le16_to_cpu(payload_header->offset);
564
565 /*print_hex_dump(KERN_ERR , "rx: ", DUMP_PREFIX_ADDRESS, 16, 1, skb->data, len, 1);*/
566
567 payload_header->checksum = 0;
568
569 /* chop off the header from skb */
570 skb_pull(skb, offset);
571
572 if (payload_header->if_type == ESP_STA_IF || payload_header->if_type == ESP_AP_IF) {
573
574 /* retrieve priv based on payload header contents */
575 priv = get_priv_from_payload_header(payload_header);
576
577 if (!priv) {
578 printk(KERN_ERR "%s: empty priv\n", __func__);
579 dev_kfree_skb_any(skb);
580 return;
581 }
582
583 if (payload_header->packet_type == PACKET_TYPE_EAPOL) {
584 esp_port_open(priv);
585
586 eap_skb = alloc_skb(skb->len, GFP_KERNEL);
587 if(!eap_skb) {
588 printk(KERN_INFO "%s:%u memory alloc failed\n",__func__, __LINE__);
589 return;
590 }
591 eap_skb->dev = priv->ndev;
592
593 if (!IS_ALIGNED((unsigned long) eap_skb->data, SKB_DATA_ADDR_ALIGNMENT)) {
594 printk(KERN_INFO "%s:%u eap skb unaligned\n",__func__, __LINE__);
595 }
596
597 eth = skb_put(eap_skb, ETH_HLEN);
598 ether_addr_copy(eth->h_dest, /*skb->data*/priv->ndev->dev_addr);
599 ether_addr_copy(eth->h_source, /*skb->data+6*/ ap_bssid);
600 eth->h_proto = cpu_to_be16(ETH_P_PAE);
601
602 skb_put_data(eap_skb, skb->data, skb->len);
603 eap_skb->protocol = eth_type_trans(eap_skb, eap_skb->dev);
604
605 netif_rx(eap_skb);
606
607 } else if (payload_header->packet_type == PACKET_TYPE_DATA) {
608
609 skb->dev = priv->ndev;
610 skb->protocol = eth_type_trans(skb, priv->ndev);
611 skb->ip_summed = CHECKSUM_NONE;
612
613 priv->stats.rx_bytes += skb->len;
614 /* Forward skb to kernel */
615 netif_rx_ni(skb);
616
617 priv->stats.rx_packets++;
618 } else if (payload_header->packet_type == PACKET_TYPE_COMMAND_RESPONSE) {
225e14eb 619 process_cmd_resp(priv->adapter, skb);
774e9b2e 620 } else if (payload_header->packet_type == PACKET_TYPE_EVENT) {
225e14eb 621 process_cmd_event(priv, skb);
774e9b2e
MM
622 dev_kfree_skb_any(skb);
623 }
624
625 } else if (payload_header->if_type == ESP_HCI_IF) {
626 if (hdev) {
627
628 type = skb->data;
629 hci_skb_pkt_type(skb) = *type;
630 skb_pull(skb, 1);
631
632#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0))
633 if (hci_recv_frame(hdev, skb)) {
634#else
635 if (hci_recv_frame(skb)) {
636#endif
637 hdev->stat.err_rx++;
638 } else {
639 esp_hci_update_rx_counter(hdev, *type, skb->len);
640 }
641 }
642 } else if (payload_header->if_type == ESP_INTERNAL_IF) {
643
644 /* Queue event skb for processing in events workqueue */
645 skb_queue_tail(&adapter->events_skb_q, skb);
646
647 if (adapter->events_wq)
648 queue_work(adapter->events_wq, &adapter->events_work);
649 else
650 dev_kfree_skb_any(skb);
651
652 } else {
653 dev_kfree_skb_any(skb);
654 }
655}
656
657void esp_tx_pause(struct esp_wifi_device *priv)
658{
659 if (!priv || !priv->ndev)
660 return;
661
662 if (!netif_queue_stopped((const struct net_device *)priv->ndev)) {
663 netif_stop_queue(priv->ndev);
664 }
665}
666
667void esp_tx_resume(struct esp_wifi_device *priv)
668{
669 if (!priv || !priv->ndev)
670 return;
671
672 if (netif_queue_stopped((const struct net_device *)priv->ndev)) {
673 netif_wake_queue(priv->ndev);
674 }
675}
676
677struct sk_buff * esp_alloc_skb(u32 len)
678{
679 struct sk_buff *skb = NULL;
680
681 u8 offset;
682
683 skb = netdev_alloc_skb(NULL, len + INTERFACE_HEADER_PADDING);
684
685 if (skb) {
686 /* Align SKB data pointer */
687 offset = ((unsigned long)skb->data) & (SKB_DATA_ADDR_ALIGNMENT - 1);
688
689 if (offset)
690 skb_reserve(skb, INTERFACE_HEADER_PADDING - offset);
691 }
692
693 return skb;
694}
695
696
697static int esp_get_packets(struct esp_adapter *adapter)
698{
699 struct sk_buff *skb = NULL;
700
701 if (!adapter || !adapter->if_ops || !adapter->if_ops->read)
702 return -EINVAL;
703
704 skb = adapter->if_ops->read(adapter);
705
706 if (!skb)
707 return -EFAULT;
708
709 process_rx_packet(adapter, skb);
710
711 return 0;
712}
713
714int esp_send_packet(struct esp_adapter *adapter, struct sk_buff *skb)
715{
225e14eb
YM
716 if (!adapter || !adapter->if_ops || !adapter->if_ops->write) {
717 printk(KERN_ERR "esp32: %s:%u adapter: %p\n", __func__, __LINE__, adapter);
774e9b2e 718 return -EINVAL;
225e14eb 719 }
774e9b2e
MM
720
721 return adapter->if_ops->write(adapter, skb);
722}
723
724static void esp_if_rx_work(struct work_struct *work)
725{
726 /* read inbound packet and forward it to network/serial interface */
727 esp_get_packets(&adapter);
728}
729
730static void esp_events_work(struct work_struct *work)
731{
732 struct sk_buff *skb = NULL;
733
734 skb = skb_dequeue(&adapter.events_skb_q);
735 if (!skb)
736 return;
737
738 process_internal_event(&adapter, skb);
739 dev_kfree_skb_any(skb);
740}
741
742static struct esp_adapter * init_adapter(void)
743{
744 memset(&adapter, 0, sizeof(adapter));
745
746 /* Prepare interface RX work */
747 adapter.if_rx_workqueue = alloc_workqueue("ESP_IF_RX_WORK_QUEUE", 0, 0);
748
749 if (!adapter.if_rx_workqueue) {
750 deinit_adapter();
751 return NULL;
752 }
753
754 INIT_WORK(&adapter.if_rx_work, esp_if_rx_work);
755
756 skb_queue_head_init(&adapter.events_skb_q);
757
758 adapter.events_wq = alloc_workqueue("ESP_EVENTS_WORKQUEUE", WQ_HIGHPRI, 0);
759
760 if (!adapter.events_wq) {
761 deinit_adapter();
762 return NULL;
763 }
764
765 INIT_WORK(&adapter.events_work, esp_events_work);
766
767 return &adapter;
768}
769
770static void deinit_adapter(void)
771{
772 skb_queue_purge(&adapter.events_skb_q);
773
774 if (adapter.events_wq)
775 destroy_workqueue(adapter.events_wq);
776
777 if (adapter.if_rx_workqueue)
778 destroy_workqueue(adapter.if_rx_workqueue);
779}
780
781static void esp_reset(void)
782{
783 if (resetpin != HOST_GPIO_PIN_INVALID) {
784 /* Check valid GPIO or not */
785 if (!gpio_is_valid(resetpin)) {
786 printk(KERN_WARNING "%s, ESP32: host resetpin (%d) configured is invalid GPIO\n", __func__, resetpin);
787 resetpin = HOST_GPIO_PIN_INVALID;
788 } else {
789 gpio_request(resetpin, "sysfs");
790
791 /* HOST's resetpin set to OUTPUT, HIGH */
792 gpio_direction_output(resetpin, true);
793
794 /* HOST's resetpin set to LOW */
795 gpio_set_value(resetpin, 0);
796 udelay(200);
797
798 /* HOST's resetpin set to INPUT */
799 gpio_direction_input(resetpin);
800
801 printk(KERN_DEBUG "%s, ESP32: Triggering ESP reset.\n", __func__);
802 }
803 }
804}
805
806
807static int __init esp_init(void)
808{
809 int ret = 0;
810 struct esp_adapter *adapter = NULL;
811
812 /* Reset ESP, Clean start ESP */
813 esp_reset();
814 msleep(200);
815
816 adapter = init_adapter();
817
818 if (!adapter)
819 return -EFAULT;
820
821 /* Init transport layer */
822 ret = esp_init_interface_layer(adapter);
823
824 if (ret != 0) {
825 deinit_adapter();
826 }
827
828 return ret;
829}
830
831static void __exit esp_exit(void)
832{
833 uint8_t iface_idx = 0;
834
835 for (iface_idx=0; iface_idx<ESP_MAX_INTERFACE; iface_idx++) {
836 cmd_deinit_interface(adapter.priv[iface_idx]);
837 }
225e14eb 838 clear_bit(ESP_DRIVER_ACTIVE, &adapter.state_flags);
774e9b2e
MM
839
840 esp_deinit_interface_layer();
841 deinit_adapter();
842
843 if (resetpin != HOST_GPIO_PIN_INVALID) {
844 gpio_free(resetpin);
845 }
846}
847MODULE_LICENSE("GPL");
848MODULE_AUTHOR("Amey Inamdar <[email protected]>");
849MODULE_AUTHOR("Mangesh Malusare <[email protected]>");
850MODULE_AUTHOR("Yogesh Mantri <[email protected]>");
851MODULE_DESCRIPTION("Wifi driver for ESP-Hosted solution");
852MODULE_VERSION("0.1");
853module_init(esp_init);
854module_exit(esp_exit);
This page took 0.11429 seconds and 4 git commands to generate.