firmware.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __FIRMWARE_LOADER_H
  3. #define __FIRMWARE_LOADER_H
  4. #include <linux/firmware.h>
  5. #include <linux/types.h>
  6. #include <linux/kref.h>
  7. #include <linux/list.h>
  8. #include <linux/completion.h>
  9. #include <generated/utsrelease.h>
  10. /* firmware behavior options */
  11. #define FW_OPT_UEVENT (1U << 0)
  12. #define FW_OPT_NOWAIT (1U << 1)
  13. #define FW_OPT_USERHELPER (1U << 2)
  14. #define FW_OPT_NO_WARN (1U << 3)
  15. #define FW_OPT_NOCACHE (1U << 4)
  16. #define FW_OPT_NOFALLBACK (1U << 5)
  17. enum fw_status {
  18. FW_STATUS_UNKNOWN,
  19. FW_STATUS_LOADING,
  20. FW_STATUS_DONE,
  21. FW_STATUS_ABORTED,
  22. };
  23. /*
  24. * Concurrent request_firmware() for the same firmware need to be
  25. * serialized. struct fw_state is simple state machine which hold the
  26. * state of the firmware loading.
  27. */
  28. struct fw_state {
  29. struct completion completion;
  30. enum fw_status status;
  31. };
  32. struct fw_priv {
  33. struct kref ref;
  34. struct list_head list;
  35. struct firmware_cache *fwc;
  36. struct fw_state fw_st;
  37. void *data;
  38. size_t size;
  39. size_t allocated_size;
  40. #ifdef CONFIG_FW_LOADER_USER_HELPER
  41. bool is_paged_buf;
  42. bool need_uevent;
  43. struct page **pages;
  44. int nr_pages;
  45. int page_array_size;
  46. struct list_head pending_list;
  47. #endif
  48. const char *fw_name;
  49. };
  50. extern struct mutex fw_lock;
  51. static inline bool __fw_state_check(struct fw_priv *fw_priv,
  52. enum fw_status status)
  53. {
  54. struct fw_state *fw_st = &fw_priv->fw_st;
  55. return fw_st->status == status;
  56. }
  57. static inline int __fw_state_wait_common(struct fw_priv *fw_priv, long timeout)
  58. {
  59. struct fw_state *fw_st = &fw_priv->fw_st;
  60. long ret;
  61. ret = wait_for_completion_killable_timeout(&fw_st->completion, timeout);
  62. if (ret != 0 && fw_st->status == FW_STATUS_ABORTED)
  63. return -ENOENT;
  64. if (!ret)
  65. return -ETIMEDOUT;
  66. return ret < 0 ? ret : 0;
  67. }
  68. static inline void __fw_state_set(struct fw_priv *fw_priv,
  69. enum fw_status status)
  70. {
  71. struct fw_state *fw_st = &fw_priv->fw_st;
  72. WRITE_ONCE(fw_st->status, status);
  73. if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED)
  74. complete_all(&fw_st->completion);
  75. }
  76. static inline void fw_state_aborted(struct fw_priv *fw_priv)
  77. {
  78. __fw_state_set(fw_priv, FW_STATUS_ABORTED);
  79. }
  80. static inline bool fw_state_is_aborted(struct fw_priv *fw_priv)
  81. {
  82. return __fw_state_check(fw_priv, FW_STATUS_ABORTED);
  83. }
  84. static inline void fw_state_start(struct fw_priv *fw_priv)
  85. {
  86. __fw_state_set(fw_priv, FW_STATUS_LOADING);
  87. }
  88. static inline void fw_state_done(struct fw_priv *fw_priv)
  89. {
  90. __fw_state_set(fw_priv, FW_STATUS_DONE);
  91. }
  92. int assign_fw(struct firmware *fw, struct device *device,
  93. unsigned int opt_flags);
  94. #endif /* __FIRMWARE_LOADER_H */