fw.c revision b42f2074dedef559ecf72dce61a6501f9f9b273a
1/* Firmware file reading and download helpers
2 *
3 * See copyright notice in main.c
4 */
5#include <linux/kernel.h>
6#include <linux/firmware.h>
7#include <linux/device.h>
8
9#include "hermes.h"
10#include "hermes_dld.h"
11#include "orinoco.h"
12
13#include "fw.h"
14
15/* End markers (for Symbol firmware only) */
16#define TEXT_END	0x1A		/* End of text header */
17
18struct fw_info {
19	char *pri_fw;
20	char *sta_fw;
21	char *ap_fw;
22	u32 pda_addr;
23	u16 pda_size;
24};
25
26static const struct fw_info orinoco_fw[] = {
27	{ NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
28	{ NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
29	{ "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
30};
31MODULE_FIRMWARE("agere_sta_fw.bin");
32MODULE_FIRMWARE("agere_ap_fw.bin");
33MODULE_FIRMWARE("prism_sta_fw.bin");
34MODULE_FIRMWARE("prism_ap_fw.bin");
35MODULE_FIRMWARE("symbol_sp24t_prim_fw");
36MODULE_FIRMWARE("symbol_sp24t_sec_fw");
37
38/* Structure used to access fields in FW
39 * Make sure LE decoding macros are used
40 */
41struct orinoco_fw_header {
42	char hdr_vers[6];       /* ASCII string for header version */
43	__le16 headersize;      /* Total length of header */
44	__le32 entry_point;     /* NIC entry point */
45	__le32 blocks;          /* Number of blocks to program */
46	__le32 block_offset;    /* Offset of block data from eof header */
47	__le32 pdr_offset;      /* Offset to PDR data from eof header */
48	__le32 pri_offset;      /* Offset to primary plug data */
49	__le32 compat_offset;   /* Offset to compatibility data*/
50	char signature[0];      /* FW signature length headersize-20 */
51} __attribute__ ((packed));
52
53/* Check the range of various header entries. Return a pointer to a
54 * description of the problem, or NULL if everything checks out. */
55static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
56{
57	u16 hdrsize;
58
59	if (len < sizeof(*hdr))
60		return "image too small";
61	if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
62		return "format not recognised";
63
64	hdrsize = le16_to_cpu(hdr->headersize);
65	if (hdrsize > len)
66		return "bad headersize";
67	if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
68		return "bad block offset";
69	if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
70		return "bad PDR offset";
71	if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
72		return "bad PRI offset";
73	if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
74		return "bad compat offset";
75
76	/* TODO: consider adding a checksum or CRC to the firmware format */
77	return NULL;
78}
79
80#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
81static inline const struct firmware *
82orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
83{
84	if (primary)
85		return priv->cached_pri_fw;
86	else
87		return priv->cached_fw;
88}
89#else
90#define orinoco_cached_fw_get(priv, primary) (NULL)
91#endif
92
93/* Download either STA or AP firmware into the card. */
94static int
95orinoco_dl_firmware(struct orinoco_private *priv,
96		    const struct fw_info *fw,
97		    int ap)
98{
99	/* Plug Data Area (PDA) */
100	__le16 *pda;
101
102	hermes_t *hw = &priv->hw;
103	const struct firmware *fw_entry;
104	const struct orinoco_fw_header *hdr;
105	const unsigned char *first_block;
106	const void *end;
107	const char *firmware;
108	const char *fw_err;
109	struct device *dev = priv->dev;
110	int err = 0;
111
112	pda = kzalloc(fw->pda_size, GFP_KERNEL);
113	if (!pda)
114		return -ENOMEM;
115
116	if (ap)
117		firmware = fw->ap_fw;
118	else
119		firmware = fw->sta_fw;
120
121	dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
122
123	/* Read current plug data */
124	err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
125	dev_dbg(dev, "Read PDA returned %d\n", err);
126	if (err)
127		goto free;
128
129	if (!orinoco_cached_fw_get(priv, false)) {
130		err = request_firmware(&fw_entry, firmware, priv->dev);
131
132		if (err) {
133			dev_err(dev, "Cannot find firmware %s\n", firmware);
134			err = -ENOENT;
135			goto free;
136		}
137	} else
138		fw_entry = orinoco_cached_fw_get(priv, false);
139
140	hdr = (const struct orinoco_fw_header *) fw_entry->data;
141
142	fw_err = validate_fw(hdr, fw_entry->size);
143	if (fw_err) {
144		dev_warn(dev, "Invalid firmware image detected (%s). "
145			 "Aborting download\n", fw_err);
146		err = -EINVAL;
147		goto abort;
148	}
149
150	/* Enable aux port to allow programming */
151	err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
152	dev_dbg(dev, "Program init returned %d\n", err);
153	if (err != 0)
154		goto abort;
155
156	/* Program data */
157	first_block = (fw_entry->data +
158		       le16_to_cpu(hdr->headersize) +
159		       le32_to_cpu(hdr->block_offset));
160	end = fw_entry->data + fw_entry->size;
161
162	err = hermes_program(hw, first_block, end);
163	dev_dbg(dev, "Program returned %d\n", err);
164	if (err != 0)
165		goto abort;
166
167	/* Update production data */
168	first_block = (fw_entry->data +
169		       le16_to_cpu(hdr->headersize) +
170		       le32_to_cpu(hdr->pdr_offset));
171
172	err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
173					     &pda[fw->pda_size / sizeof(*pda)]);
174	dev_dbg(dev, "Apply PDA returned %d\n", err);
175	if (err)
176		goto abort;
177
178	/* Tell card we've finished */
179	err = hermesi_program_end(hw);
180	dev_dbg(dev, "Program end returned %d\n", err);
181	if (err != 0)
182		goto abort;
183
184	/* Check if we're running */
185	dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
186
187abort:
188	/* If we requested the firmware, release it. */
189	if (!orinoco_cached_fw_get(priv, false))
190		release_firmware(fw_entry);
191
192free:
193	kfree(pda);
194	return err;
195}
196
197/*
198 * Process a firmware image - stop the card, load the firmware, reset
199 * the card and make sure it responds.  For the secondary firmware take
200 * care of the PDA - read it and then write it on top of the firmware.
201 */
202static int
203symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
204		const unsigned char *image, const void *end,
205		int secondary)
206{
207	hermes_t *hw = &priv->hw;
208	int ret = 0;
209	const unsigned char *ptr;
210	const unsigned char *first_block;
211
212	/* Plug Data Area (PDA) */
213	__le16 *pda = NULL;
214
215	/* Binary block begins after the 0x1A marker */
216	ptr = image;
217	while (*ptr++ != TEXT_END);
218	first_block = ptr;
219
220	/* Read the PDA from EEPROM */
221	if (secondary) {
222		pda = kzalloc(fw->pda_size, GFP_KERNEL);
223		if (!pda)
224			return -ENOMEM;
225
226		ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
227		if (ret)
228			goto free;
229	}
230
231	/* Stop the firmware, so that it can be safely rewritten */
232	if (priv->stop_fw) {
233		ret = priv->stop_fw(priv, 1);
234		if (ret)
235			goto free;
236	}
237
238	/* Program the adapter with new firmware */
239	ret = hermes_program(hw, first_block, end);
240	if (ret)
241		goto free;
242
243	/* Write the PDA to the adapter */
244	if (secondary) {
245		size_t len = hermes_blocks_length(first_block, end);
246		ptr = first_block + len;
247		ret = hermes_apply_pda(hw, ptr, end, pda,
248				       &pda[fw->pda_size / sizeof(*pda)]);
249		kfree(pda);
250		if (ret)
251			return ret;
252	}
253
254	/* Run the firmware */
255	if (priv->stop_fw) {
256		ret = priv->stop_fw(priv, 0);
257		if (ret)
258			return ret;
259	}
260
261	/* Reset hermes chip and make sure it responds */
262	ret = hw->ops->init(hw);
263
264	/* hermes_reset() should return 0 with the secondary firmware */
265	if (secondary && ret != 0)
266		return -ENODEV;
267
268	/* And this should work with any firmware */
269	if (!hermes_present(hw))
270		return -ENODEV;
271
272	return 0;
273
274free:
275	kfree(pda);
276	return ret;
277}
278
279
280/*
281 * Download the firmware into the card, this also does a PCMCIA soft
282 * reset on the card, to make sure it's in a sane state.
283 */
284static int
285symbol_dl_firmware(struct orinoco_private *priv,
286		   const struct fw_info *fw)
287{
288	struct device *dev = priv->dev;
289	int ret;
290	const struct firmware *fw_entry;
291
292	if (!orinoco_cached_fw_get(priv, true)) {
293		if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
294			dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
295			return -ENOENT;
296		}
297	} else
298		fw_entry = orinoco_cached_fw_get(priv, true);
299
300	/* Load primary firmware */
301	ret = symbol_dl_image(priv, fw, fw_entry->data,
302			      fw_entry->data + fw_entry->size, 0);
303
304	if (!orinoco_cached_fw_get(priv, true))
305		release_firmware(fw_entry);
306	if (ret) {
307		dev_err(dev, "Primary firmware download failed\n");
308		return ret;
309	}
310
311	if (!orinoco_cached_fw_get(priv, false)) {
312		if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
313			dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
314			return -ENOENT;
315		}
316	} else
317		fw_entry = orinoco_cached_fw_get(priv, false);
318
319	/* Load secondary firmware */
320	ret = symbol_dl_image(priv, fw, fw_entry->data,
321			      fw_entry->data + fw_entry->size, 1);
322	if (!orinoco_cached_fw_get(priv, false))
323		release_firmware(fw_entry);
324	if (ret) {
325		dev_err(dev, "Secondary firmware download failed\n");
326	}
327
328	return ret;
329}
330
331int orinoco_download(struct orinoco_private *priv)
332{
333	int err = 0;
334	/* Reload firmware */
335	switch (priv->firmware_type) {
336	case FIRMWARE_TYPE_AGERE:
337		/* case FIRMWARE_TYPE_INTERSIL: */
338		err = orinoco_dl_firmware(priv,
339					  &orinoco_fw[priv->firmware_type], 0);
340		break;
341
342	case FIRMWARE_TYPE_SYMBOL:
343		err = symbol_dl_firmware(priv,
344					 &orinoco_fw[priv->firmware_type]);
345		break;
346	case FIRMWARE_TYPE_INTERSIL:
347		break;
348	}
349	/* TODO: if we fail we probably need to reinitialise
350	 * the driver */
351
352	return err;
353}
354
355#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
356void orinoco_cache_fw(struct orinoco_private *priv, int ap)
357{
358	const struct firmware *fw_entry = NULL;
359	const char *pri_fw;
360	const char *fw;
361
362	pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
363	if (ap)
364		fw = orinoco_fw[priv->firmware_type].ap_fw;
365	else
366		fw = orinoco_fw[priv->firmware_type].sta_fw;
367
368	if (pri_fw) {
369		if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
370			priv->cached_pri_fw = fw_entry;
371	}
372
373	if (fw) {
374		if (request_firmware(&fw_entry, fw, priv->dev) == 0)
375			priv->cached_fw = fw_entry;
376	}
377}
378
379void orinoco_uncache_fw(struct orinoco_private *priv)
380{
381	if (priv->cached_pri_fw)
382		release_firmware(priv->cached_pri_fw);
383	if (priv->cached_fw)
384		release_firmware(priv->cached_fw);
385
386	priv->cached_pri_fw = NULL;
387	priv->cached_fw = NULL;
388}
389#endif
390