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