firmware.c 5.5 KB

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