firmware.c 5.5 KB

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