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 */
38 _Static_assert(ARRAY_SIZE(type_name) == EVT_COUNT, "event type_name size");
41 static const char *event_type_name(enum event_t type)
43 #if CONFIG_IS_ENABLED(EVENT_DEBUG)
44 return type_name[type];
50 static int notify_static(struct event *ev)
52 struct evspy_info *start =
53 ll_entry_start(struct evspy_info, evspy_info);
54 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
55 struct evspy_info *spy;
57 for (spy = start; spy != start + n_ents; spy++) {
58 if (spy->type == ev->type) {
61 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
62 event_type_name(ev->type), event_spy_id(spy));
63 ret = spy->func(NULL, ev);
66 * TODO: Handle various return codes to
68 * - claim an event (no others will see it)
69 * - return an error from the event
72 return log_msg_ret("spy", ret);
79 static int notify_dynamic(struct event *ev)
81 struct event_state *state = gd_event_state();
82 struct event_spy *spy, *next;
84 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node) {
85 if (spy->type == ev->type) {
88 log_debug("Sending event %x/%s to spy '%s'\n", ev->type,
89 event_type_name(ev->type), spy->id);
90 ret = spy->func(spy->ctx, ev);
93 * TODO: Handle various return codes to
95 * - claim an event (no others will see it)
96 * - return an error from the event
99 return log_msg_ret("spy", ret);
106 int event_notify(enum event_t type, void *data, int size)
112 if (size > sizeof(event.data))
113 return log_msg_ret("size", -E2BIG);
114 memcpy(&event.data, data, size);
116 ret = notify_static(&event);
118 return log_msg_ret("dyn", ret);
120 if (CONFIG_IS_ENABLED(EVENT_DYNAMIC)) {
121 ret = notify_dynamic(&event);
123 return log_msg_ret("dyn", ret);
129 int event_notify_null(enum event_t type)
131 return event_notify(type, NULL, 0);
134 void event_show_spy_list(void)
136 struct evspy_info *start =
137 ll_entry_start(struct evspy_info, evspy_info);
138 const int n_ents = ll_entry_count(struct evspy_info, evspy_info);
139 struct evspy_info *spy;
140 const int size = sizeof(ulong) * 2;
142 printf("Seq %-24s %*s %s\n", "Type", size, "Function", "ID");
143 for (spy = start; spy != start + n_ents; spy++) {
144 printf("%3x %-3x %-20s %*p %s\n", (uint)(spy - start),
145 spy->type, event_type_name(spy->type), size, spy->func,
150 #if CONFIG_IS_ENABLED(EVENT_DYNAMIC)
151 static void spy_free(struct event_spy *spy)
153 list_del(&spy->sibling_node);
156 int event_register(const char *id, enum event_t type, event_handler_t func, void *ctx)
158 struct event_state *state = gd_event_state();
159 struct event_spy *spy;
161 if (!CONFIG_IS_ENABLED(EVENT_DYNAMIC))
163 spy = malloc(sizeof(*spy));
165 return log_msg_ret("alloc", -ENOMEM);
171 list_add_tail(&spy->sibling_node, &state->spy_head);
176 int event_uninit(void)
178 struct event_state *state = gd_event_state();
179 struct event_spy *spy, *next;
181 list_for_each_entry_safe(spy, next, &state->spy_head, sibling_node)
189 struct event_state *state = gd_event_state();
191 INIT_LIST_HEAD(&state->spy_head);
195 #endif /* EVENT_DYNAMIC */