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
7 * Written by Simon Glass <sjg@chromium.org>
10 #define LOG_CATEGORY LOGC_EVENT
14 #include <event_internal.h>
16 #include <linker_lists.h>
18 #include <asm/global_data.h>
19 #include <linux/list.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
24 const char *const type_name[] = {
29 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
32 static const char *event_type_name(enum event_t type)
34 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
35 return type_name[type];
41 static int notify_static(struct event *ev)
43 struct evspy_info *start =
44 ll_entry_start(struct evspy_info, evspy_info);
45 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
46 struct evspy_info *spy;
48 for (spy = start; spy != start + n_ents; spy++) {
49 if (spy->type == ev->type) {
52 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
53 event_type_name(ev->type), event_spy_id(spy));
54 ret = spy->func(NULL, ev);
57 * TODO: Handle various return codes to
59 * - claim an event (no others will see it)
60 * - return an error from the event
63 return log_msg_ret("spy", ret);
70 static int notify_dynamic(struct event *ev)
72 struct event_state *state = gd_event_state();
73 struct event_spy *spy, *next;
75 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
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), spy->id);
81 ret = spy->func(spy->ctx, ev);
84 * TODO: Handle various return codes to
86 * - claim an event (no others will see it)
87 * - return an error from the event
90 return log_msg_ret("spy", ret);
97 int event_notify(enum event_t type, void *data, int size)
103 if (size > sizeof(event.data))
104 return log_msg_ret("size", -E2BIG);
105 memcpy(&event.data, data, size);
107 ret = notify_static(&event);
109 return log_msg_ret("dyn", ret);
111 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
112 ret = notify_dynamic(&event);
114 return log_msg_ret("dyn", ret);
120 int event_notify_null(enum event_t type)
122 return event_notify(type, NULL, 0);
125 void event_show_spy_list(void)
127 struct evspy_info *start =
128 ll_entry_start(struct evspy_info, evspy_info);
129 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
130 struct evspy_info *spy;
131 const int size = sizeof(ulong) * 2;
133 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
134 for (spy = start; spy != start + n_ents; spy++) {
135 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
136 spy->type, event_type_name(spy->type), size, spy->func,
141 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
142 static void spy_free(struct event_spy *spy)
144 list_del(&spy->sibling_node);
147 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
149 struct event_state *state = gd_event_state();
150 struct event_spy *spy;
152 if (!CONFIG_IS_ENABLED(EVENT_DYNAMIC))
154 spy = malloc(sizeof(*spy));
156 return log_msg_ret("alloc", -ENOMEM);
162 list_add_tail(&spy->sibling_node, &state->spy_head);
167 int event_uninit(void)
169 struct event_state *state = gd_event_state();
170 struct event_spy *spy, *next;
172 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
180 struct event_state *state = gd_event_state();
182 INIT_LIST_HEAD(&state->spy_head);
186 #endif /* EVENT_DYNAMIC */