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