blob: 1257250a1e22c3822fb3372dd09eca23b740f76e [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>
6#include <linux/firmware.h>
David Kilroy44d8dad2009-06-18 23:21:22 +01007#include <linux/device.h>
David Kilroy37a2e562009-02-04 23:05:52 +00008
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
Tobias Klauserf733ded2009-02-09 23:06:53 +010026static const struct fw_info orinoco_fw[] = {
David Kilroy37a2e562009-02-04 23:05:52 +000027 { 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};
31
32/* Structure used to access fields in FW
33 * Make sure LE decoding macros are used
34 */
35struct orinoco_fw_header {
36 char hdr_vers[6]; /* ASCII string for header version */
37 __le16 headersize; /* Total length of header */
38 __le32 entry_point; /* NIC entry point */
39 __le32 blocks; /* Number of blocks to program */
40 __le32 block_offset; /* Offset of block data from eof header */
41 __le32 pdr_offset; /* Offset to PDR data from eof header */
42 __le32 pri_offset; /* Offset to primary plug data */
43 __le32 compat_offset; /* Offset to compatibility data*/
44 char signature[0]; /* FW signature length headersize-20 */
45} __attribute__ ((packed));
46
David Kilroy7e578112009-02-21 16:52:53 +000047/* Check the range of various header entries. Return a pointer to a
48 * description of the problem, or NULL if everything checks out. */
49static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
50{
51 u16 hdrsize;
52
53 if (len < sizeof(*hdr))
54 return "image too small";
55 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
56 return "format not recognised";
57
58 hdrsize = le16_to_cpu(hdr->headersize);
59 if (hdrsize > len)
60 return "bad headersize";
61 if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
62 return "bad block offset";
63 if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
64 return "bad PDR offset";
65 if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
66 return "bad PRI offset";
67 if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
68 return "bad compat offset";
69
70 /* TODO: consider adding a checksum or CRC to the firmware format */
71 return NULL;
72}
73
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +030074#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
75static inline const struct firmware *
76orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
77{
78 if (primary)
79 return priv->cached_pri_fw;
80 else
81 return priv->cached_fw;
82}
83#else
84#define orinoco_cached_fw_get(priv, primary) (NULL)
85#endif
86
David Kilroy37a2e562009-02-04 23:05:52 +000087/* Download either STA or AP firmware into the card. */
88static int
89orinoco_dl_firmware(struct orinoco_private *priv,
90 const struct fw_info *fw,
91 int ap)
92{
93 /* Plug Data Area (PDA) */
94 __le16 *pda;
95
96 hermes_t *hw = &priv->hw;
97 const struct firmware *fw_entry;
98 const struct orinoco_fw_header *hdr;
99 const unsigned char *first_block;
David Kilroy3faa19c2009-02-21 16:52:54 +0000100 const void *end;
David Kilroy37a2e562009-02-04 23:05:52 +0000101 const char *firmware;
David Kilroy7e578112009-02-21 16:52:53 +0000102 const char *fw_err;
David Kilroy44d8dad2009-06-18 23:21:22 +0100103 struct device *dev = priv->dev;
David Kilroy37a2e562009-02-04 23:05:52 +0000104 int err = 0;
105
106 pda = kzalloc(fw->pda_size, GFP_KERNEL);
107 if (!pda)
108 return -ENOMEM;
109
110 if (ap)
111 firmware = fw->ap_fw;
112 else
113 firmware = fw->sta_fw;
114
David Kilroy44d8dad2009-06-18 23:21:22 +0100115 dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
David Kilroy37a2e562009-02-04 23:05:52 +0000116
117 /* Read current plug data */
118 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
David Kilroy44d8dad2009-06-18 23:21:22 +0100119 dev_dbg(dev, "Read PDA returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000120 if (err)
121 goto free;
122
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300123 if (!orinoco_cached_fw_get(priv, false)) {
David Kilroy37a2e562009-02-04 23:05:52 +0000124 err = request_firmware(&fw_entry, firmware, priv->dev);
125
126 if (err) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100127 dev_err(dev, "Cannot find firmware %s\n", firmware);
David Kilroy37a2e562009-02-04 23:05:52 +0000128 err = -ENOENT;
129 goto free;
130 }
131 } else
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300132 fw_entry = orinoco_cached_fw_get(priv, false);
David Kilroy37a2e562009-02-04 23:05:52 +0000133
134 hdr = (const struct orinoco_fw_header *) fw_entry->data;
135
David Kilroy7e578112009-02-21 16:52:53 +0000136 fw_err = validate_fw(hdr, fw_entry->size);
137 if (fw_err) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100138 dev_warn(dev, "Invalid firmware image detected (%s). "
139 "Aborting download\n", fw_err);
David Kilroy7e578112009-02-21 16:52:53 +0000140 err = -EINVAL;
141 goto abort;
142 }
143
David Kilroy37a2e562009-02-04 23:05:52 +0000144 /* Enable aux port to allow programming */
145 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
David Kilroy44d8dad2009-06-18 23:21:22 +0100146 dev_dbg(dev, "Program init returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000147 if (err != 0)
148 goto abort;
149
150 /* Program data */
151 first_block = (fw_entry->data +
152 le16_to_cpu(hdr->headersize) +
153 le32_to_cpu(hdr->block_offset));
154 end = fw_entry->data + fw_entry->size;
155
156 err = hermes_program(hw, first_block, end);
David Kilroy44d8dad2009-06-18 23:21:22 +0100157 dev_dbg(dev, "Program returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000158 if (err != 0)
159 goto abort;
160
161 /* Update production data */
162 first_block = (fw_entry->data +
163 le16_to_cpu(hdr->headersize) +
164 le32_to_cpu(hdr->pdr_offset));
165
David Kilroy3faa19c2009-02-21 16:52:54 +0000166 err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
167 &pda[fw->pda_size / sizeof(*pda)]);
David Kilroy44d8dad2009-06-18 23:21:22 +0100168 dev_dbg(dev, "Apply PDA returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000169 if (err)
170 goto abort;
171
172 /* Tell card we've finished */
173 err = hermesi_program_end(hw);
David Kilroy44d8dad2009-06-18 23:21:22 +0100174 dev_dbg(dev, "Program end returned %d\n", err);
David Kilroy37a2e562009-02-04 23:05:52 +0000175 if (err != 0)
176 goto abort;
177
178 /* Check if we're running */
David Kilroy44d8dad2009-06-18 23:21:22 +0100179 dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
David Kilroy37a2e562009-02-04 23:05:52 +0000180
181abort:
182 /* If we requested the firmware, release it. */
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300183 if (!orinoco_cached_fw_get(priv, false))
David Kilroy37a2e562009-02-04 23:05:52 +0000184 release_firmware(fw_entry);
185
186free:
187 kfree(pda);
188 return err;
189}
190
191/*
192 * Process a firmware image - stop the card, load the firmware, reset
193 * the card and make sure it responds. For the secondary firmware take
194 * care of the PDA - read it and then write it on top of the firmware.
195 */
196static int
197symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
David Kilroy3faa19c2009-02-21 16:52:54 +0000198 const unsigned char *image, const void *end,
David Kilroy37a2e562009-02-04 23:05:52 +0000199 int secondary)
200{
201 hermes_t *hw = &priv->hw;
202 int ret = 0;
203 const unsigned char *ptr;
204 const unsigned char *first_block;
205
206 /* Plug Data Area (PDA) */
207 __le16 *pda = NULL;
208
209 /* Binary block begins after the 0x1A marker */
210 ptr = image;
211 while (*ptr++ != TEXT_END);
212 first_block = ptr;
213
214 /* Read the PDA from EEPROM */
215 if (secondary) {
216 pda = kzalloc(fw->pda_size, GFP_KERNEL);
217 if (!pda)
218 return -ENOMEM;
219
220 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
221 if (ret)
222 goto free;
223 }
224
225 /* Stop the firmware, so that it can be safely rewritten */
226 if (priv->stop_fw) {
227 ret = priv->stop_fw(priv, 1);
228 if (ret)
229 goto free;
230 }
231
232 /* Program the adapter with new firmware */
233 ret = hermes_program(hw, first_block, end);
234 if (ret)
235 goto free;
236
237 /* Write the PDA to the adapter */
238 if (secondary) {
David Kilroy3faa19c2009-02-21 16:52:54 +0000239 size_t len = hermes_blocks_length(first_block, end);
David Kilroy37a2e562009-02-04 23:05:52 +0000240 ptr = first_block + len;
David Kilroy3faa19c2009-02-21 16:52:54 +0000241 ret = hermes_apply_pda(hw, ptr, end, pda,
242 &pda[fw->pda_size / sizeof(*pda)]);
David Kilroy37a2e562009-02-04 23:05:52 +0000243 kfree(pda);
244 if (ret)
245 return ret;
246 }
247
248 /* Run the firmware */
249 if (priv->stop_fw) {
250 ret = priv->stop_fw(priv, 0);
251 if (ret)
252 return ret;
253 }
254
255 /* Reset hermes chip and make sure it responds */
256 ret = hermes_init(hw);
257
258 /* hermes_reset() should return 0 with the secondary firmware */
259 if (secondary && ret != 0)
260 return -ENODEV;
261
262 /* And this should work with any firmware */
263 if (!hermes_present(hw))
264 return -ENODEV;
265
266 return 0;
267
268free:
269 kfree(pda);
270 return ret;
271}
272
273
274/*
275 * Download the firmware into the card, this also does a PCMCIA soft
276 * reset on the card, to make sure it's in a sane state.
277 */
278static int
279symbol_dl_firmware(struct orinoco_private *priv,
280 const struct fw_info *fw)
281{
David Kilroy44d8dad2009-06-18 23:21:22 +0100282 struct device *dev = priv->dev;
David Kilroy37a2e562009-02-04 23:05:52 +0000283 int ret;
284 const struct firmware *fw_entry;
285
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300286 if (!orinoco_cached_fw_get(priv, true)) {
David Kilroy37a2e562009-02-04 23:05:52 +0000287 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100288 dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
David Kilroy37a2e562009-02-04 23:05:52 +0000289 return -ENOENT;
290 }
291 } else
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300292 fw_entry = orinoco_cached_fw_get(priv, true);
David Kilroy37a2e562009-02-04 23:05:52 +0000293
294 /* Load primary firmware */
295 ret = symbol_dl_image(priv, fw, fw_entry->data,
296 fw_entry->data + fw_entry->size, 0);
297
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300298 if (!orinoco_cached_fw_get(priv, true))
David Kilroy37a2e562009-02-04 23:05:52 +0000299 release_firmware(fw_entry);
300 if (ret) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100301 dev_err(dev, "Primary firmware download failed\n");
David Kilroy37a2e562009-02-04 23:05:52 +0000302 return ret;
303 }
304
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300305 if (!orinoco_cached_fw_get(priv, false)) {
David Kilroy37a2e562009-02-04 23:05:52 +0000306 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100307 dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
David Kilroy37a2e562009-02-04 23:05:52 +0000308 return -ENOENT;
309 }
310 } else
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300311 fw_entry = orinoco_cached_fw_get(priv, false);
David Kilroy37a2e562009-02-04 23:05:52 +0000312
313 /* Load secondary firmware */
314 ret = symbol_dl_image(priv, fw, fw_entry->data,
315 fw_entry->data + fw_entry->size, 1);
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300316 if (!orinoco_cached_fw_get(priv, false))
David Kilroy37a2e562009-02-04 23:05:52 +0000317 release_firmware(fw_entry);
318 if (ret) {
David Kilroy44d8dad2009-06-18 23:21:22 +0100319 dev_err(dev, "Secondary firmware download failed\n");
David Kilroy37a2e562009-02-04 23:05:52 +0000320 }
321
322 return ret;
323}
324
325int orinoco_download(struct orinoco_private *priv)
326{
327 int err = 0;
328 /* Reload firmware */
329 switch (priv->firmware_type) {
330 case FIRMWARE_TYPE_AGERE:
331 /* case FIRMWARE_TYPE_INTERSIL: */
332 err = orinoco_dl_firmware(priv,
333 &orinoco_fw[priv->firmware_type], 0);
334 break;
335
336 case FIRMWARE_TYPE_SYMBOL:
337 err = symbol_dl_firmware(priv,
338 &orinoco_fw[priv->firmware_type]);
339 break;
340 case FIRMWARE_TYPE_INTERSIL:
341 break;
342 }
343 /* TODO: if we fail we probably need to reinitialise
344 * the driver */
345
346 return err;
347}
348
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300349#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
David Kilroy37a2e562009-02-04 23:05:52 +0000350void orinoco_cache_fw(struct orinoco_private *priv, int ap)
351{
David Kilroy37a2e562009-02-04 23:05:52 +0000352 const struct firmware *fw_entry = NULL;
353 const char *pri_fw;
354 const char *fw;
355
356 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
357 if (ap)
358 fw = orinoco_fw[priv->firmware_type].ap_fw;
359 else
360 fw = orinoco_fw[priv->firmware_type].sta_fw;
361
362 if (pri_fw) {
363 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
364 priv->cached_pri_fw = fw_entry;
365 }
366
367 if (fw) {
368 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
369 priv->cached_fw = fw_entry;
370 }
David Kilroy37a2e562009-02-04 23:05:52 +0000371}
372
373void orinoco_uncache_fw(struct orinoco_private *priv)
374{
David Kilroy37a2e562009-02-04 23:05:52 +0000375 if (priv->cached_pri_fw)
376 release_firmware(priv->cached_pri_fw);
377 if (priv->cached_fw)
378 release_firmware(priv->cached_fw);
379
380 priv->cached_pri_fw = NULL;
381 priv->cached_fw = NULL;
David Kilroy37a2e562009-02-04 23:05:52 +0000382}
Andrey Borzenkov2bfc5cb2009-02-28 23:09:09 +0300383#endif