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