blob: 259d7585398493720d68ceaa285ebe850a063cdd [file] [log] [blame]
David Kilroy37a2e562009-02-04 23:05:52 +00001/* Firmware file reading and download helpers
2 *
3 * See copyright notice in main.c
4 */
5#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
David Kilroy37a2e562009-02-04 23:05:52 +00007#include <linux/firmware.h>
David Kilroy44d8dad2009-06-18 23:21:22 +01008#include <linux/device.h>
David Kilroy37a2e562009-02-04 23:05:52 +00009
10#include "hermes.h"
11#include "hermes_dld.h"
12#include "orinoco.h"
13
14#include "fw.h"
15
16/* End markers (for Symbol firmware only) */
17#define TEXT_END 0x1A /* End of text header */
18
19struct fw_info {
20 char *pri_fw;
21 char *sta_fw;
22 char *ap_fw;
23 u32 pda_addr;
24 u16 pda_size;
25};
26
Tobias Klauserf733ded2009-02-09 23:06:53 +010027static const struct fw_info orinoco_fw[] = {
David Kilroy37a2e562009-02-04 23:05:52 +000028 { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
29 { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
30 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
31};
Ben Hutchings6f48d0e92009-11-07 22:01:29 +000032MODULE_FIRMWARE("agere_sta_fw.bin");
33MODULE_FIRMWARE("agere_ap_fw.bin");
34MODULE_FIRMWARE("prism_sta_fw.bin");
35MODULE_FIRMWARE("prism_ap_fw.bin");
36MODULE_FIRMWARE("symbol_sp24t_prim_fw");
37MODULE_FIRMWARE("symbol_sp24t_sec_fw");
David Kilroy37a2e562009-02-04 23:05:52 +000038
39/* Structure used to access fields in FW
40 * Make sure LE decoding macros are used
41 */
42struct orinoco_fw_header {
43 char hdr_vers[6]; /* ASCII string for header version */
44 __le16 headersize; /* Total length of header */
45 __le32 entry_point; /* NIC entry point */
46 __le32 blocks; /* Number of blocks to program */
47 __le32 block_offset; /* Offset of block data from eof header */
48 __le32 pdr_offset; /* Offset to PDR data from eof header */
49 __le32 pri_offset; /* Offset to primary plug data */
50 __le32 compat_offset; /* Offset to compatibility data*/
51 char signature[0]; /* FW signature length headersize-20 */
Eric Dumazetba2d3582010-06-02 18:10:09 +000052} __packed;
David Kilroy37a2e562009-02-04 23:05:52 +000053
David Kilroy7e578112009-02-21 16:52:53 +000054/* Check the range of various header entries. Return a pointer to a
55 * description of the problem, or NULL if everything checks out. */
56static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
57{
58 u16 hdrsize;
59
60 if (len < sizeof(*hdr))
61 return "image too small";
62 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
63 return "format not recognised";
64
65 hdrsize = le16_to_cpu(hdr->headersize);
66 if (hdrsize > len)
67 return "bad headersize";
68 if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
69 return "bad block offset";
70 if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
71 return "bad PDR offset";
72 if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
73 return "bad PRI offset";
74 if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
75 return "bad compat offset";
76
77 /* TODO: consider adding a checksum or CRC to the firmware format */
78 return NULL;
79}
80
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +030081#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
82static inline const struct firmware *
83orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
84{
85 if (primary)
86 return priv->cached_pri_fw;
87 else
88 return priv->cached_fw;
89}
90#else
91#define orinoco_cached_fw_get(priv, primary) (NULL)
92#endif
93
David Kilroy37a2e562009-02-04 23:05:52 +000094/* Download either STA or AP firmware into the card. */
95static int
96orinoco_dl_firmware(struct orinoco_private *priv,
97 const struct fw_info *fw,
98 int ap)
99{
100 /* Plug Data Area (PDA) */
101 __le16 *pda;
102
103 hermes_t *hw = &priv->hw;
104 const struct firmware *fw_entry;
105 const struct orinoco_fw_header *hdr;
106 const unsigned char *first_block;
David Kilroy3faa19c2009-02-21 16:52:54 +0000107 const void *end;
David Kilroy37a2e562009-02-04 23:05:52 +0000108 const char *firmware;
David Kilroy7e578112009-02-21 16:52:53 +0000109 const char *fw_err;
David Kilroy44d8dad2009-06-18 23:21:22 +0100110 struct device *dev = priv->dev;
David Kilroy37a2e562009-02-04 23:05:52 +0000111 int err = 0;
112
113 pda = kzalloc(fw->pda_size, GFP_KERNEL);
114 if (!pda)
115 return -ENOMEM;
116
117 if (ap)
118 firmware = fw->ap_fw;
119 else
120 firmware = fw->sta_fw;
121
David Kilroy44d8dad2009-06-18 23:21:22 +0100122 dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
David Kilroy37a2e562009-02-04 23:05:52 +0000123
124 /* Read current plug data */
David Kilroy07cefe72010-05-01 14:05:43 +0100125 err = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
David Kilroy44d8dad2009-06-18 23:21:22 +0100126 dev_dbg(dev, "Read PDA returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000127 if (err)
128 goto free;
129
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300130 if (!orinoco_cached_fw_get(priv, false)) {
David Kilroy37a2e562009-02-04 23:05:52 +0000131 err = request_firmware(&fw_entry, firmware, priv->dev);
132
133 if (err) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100134 dev_err(dev, "Cannot find firmware %s\n", firmware);
David Kilroy37a2e562009-02-04 23:05:52 +0000135 err = -ENOENT;
136 goto free;
137 }
138 } else
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300139 fw_entry = orinoco_cached_fw_get(priv, false);
David Kilroy37a2e562009-02-04 23:05:52 +0000140
141 hdr = (const struct orinoco_fw_header *) fw_entry->data;
142
David Kilroy7e578112009-02-21 16:52:53 +0000143 fw_err = validate_fw(hdr, fw_entry->size);
144 if (fw_err) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100145 dev_warn(dev, "Invalid firmware image detected (%s). "
146 "Aborting download\n", fw_err);
David Kilroy7e578112009-02-21 16:52:53 +0000147 err = -EINVAL;
148 goto abort;
149 }
150
David Kilroy37a2e562009-02-04 23:05:52 +0000151 /* Enable aux port to allow programming */
David Kilroy07cefe72010-05-01 14:05:43 +0100152 err = hw->ops->program_init(hw, le32_to_cpu(hdr->entry_point));
David Kilroy44d8dad2009-06-18 23:21:22 +0100153 dev_dbg(dev, "Program init returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000154 if (err != 0)
155 goto abort;
156
157 /* Program data */
158 first_block = (fw_entry->data +
159 le16_to_cpu(hdr->headersize) +
160 le32_to_cpu(hdr->block_offset));
161 end = fw_entry->data + fw_entry->size;
162
163 err = hermes_program(hw, first_block, end);
David Kilroy44d8dad2009-06-18 23:21:22 +0100164 dev_dbg(dev, "Program returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000165 if (err != 0)
166 goto abort;
167
168 /* Update production data */
169 first_block = (fw_entry->data +
170 le16_to_cpu(hdr->headersize) +
171 le32_to_cpu(hdr->pdr_offset));
172
David Kilroy3faa19c2009-02-21 16:52:54 +0000173 err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
174 &pda[fw->pda_size / sizeof(*pda)]);
David Kilroy44d8dad2009-06-18 23:21:22 +0100175 dev_dbg(dev, "Apply PDA returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000176 if (err)
177 goto abort;
178
179 /* Tell card we've finished */
David Kilroy07cefe72010-05-01 14:05:43 +0100180 err = hw->ops->program_end(hw);
David Kilroy44d8dad2009-06-18 23:21:22 +0100181 dev_dbg(dev, "Program end returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000182 if (err != 0)
183 goto abort;
184
185 /* Check if we're running */
David Kilroy44d8dad2009-06-18 23:21:22 +0100186 dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
David Kilroy37a2e562009-02-04 23:05:52 +0000187
188abort:
189 /* If we requested the firmware, release it. */
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300190 if (!orinoco_cached_fw_get(priv, false))
David Kilroy37a2e562009-02-04 23:05:52 +0000191 release_firmware(fw_entry);
192
193free:
194 kfree(pda);
195 return err;
196}
197
198/*
199 * Process a firmware image - stop the card, load the firmware, reset
200 * the card and make sure it responds. For the secondary firmware take
201 * care of the PDA - read it and then write it on top of the firmware.
202 */
203static int
204symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
David Kilroy3faa19c2009-02-21 16:52:54 +0000205 const unsigned char *image, const void *end,
David Kilroy37a2e562009-02-04 23:05:52 +0000206 int secondary)
207{
208 hermes_t *hw = &priv->hw;
209 int ret = 0;
210 const unsigned char *ptr;
211 const unsigned char *first_block;
212
213 /* Plug Data Area (PDA) */
214 __le16 *pda = NULL;
215
216 /* Binary block begins after the 0x1A marker */
217 ptr = image;
218 while (*ptr++ != TEXT_END);
219 first_block = ptr;
220
221 /* Read the PDA from EEPROM */
222 if (secondary) {
223 pda = kzalloc(fw->pda_size, GFP_KERNEL);
224 if (!pda)
225 return -ENOMEM;
226
David Kilroy07cefe72010-05-01 14:05:43 +0100227 ret = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
David Kilroy37a2e562009-02-04 23:05:52 +0000228 if (ret)
229 goto free;
230 }
231
232 /* Stop the firmware, so that it can be safely rewritten */
233 if (priv->stop_fw) {
234 ret = priv->stop_fw(priv, 1);
235 if (ret)
236 goto free;
237 }
238
239 /* Program the adapter with new firmware */
240 ret = hermes_program(hw, first_block, end);
241 if (ret)
242 goto free;
243
244 /* Write the PDA to the adapter */
245 if (secondary) {
David Kilroy3faa19c2009-02-21 16:52:54 +0000246 size_t len = hermes_blocks_length(first_block, end);
David Kilroy37a2e562009-02-04 23:05:52 +0000247 ptr = first_block + len;
David Kilroy3faa19c2009-02-21 16:52:54 +0000248 ret = hermes_apply_pda(hw, ptr, end, pda,
249 &pda[fw->pda_size / sizeof(*pda)]);
David Kilroy37a2e562009-02-04 23:05:52 +0000250 kfree(pda);
251 if (ret)
252 return ret;
253 }
254
255 /* Run the firmware */
256 if (priv->stop_fw) {
257 ret = priv->stop_fw(priv, 0);
258 if (ret)
259 return ret;
260 }
261
262 /* Reset hermes chip and make sure it responds */
David Kilroyb42f2072010-05-01 14:05:38 +0100263 ret = hw->ops->init(hw);
David Kilroy37a2e562009-02-04 23:05:52 +0000264
265 /* hermes_reset() should return 0 with the secondary firmware */
266 if (secondary && ret != 0)
267 return -ENODEV;
268
269 /* And this should work with any firmware */
270 if (!hermes_present(hw))
271 return -ENODEV;
272
273 return 0;
274
275free:
276 kfree(pda);
277 return ret;
278}
279
280
281/*
282 * Download the firmware into the card, this also does a PCMCIA soft
283 * reset on the card, to make sure it's in a sane state.
284 */
285static int
286symbol_dl_firmware(struct orinoco_private *priv,
287 const struct fw_info *fw)
288{
David Kilroy44d8dad2009-06-18 23:21:22 +0100289 struct device *dev = priv->dev;
David Kilroy37a2e562009-02-04 23:05:52 +0000290 int ret;
291 const struct firmware *fw_entry;
292
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300293 if (!orinoco_cached_fw_get(priv, true)) {
David Kilroy37a2e562009-02-04 23:05:52 +0000294 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100295 dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
David Kilroy37a2e562009-02-04 23:05:52 +0000296 return -ENOENT;
297 }
298 } else
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300299 fw_entry = orinoco_cached_fw_get(priv, true);
David Kilroy37a2e562009-02-04 23:05:52 +0000300
301 /* Load primary firmware */
302 ret = symbol_dl_image(priv, fw, fw_entry->data,
303 fw_entry->data + fw_entry->size, 0);
304
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300305 if (!orinoco_cached_fw_get(priv, true))
David Kilroy37a2e562009-02-04 23:05:52 +0000306 release_firmware(fw_entry);
307 if (ret) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100308 dev_err(dev, "Primary firmware download failed\n");
David Kilroy37a2e562009-02-04 23:05:52 +0000309 return ret;
310 }
311
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300312 if (!orinoco_cached_fw_get(priv, false)) {
David Kilroy37a2e562009-02-04 23:05:52 +0000313 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100314 dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
David Kilroy37a2e562009-02-04 23:05:52 +0000315 return -ENOENT;
316 }
317 } else
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300318 fw_entry = orinoco_cached_fw_get(priv, false);
David Kilroy37a2e562009-02-04 23:05:52 +0000319
320 /* Load secondary firmware */
321 ret = symbol_dl_image(priv, fw, fw_entry->data,
322 fw_entry->data + fw_entry->size, 1);
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300323 if (!orinoco_cached_fw_get(priv, false))
David Kilroy37a2e562009-02-04 23:05:52 +0000324 release_firmware(fw_entry);
325 if (ret) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100326 dev_err(dev, "Secondary firmware download failed\n");
David Kilroy37a2e562009-02-04 23:05:52 +0000327 }
328
329 return ret;
330}
331
332int orinoco_download(struct orinoco_private *priv)
333{
334 int err = 0;
335 /* Reload firmware */
336 switch (priv->firmware_type) {
337 case FIRMWARE_TYPE_AGERE:
338 /* case FIRMWARE_TYPE_INTERSIL: */
339 err = orinoco_dl_firmware(priv,
340 &orinoco_fw[priv->firmware_type], 0);
341 break;
342
343 case FIRMWARE_TYPE_SYMBOL:
344 err = symbol_dl_firmware(priv,
345 &orinoco_fw[priv->firmware_type]);
346 break;
347 case FIRMWARE_TYPE_INTERSIL:
348 break;
349 }
350 /* TODO: if we fail we probably need to reinitialise
351 * the driver */
352
353 return err;
354}
355
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300356#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy37a2e562009-02-04 23:05:52 +0000357void orinoco_cache_fw(struct orinoco_private *priv, int ap)
358{
David Kilroy37a2e562009-02-04 23:05:52 +0000359 const struct firmware *fw_entry = NULL;
360 const char *pri_fw;
361 const char *fw;
362
363 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
364 if (ap)
365 fw = orinoco_fw[priv->firmware_type].ap_fw;
366 else
367 fw = orinoco_fw[priv->firmware_type].sta_fw;
368
369 if (pri_fw) {
370 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
371 priv->cached_pri_fw = fw_entry;
372 }
373
374 if (fw) {
375 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
376 priv->cached_fw = fw_entry;
377 }
David Kilroy37a2e562009-02-04 23:05:52 +0000378}
379
380void orinoco_uncache_fw(struct orinoco_private *priv)
381{
David Kilroy37a2e562009-02-04 23:05:52 +0000382 if (priv->cached_pri_fw)
383 release_firmware(priv->cached_pri_fw);
384 if (priv->cached_fw)
385 release_firmware(priv->cached_fw);
386
387 priv->cached_pri_fw = NULL;
388 priv->cached_fw = NULL;
David Kilroy37a2e562009-02-04 23:05:52 +0000389}
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300390#endif