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[] = {
28 /* Events related to driver model */
35 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
38 static const char *event_type_name(enum event_t type)
40 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
41 return type_name[type];
47 static int notify_static(struct event *ev)
49 struct evspy_info *start =
50 ll_entry_start(struct evspy_info, evspy_info);
51 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
52 struct evspy_info *spy;
54 for (spy = start; spy != start + n_ents; spy++) {
55 if (spy->type == ev->type) {
58 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
59 event_type_name(ev->type), event_spy_id(spy));
60 ret = spy->func(NULL, ev);
63 * TODO: Handle various return codes to
65 * - claim an event (no others will see it)
66 * - return an error from the event
69 return log_msg_ret("spy", ret);
76 static int notify_dynamic(struct event *ev)
78 struct event_state *state = gd_event_state();
79 struct event_spy *spy, *next;
81 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
82 if (spy->type == ev->type) {
85 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
86 event_type_name(ev->type), spy->id);
87 ret = spy->func(spy->ctx, ev);
90 * TODO: Handle various return codes to
92 * - claim an event (no others will see it)
93 * - return an error from the event
96 return log_msg_ret("spy", ret);
103 int event_notify(enum event_t type, void *data, int size)
109 if (size > sizeof(event.data))
110 return log_msg_ret("size", -E2BIG);
111 memcpy(&event.data, data, size);
113 ret = notify_static(&event);
115 return log_msg_ret("dyn", ret);
117 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
118 ret = notify_dynamic(&event);
120 return log_msg_ret("dyn", ret);
126 int event_notify_null(enum event_t type)
128 return event_notify(type, NULL, 0);
131 void event_show_spy_list(void)
133 struct evspy_info *start =
134 ll_entry_start(struct evspy_info, evspy_info);
135 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
136 struct evspy_info *spy;
137 const int size = sizeof(ulong) * 2;
139 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
140 for (spy = start; spy != start + n_ents; spy++) {
141 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
142 spy->type, event_type_name(spy->type), size, spy->func,
147 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
148 static void spy_free(struct event_spy *spy)
150 list_del(&spy->sibling_node);
153 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
155 struct event_state *state = gd_event_state();
156 struct event_spy *spy;
158 if (!CONFIG_IS_ENABLED(EVENT_DYNAMIC))
160 spy = malloc(sizeof(*spy));
162 return log_msg_ret("alloc", -ENOMEM);
168 list_add_tail(&spy->sibling_node, &state->spy_head);
173 int event_uninit(void)
175 struct event_state *state = gd_event_state();
176 struct event_spy *spy, *next;
178 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
186 struct event_state *state = gd_event_state();
188 INIT_LIST_HEAD(&state->spy_head);
192 #endif /* EVENT_DYNAMIC */