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