2 * Firmware loading and handling functions.
5 #include <linux/sched.h>
6 #include <linux/firmware.h>
7 #include <linux/module.h>
12 static void load_next_firmware_from_table(struct lbs_private
*private);
14 static void lbs_fw_loaded(struct lbs_private
*priv
, int ret
,
15 const struct firmware
*helper
, const struct firmware
*mainfw
)
19 lbs_deb_fw("firmware load complete, code %d\n", ret
);
21 /* User must free helper/mainfw */
22 priv
->fw_callback(priv
, ret
, helper
, mainfw
);
24 spin_lock_irqsave(&priv
->driver_lock
, flags
);
25 priv
->fw_callback
= NULL
;
26 wake_up(&priv
->fw_waitq
);
27 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
30 static void do_load_firmware(struct lbs_private
*priv
, const char *name
,
31 void (*cb
)(const struct firmware
*fw
, void *context
))
35 lbs_deb_fw("Requesting %s\n", name
);
36 ret
= request_firmware_nowait(THIS_MODULE
, true, name
,
37 priv
->fw_device
, GFP_KERNEL
, priv
, cb
);
39 lbs_deb_fw("request_firmware_nowait error %d\n", ret
);
40 lbs_fw_loaded(priv
, ret
, NULL
, NULL
);
44 static void main_firmware_cb(const struct firmware
*firmware
, void *context
)
46 struct lbs_private
*priv
= context
;
49 /* Failed to find firmware: try next table entry */
50 load_next_firmware_from_table(priv
);
55 lbs_fw_loaded(priv
, 0, priv
->helper_fw
, firmware
);
58 static void helper_firmware_cb(const struct firmware
*firmware
, void *context
)
60 struct lbs_private
*priv
= context
;
63 /* Failed to find firmware: try next table entry */
64 load_next_firmware_from_table(priv
);
69 if (priv
->fw_iter
->fwname
) {
70 priv
->helper_fw
= firmware
;
71 do_load_firmware(priv
, priv
->fw_iter
->fwname
, main_firmware_cb
);
73 /* No main firmware needed for this helper --> success! */
74 lbs_fw_loaded(priv
, 0, firmware
, NULL
);
78 static void load_next_firmware_from_table(struct lbs_private
*priv
)
80 const struct lbs_fw_table
*iter
;
83 iter
= priv
->fw_table
;
85 iter
= ++priv
->fw_iter
;
87 if (priv
->helper_fw
) {
88 release_firmware(priv
->helper_fw
);
89 priv
->helper_fw
= NULL
;
94 /* End of table hit. */
95 lbs_fw_loaded(priv
, -ENOENT
, NULL
, NULL
);
99 if (iter
->model
!= priv
->fw_model
) {
104 priv
->fw_iter
= iter
;
105 do_load_firmware(priv
, iter
->helper
, helper_firmware_cb
);
108 void lbs_wait_for_firmware_load(struct lbs_private
*priv
)
110 wait_event(priv
->fw_waitq
, priv
->fw_callback
== NULL
);
114 * lbs_get_firmware_async - Retrieves firmware asynchronously. Can load
115 * either a helper firmware and a main firmware (2-stage), or just the helper.
117 * @priv: Pointer to lbs_private instance
118 * @dev: A pointer to &device structure
119 * @card_model: Bus-specific card model ID used to filter firmware table
121 * @fw_table: Table of firmware file names and device model numbers
122 * terminated by an entry with a NULL helper name
123 * @callback: User callback to invoke when firmware load succeeds or fails.
125 int lbs_get_firmware_async(struct lbs_private
*priv
, struct device
*device
,
126 u32 card_model
, const struct lbs_fw_table
*fw_table
,
131 spin_lock_irqsave(&priv
->driver_lock
, flags
);
132 if (priv
->fw_callback
) {
133 lbs_deb_fw("firmware load already in progress\n");
134 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
138 priv
->fw_device
= device
;
139 priv
->fw_callback
= callback
;
140 priv
->fw_table
= fw_table
;
141 priv
->fw_iter
= NULL
;
142 priv
->fw_model
= card_model
;
143 spin_unlock_irqrestore(&priv
->driver_lock
, flags
);
145 lbs_deb_fw("Starting async firmware load\n");
146 load_next_firmware_from_table(priv
);
149 EXPORT_SYMBOL_GPL(lbs_get_firmware_async
);
152 * lbs_get_firmware - Retrieves two-stage firmware
154 * @dev: A pointer to &device structure
155 * @card_model: Bus-specific card model ID used to filter firmware table
157 * @fw_table: Table of firmware file names and device model numbers
158 * terminated by an entry with a NULL helper name
159 * @helper: On success, the helper firmware; caller must free
160 * @mainfw: On success, the main firmware; caller must free
162 * Deprecated: use lbs_get_firmware_async() instead.
164 * returns: 0 on success, non-zero on failure
166 int lbs_get_firmware(struct device
*dev
, u32 card_model
,
167 const struct lbs_fw_table
*fw_table
,
168 const struct firmware
**helper
,
169 const struct firmware
**mainfw
)
171 const struct lbs_fw_table
*iter
;
174 BUG_ON(helper
== NULL
);
175 BUG_ON(mainfw
== NULL
);
177 /* Search for firmware to use from the table. */
179 while (iter
&& iter
->helper
) {
180 if (iter
->model
!= card_model
)
183 if (*helper
== NULL
) {
184 ret
= request_firmware(helper
, iter
->helper
, dev
);
188 /* If the device has one-stage firmware (ie cf8305) and
189 * we've got it then we don't need to bother with the
192 if (iter
->fwname
== NULL
)
196 if (*mainfw
== NULL
) {
197 ret
= request_firmware(mainfw
, iter
->fwname
, dev
);
199 /* Clear the helper to ensure we don't have
200 * mismatched firmware pairs.
202 release_firmware(*helper
);
207 if (*helper
&& *mainfw
)
215 release_firmware(*helper
);
217 release_firmware(*mainfw
);
222 EXPORT_SYMBOL_GPL(lbs_get_firmware
);