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 */
39 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
42 static const char *event_type_name(enum event_t type)
44 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
45 return type_name[type];
51 static int notify_static(struct event *ev)
53 struct evspy_info *start =
54 ll_entry_start(struct evspy_info, evspy_info);
55 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
56 struct evspy_info *spy;
58 for (spy = start; spy != start + n_ents; spy++) {
59 if (spy->type == ev->type) {
62 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
63 event_type_name(ev->type), event_spy_id(spy));
64 ret = spy->func(NULL, ev);
67 * TODO: Handle various return codes to
69 * - claim an event (no others will see it)
70 * - return an error from the event
73 return log_msg_ret("spy", ret);
80 static int notify_dynamic(struct event *ev)
82 struct event_state *state = gd_event_state();
83 struct event_spy *spy, *next;
85 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
86 if (spy->type == ev->type) {
89 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
90 event_type_name(ev->type), spy->id);
91 ret = spy->func(spy->ctx, ev);
94 * TODO: Handle various return codes to
96 * - claim an event (no others will see it)
97 * - return an error from the event
100 return log_msg_ret("spy", ret);
107 int event_notify(enum event_t type, void *data, int size)
113 if (size > sizeof(event.data))
114 return log_msg_ret("size", -E2BIG);
115 memcpy(&event.data, data, size);
117 ret = notify_static(&event);
119 return log_msg_ret("dyn", ret);
121 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
122 ret = notify_dynamic(&event);
124 return log_msg_ret("dyn", ret);
130 int event_notify_null(enum event_t type)
132 return event_notify(type, NULL, 0);
135 void event_show_spy_list(void)
137 struct evspy_info *start =
138 ll_entry_start(struct evspy_info, evspy_info);
139 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
140 struct evspy_info *spy;
141 const int size = sizeof(ulong) * 2;
143 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
144 for (spy = start; spy != start + n_ents; spy++) {
145 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
146 spy->type, event_type_name(spy->type), size, spy->func,
151 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
152 static void spy_free(struct event_spy *spy)
154 list_del(&spy->sibling_node);
157 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
159 struct event_state *state = gd_event_state();
160 struct event_spy *spy;
162 if (!CONFIG_IS_ENABLED(EVENT_DYNAMIC))
164 spy = malloc(sizeof(*spy));
166 return log_msg_ret("alloc", -ENOMEM);
172 list_add_tail(&spy->sibling_node, &state->spy_head);
177 int event_uninit(void)
179 struct event_state *state = gd_event_state();
180 struct event_spy *spy, *next;
182 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
190 struct event_state *state = gd_event_state();
192 INIT_LIST_HEAD(&state->spy_head);
196 #endif /* EVENT_DYNAMIC */