1 // SPDX-License-Identifier: GPL-2.0+
3 * Events provide a general-purpose way to react to / subscribe to changes
6 * Copyright 2021 Google LLC
10 #define LOG_CATEGORY LOGC_EVENT
13 #include <event_internal.h>
15 #include <linker_lists.h>
17 #include <asm/global_data.h>
18 #include <linux/errno.h>
19 #include <linux/list.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
25 const char *const type_name[] = {
29 /* Events related to driver model */
49 /* main loop events */
53 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
56 const char *event_type_name(enum event_t type)
58 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
59 if (type < ARRAY_SIZE(type_name))
60 return type_name[type];
68 static int notify_static(struct event *ev)
70 struct evspy_info *start =
71 ll_entry_start(struct evspy_info, evspy_info);
72 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
73 struct evspy_info *spy;
75 for (spy = start; spy != start + n_ents; spy++) {
76 if (spy->type == ev->type) {
79 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
80 event_type_name(ev->type), event_spy_id(spy));
81 if (spy->flags & EVSPYF_SIMPLE) {
82 const struct evspy_info_simple *simple;
84 simple = (struct evspy_info_simple *)spy;
87 ret = spy->func(NULL, ev);
91 * TODO: Handle various return codes to
93 * - claim an event (no others will see it)
94 * - return an error from the event
97 return log_msg_ret("spy", ret);
104 static int notify_dynamic(struct event *ev)
106 struct event_state *state = gd_event_state();
107 struct event_spy *spy, *next;
109 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
110 if (spy->type == ev->type) {
113 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
114 event_type_name(ev->type), spy->id);
115 ret = spy->func(spy->ctx, ev);
118 * TODO: Handle various return codes to
120 * - claim an event (no others will see it)
121 * - return an error from the event
124 return log_msg_ret("spy", ret);
131 int event_notify(enum event_t type, void *data, int size)
137 if (size > sizeof(event.data))
138 return log_msg_ret("size", -E2BIG);
139 memcpy(&event.data, data, size);
141 ret = notify_static(&event);
143 return log_msg_ret("sta", ret);
145 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
146 ret = notify_dynamic(&event);
148 return log_msg_ret("dyn", ret);
154 int event_notify_null(enum event_t type)
156 return event_notify(type, NULL, 0);
159 void event_show_spy_list(void)
161 struct evspy_info *start =
162 ll_entry_start(struct evspy_info, evspy_info);
163 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
164 struct evspy_info *spy;
165 const int size = sizeof(ulong) * 2;
167 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
168 for (spy = start; spy != start + n_ents; spy++) {
169 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
170 spy->type, event_type_name(spy->type), size, spy->func,
175 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
176 static void spy_free(struct event_spy *spy)
178 list_del(&spy->sibling_node);
181 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
183 struct event_state *state = gd_event_state();
184 struct event_spy *spy;
186 spy = malloc(sizeof(*spy));
188 return log_msg_ret("alloc", -ENOMEM);
194 list_add_tail(&spy->sibling_node, &state->spy_head);
199 int event_uninit(void)
201 struct event_state *state = gd_event_state();
202 struct event_spy *spy, *next;
204 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
212 struct event_state *state = gd_event_state();
214 INIT_LIST_HEAD(&state->spy_head);
218 #endif /* EVENT_DYNAMIC */