firmware.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_FIRMWARE_H
  3. #define _LINUX_FIRMWARE_H
  4. #include <linux/types.h>
  5. #include <linux/compiler.h>
  6. #include <linux/gfp.h>
  7. #define FW_ACTION_NOUEVENT 0
  8. #define FW_ACTION_UEVENT 1
  9. struct firmware {
  10. size_t size;
  11. const u8 *data;
  12. /* firmware loader private fields */
  13. void *priv;
  14. };
  15. /**
  16. * enum fw_upload_err - firmware upload error codes
  17. * @FW_UPLOAD_ERR_NONE: returned to indicate success
  18. * @FW_UPLOAD_ERR_HW_ERROR: error signalled by hardware, see kernel log
  19. * @FW_UPLOAD_ERR_TIMEOUT: SW timed out on handshake with HW/firmware
  20. * @FW_UPLOAD_ERR_CANCELED: upload was cancelled by the user
  21. * @FW_UPLOAD_ERR_BUSY: there is an upload operation already in progress
  22. * @FW_UPLOAD_ERR_INVALID_SIZE: invalid firmware image size
  23. * @FW_UPLOAD_ERR_RW_ERROR: read or write to HW failed, see kernel log
  24. * @FW_UPLOAD_ERR_WEAROUT: FLASH device is approaching wear-out, wait & retry
  25. * @FW_UPLOAD_ERR_MAX: Maximum error code marker
  26. */
  27. enum fw_upload_err {
  28. FW_UPLOAD_ERR_NONE,
  29. FW_UPLOAD_ERR_HW_ERROR,
  30. FW_UPLOAD_ERR_TIMEOUT,
  31. FW_UPLOAD_ERR_CANCELED,
  32. FW_UPLOAD_ERR_BUSY,
  33. FW_UPLOAD_ERR_INVALID_SIZE,
  34. FW_UPLOAD_ERR_RW_ERROR,
  35. FW_UPLOAD_ERR_WEAROUT,
  36. FW_UPLOAD_ERR_MAX
  37. };
  38. struct fw_upload {
  39. void *dd_handle; /* reference to parent driver */
  40. void *priv; /* firmware loader private fields */
  41. };
  42. /**
  43. * struct fw_upload_ops - device specific operations to support firmware upload
  44. * @prepare: Required: Prepare secure update
  45. * @write: Required: The write() op receives the remaining
  46. * size to be written and must return the actual
  47. * size written or a negative error code. The write()
  48. * op will be called repeatedly until all data is
  49. * written.
  50. * @poll_complete: Required: Check for the completion of the
  51. * HW authentication/programming process.
  52. * @cancel: Required: Request cancellation of update. This op
  53. * is called from the context of a different kernel
  54. * thread, so race conditions need to be considered.
  55. * @cleanup: Optional: Complements the prepare()
  56. * function and is called at the completion
  57. * of the update, on success or failure, if the
  58. * prepare function succeeded.
  59. */
  60. struct fw_upload_ops {
  61. enum fw_upload_err (*prepare)(struct fw_upload *fw_upload,
  62. const u8 *data, u32 size);
  63. enum fw_upload_err (*write)(struct fw_upload *fw_upload,
  64. const u8 *data, u32 offset,
  65. u32 size, u32 *written);
  66. enum fw_upload_err (*poll_complete)(struct fw_upload *fw_upload);
  67. void (*cancel)(struct fw_upload *fw_upload);
  68. void (*cleanup)(struct fw_upload *fw_upload);
  69. };
  70. struct module;
  71. struct device;
  72. /*
  73. * Built-in firmware functionality is only available if FW_LOADER=y, but not
  74. * FW_LOADER=m
  75. */
  76. #ifdef CONFIG_FW_LOADER
  77. bool firmware_request_builtin(struct firmware *fw, const char *name);
  78. #else
  79. static inline bool firmware_request_builtin(struct firmware *fw,
  80. const char *name)
  81. {
  82. return false;
  83. }
  84. #endif
  85. #if IS_REACHABLE(CONFIG_FW_LOADER)
  86. int request_firmware(const struct firmware **fw, const char *name,
  87. struct device *device);
  88. int firmware_request_nowarn(const struct firmware **fw, const char *name,
  89. struct device *device);
  90. int firmware_request_platform(const struct firmware **fw, const char *name,
  91. struct device *device);
  92. int request_firmware_nowait(
  93. struct module *module, bool uevent,
  94. const char *name, struct device *device, gfp_t gfp, void *context,
  95. void (*cont)(const struct firmware *fw, void *context));
  96. int request_firmware_direct(const struct firmware **fw, const char *name,
  97. struct device *device);
  98. int request_firmware_into_buf(const struct firmware **firmware_p,
  99. const char *name, struct device *device, void *buf, size_t size);
  100. int request_partial_firmware_into_buf(const struct firmware **firmware_p,
  101. const char *name, struct device *device,
  102. void *buf, size_t size, size_t offset);
  103. void release_firmware(const struct firmware *fw);
  104. #else
  105. static inline int request_firmware(const struct firmware **fw,
  106. const char *name,
  107. struct device *device)
  108. {
  109. return -EINVAL;
  110. }
  111. static inline int firmware_request_nowarn(const struct firmware **fw,
  112. const char *name,
  113. struct device *device)
  114. {
  115. return -EINVAL;
  116. }
  117. static inline int firmware_request_platform(const struct firmware **fw,
  118. const char *name,
  119. struct device *device)
  120. {
  121. return -EINVAL;
  122. }
  123. static inline int request_firmware_nowait(
  124. struct module *module, bool uevent,
  125. const char *name, struct device *device, gfp_t gfp, void *context,
  126. void (*cont)(const struct firmware *fw, void *context))
  127. {
  128. return -EINVAL;
  129. }
  130. static inline void release_firmware(const struct firmware *fw)
  131. {
  132. }
  133. static inline int request_firmware_direct(const struct firmware **fw,
  134. const char *name,
  135. struct device *device)
  136. {
  137. return -EINVAL;
  138. }
  139. static inline int request_firmware_into_buf(const struct firmware **firmware_p,
  140. const char *name, struct device *device, void *buf, size_t size)
  141. {
  142. return -EINVAL;
  143. }
  144. static inline int request_partial_firmware_into_buf
  145. (const struct firmware **firmware_p,
  146. const char *name,
  147. struct device *device,
  148. void *buf, size_t size, size_t offset)
  149. {
  150. return -EINVAL;
  151. }
  152. #endif
  153. #ifdef CONFIG_FW_UPLOAD
  154. struct fw_upload *
  155. firmware_upload_register(struct module *module, struct device *parent,
  156. const char *name, const struct fw_upload_ops *ops,
  157. void *dd_handle);
  158. void firmware_upload_unregister(struct fw_upload *fw_upload);
  159. #else
  160. static inline struct fw_upload *
  161. firmware_upload_register(struct module *module, struct device *parent,
  162. const char *name, const struct fw_upload_ops *ops,
  163. void *dd_handle)
  164. {
  165. return ERR_PTR(-EINVAL);
  166. }
  167. static inline void firmware_upload_unregister(struct fw_upload *fw_upload)
  168. {
  169. }
  170. #endif
  171. int firmware_request_cache(struct device *device, const char *name);
  172. #endif