1 /*
2  * Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #define CONFIG_POSIX_FS
7 
8 #include "esp_wifi.h"
9 #include "stdlib.h"
10 #include "string.h"
11 #include "esp_private/wifi.h"
12 #include "soc/soc.h"
13 #include "soc/dport_access.h"
14 #include "soc/wdev_reg.h"
15 #include "soc/soc_caps.h"
16 #include "soc/rtc.h"
17 #include "soc/system_reg.h"
18 #include "esp_log.h"
19 #include "esp_attr.h"
20 #include "esp_private/wifi_os_adapter.h"
21 #include "esp_system.h"
22 #include "esp_timer.h"
23 #include "esp_private/esp_modem_clock.h"
24 #include "os.h"
25 #include "private/esp_coexist_internal.h"
26 #include "private/esp_modem_wrapper.h"
27 #include "esp_wpa.h"
28 #include "esp_private/periph_ctrl.h"
29 #include "esp_phy_init.h"
30 #include <rom/efuse.h>
31 #include <rom/rtc.h>
32 #include <rom/ets_sys.h>
33 #include <riscv/interrupt.h>
34 #include "esp32c6/rom/ets_sys.h"
35 #include "esp_mac.h"
36 #include "wifi/wifi_event.h"
37 #include "esp_heap_adapter.h"
38 
39 #if SOC_PM_MODEM_RETENTION_BY_REGDMA
40 #include "esp_private/esp_regdma.h"
41 #include "esp_private/sleep_retention.h"
42 #endif
43 
44 #include <soc.h>
45 #include <zephyr/kernel.h>
46 #include <zephyr/sys/printk.h>
47 #include <zephyr/random/random.h>
48 #include <zephyr/drivers/interrupt_controller/intc_esp32c3.h>
49 #include "zephyr_compat.h"
50 
51 #include <zephyr/logging/log.h>
52 LOG_MODULE_REGISTER(esp32_wifi_adapter, CONFIG_WIFI_LOG_LEVEL);
53 
54 static void esp_wifi_free(void *mem);
55 
56 static void *wifi_msgq_buffer;
57 
58 static struct k_thread wifi_task_handle;
59 
wifi_malloc(size_t size)60 IRAM_ATTR void *wifi_malloc(size_t size)
61 {
62 	void *ptr = esp_wifi_malloc_func(size);
63 
64 	if (ptr == NULL) {
65 		LOG_ERR("memory allocation failed");
66 	}
67 
68 	return ptr;
69 }
70 
wifi_realloc(void * ptr,size_t size)71 IRAM_ATTR void *wifi_realloc(void *ptr, size_t size)
72 {
73 	ARG_UNUSED(ptr);
74 	ARG_UNUSED(size);
75 
76 	LOG_ERR("%s not yet supported", __func__);
77 	return NULL;
78 }
79 
wifi_calloc(size_t n,size_t size)80 IRAM_ATTR void *wifi_calloc(size_t n, size_t size)
81 {
82 	void *ptr = esp_wifi_calloc_func(n, size);
83 
84 	if (ptr == NULL) {
85 		LOG_ERR("memory allocation failed");
86 	}
87 
88 	return ptr;
89 }
90 
wifi_zalloc_wrapper(size_t size)91 static void *IRAM_ATTR wifi_zalloc_wrapper(size_t size)
92 {
93 	return wifi_calloc(1, size);
94 }
95 
esp_wifi_free(void * mem)96 static void esp_wifi_free(void *mem)
97 {
98 	esp_wifi_free_func(mem);
99 }
100 
wifi_create_queue(int queue_len,int item_size)101 wifi_static_queue_t *wifi_create_queue(int queue_len, int item_size)
102 {
103 	wifi_static_queue_t *queue = NULL;
104 
105 	queue = (wifi_static_queue_t *) wifi_malloc(sizeof(wifi_static_queue_t));
106 	if (!queue) {
107 		LOG_ERR("msg buffer allocation failed");
108 		return NULL;
109 	}
110 
111 	wifi_msgq_buffer = wifi_malloc(queue_len * item_size);
112 	if (wifi_msgq_buffer == NULL) {
113 		LOG_ERR("msg buffer allocation failed");
114 		return NULL;
115 	}
116 
117 	queue->handle = wifi_malloc(sizeof(struct k_msgq));
118 	if (queue->handle == NULL) {
119 		esp_wifi_free(wifi_msgq_buffer);
120 		LOG_ERR("queue handle allocation failed");
121 		return NULL;
122 	}
123 
124 	k_msgq_init((struct k_msgq *)queue->handle, wifi_msgq_buffer, item_size, queue_len);
125 
126 	return queue;
127 }
128 
wifi_delete_queue(wifi_static_queue_t * queue)129 void wifi_delete_queue(wifi_static_queue_t *queue)
130 {
131 	if (queue) {
132 		esp_wifi_free(queue->handle);
133 		esp_wifi_free(queue);
134 	}
135 }
136 
wifi_create_queue_wrapper(int queue_len,int item_size)137 static void *wifi_create_queue_wrapper(int queue_len, int item_size)
138 {
139 	return wifi_create_queue(queue_len, item_size);
140 }
141 
wifi_delete_queue_wrapper(void * queue)142 static void wifi_delete_queue_wrapper(void *queue)
143 {
144 	wifi_delete_queue(queue);
145 }
146 
wifi_thread_semphr_get_wrapper(void)147 static void *wifi_thread_semphr_get_wrapper(void)
148 {
149 	struct k_sem *sem = NULL;
150 
151 	sem = k_thread_custom_data_get();
152 	if (!sem) {
153 		sem = (struct k_sem *) wifi_malloc(sizeof(struct k_sem));
154 		if (sem == NULL) {
155 			LOG_ERR("wifi_thread_semphr_get_wrapper allocation failed");
156 		}
157 		k_sem_init(sem, 0, 1);
158 		if (sem) {
159 			k_thread_custom_data_set(sem);
160 		}
161 	}
162 	return (void *)sem;
163 }
164 
recursive_mutex_create_wrapper(void)165 static void *recursive_mutex_create_wrapper(void)
166 {
167 	struct k_mutex *my_mutex = (struct k_mutex *) wifi_malloc(sizeof(struct k_mutex));
168 
169 	if (my_mutex == NULL) {
170 		LOG_ERR("recursive_mutex_create_wrapper allocation failed");
171 	}
172 
173 	k_mutex_init(my_mutex);
174 
175 	return (void *)my_mutex;
176 }
177 
mutex_create_wrapper(void)178 static void *mutex_create_wrapper(void)
179 {
180 	struct k_mutex *my_mutex = (struct k_mutex *) wifi_malloc(sizeof(struct k_mutex));
181 
182 	if (my_mutex == NULL) {
183 		LOG_ERR("mutex_create_wrapper allocation failed");
184 	}
185 
186 	k_mutex_init(my_mutex);
187 
188 	return (void *)my_mutex;
189 }
190 
mutex_delete_wrapper(void * mutex)191 static void mutex_delete_wrapper(void *mutex)
192 {
193 	esp_wifi_free(mutex);
194 }
195 
mutex_lock_wrapper(void * mutex)196 static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
197 {
198 	struct k_mutex *my_mutex = (struct k_mutex *) mutex;
199 
200 	k_mutex_lock(my_mutex, K_FOREVER);
201 	return 0;
202 }
203 
mutex_unlock_wrapper(void * mutex)204 static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
205 {
206 	struct k_mutex *my_mutex = (struct k_mutex *) mutex;
207 
208 	k_mutex_unlock(my_mutex);
209 	return 0;
210 }
211 
queue_create_wrapper(uint32_t queue_len,uint32_t item_size)212 static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
213 {
214 	struct k_queue *queue = (struct k_queue *) wifi_malloc(sizeof(struct k_queue));
215 
216 	if (queue == NULL) {
217 		LOG_ERR("queue malloc failed");
218 		return NULL;
219 	}
220 
221 	k_msgq_init((struct k_msgq *)queue, wifi_msgq_buffer, item_size, queue_len);
222 
223 	return (void *)queue;
224 }
225 
queue_delete_wrapper(void * handle)226 static void queue_delete_wrapper(void *handle)
227 {
228 	if (handle != NULL) {
229 		esp_wifi_free(handle);
230 	}
231 }
232 
task_delete_wrapper(void * handle)233 static void task_delete_wrapper(void *handle)
234 {
235 	if (handle != NULL) {
236 		k_thread_abort((k_tid_t) handle);
237 	}
238 
239 	k_object_release(&wifi_task_handle);
240 }
241 
queue_send_wrapper(void * queue,void * item,uint32_t block_time_tick)242 static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_tick)
243 {
244 	if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
245 		k_msgq_put((struct k_msgq *)queue, item, K_FOREVER);
246 	} else {
247 		k_msgq_put((struct k_msgq *)queue, item, K_TICKS(block_time_tick));
248 	}
249 	return 1;
250 }
251 
queue_send_from_isr_wrapper(void * queue,void * item,void * hptw)252 static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
253 {
254 	int *hpt = (int *) hptw;
255 
256 	k_msgq_put((struct k_msgq *)queue, item, K_NO_WAIT);
257 	*hpt = 0;
258 	return 1;
259 }
260 
queue_send_to_back_wrapper(void * queue,void * item,uint32_t block_time_tick)261 int32_t queue_send_to_back_wrapper(void *queue, void *item, uint32_t block_time_tick)
262 {
263 	ARG_UNUSED(queue);
264 	ARG_UNUSED(item);
265 	ARG_UNUSED(block_time_tick);
266 
267 	return 0;
268 }
269 
queue_send_to_front_wrapper(void * queue,void * item,uint32_t block_time_tick)270 int32_t queue_send_to_front_wrapper(void *queue, void *item, uint32_t block_time_tick)
271 {
272 	ARG_UNUSED(queue);
273 	ARG_UNUSED(item);
274 	ARG_UNUSED(block_time_tick);
275 
276 	return 0;
277 }
278 
queue_recv_wrapper(void * queue,void * item,uint32_t block_time_tick)279 static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_tick)
280 {
281 	if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
282 		k_msgq_get((struct k_msgq *)queue, item, K_FOREVER);
283 	} else {
284 		k_msgq_get((struct k_msgq *)queue, item, K_MSEC(block_time_tick));
285 	}
286 	return 1;
287 }
288 
event_group_wait_bits_wrapper(void * event,uint32_t bits_to_wait_for,int clear_on_exit,int wait_for_all_bits,uint32_t block_time_tick)289 static uint32_t event_group_wait_bits_wrapper(void *event, uint32_t bits_to_wait_for, int clear_on_exit, int wait_for_all_bits, uint32_t block_time_tick)
290 {
291 	ARG_UNUSED(event);
292 	ARG_UNUSED(bits_to_wait_for);
293 	ARG_UNUSED(clear_on_exit);
294 	ARG_UNUSED(wait_for_all_bits);
295 	ARG_UNUSED(block_time_tick);
296 
297 	return 0;
298 }
299 
task_create_pinned_to_core_wrapper(void * task_func,const char * name,uint32_t stack_depth,void * param,uint32_t prio,void * task_handle,uint32_t core_id)300 static int32_t task_create_pinned_to_core_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id)
301 {
302 	k_thread_stack_t *wifi_stack = k_thread_stack_alloc(stack_depth,
303 									IS_ENABLED(CONFIG_USERSPACE) ? K_USER : 0);
304 
305 	k_tid_t tid = k_thread_create(&wifi_task_handle, wifi_stack, stack_depth,
306 				      (k_thread_entry_t)task_func, param, NULL, NULL,
307 				      prio, K_INHERIT_PERMS, K_NO_WAIT);
308 
309 	k_thread_name_set(tid, name);
310 
311 	*(int32_t *)task_handle = (int32_t) tid;
312 	return 1;
313 }
314 
task_create_wrapper(void * task_func,const char * name,uint32_t stack_depth,void * param,uint32_t prio,void * task_handle)315 static int32_t task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle)
316 {
317 	k_thread_stack_t *wifi_stack = k_thread_stack_alloc(stack_depth,
318 									IS_ENABLED(CONFIG_USERSPACE) ? K_USER : 0);
319 
320 	k_tid_t tid = k_thread_create(&wifi_task_handle, wifi_stack, stack_depth,
321 				      (k_thread_entry_t)task_func, param, NULL, NULL,
322 				      prio, K_INHERIT_PERMS, K_NO_WAIT);
323 
324 	k_thread_name_set(tid, name);
325 
326 	*(int32_t *)task_handle = (int32_t) tid;
327 	return 1;
328 }
329 
task_ms_to_tick_wrapper(uint32_t ms)330 static int32_t IRAM_ATTR task_ms_to_tick_wrapper(uint32_t ms)
331 {
332 	return (int32_t)(k_ms_to_ticks_ceil32(ms));
333 }
334 
task_get_max_priority_wrapper(void)335 static int32_t task_get_max_priority_wrapper(void)
336 {
337 	return (int32_t)(CONFIG_ESP32_WIFI_MAX_THREAD_PRIORITY);
338 }
339 
esp_event_post_wrapper(const char * event_base,int32_t event_id,void * event_data,size_t event_data_size,uint32_t ticks_to_wait)340 static int32_t esp_event_post_wrapper(const char* event_base, int32_t event_id, void* event_data, size_t event_data_size, uint32_t ticks_to_wait)
341 {
342 	esp_wifi_event_handler(event_base, event_id, event_data, event_data_size, ticks_to_wait);
343 	return 0;
344 }
345 
wifi_apb80m_request_wrapper(void)346 static void IRAM_ATTR wifi_apb80m_request_wrapper(void)
347 {
348 #ifdef CONFIG_PM_ENABLE
349 	wifi_apb80m_request();
350 #endif
351 }
352 
wifi_apb80m_release_wrapper(void)353 static void IRAM_ATTR wifi_apb80m_release_wrapper(void)
354 {
355 #ifdef CONFIG_PM_ENABLE
356 	wifi_apb80m_release();
357 #endif
358 }
359 
timer_arm_wrapper(void * timer,uint32_t tmout,bool repeat)360 static void IRAM_ATTR timer_arm_wrapper(void *timer, uint32_t tmout, bool repeat)
361 {
362 	ets_timer_arm(timer, tmout, repeat);
363 }
364 
get_time_wrapper(void * t)365 static int get_time_wrapper(void *t)
366 {
367 	return os_get_time(t);
368 }
369 
realloc_internal_wrapper(void * ptr,size_t size)370 static void *IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size)
371 {
372 	ARG_UNUSED(ptr);
373 	ARG_UNUSED(size);
374 
375 	LOG_ERR("%s not yet supported", __func__);
376 	return NULL;
377 }
378 
calloc_internal_wrapper(size_t n,size_t size)379 static void *IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size)
380 {
381 	return wifi_calloc(n, size);
382 }
383 
zalloc_internal_wrapper(size_t size)384 static void *IRAM_ATTR zalloc_internal_wrapper(size_t size)
385 {
386 	return wifi_calloc(1, size);
387 }
388 
uxQueueMessagesWaiting(void * queue)389 uint32_t uxQueueMessagesWaiting(void *queue)
390 {
391 	ARG_UNUSED(queue);
392 
393 	return 0;
394 }
395 
xEventGroupCreate(void)396 void *xEventGroupCreate(void)
397 {
398 	LOG_ERR("EventGroup not supported!");
399 	return NULL;
400 }
401 
vEventGroupDelete(void * grp)402 void vEventGroupDelete(void *grp)
403 {
404 	ARG_UNUSED(grp);
405 }
406 
xEventGroupSetBits(void * ptr,uint32_t data)407 uint32_t xEventGroupSetBits(void *ptr, uint32_t data)
408 {
409 	ARG_UNUSED(ptr);
410 	ARG_UNUSED(data);
411 
412 	return 0;
413 }
414 
xEventGroupClearBits(void * ptr,uint32_t data)415 uint32_t xEventGroupClearBits(void *ptr, uint32_t data)
416 {
417 	ARG_UNUSED(ptr);
418 	ARG_UNUSED(data);
419 
420 	return 0;
421 }
422 
xTaskGetCurrentTaskHandle(void)423 void *xTaskGetCurrentTaskHandle(void)
424 {
425 	return (void *)k_current_get();
426 }
427 
task_delay(uint32_t ticks)428 void task_delay(uint32_t ticks)
429 {
430 	k_sleep(K_TICKS(ticks));
431 }
432 
set_intr_wrapper(int32_t cpu_no,uint32_t intr_source,uint32_t intr_num,int32_t intr_prio)433 static void set_intr_wrapper(int32_t cpu_no, uint32_t intr_source, uint32_t intr_num, int32_t intr_prio)
434 {
435 	ARG_UNUSED(cpu_no);
436 
437 	intr_matrix_route(intr_source, intr_num);
438 	esprv_intc_int_set_priority(intr_num, intr_prio);
439 	esprv_intc_int_set_type(intr_num, INTR_TYPE_LEVEL);
440 }
441 
clear_intr_wrapper(uint32_t intr_source,uint32_t intr_num)442 static void clear_intr_wrapper(uint32_t intr_source, uint32_t intr_num)
443 {
444 	ARG_UNUSED(intr_source);
445 	ARG_UNUSED(intr_num);
446 }
447 
set_isr_wrapper(int32_t n,void * f,void * arg)448 static void set_isr_wrapper(int32_t n, void *f, void *arg)
449 {
450 	ARG_UNUSED(n);
451 
452 	/* workaround to force allocating same handler for wifi interrupts */
453 	esp_intr_alloc(0, 0, (isr_handler_t)f, arg, NULL);
454 	esp_intr_alloc(2, 0, (isr_handler_t)f, arg,	NULL);
455 }
456 
enable_intr_wrapper(unsigned int mask)457 static void enable_intr_wrapper(unsigned int mask)
458 {
459 	esp_intr_enable(mask);
460 }
461 
disable_intr_wrapper(unsigned int mask)462 static void disable_intr_wrapper(unsigned int mask)
463 {
464 	esp_intr_disable(mask);
465 }
466 
esp_get_free_heap_size(void)467 uint32_t esp_get_free_heap_size(void)
468 {
469 	/* FIXME: API to get free heap size is not available in Zephyr. */
470 	/* It is only used by ESP-MESH feature (not supported yet) */
471 	return 10000;
472 }
473 
wifi_clock_enable_wrapper(void)474 static void wifi_clock_enable_wrapper(void)
475 {
476 	wifi_module_enable();
477 }
478 
wifi_clock_disable_wrapper(void)479 static void wifi_clock_disable_wrapper(void)
480 {
481 	wifi_module_disable();
482 }
483 
wifi_reset_mac_wrapper(void)484 static void wifi_reset_mac_wrapper(void)
485 {
486 	modem_clock_module_mac_reset(PERIPH_WIFI_MODULE);
487 }
488 
nvs_set_i8(uint32_t handle,const char * key,int8_t value)489 int32_t nvs_set_i8(uint32_t handle, const char *key, int8_t value)
490 {
491 	ARG_UNUSED(handle);
492 	ARG_UNUSED(key);
493 	ARG_UNUSED(value);
494 
495 	return 0;
496 }
497 
nvs_get_i8(uint32_t handle,const char * key,int8_t * out_value)498 int32_t nvs_get_i8(uint32_t handle, const char *key, int8_t *out_value)
499 {
500 	ARG_UNUSED(handle);
501 	ARG_UNUSED(key);
502 	ARG_UNUSED(out_value);
503 
504 	return 0;
505 }
506 
nvs_set_u8(uint32_t handle,const char * key,uint8_t value)507 int32_t nvs_set_u8(uint32_t handle, const char *key, uint8_t value)
508 {
509 	ARG_UNUSED(handle);
510 	ARG_UNUSED(key);
511 	ARG_UNUSED(value);
512 
513 	return 0;
514 }
515 
nvs_get_u8(uint32_t handle,const char * key,uint8_t * out_value)516 int32_t nvs_get_u8(uint32_t handle, const char *key, uint8_t *out_value)
517 {
518 	ARG_UNUSED(handle);
519 	ARG_UNUSED(key);
520 	ARG_UNUSED(out_value);
521 
522 	return 0;
523 }
524 
nvs_set_u16(uint32_t handle,const char * key,uint16_t value)525 int32_t nvs_set_u16(uint32_t handle, const char *key, uint16_t value)
526 {
527 	ARG_UNUSED(handle);
528 	ARG_UNUSED(key);
529 	ARG_UNUSED(value);
530 
531 	return 0;
532 }
533 
nvs_get_u16(uint32_t handle,const char * key,uint16_t * out_value)534 int32_t nvs_get_u16(uint32_t handle, const char *key, uint16_t *out_value)
535 {
536 	ARG_UNUSED(handle);
537 	ARG_UNUSED(key);
538 	ARG_UNUSED(out_value);
539 
540 	return 0;
541 }
542 
nvs_open_wrapper(const char * name,uint32_t open_mode,uint32_t * out_handle)543 int32_t nvs_open_wrapper(const char *name, uint32_t open_mode, uint32_t *out_handle)
544 {
545 	ARG_UNUSED(name);
546 	ARG_UNUSED(open_mode);
547 	ARG_UNUSED(out_handle);
548 
549 	return 0;
550 }
551 
nvs_close(uint32_t handle)552 void nvs_close(uint32_t handle)
553 {
554 	ARG_UNUSED(handle);
555 
556 	return;
557 }
558 
nvs_commit(uint32_t handle)559 int32_t nvs_commit(uint32_t handle)
560 {
561 	ARG_UNUSED(handle);
562 
563 	return 0;
564 }
565 
nvs_set_blob(uint32_t handle,const char * key,const void * value,size_t length)566 int32_t nvs_set_blob(uint32_t handle, const char *key, const void *value,
567 		     size_t length)
568 {
569 	ARG_UNUSED(handle);
570 	ARG_UNUSED(key);
571 	ARG_UNUSED(value);
572 	ARG_UNUSED(length);
573 
574 	return 0;
575 }
576 
nvs_get_blob(uint32_t handle,const char * key,void * out_value,size_t * length)577 int32_t nvs_get_blob(uint32_t handle, const char *key, void *out_value,
578 		     size_t *length)
579 {
580 	ARG_UNUSED(handle);
581 	ARG_UNUSED(key);
582 	ARG_UNUSED(out_value);
583 	ARG_UNUSED(length);
584 
585 	return 0;
586 }
587 
nvs_erase_key(uint32_t handle,const char * key)588 int32_t nvs_erase_key(uint32_t handle, const char *key)
589 {
590 	ARG_UNUSED(handle);
591 	ARG_UNUSED(key);
592 
593 	return 0;
594 }
595 
coex_init_wrapper(void)596 static int coex_init_wrapper(void)
597 {
598 #if CONFIG_SW_COEXIST_ENABLE
599 	return coex_init();
600 #else
601 	return 0;
602 #endif
603 }
604 
coex_deinit_wrapper(void)605 static void coex_deinit_wrapper(void)
606 {
607 #if CONFIG_SW_COEXIST_ENABLE
608 	coex_deinit();
609 #endif
610 }
611 
coex_enable_wrapper(void)612 static int coex_enable_wrapper(void)
613 {
614 #if CONFIG_SW_COEXIST_ENABLE
615 	return coex_enable();
616 #else
617 	return 0;
618 #endif
619 }
620 
coex_disable_wrapper(void)621 static void coex_disable_wrapper(void)
622 {
623 #if CONFIG_SW_COEXIST_ENABLE
624 	coex_disable();
625 #endif
626 }
627 
coex_status_get_wrapper(void)628 static IRAM_ATTR uint32_t coex_status_get_wrapper(void)
629 {
630 #if CONFIG_SW_COEXIST_ENABLE
631 	return coex_status_get(COEX_STATUS_GET_WIFI_BITMAP);
632 #else
633 	return 0;
634 #endif
635 }
636 
coex_wifi_request_wrapper(uint32_t event,uint32_t latency,uint32_t duration)637 static int coex_wifi_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration)
638 {
639 #if CONFIG_SW_COEXIST_ENABLE
640 	return coex_wifi_request(event, latency, duration);
641 #else
642 	return 0;
643 #endif
644 }
645 
coex_wifi_release_wrapper(uint32_t event)646 static IRAM_ATTR int coex_wifi_release_wrapper(uint32_t event)
647 {
648 #if CONFIG_SW_COEXIST_ENABLE
649 	return coex_wifi_release(event);
650 #else
651 	return 0;
652 #endif
653 }
654 
coex_wifi_channel_set_wrapper(uint8_t primary,uint8_t secondary)655 static int coex_wifi_channel_set_wrapper(uint8_t primary, uint8_t secondary)
656 {
657 #if CONFIG_SW_COEXIST_ENABLE
658 	return coex_wifi_channel_set(primary, secondary);
659 #else
660 	return 0;
661 #endif
662 }
663 
coex_event_duration_get_wrapper(uint32_t event,uint32_t * duration)664 static IRAM_ATTR int coex_event_duration_get_wrapper(uint32_t event, uint32_t *duration)
665 {
666 #if CONFIG_SW_COEXIST_ENABLE
667 	return coex_event_duration_get(event, duration);
668 #else
669 	return 0;
670 #endif
671 }
672 
coex_pti_get_wrapper(uint32_t event,uint8_t * pti)673 static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti)
674 {
675 	ARG_UNUSED(event);
676 	ARG_UNUSED(pti);
677 
678 	return 0;
679 }
680 
coex_schm_status_bit_clear_wrapper(uint32_t type,uint32_t status)681 static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
682 {
683 #if CONFIG_SW_COEXIST_ENABLE
684 	coex_schm_status_bit_clear(type, status);
685 #endif
686 }
687 
coex_schm_status_bit_set_wrapper(uint32_t type,uint32_t status)688 static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
689 {
690 #if CONFIG_SW_COEXIST_ENABLE
691 	coex_schm_status_bit_set(type, status);
692 #endif
693 }
694 
coex_schm_interval_set_wrapper(uint32_t interval)695 static IRAM_ATTR int coex_schm_interval_set_wrapper(uint32_t interval)
696 {
697 #if CONFIG_SW_COEXIST_ENABLE
698 	return coex_schm_interval_set(interval);
699 #else
700 	return 0;
701 #endif
702 }
703 
coex_schm_interval_get_wrapper(void)704 static uint32_t coex_schm_interval_get_wrapper(void)
705 {
706 #if CONFIG_SW_COEXIST_ENABLE
707 	return coex_schm_interval_get();
708 #else
709 	return 0;
710 #endif
711 }
712 
coex_schm_curr_period_get_wrapper(void)713 static uint8_t coex_schm_curr_period_get_wrapper(void)
714 {
715 #if CONFIG_SW_COEXIST_ENABLE
716 	return coex_schm_curr_period_get();
717 #else
718 	return 0;
719 #endif
720 }
721 
coex_schm_curr_phase_get_wrapper(void)722 static void * coex_schm_curr_phase_get_wrapper(void)
723 {
724 #if CONFIG_SW_COEXIST_ENABLE
725 	return coex_schm_curr_phase_get();
726 #else
727 	return NULL;
728 #endif
729 }
730 
esp_empty_wrapper(void)731 static void IRAM_ATTR esp_empty_wrapper(void)
732 {
733 
734 }
735 
esp_log_writev_wrapper(uint32_t level,const char * tag,const char * format,va_list args)736 static void esp_log_writev_wrapper(uint32_t level, const char *tag, const char *format, va_list args)
737 {
738 #if CONFIG_WIFI_LOG_LEVEL >= LOG_LEVEL_DBG
739 	esp_log_writev((esp_log_level_t)level,tag,format,args);
740 #endif
741 }
742 
esp_log_write_wrapper(uint32_t level,const char * tag,const char * format,...)743 static void esp_log_write_wrapper(uint32_t level,const char *tag,const char *format, ...)
744 {
745 #if CONFIG_WIFI_LOG_LEVEL >= LOG_LEVEL_DBG
746 	va_list list;
747 	va_start(list, format);
748 	esp_log_writev((esp_log_level_t)level, tag, format, list);
749 	va_end(list);
750 #endif
751 }
752 
coex_register_start_cb_wrapper(int (* cb)(void))753 static int coex_register_start_cb_wrapper(int (* cb)(void))
754 {
755 #if CONFIG_SW_COEXIST_ENABLE
756 	return coex_register_start_cb(cb);
757 #else
758 	return 0;
759 #endif
760 }
761 
coex_schm_process_restart_wrapper(void)762 static int coex_schm_process_restart_wrapper(void)
763 {
764 #if CONFIG_SW_COEXIST_ENABLE
765 	return coex_schm_process_restart();
766 #else
767 	return 0;
768 #endif
769 }
770 
coex_schm_register_cb_wrapper(int type,int (* cb)(int))771 static int coex_schm_register_cb_wrapper(int type, int(*cb)(int))
772 {
773 #if CONFIG_SW_COEXIST_ENABLE
774 	return coex_schm_register_callback(type, cb);
775 #else
776 	return 0;
777 #endif
778 }
779 
coex_schm_flexible_period_set_wrapper(uint8_t period)780 static int coex_schm_flexible_period_set_wrapper(uint8_t period)
781 {
782 #if CONFIG_ESP_COEX_POWER_MANAGEMENT
783 	return coex_schm_flexible_period_set(period);
784 #else
785 	return 0;
786 #endif
787 }
788 
coex_schm_flexible_period_get_wrapper(void)789 static uint8_t coex_schm_flexible_period_get_wrapper(void)
790 {
791 #if CONFIG_ESP_COEX_POWER_MANAGEMENT
792 	return coex_schm_flexible_period_get();
793 #else
794 	return 1;
795 #endif
796 }
797 
esp_phy_enable_wrapper(void)798 static void esp_phy_enable_wrapper(void)
799 {
800 	esp_phy_enable(PHY_MODEM_WIFI);
801 	phy_wifi_enable_set(1);
802 }
803 
esp_phy_disable_wrapper(void)804 static void esp_phy_disable_wrapper(void)
805 {
806 	phy_wifi_enable_set(0);
807 	esp_phy_disable(PHY_MODEM_WIFI);
808 }
809 
810 #if SOC_PM_MODEM_RETENTION_BY_REGDMA
regdma_link_set_write_wait_content_wrapper(void * addr,uint32_t value,uint32_t mask)811 static void regdma_link_set_write_wait_content_wrapper(void *addr, uint32_t value, uint32_t mask)
812 {
813 	regdma_link_set_write_wait_content(addr, value, mask);
814 }
815 
sleep_retention_find_link_by_id_wrapper(int id)816 static void *sleep_retention_find_link_by_id_wrapper(int id)
817 {
818 	return sleep_retention_find_link_by_id(id);
819 }
820 #endif
821 
822 wifi_osi_funcs_t g_wifi_osi_funcs = {
823 	._version = ESP_WIFI_OS_ADAPTER_VERSION,
824 	._env_is_chip = esp_coex_common_env_is_chip_wrapper,
825 	._set_intr = set_intr_wrapper,
826 	._clear_intr = clear_intr_wrapper,
827 	._set_isr = set_isr_wrapper,
828 	._ints_on = enable_intr_wrapper,
829 	._ints_off = disable_intr_wrapper,
830 	._is_from_isr = k_is_in_isr,
831 	._spin_lock_create = esp_coex_common_spin_lock_create_wrapper,
832 	._spin_lock_delete = esp_wifi_free,
833 	._wifi_int_disable = esp_coex_common_int_disable_wrapper,
834 	._wifi_int_restore = esp_coex_common_int_restore_wrapper,
835 	._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
836 	._semphr_create = esp_coex_common_semphr_create_wrapper,
837 	._semphr_delete = esp_coex_common_semphr_delete_wrapper,
838 	._semphr_take = esp_coex_common_semphr_take_wrapper,
839 	._semphr_give = esp_coex_common_semphr_give_wrapper,
840 	._wifi_thread_semphr_get = wifi_thread_semphr_get_wrapper,
841 	._mutex_create = mutex_create_wrapper,
842 	._recursive_mutex_create = recursive_mutex_create_wrapper,
843 	._mutex_delete = mutex_delete_wrapper,
844 	._mutex_lock = mutex_lock_wrapper,
845 	._mutex_unlock = mutex_unlock_wrapper,
846 	._queue_create = queue_create_wrapper,
847 	._queue_delete = queue_delete_wrapper,
848 	._queue_send = queue_send_wrapper,
849 	._queue_send_from_isr = queue_send_from_isr_wrapper,
850 	._queue_send_to_back = queue_send_to_back_wrapper,
851 	._queue_send_to_front = queue_send_to_front_wrapper,
852 	._queue_recv = queue_recv_wrapper,
853 	._queue_msg_waiting = (uint32_t(*)(void *))uxQueueMessagesWaiting,
854 	._event_group_create = (void *(*)(void))xEventGroupCreate,
855 	._event_group_delete = (void(*)(void *))vEventGroupDelete,
856 	._event_group_set_bits = (uint32_t(*)(void *,uint32_t))xEventGroupSetBits,
857 	._event_group_clear_bits = (uint32_t(*)(void *,uint32_t))xEventGroupClearBits,
858 	._event_group_wait_bits = event_group_wait_bits_wrapper,
859 	._task_create_pinned_to_core = task_create_pinned_to_core_wrapper,
860 	._task_create = task_create_wrapper,
861 	._task_delete = task_delete_wrapper,
862 	._task_delay = task_delay,
863 	._task_ms_to_tick = task_ms_to_tick_wrapper,
864 	._task_get_current_task = (void *(*)(void))k_current_get,
865 	._task_get_max_priority = task_get_max_priority_wrapper,
866 	._malloc = wifi_malloc,
867 	._free = esp_wifi_free,
868 	._event_post = esp_event_post_wrapper,
869 	._get_free_heap_size = esp_get_free_heap_size,
870 	._rand = sys_rand32_get,
871 	._dport_access_stall_other_cpu_start_wrap = esp_empty_wrapper,
872 	._dport_access_stall_other_cpu_end_wrap = esp_empty_wrapper,
873 	._wifi_apb80m_request = wifi_apb80m_request_wrapper,
874 	._wifi_apb80m_release = wifi_apb80m_release_wrapper,
875 	._phy_disable = esp_phy_disable_wrapper,
876 	._phy_enable = esp_phy_enable_wrapper,
877 	._phy_update_country_info = esp_phy_update_country_info,
878 	._read_mac = esp_read_mac,
879 	._timer_arm = timer_arm_wrapper,
880 	._timer_disarm = esp_coex_common_timer_disarm_wrapper,
881 	._timer_done = esp_coex_common_timer_done_wrapper,
882 	._timer_setfn = esp_coex_common_timer_setfn_wrapper,
883 	._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
884 	._wifi_reset_mac = wifi_reset_mac_wrapper,
885 	._wifi_clock_enable = wifi_clock_enable_wrapper,
886 	._wifi_clock_disable = wifi_clock_disable_wrapper,
887 	._wifi_rtc_enable_iso = esp_empty_wrapper,
888 	._wifi_rtc_disable_iso = esp_empty_wrapper,
889 	._esp_timer_get_time = esp_timer_get_time,
890 	._nvs_set_i8 = nvs_set_i8,
891 	._nvs_get_i8 = nvs_get_i8,
892 	._nvs_set_u8 = nvs_set_u8,
893 	._nvs_get_u8 = nvs_get_u8,
894 	._nvs_set_u16 = nvs_set_u16,
895 	._nvs_get_u16 = nvs_get_u16,
896 	._nvs_open = nvs_open_wrapper,
897 	._nvs_close = nvs_close,
898 	._nvs_commit = nvs_commit,
899 	._nvs_set_blob = nvs_set_blob,
900 	._nvs_get_blob = nvs_get_blob,
901 	._nvs_erase_key = nvs_erase_key,
902 	._get_random = os_get_random,
903 	._get_time = get_time_wrapper,
904 	._random = os_random,
905 	._slowclk_cal_get = esp_coex_common_clk_slowclk_cal_get_wrapper,
906 	._log_write = esp_log_write_wrapper,
907 	._log_writev = esp_log_writev_wrapper,
908 	._log_timestamp = k_uptime_get_32,
909 	._malloc_internal =  esp_coex_common_malloc_internal_wrapper,
910 	._realloc_internal = realloc_internal_wrapper,
911 	._calloc_internal = calloc_internal_wrapper,
912 	._zalloc_internal = zalloc_internal_wrapper,
913 	._wifi_malloc = wifi_malloc,
914 	._wifi_realloc = wifi_realloc,
915 	._wifi_calloc = wifi_calloc,
916 	._wifi_zalloc = wifi_zalloc_wrapper,
917 	._wifi_create_queue = wifi_create_queue_wrapper,
918 	._wifi_delete_queue = wifi_delete_queue_wrapper,
919 	._coex_init = coex_init_wrapper,
920 	._coex_deinit = coex_deinit_wrapper,
921 	._coex_enable = coex_enable_wrapper,
922 	._coex_disable = coex_disable_wrapper,
923 	._coex_status_get = coex_status_get_wrapper,
924 	._coex_wifi_request = coex_wifi_request_wrapper,
925 	._coex_wifi_release = coex_wifi_release_wrapper,
926 	._coex_wifi_channel_set = coex_wifi_channel_set_wrapper,
927 	._coex_event_duration_get = coex_event_duration_get_wrapper,
928 	._coex_pti_get = coex_pti_get_wrapper,
929 	._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
930 	._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
931 	._coex_schm_interval_set = coex_schm_interval_set_wrapper,
932 	._coex_schm_interval_get = coex_schm_interval_get_wrapper,
933 	._coex_schm_curr_period_get = coex_schm_curr_period_get_wrapper,
934 	._coex_schm_curr_phase_get = coex_schm_curr_phase_get_wrapper,
935 	._coex_register_start_cb = coex_register_start_cb_wrapper,
936 #if SOC_PM_MODEM_RETENTION_BY_REGDMA
937 	._regdma_link_set_write_wait_content = regdma_link_set_write_wait_content_wrapper,
938 	._sleep_retention_find_link_by_id = sleep_retention_find_link_by_id_wrapper,
939 #endif
940 	._coex_schm_process_restart = coex_schm_process_restart_wrapper,
941 	._coex_schm_register_cb = coex_schm_register_cb_wrapper,
942 	._coex_schm_flexible_period_set = coex_schm_flexible_period_set_wrapper,
943 	._coex_schm_flexible_period_get = coex_schm_flexible_period_get_wrapper,
944 	._magic = ESP_WIFI_OS_ADAPTER_MAGIC,
945 };
946