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