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