]>
Commit | Line | Data |
---|---|---|
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 |
26 | static int resetpin = HOST_GPIO_PIN_INVALID; | |
27 | extern u8 ap_bssid[MAC_ADDR_LEN]; | |
3fef9acf | 28 | extern volatile u8 host_sleep; |
774e9b2e MM |
29 | |
30 | module_param(resetpin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | |
31 | MODULE_PARM_DESC(resetpin, "Host's GPIO pin number which is connected to ESP32's EN to reset ESP32 device"); | |
32 | ||
33 | static void deinit_adapter(void); | |
34 | ||
35 | ||
7406fe82 | 36 | struct multicast_list mcast_list = {0}; |
774e9b2e MM |
37 | struct esp_adapter adapter; |
38 | /*struct esp_device esp_dev;*/ | |
39 | ||
a3829bf3 | 40 | struct esp_adapter *esp_get_adapter(void) |
774e9b2e MM |
41 | { |
42 | return &adapter; | |
43 | } | |
44 | ||
45 | void esp_process_new_packet_intr(struct esp_adapter *adapter) | |
46 | { | |
a3829bf3 | 47 | if (adapter) |
774e9b2e MM |
48 | queue_work(adapter->if_rx_workqueue, &adapter->if_rx_work); |
49 | } | |
50 | ||
a3829bf3 | 51 | static int process_tx_packet(struct sk_buff *skb) |
774e9b2e MM |
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; | |
a3829bf3 | 61 | static u8 c; |
774e9b2e MM |
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 | ||
a3829bf3 | 163 | void esp_port_open(struct esp_wifi_device *priv) |
774e9b2e MM |
164 | { |
165 | priv->port_open = 1; | |
166 | priv->stop_data = 0; | |
167 | } | |
168 | ||
a3829bf3 | 169 | void esp_port_close(struct esp_wifi_device *priv) |
774e9b2e MM |
170 | { |
171 | if (!priv) | |
172 | return; | |
173 | ||
174 | priv->port_open = 0; | |
175 | priv->stop_data = 1; | |
176 | } | |
177 | ||
178 | void 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 | ||
206 | void 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 | ||
221 | static 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 | ||
232 | static void print_reset_reason(uint32_t reason) | |
233 | { | |
234 | switch (reason) | |
235 | { | |
a3829bf3 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 | ||
255 | int 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 | ||
e58fd496 | 262 | esp_info("ESP chipset's last reset cause:\n"); |
774e9b2e MM |
263 | print_reset_reason(le32_to_cpu(fw_p->last_reset_reason)); |
264 | return check_esp_version(&fw_p->version); | |
265 | } | |
266 | ||
267 | static int esp_open(struct net_device *ndev) | |
268 | { | |
774e9b2e MM |
269 | return 0; |
270 | } | |
271 | ||
272 | static int esp_stop(struct net_device *ndev) | |
273 | { | |
225e14eb | 274 | struct esp_wifi_device *priv = netdev_priv(ndev); |
a3829bf3 | 275 | |
0af9c02e | 276 | ESP_MARK_SCAN_DONE(priv, true); |
774e9b2e MM |
277 | return 0; |
278 | } | |
279 | ||
a3829bf3 | 280 | static struct net_device_stats *esp_get_stats(struct net_device *ndev) |
774e9b2e MM |
281 | { |
282 | struct esp_wifi_device *priv = netdev_priv(ndev); | |
283 | ||
284 | if (!priv) | |
285 | return NULL; | |
286 | ||
287 | return &priv->stats; | |
288 | } | |
289 | ||
e33b3012 | 290 | #if 0 |
774e9b2e MM |
291 | static int esp_set_mac_address(struct net_device *ndev, void *data) |
292 | { | |
293 | struct esp_wifi_device *priv = netdev_priv(ndev); | |
294 | //struct sockaddr *mac_addr = data; | |
295 | ||
296 | if (!priv || !priv->adapter) | |
297 | return -EINVAL; | |
298 | ||
e58fd496 | 299 | esp_info("%u "MACSTR"\n", __LINE__, MAC2STR(priv->mac_address)); |
e33b3012 | 300 | eth_hw_addr_set(ndev, priv->mac_address/*mac_addr->sa_data*/); |
774e9b2e | 301 | |
774e9b2e MM |
302 | return 0; |
303 | } | |
e33b3012 | 304 | #endif |
774e9b2e | 305 | |
4c494b4b | 306 | static NDO_TX_TIMEOUT_PROTOTYPE() |
774e9b2e MM |
307 | { |
308 | } | |
309 | ||
310 | static void esp_set_rx_mode(struct net_device *ndev) | |
311 | { | |
7406fe82 MM |
312 | struct esp_wifi_device *priv = netdev_priv(ndev); |
313 | struct netdev_hw_addr *mac_addr; | |
314 | u32 count = 0; | |
5a47b073 | 315 | #if 0 |
7406fe82 MM |
316 | struct in_device *in_dev = in_dev_get(ndev); |
317 | struct ip_mc_list *ip_list = in_dev->mc_list; | |
5a47b073 | 318 | #endif |
7406fe82 MM |
319 | netdev_for_each_mc_addr(mac_addr, ndev) { |
320 | if (count < MAX_MULTICAST_ADDR_COUNT) { | |
e58fd496 | 321 | /*esp_info("%d: "MACSTR"\n", count+1, MAC2STR(mac_addr->addr));*/ |
7406fe82 MM |
322 | memcpy(&mcast_list.mcast_addr[count++], mac_addr->addr, ETH_ALEN); |
323 | } | |
324 | } | |
325 | ||
326 | mcast_list.priv = priv; | |
327 | mcast_list.addr_count = count; | |
328 | ||
329 | if (priv->port_open) { | |
6f8fee57 | 330 | /*esp_info("Set Multicast list\n");*/ |
7406fe82 MM |
331 | if (adapter.mac_filter_wq) |
332 | queue_work(adapter.mac_filter_wq, &adapter.mac_flter_work); | |
333 | } | |
334 | #if 0 | |
5a47b073 | 335 | cmd_set_mcast_mac_list(priv, &mcast_list); |
a3829bf3 | 336 | while (ip_list) { |
6f8fee57 | 337 | esp_dbg(" IP MC Address: 0x%x\n", ip_list->multiaddr); |
7406fe82 MM |
338 | ip_list = ip_list->next; |
339 | } | |
340 | #endif | |
341 | ||
774e9b2e MM |
342 | } |
343 | ||
344 | static int esp_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev) | |
345 | { | |
346 | struct esp_wifi_device *priv = NULL; | |
347 | struct esp_skb_cb *cb = NULL; | |
348 | ||
349 | if (!skb || !ndev) | |
350 | return NETDEV_TX_OK; | |
351 | ||
352 | priv = netdev_priv(ndev); | |
353 | if (!priv) { | |
354 | dev_kfree_skb(skb); | |
355 | return NETDEV_TX_OK; | |
356 | } | |
357 | ||
358 | if (!priv->port_open) { | |
359 | priv->stats.tx_dropped++; | |
6f8fee57 | 360 | /*esp_err("Port not yet open\n");*/ |
774e9b2e MM |
361 | dev_kfree_skb(skb); |
362 | return NETDEV_TX_OK; | |
363 | } | |
364 | ||
365 | if (!skb->len || (skb->len > ETH_FRAME_LEN)) { | |
6f8fee57 | 366 | esp_err("Bad len %d\n", skb->len); |
774e9b2e MM |
367 | priv->stats.tx_dropped++; |
368 | dev_kfree_skb(skb); | |
369 | return NETDEV_TX_OK; | |
370 | } | |
371 | ||
372 | cb = (struct esp_skb_cb *) skb->cb; | |
373 | cb->priv = priv; | |
374 | ||
375 | return process_tx_packet(skb); | |
376 | } | |
377 | ||
378 | static const struct net_device_ops esp_netdev_ops = { | |
379 | .ndo_open = esp_open, | |
380 | .ndo_stop = esp_stop, | |
381 | .ndo_start_xmit = esp_hard_start_xmit, | |
774e9b2e MM |
382 | .ndo_validate_addr = eth_validate_addr, |
383 | .ndo_tx_timeout = esp_tx_timeout, | |
384 | .ndo_get_stats = esp_get_stats, | |
385 | .ndo_set_rx_mode = esp_set_rx_mode, | |
386 | }; | |
387 | ||
388 | ||
389 | void esp_init_priv(struct net_device *ndev) | |
390 | { | |
391 | ndev->netdev_ops = &esp_netdev_ops; | |
392 | ndev->needed_headroom = roundup(sizeof(struct esp_payload_header) + | |
393 | INTERFACE_HEADER_PADDING, 4); | |
394 | } | |
395 | ||
774e9b2e MM |
396 | static int add_network_iface(void) |
397 | { | |
398 | int ret = 0; | |
a3829bf3 KG |
399 | struct esp_adapter *adapter = esp_get_adapter(); |
400 | struct wireless_dev *wdev = NULL; | |
774e9b2e MM |
401 | |
402 | if (!adapter) { | |
6f8fee57 | 403 | esp_info("adapter not yet init\n"); |
774e9b2e MM |
404 | return -EINVAL; |
405 | } | |
406 | ||
407 | ret = esp_cfg80211_register(adapter); | |
408 | if (ret) { | |
6f8fee57 | 409 | esp_err("Failed to register with cfg80211 (err code 0x%x)\n", ret); |
774e9b2e MM |
410 | return ret; |
411 | } | |
412 | ||
413 | rtnl_lock(); | |
225e14eb | 414 | wdev = esp_cfg80211_add_iface(adapter->wiphy, "espsta%d", 1, NL80211_IFTYPE_STATION, NULL); |
774e9b2e MM |
415 | rtnl_unlock(); |
416 | ||
225e14eb YM |
417 | /* Return success if network added successfully */ |
418 | if (wdev) | |
419 | return 0; | |
420 | ||
421 | return -1; | |
774e9b2e MM |
422 | } |
423 | ||
424 | int esp_add_card(struct esp_adapter *adapter) | |
425 | { | |
225e14eb | 426 | RET_ON_FAIL(esp_commands_setup(adapter)); |
774e9b2e | 427 | |
225e14eb | 428 | RET_ON_FAIL(add_network_iface()); |
774e9b2e | 429 | |
225e14eb | 430 | return 0; |
774e9b2e MM |
431 | } |
432 | ||
433 | void esp_remove_network_interfaces(struct esp_adapter *adapter) | |
434 | { | |
435 | uint8_t iface_idx = 0; | |
436 | struct net_device *ndev = NULL; | |
437 | struct esp_wifi_device *priv = NULL; | |
438 | ||
a3829bf3 | 439 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { |
774e9b2e MM |
440 | |
441 | priv = adapter->priv[iface_idx]; | |
442 | ||
443 | if (!priv) | |
444 | continue; | |
445 | ||
446 | if (!test_bit(ESP_NETWORK_UP, &priv->priv_flags)) | |
447 | continue; | |
448 | ||
449 | /* stop and unregister network */ | |
450 | ndev = priv->ndev; | |
451 | ||
452 | if (ndev) { | |
453 | ||
454 | if (netif_carrier_ok(ndev)) | |
455 | netif_carrier_off(ndev); | |
456 | ||
457 | netif_device_detach(ndev); | |
458 | ||
459 | if (ndev->reg_state == NETREG_REGISTERED) { | |
7406fe82 | 460 | unregister_inetaddr_notifier(&(adapter->priv[0]->nb)); |
774e9b2e MM |
461 | unregister_netdev(ndev); |
462 | free_netdev(ndev); | |
463 | ndev = NULL; | |
464 | } | |
465 | } | |
466 | clear_bit(ESP_NETWORK_UP, &priv->priv_flags); | |
467 | } | |
468 | ||
469 | if (adapter->wiphy) { | |
470 | ||
471 | wiphy_unregister(adapter->wiphy); | |
472 | wiphy_free(adapter->wiphy); | |
473 | adapter->wiphy = NULL; | |
474 | } | |
475 | } | |
476 | ||
477 | int esp_remove_card(struct esp_adapter *adapter) | |
478 | { | |
479 | uint8_t iface_idx = 0; | |
480 | ||
481 | if (!adapter) { | |
482 | return 0; | |
483 | } | |
484 | ||
225e14eb | 485 | esp_deinit_bt(adapter); |
774e9b2e | 486 | |
225e14eb | 487 | esp_commands_teardown(adapter); |
774e9b2e MM |
488 | |
489 | esp_remove_network_interfaces(adapter); | |
490 | ||
a3829bf3 | 491 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { |
774e9b2e MM |
492 | esp_port_close(adapter->priv[iface_idx]); |
493 | adapter->priv[iface_idx] = NULL; | |
494 | } | |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
a3829bf3 | 499 | struct esp_wifi_device *get_priv_from_payload_header( |
774e9b2e MM |
500 | struct esp_payload_header *header) |
501 | { | |
502 | struct esp_wifi_device *priv = NULL; | |
503 | u8 i = 0; | |
504 | ||
505 | if (!header) | |
506 | return NULL; | |
507 | ||
508 | for (i = 0; i < ESP_MAX_INTERFACE; i++) { | |
509 | priv = adapter.priv[i]; | |
510 | ||
511 | if (!priv) | |
512 | continue; | |
513 | ||
514 | if (priv->if_type == header->if_type && | |
515 | priv->if_num == header->if_num) { | |
516 | return priv; | |
517 | } | |
518 | } | |
519 | return NULL; | |
520 | } | |
521 | ||
522 | static void process_esp_bootup_event(struct esp_adapter *adapter, | |
523 | struct esp_internal_bootup_event *evt) | |
524 | { | |
525 | if (!adapter || !evt) { | |
6f8fee57 | 526 | esp_err("Invalid arguments\n"); |
774e9b2e MM |
527 | return; |
528 | } | |
529 | ||
530 | if (evt->header.status) { | |
6f8fee57 | 531 | esp_err("Incorrect ESP bootup event\n"); |
774e9b2e MM |
532 | return; |
533 | } | |
534 | ||
6f8fee57 | 535 | esp_info("\nReceived ESP bootup event\n"); |
774e9b2e MM |
536 | process_event_esp_bootup(adapter, evt->data, evt->len); |
537 | } | |
538 | ||
539 | static int process_internal_event(struct esp_adapter *adapter, | |
540 | struct sk_buff *skb) | |
541 | { | |
542 | struct event_header *header = NULL; | |
543 | ||
544 | if (!skb || !adapter) { | |
6f8fee57 | 545 | esp_err("esp32: Incorrect event data!\n"); |
774e9b2e MM |
546 | return -1; |
547 | } | |
548 | ||
549 | header = (struct event_header *) (skb->data); | |
550 | ||
551 | switch (header->event_code) { | |
552 | ||
553 | case ESP_INTERNAL_BOOTUP_EVENT: | |
554 | process_esp_bootup_event(adapter, | |
555 | (struct esp_internal_bootup_event *)(skb->data)); | |
556 | break; | |
557 | ||
558 | default: | |
6f8fee57 KG |
559 | esp_info("%u unhandled internal event[%u]\n", |
560 | __LINE__, header->event_code); | |
774e9b2e MM |
561 | break; |
562 | } | |
563 | ||
564 | return 0; | |
565 | } | |
566 | ||
567 | static void process_rx_packet(struct esp_adapter *adapter, struct sk_buff *skb) | |
568 | { | |
569 | struct esp_wifi_device *priv = NULL; | |
570 | struct esp_payload_header *payload_header = NULL; | |
571 | u16 len = 0, offset = 0; | |
bf3d6cb6 | 572 | u16 rx_checksum = 0, checksum = 0; |
774e9b2e MM |
573 | struct hci_dev *hdev = adapter->hcidev; |
574 | u8 *type = NULL; | |
a3829bf3 KG |
575 | struct sk_buff *eap_skb = NULL; |
576 | struct ethhdr *eth = NULL; | |
774e9b2e MM |
577 | |
578 | if (!skb) | |
579 | return; | |
580 | ||
581 | /* get the paload header */ | |
582 | payload_header = (struct esp_payload_header *) skb->data; | |
583 | ||
584 | len = le16_to_cpu(payload_header->len); | |
585 | offset = le16_to_cpu(payload_header->offset); | |
586 | ||
3fef9acf MM |
587 | if (payload_header->reserved2 == 0xFF) { |
588 | print_hex_dump(KERN_INFO, "Wake up packet: ", DUMP_PREFIX_ADDRESS, 16, 1, skb->data, len+offset, 1); | |
589 | } | |
774e9b2e | 590 | |
bf3d6cb6 SR |
591 | if (adapter->capabilities & ESP_CHECKSUM_ENABLED) { |
592 | rx_checksum = le16_to_cpu(payload_header->checksum); | |
593 | payload_header->checksum = 0; | |
594 | ||
595 | checksum = compute_checksum(skb->data, (len + offset)); | |
596 | ||
597 | if (checksum != rx_checksum) { | |
598 | dev_kfree_skb_any(skb); | |
599 | return; | |
600 | } | |
601 | } | |
602 | ||
774e9b2e MM |
603 | /* chop off the header from skb */ |
604 | skb_pull(skb, offset); | |
605 | ||
606 | if (payload_header->if_type == ESP_STA_IF || payload_header->if_type == ESP_AP_IF) { | |
607 | ||
608 | /* retrieve priv based on payload header contents */ | |
609 | priv = get_priv_from_payload_header(payload_header); | |
610 | ||
611 | if (!priv) { | |
6f8fee57 | 612 | esp_err("Empty priv\n"); |
774e9b2e MM |
613 | dev_kfree_skb_any(skb); |
614 | return; | |
615 | } | |
616 | ||
617 | if (payload_header->packet_type == PACKET_TYPE_EAPOL) { | |
6f8fee57 | 618 | esp_info("Rx PACKET_TYPE_EAPOL!!!!\n"); |
774e9b2e MM |
619 | esp_port_open(priv); |
620 | ||
7b6ed49e | 621 | eap_skb = alloc_skb(skb->len + ETH_HLEN, GFP_KERNEL); |
a3829bf3 | 622 | if (!eap_skb) { |
6f8fee57 | 623 | esp_info("%u memory alloc failed\n", __LINE__); |
774e9b2e MM |
624 | return; |
625 | } | |
626 | eap_skb->dev = priv->ndev; | |
627 | ||
628 | if (!IS_ALIGNED((unsigned long) eap_skb->data, SKB_DATA_ADDR_ALIGNMENT)) { | |
6f8fee57 | 629 | esp_info("%u eap skb unaligned\n", __LINE__); |
774e9b2e MM |
630 | } |
631 | ||
aad2ae2e | 632 | eth = (struct ethhdr *) skb_put(eap_skb, ETH_HLEN); |
774e9b2e MM |
633 | ether_addr_copy(eth->h_dest, /*skb->data*/priv->ndev->dev_addr); |
634 | ether_addr_copy(eth->h_source, /*skb->data+6*/ ap_bssid); | |
635 | eth->h_proto = cpu_to_be16(ETH_P_PAE); | |
636 | ||
637 | skb_put_data(eap_skb, skb->data, skb->len); | |
638 | eap_skb->protocol = eth_type_trans(eap_skb, eap_skb->dev); | |
639 | ||
640 | netif_rx(eap_skb); | |
641 | ||
642 | } else if (payload_header->packet_type == PACKET_TYPE_DATA) { | |
643 | ||
644 | skb->dev = priv->ndev; | |
645 | skb->protocol = eth_type_trans(skb, priv->ndev); | |
646 | skb->ip_summed = CHECKSUM_NONE; | |
647 | ||
648 | priv->stats.rx_bytes += skb->len; | |
649 | /* Forward skb to kernel */ | |
1294f8ac | 650 | NETIF_RX_NI(skb); |
774e9b2e MM |
651 | priv->stats.rx_packets++; |
652 | } else if (payload_header->packet_type == PACKET_TYPE_COMMAND_RESPONSE) { | |
225e14eb | 653 | process_cmd_resp(priv->adapter, skb); |
774e9b2e | 654 | } else if (payload_header->packet_type == PACKET_TYPE_EVENT) { |
225e14eb | 655 | process_cmd_event(priv, skb); |
774e9b2e MM |
656 | dev_kfree_skb_any(skb); |
657 | } | |
658 | ||
659 | } else if (payload_header->if_type == ESP_HCI_IF) { | |
660 | if (hdev) { | |
661 | ||
662 | type = skb->data; | |
663 | hci_skb_pkt_type(skb) = *type; | |
664 | skb_pull(skb, 1); | |
665 | ||
666 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) | |
667 | if (hci_recv_frame(hdev, skb)) { | |
668 | #else | |
669 | if (hci_recv_frame(skb)) { | |
670 | #endif | |
671 | hdev->stat.err_rx++; | |
672 | } else { | |
673 | esp_hci_update_rx_counter(hdev, *type, skb->len); | |
674 | } | |
675 | } | |
676 | } else if (payload_header->if_type == ESP_INTERNAL_IF) { | |
677 | ||
678 | /* Queue event skb for processing in events workqueue */ | |
679 | skb_queue_tail(&adapter->events_skb_q, skb); | |
680 | ||
681 | if (adapter->events_wq) | |
682 | queue_work(adapter->events_wq, &adapter->events_work); | |
683 | else | |
684 | dev_kfree_skb_any(skb); | |
685 | ||
bf3d6cb6 SR |
686 | } else if (payload_header->if_type == ESP_TEST_IF) { |
687 | #if TEST_RAW_TP | |
688 | update_test_raw_tp_rx_stats(len); | |
689 | #endif | |
690 | dev_kfree_skb_any(skb); | |
774e9b2e MM |
691 | } else { |
692 | dev_kfree_skb_any(skb); | |
693 | } | |
694 | } | |
695 | ||
bf3d6cb6 SR |
696 | int esp_is_tx_queue_paused(struct esp_wifi_device *priv) |
697 | { | |
698 | if (!priv || !priv->ndev) | |
699 | return 0; | |
700 | ||
701 | if ((priv->ndev && | |
702 | !netif_queue_stopped((const struct net_device *)priv->ndev))) | |
703 | return 1; | |
704 | return 0; | |
705 | } | |
706 | ||
774e9b2e MM |
707 | void esp_tx_pause(struct esp_wifi_device *priv) |
708 | { | |
709 | if (!priv || !priv->ndev) | |
710 | return; | |
711 | ||
712 | if (!netif_queue_stopped((const struct net_device *)priv->ndev)) { | |
713 | netif_stop_queue(priv->ndev); | |
714 | } | |
715 | } | |
716 | ||
717 | void esp_tx_resume(struct esp_wifi_device *priv) | |
718 | { | |
719 | if (!priv || !priv->ndev) | |
720 | return; | |
721 | ||
722 | if (netif_queue_stopped((const struct net_device *)priv->ndev)) { | |
723 | netif_wake_queue(priv->ndev); | |
724 | } | |
725 | } | |
726 | ||
a3829bf3 | 727 | struct sk_buff *esp_alloc_skb(u32 len) |
774e9b2e MM |
728 | { |
729 | struct sk_buff *skb = NULL; | |
730 | ||
731 | u8 offset; | |
732 | ||
733 | skb = netdev_alloc_skb(NULL, len + INTERFACE_HEADER_PADDING); | |
734 | ||
735 | if (skb) { | |
736 | /* Align SKB data pointer */ | |
737 | offset = ((unsigned long)skb->data) & (SKB_DATA_ADDR_ALIGNMENT - 1); | |
738 | ||
739 | if (offset) | |
740 | skb_reserve(skb, INTERFACE_HEADER_PADDING - offset); | |
741 | } | |
742 | ||
743 | return skb; | |
744 | } | |
745 | ||
746 | ||
747 | static int esp_get_packets(struct esp_adapter *adapter) | |
748 | { | |
749 | struct sk_buff *skb = NULL; | |
750 | ||
751 | if (!adapter || !adapter->if_ops || !adapter->if_ops->read) | |
752 | return -EINVAL; | |
753 | ||
754 | skb = adapter->if_ops->read(adapter); | |
755 | ||
756 | if (!skb) | |
757 | return -EFAULT; | |
758 | ||
759 | process_rx_packet(adapter, skb); | |
760 | ||
761 | return 0; | |
762 | } | |
763 | ||
764 | int esp_send_packet(struct esp_adapter *adapter, struct sk_buff *skb) | |
765 | { | |
225e14eb | 766 | if (!adapter || !adapter->if_ops || !adapter->if_ops->write) { |
6f8fee57 | 767 | esp_err("%u adapter: %p\n", __LINE__, adapter); |
774e9b2e | 768 | return -EINVAL; |
225e14eb | 769 | } |
774e9b2e MM |
770 | |
771 | return adapter->if_ops->write(adapter, skb); | |
772 | } | |
773 | ||
774 | static void esp_if_rx_work(struct work_struct *work) | |
775 | { | |
776 | /* read inbound packet and forward it to network/serial interface */ | |
777 | esp_get_packets(&adapter); | |
778 | } | |
779 | ||
7406fe82 MM |
780 | static void update_mac_filter(struct work_struct *work) |
781 | { | |
5a47b073 | 782 | cmd_set_mcast_mac_list(mcast_list.priv, &mcast_list); |
7406fe82 MM |
783 | } |
784 | ||
774e9b2e MM |
785 | static void esp_events_work(struct work_struct *work) |
786 | { | |
787 | struct sk_buff *skb = NULL; | |
788 | ||
789 | skb = skb_dequeue(&adapter.events_skb_q); | |
790 | if (!skb) | |
791 | return; | |
792 | ||
793 | process_internal_event(&adapter, skb); | |
794 | dev_kfree_skb_any(skb); | |
795 | } | |
796 | ||
a3829bf3 | 797 | static struct esp_adapter *init_adapter(void) |
774e9b2e MM |
798 | { |
799 | memset(&adapter, 0, sizeof(adapter)); | |
800 | ||
801 | /* Prepare interface RX work */ | |
802 | adapter.if_rx_workqueue = alloc_workqueue("ESP_IF_RX_WORK_QUEUE", 0, 0); | |
803 | ||
804 | if (!adapter.if_rx_workqueue) { | |
805 | deinit_adapter(); | |
806 | return NULL; | |
807 | } | |
808 | ||
809 | INIT_WORK(&adapter.if_rx_work, esp_if_rx_work); | |
810 | ||
811 | skb_queue_head_init(&adapter.events_skb_q); | |
812 | ||
813 | adapter.events_wq = alloc_workqueue("ESP_EVENTS_WORKQUEUE", WQ_HIGHPRI, 0); | |
814 | ||
815 | if (!adapter.events_wq) { | |
816 | deinit_adapter(); | |
817 | return NULL; | |
818 | } | |
819 | ||
820 | INIT_WORK(&adapter.events_work, esp_events_work); | |
821 | ||
7406fe82 MM |
822 | adapter.mac_filter_wq = alloc_workqueue("MAC_FILTER", 0, 0); |
823 | if (!adapter.mac_filter_wq) { | |
824 | deinit_adapter(); | |
825 | return NULL; | |
826 | } | |
827 | ||
828 | INIT_WORK(&adapter.mac_flter_work, update_mac_filter); | |
829 | ||
774e9b2e MM |
830 | return &adapter; |
831 | } | |
832 | ||
833 | static void deinit_adapter(void) | |
834 | { | |
835 | skb_queue_purge(&adapter.events_skb_q); | |
836 | ||
837 | if (adapter.events_wq) | |
838 | destroy_workqueue(adapter.events_wq); | |
839 | ||
840 | if (adapter.if_rx_workqueue) | |
841 | destroy_workqueue(adapter.if_rx_workqueue); | |
5a47b073 MM |
842 | |
843 | if (adapter.mac_filter_wq) | |
844 | destroy_workqueue(adapter.mac_filter_wq); | |
774e9b2e MM |
845 | } |
846 | ||
847 | static void esp_reset(void) | |
848 | { | |
849 | if (resetpin != HOST_GPIO_PIN_INVALID) { | |
850 | /* Check valid GPIO or not */ | |
851 | if (!gpio_is_valid(resetpin)) { | |
6f8fee57 | 852 | esp_warn("host resetpin (%d) configured is invalid GPIO\n", resetpin); |
774e9b2e MM |
853 | resetpin = HOST_GPIO_PIN_INVALID; |
854 | } else { | |
855 | gpio_request(resetpin, "sysfs"); | |
856 | ||
857 | /* HOST's resetpin set to OUTPUT, HIGH */ | |
858 | gpio_direction_output(resetpin, true); | |
859 | ||
860 | /* HOST's resetpin set to LOW */ | |
861 | gpio_set_value(resetpin, 0); | |
862 | udelay(200); | |
863 | ||
864 | /* HOST's resetpin set to INPUT */ | |
865 | gpio_direction_input(resetpin); | |
866 | ||
6f8fee57 | 867 | esp_dbg("Triggering ESP reset.\n"); |
774e9b2e MM |
868 | } |
869 | } | |
870 | } | |
871 | ||
872 | ||
873 | static int __init esp_init(void) | |
874 | { | |
875 | int ret = 0; | |
876 | struct esp_adapter *adapter = NULL; | |
877 | ||
878 | /* Reset ESP, Clean start ESP */ | |
879 | esp_reset(); | |
880 | msleep(200); | |
881 | ||
882 | adapter = init_adapter(); | |
883 | ||
884 | if (!adapter) | |
885 | return -EFAULT; | |
886 | ||
887 | /* Init transport layer */ | |
888 | ret = esp_init_interface_layer(adapter); | |
889 | ||
890 | if (ret != 0) { | |
891 | deinit_adapter(); | |
892 | } | |
893 | ||
894 | return ret; | |
895 | } | |
896 | ||
897 | static void __exit esp_exit(void) | |
898 | { | |
899 | uint8_t iface_idx = 0; | |
bf3d6cb6 SR |
900 | #if TEST_RAW_TP |
901 | test_raw_tp_cleanup(); | |
902 | #endif | |
a3829bf3 | 903 | for (iface_idx = 0; iface_idx < ESP_MAX_INTERFACE; iface_idx++) { |
774e9b2e MM |
904 | cmd_deinit_interface(adapter.priv[iface_idx]); |
905 | } | |
225e14eb | 906 | clear_bit(ESP_DRIVER_ACTIVE, &adapter.state_flags); |
774e9b2e MM |
907 | |
908 | esp_deinit_interface_layer(); | |
909 | deinit_adapter(); | |
910 | ||
911 | if (resetpin != HOST_GPIO_PIN_INVALID) { | |
912 | gpio_free(resetpin); | |
913 | } | |
914 | } | |
915 | MODULE_LICENSE("GPL"); | |
916 | MODULE_AUTHOR("Amey Inamdar <[email protected]>"); | |
917 | MODULE_AUTHOR("Mangesh Malusare <[email protected]>"); | |
918 | MODULE_AUTHOR("Yogesh Mantri <[email protected]>"); | |
919 | MODULE_DESCRIPTION("Wifi driver for ESP-Hosted solution"); | |
920 | MODULE_VERSION("0.1"); | |
921 | module_init(esp_init); | |
922 | module_exit(esp_exit); |