efc_sm.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. * Generic state machine framework.
  8. */
  9. #include "efc.h"
  10. #include "efc_sm.h"
  11. /**
  12. * efc_sm_post_event() - Post an event to a context.
  13. *
  14. * @ctx: State machine context
  15. * @evt: Event to post
  16. * @data: Event-specific data (if any)
  17. */
  18. int
  19. efc_sm_post_event(struct efc_sm_ctx *ctx,
  20. enum efc_sm_event evt, void *data)
  21. {
  22. if (!ctx->current_state)
  23. return -EIO;
  24. ctx->current_state(ctx, evt, data);
  25. return 0;
  26. }
  27. void
  28. efc_sm_transition(struct efc_sm_ctx *ctx,
  29. void (*state)(struct efc_sm_ctx *,
  30. enum efc_sm_event, void *), void *data)
  31. {
  32. if (ctx->current_state == state) {
  33. efc_sm_post_event(ctx, EFC_EVT_REENTER, data);
  34. } else {
  35. efc_sm_post_event(ctx, EFC_EVT_EXIT, data);
  36. ctx->current_state = state;
  37. efc_sm_post_event(ctx, EFC_EVT_ENTER, data);
  38. }
  39. }
  40. static char *event_name[] = EFC_SM_EVENT_NAME;
  41. const char *efc_sm_event_name(enum efc_sm_event evt)
  42. {
  43. if (evt > EFC_EVT_LAST)
  44. return "unknown";
  45. return event_name[evt];
  46. }