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