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