firmware.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef _LINUX_FIRMWARE_H
  2. #define _LINUX_FIRMWARE_H
  3. #include <linux/types.h>
  4. #include <linux/compiler.h>
  5. #include <linux/gfp.h>
  6. #define FW_ACTION_NOHOTPLUG 0
  7. #define FW_ACTION_HOTPLUG 1
  8. struct firmware {
  9. size_t size;
  10. const u8 *data;
  11. struct page **pages;
  12. /* firmware loader private fields */
  13. void *priv;
  14. };
  15. struct module;
  16. struct device;
  17. struct builtin_fw {
  18. char *name;
  19. void *data;
  20. unsigned long size;
  21. };
  22. /* We have to play tricks here much like stringify() to get the
  23. __COUNTER__ macro to be expanded as we want it */
  24. #define __fw_concat1(x, y) x##y
  25. #define __fw_concat(x, y) __fw_concat1(x, y)
  26. #define DECLARE_BUILTIN_FIRMWARE(name, blob) \
  27. DECLARE_BUILTIN_FIRMWARE_SIZE(name, &(blob), sizeof(blob))
  28. #define DECLARE_BUILTIN_FIRMWARE_SIZE(name, blob, size) \
  29. static const struct builtin_fw __fw_concat(__builtin_fw,__COUNTER__) \
  30. __used __section(.builtin_fw) = { name, blob, size }
  31. #if defined(CONFIG_FW_LOADER) || (defined(CONFIG_FW_LOADER_MODULE) && defined(MODULE))
  32. int request_firmware(const struct firmware **fw, const char *name,
  33. struct device *device);
  34. int request_firmware_nowait(
  35. struct module *module, bool uevent,
  36. const char *name, struct device *device, gfp_t gfp, void *context,
  37. void (*cont)(const struct firmware *fw, void *context));
  38. int request_firmware_direct(const struct firmware **fw, const char *name,
  39. struct device *device);
  40. int request_firmware_into_buf(const struct firmware **firmware_p,
  41. const char *name, struct device *device, void *buf, size_t size);
  42. void release_firmware(const struct firmware *fw);
  43. #else
  44. static inline int request_firmware(const struct firmware **fw,
  45. const char *name,
  46. struct device *device)
  47. {
  48. return -EINVAL;
  49. }
  50. static inline int request_firmware_nowait(
  51. struct module *module, bool uevent,
  52. const char *name, struct device *device, gfp_t gfp, void *context,
  53. void (*cont)(const struct firmware *fw, void *context))
  54. {
  55. return -EINVAL;
  56. }
  57. static inline void release_firmware(const struct firmware *fw)
  58. {
  59. }
  60. static inline int request_firmware_direct(const struct firmware **fw,
  61. const char *name,
  62. struct device *device)
  63. {
  64. return -EINVAL;
  65. }
  66. static inline int request_firmware_into_buf(const struct firmware **firmware_p,
  67. const char *name, struct device *device, void *buf, size_t size)
  68. {
  69. return -EINVAL;
  70. }
  71. #endif
  72. #ifndef _LINUX_LIBRE_FIRMWARE_H
  73. #define _LINUX_LIBRE_FIRMWARE_H
  74. #include <linux/device.h>
  75. #define NONFREE_FIRMWARE "/*(DEBLOBBED)*/"
  76. static inline int
  77. is_nonfree_firmware(const char *name)
  78. {
  79. return strstr(name, NONFREE_FIRMWARE) != 0;
  80. }
  81. static inline int
  82. report_missing_free_firmware(const char *name, const char *what)
  83. {
  84. printk(KERN_ERR "%s: Missing Free %s (non-Free firmware loading is disabled)\n", name,
  85. what ? what : "firmware");
  86. return -EINVAL;
  87. }
  88. static inline int
  89. reject_firmware(const struct firmware **fw,
  90. const char *name, struct device *device)
  91. {
  92. const struct firmware *xfw = NULL;
  93. int retval;
  94. report_missing_free_firmware(dev_name(device), NULL);
  95. retval = request_firmware(&xfw, NONFREE_FIRMWARE, device);
  96. if (!retval)
  97. release_firmware(xfw);
  98. return -EINVAL;
  99. }
  100. static inline int
  101. maybe_reject_firmware(const struct firmware **fw,
  102. const char *name, struct device *device)
  103. {
  104. if (is_nonfree_firmware(name))
  105. return reject_firmware(fw, name, device);
  106. else
  107. return request_firmware(fw, name, device);
  108. }
  109. static inline int
  110. reject_firmware_direct(const struct firmware **fw,
  111. const char *name, struct device *device)
  112. {
  113. const struct firmware *xfw = NULL;
  114. int retval;
  115. report_missing_free_firmware(dev_name(device), NULL);
  116. retval = request_firmware_direct(&xfw, NONFREE_FIRMWARE, device);
  117. if (!retval)
  118. release_firmware(xfw);
  119. return -EINVAL;
  120. }
  121. static inline void
  122. discard_rejected_firmware(const struct firmware *fw, void *context)
  123. {
  124. release_firmware(fw);
  125. }
  126. static inline int
  127. reject_firmware_nowait(struct module *module, int uevent,
  128. const char *name, struct device *device,
  129. gfp_t gfp, void *context,
  130. void (*cont)(const struct firmware *fw,
  131. void *context))
  132. {
  133. int retval;
  134. report_missing_free_firmware(dev_name(device), NULL);
  135. retval = request_firmware_nowait(module, uevent, NONFREE_FIRMWARE,
  136. device, gfp, NULL,
  137. discard_rejected_firmware);
  138. if (retval)
  139. return retval;
  140. return -EINVAL;
  141. }
  142. static inline int
  143. maybe_reject_firmware_nowait(struct module *module, int uevent,
  144. const char *name, struct device *device,
  145. gfp_t gfp, void *context,
  146. void (*cont)(const struct firmware *fw,
  147. void *context))
  148. {
  149. if (is_nonfree_firmware(name))
  150. return reject_firmware_nowait(module, uevent, name,
  151. device, gfp, context, cont);
  152. else
  153. return request_firmware_nowait(module, uevent, name,
  154. device, gfp, context, cont);
  155. }
  156. #endif /* _LINUX_LIBRE_FIRMWARE_H */
  157. #endif