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