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 	} else if (!set) {
36 		return -EINVAL;
37 	}
38 
39 	if (set) {
40 		sys_slist_prepend(callbacks, &callback->node);
41 	}
42 
43 	return 0;
44 }
45 
46 /**
47  * @brief Generic function to go through and fire callback from a callback list.
48  *
49  * @param list A pointer on the espi callback list.
50  * @param dev A pointer on the espi driver instance.
51  * @param pins The details on the event that triggered the callback.
52  */
espi_send_callbacks(sys_slist_t * list,const struct device * dev,struct espi_event evt)53 static inline void espi_send_callbacks(sys_slist_t *list,
54 				       const struct device *dev,
55 				       struct espi_event evt)
56 {
57 	struct espi_callback *cb, *tmp;
58 
59 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(list, cb, tmp, node) {
60 		if (cb->evt_type & evt.evt_type) {
61 			__ASSERT(cb->handler, "No callback handler!");
62 			cb->handler(dev, cb, evt);
63 		}
64 	}
65 }
66 
67 #endif /* ZEPHYR_DRIVERS_ESPI_UTILS_H_ */
68