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