firmware.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Firmware loading and handling functions.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/firmware.h>
  7. #include <linux/module.h>
  8. #include "dev.h"
  9. #include "decl.h"
  10. static void load_next_firmware_from_table(struct lbs_private *private);
  11. static void lbs_fw_loaded(struct lbs_private *priv, int ret,
  12. const struct firmware *helper, const struct firmware *mainfw)
  13. {
  14. unsigned long flags;
  15. lbs_deb_fw("firmware load complete, code %d\n", ret);
  16. /* User must free helper/mainfw */
  17. priv->fw_callback(priv, ret, helper, mainfw);
  18. spin_lock_irqsave(&priv->driver_lock, flags);
  19. priv->fw_callback = NULL;
  20. wake_up(&priv->fw_waitq);
  21. spin_unlock_irqrestore(&priv->driver_lock, flags);
  22. }
  23. static void do_load_firmware(struct lbs_private *priv, const char *name,
  24. void (*cb)(const struct firmware *fw, void *context))
  25. {
  26. int ret;
  27. lbs_deb_fw("Requesting %s\n", name);
  28. ret = request_firmware_nowait(THIS_MODULE, true, name,
  29. priv->fw_device, GFP_KERNEL, priv, cb);
  30. if (ret) {
  31. lbs_deb_fw("request_firmware_nowait error %d\n", ret);
  32. lbs_fw_loaded(priv, ret, NULL, NULL);
  33. }
  34. }
  35. static void main_firmware_cb(const struct firmware *firmware, void *context)
  36. {
  37. struct lbs_private *priv = context;
  38. if (!firmware) {
  39. /* Failed to find firmware: try next table entry */
  40. load_next_firmware_from_table(priv);
  41. return;
  42. }
  43. /* Firmware found! */
  44. lbs_fw_loaded(priv, 0, priv->helper_fw, firmware);
  45. if (priv->helper_fw) {
  46. release_firmware (priv->helper_fw);
  47. priv->helper_fw = NULL;
  48. }
  49. release_firmware (firmware);
  50. }
  51. static void helper_firmware_cb(const struct firmware *firmware, void *context)
  52. {
  53. struct lbs_private *priv = context;
  54. if (!firmware) {
  55. /* Failed to find firmware: try next table entry */
  56. load_next_firmware_from_table(priv);
  57. return;
  58. }
  59. /* Firmware found! */
  60. if (priv->fw_iter->fwname) {
  61. priv->helper_fw = firmware;
  62. do_load_firmware(priv, priv->fw_iter->fwname, main_firmware_cb);
  63. } else {
  64. /* No main firmware needed for this helper --> success! */
  65. lbs_fw_loaded(priv, 0, firmware, NULL);
  66. }
  67. }
  68. static void load_next_firmware_from_table(struct lbs_private *priv)
  69. {
  70. const struct lbs_fw_table *iter;
  71. if (!priv->fw_iter)
  72. iter = priv->fw_table;
  73. else
  74. iter = ++priv->fw_iter;
  75. if (priv->helper_fw) {
  76. release_firmware(priv->helper_fw);
  77. priv->helper_fw = NULL;
  78. }
  79. next:
  80. if (!iter->helper) {
  81. /* End of table hit. */
  82. lbs_fw_loaded(priv, -ENOENT, NULL, NULL);
  83. return;
  84. }
  85. if (iter->model != priv->fw_model) {
  86. iter++;
  87. goto next;
  88. }
  89. priv->fw_iter = iter;
  90. do_load_firmware(priv, iter->helper, helper_firmware_cb);
  91. }
  92. void lbs_wait_for_firmware_load(struct lbs_private *priv)
  93. {
  94. wait_event(priv->fw_waitq, priv->fw_callback == NULL);
  95. }
  96. /**
  97. * lbs_get_firmware_async - Retrieves firmware asynchronously. Can load
  98. * either a helper firmware and a main firmware (2-stage), or just the helper.
  99. *
  100. * @priv: Pointer to lbs_private instance
  101. * @device: A pointer to &device structure
  102. * @card_model: Bus-specific card model ID used to filter firmware table
  103. * elements
  104. * @fw_table: Table of firmware file names and device model numbers
  105. * terminated by an entry with a NULL helper name
  106. * @callback: User callback to invoke when firmware load succeeds or fails.
  107. */
  108. int lbs_get_firmware_async(struct lbs_private *priv, struct device *device,
  109. u32 card_model, const struct lbs_fw_table *fw_table,
  110. lbs_fw_cb callback)
  111. {
  112. unsigned long flags;
  113. spin_lock_irqsave(&priv->driver_lock, flags);
  114. if (priv->fw_callback) {
  115. lbs_deb_fw("firmware load already in progress\n");
  116. spin_unlock_irqrestore(&priv->driver_lock, flags);
  117. return -EBUSY;
  118. }
  119. priv->fw_device = device;
  120. priv->fw_callback = callback;
  121. priv->fw_table = fw_table;
  122. priv->fw_iter = NULL;
  123. priv->fw_model = card_model;
  124. spin_unlock_irqrestore(&priv->driver_lock, flags);
  125. lbs_deb_fw("Starting async firmware load\n");
  126. load_next_firmware_from_table(priv);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL_GPL(lbs_get_firmware_async);
  130. /**
  131. * lbs_get_firmware - Retrieves two-stage firmware
  132. *
  133. * @dev: A pointer to &device structure
  134. * @card_model: Bus-specific card model ID used to filter firmware table
  135. * elements
  136. * @fw_table: Table of firmware file names and device model numbers
  137. * terminated by an entry with a NULL helper name
  138. * @helper: On success, the helper firmware; caller must free
  139. * @mainfw: On success, the main firmware; caller must free
  140. *
  141. * Deprecated: use lbs_get_firmware_async() instead.
  142. *
  143. * returns: 0 on success, non-zero on failure
  144. */
  145. int lbs_get_firmware(struct device *dev, u32 card_model,
  146. const struct lbs_fw_table *fw_table,
  147. const struct firmware **helper,
  148. const struct firmware **mainfw)
  149. {
  150. const struct lbs_fw_table *iter;
  151. int ret;
  152. BUG_ON(helper == NULL);
  153. BUG_ON(mainfw == NULL);
  154. /* Search for firmware to use from the table. */
  155. iter = fw_table;
  156. while (iter && iter->helper) {
  157. if (iter->model != card_model)
  158. goto next;
  159. if (*helper == NULL) {
  160. ret = request_firmware(helper, iter->helper, dev);
  161. if (ret)
  162. goto next;
  163. /* If the device has one-stage firmware (ie cf8305) and
  164. * we've got it then we don't need to bother with the
  165. * main firmware.
  166. */
  167. if (iter->fwname == NULL)
  168. return 0;
  169. }
  170. if (*mainfw == NULL) {
  171. ret = request_firmware(mainfw, iter->fwname, dev);
  172. if (ret) {
  173. /* Clear the helper to ensure we don't have
  174. * mismatched firmware pairs.
  175. */
  176. release_firmware(*helper);
  177. *helper = NULL;
  178. }
  179. }
  180. if (*helper && *mainfw)
  181. return 0;
  182. next:
  183. iter++;
  184. }
  185. /* Failed */
  186. release_firmware(*helper);
  187. *helper = NULL;
  188. release_firmware(*mainfw);
  189. *mainfw = NULL;
  190. return -ENOENT;
  191. }
  192. EXPORT_SYMBOL_GPL(lbs_get_firmware);