1 /*
2  * Copyright (c) 2019 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file Header with definitions for eSPI drivers callback functions
9  */
10 
11 #ifndef ZEPHYR_DRIVERS_ESPI_UTILS_H_
12 #define ZEPHYR_DRIVERS_ESPI_UTILS_H_
13 
14 /**
15  * @brief Generic function to insert or remove a callback from a callback list.
16  *
17  * @param callbacks A pointer to the original list of callbacks (can be NULL).
18  * @param callback A pointer of the callback to insert or remove from the list.
19  * @param set A boolean indicating insertion or removal of the callback.
20  *
21  * @return 0 on success, negative errno otherwise.
22  */
espi_manage_callback(sys_slist_t * callbacks,struct espi_callback * callback,bool set)23 static inline int espi_manage_callback(sys_slist_t *callbacks,
24 				struct espi_callback *callback, bool set)
25 {
26 	__ASSERT(callback, "No callback!");
27 	__ASSERT(callback->handler, "No callback handler!");
28 
29 	if (!sys_slist_is_empty(callbacks)) {
30 		if (!sys_slist_find_and_remove(callbacks, &callback->node)) {
31 			if (!set) {
32 				return -EINVAL;
33 			}
34 		}
35 	}
36 
37 	if (set) {
38 		sys_slist_prepend(callbacks, &callback->node);
39 	}
40 
41 	return 0;
42 }
43 
44 /**
45  * @brief Generic function to go through and fire callback from a callback list.
46  *
47  * @param list A pointer on the espi callback list.
48  * @param dev A pointer on the espi driver instance.
49  * @param pins The details on the event that triggered the callback.
50  */
espi_send_callbacks(sys_slist_t * list,const struct device * dev,struct espi_event evt)51 static inline void espi_send_callbacks(sys_slist_t *list,
52 				       const struct device *dev,
53 				       struct espi_event evt)
54 {
55 	struct espi_callback *cb, *tmp;
56 
57 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(list, cb, tmp, node) {
58 		if (cb->evt_type & evt.evt_type) {
59 			__ASSERT(cb->handler, "No callback handler!");
60 			cb->handler(dev, cb, evt);
61 		}
62 	}
63 }
64 
65 #endif /* ZEPHYR_DRIVERS_ESPI_UTILS_H_ */
66