firmware.c 5.5 KB

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