1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 Broadcom. All Rights Reserved. The term
4  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
5  */
6 
7 /*
8  * Generic state machine framework.
9  */
10 #include "efc.h"
11 #include "efc_sm.h"
12 
13 /**
14  * efc_sm_post_event() - Post an event to a context.
15  *
16  * @ctx: State machine context
17  * @evt: Event to post
18  * @data: Event-specific data (if any)
19  */
20 int
efc_sm_post_event(struct efc_sm_ctx * ctx,enum efc_sm_event evt,void * data)21 efc_sm_post_event(struct efc_sm_ctx *ctx,
22 		  enum efc_sm_event evt, void *data)
23 {
24 	if (!ctx->current_state)
25 		return -EIO;
26 
27 	ctx->current_state(ctx, evt, data);
28 	return 0;
29 }
30 
31 void
efc_sm_transition(struct efc_sm_ctx * ctx,void (* state)(struct efc_sm_ctx *,enum efc_sm_event,void *),void * data)32 efc_sm_transition(struct efc_sm_ctx *ctx,
33 		  void (*state)(struct efc_sm_ctx *,
34 				enum efc_sm_event, void *), void *data)
35 
36 {
37 	if (ctx->current_state == state) {
38 		efc_sm_post_event(ctx, EFC_EVT_REENTER, data);
39 	} else {
40 		efc_sm_post_event(ctx, EFC_EVT_EXIT, data);
41 		ctx->current_state = state;
42 		efc_sm_post_event(ctx, EFC_EVT_ENTER, data);
43 	}
44 }
45 
46 static char *event_name[] = EFC_SM_EVENT_NAME;
47 
efc_sm_event_name(enum efc_sm_event evt)48 const char *efc_sm_event_name(enum efc_sm_event evt)
49 {
50 	if (evt > EFC_EVT_LAST)
51 		return "unknown";
52 
53 	return event_name[evt];
54 }
55