1 /* Firmware file reading and download helpers
3 * See copyright notice in main.c
5 #include <linux/kernel.h>
6 #include <linux/firmware.h>
7 #include <linux/device.h>
10 #include "hermes_dld.h"
15 /* End markers (for Symbol firmware only) */
16 #define TEXT_END 0x1A /* End of text header */
26 static 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 }
32 /* Structure used to access fields in FW
33 * Make sure LE decoding macros are used
35 struct 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));
47 /* Check the range of various header entries. Return a pointer to a
48 * description of the problem, or NULL if everything checks out. */
49 static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
53 if (len < sizeof(*hdr))
54 return "image too small";
55 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
56 return "format not recognised";
58 hdrsize = le16_to_cpu(hdr->headersize);
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";
70 /* TODO: consider adding a checksum or CRC to the firmware format */
74 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
75 static inline const struct firmware *
76 orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
79 return priv->cached_pri_fw;
81 return priv->cached_fw;
84 #define orinoco_cached_fw_get(priv, primary) (NULL)
87 /* Download either STA or AP firmware into the card. */
89 orinoco_dl_firmware(struct orinoco_private *priv,
90 const struct fw_info *fw,
93 /* Plug Data Area (PDA) */
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;
101 const char *firmware;
103 struct device *dev = priv->dev;
106 pda = kzalloc(fw->pda_size, GFP_KERNEL);
111 firmware = fw->ap_fw;
113 firmware = fw->sta_fw;
115 dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
117 /* Read current plug data */
118 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
119 dev_dbg(dev, "Read PDA returned %d\n", err);
123 if (!orinoco_cached_fw_get(priv, false)) {
124 err = request_firmware(&fw_entry, firmware, priv->dev);
127 dev_err(dev, "Cannot find firmware %s\n", firmware);
132 fw_entry = orinoco_cached_fw_get(priv, false);
134 hdr = (const struct orinoco_fw_header *) fw_entry->data;
136 fw_err = validate_fw(hdr, fw_entry->size);
138 dev_warn(dev, "Invalid firmware image detected (%s). "
139 "Aborting download\n", fw_err);
144 /* Enable aux port to allow programming */
145 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
146 dev_dbg(dev, "Program init returned %d\n", err);
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;
156 err = hermes_program(hw, first_block, end);
157 dev_dbg(dev, "Program returned %d\n", err);
161 /* Update production data */
162 first_block = (fw_entry->data +
163 le16_to_cpu(hdr->headersize) +
164 le32_to_cpu(hdr->pdr_offset));
166 err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
167 &pda[fw->pda_size / sizeof(*pda)]);
168 dev_dbg(dev, "Apply PDA returned %d\n", err);
172 /* Tell card we've finished */
173 err = hermesi_program_end(hw);
174 dev_dbg(dev, "Program end returned %d\n", err);
178 /* Check if we're running */
179 dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
182 /* If we requested the firmware, release it. */
183 if (!orinoco_cached_fw_get(priv, false))
184 release_firmware(fw_entry);
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.
197 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
198 const unsigned char *image, const void *end,
201 hermes_t *hw = &priv->hw;
203 const unsigned char *ptr;
204 const unsigned char *first_block;
206 /* Plug Data Area (PDA) */
209 /* Binary block begins after the 0x1A marker */
211 while (*ptr++ != TEXT_END);
214 /* Read the PDA from EEPROM */
216 pda = kzalloc(fw->pda_size, GFP_KERNEL);
220 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
225 /* Stop the firmware, so that it can be safely rewritten */
227 ret = priv->stop_fw(priv, 1);
232 /* Program the adapter with new firmware */
233 ret = hermes_program(hw, first_block, end);
237 /* Write the PDA to the adapter */
239 size_t len = hermes_blocks_length(first_block, end);
240 ptr = first_block + len;
241 ret = hermes_apply_pda(hw, ptr, end, pda,
242 &pda[fw->pda_size / sizeof(*pda)]);
248 /* Run the firmware */
250 ret = priv->stop_fw(priv, 0);
255 /* Reset hermes chip and make sure it responds */
256 ret = hermes_init(hw);
258 /* hermes_reset() should return 0 with the secondary firmware */
259 if (secondary && ret != 0)
262 /* And this should work with any firmware */
263 if (!hermes_present(hw))
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.
279 symbol_dl_firmware(struct orinoco_private *priv,
280 const struct fw_info *fw)
282 struct device *dev = priv->dev;
284 const struct firmware *fw_entry;
286 if (!orinoco_cached_fw_get(priv, true)) {
287 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
288 dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
292 fw_entry = orinoco_cached_fw_get(priv, true);
294 /* Load primary firmware */
295 ret = symbol_dl_image(priv, fw, fw_entry->data,
296 fw_entry->data + fw_entry->size, 0);
298 if (!orinoco_cached_fw_get(priv, true))
299 release_firmware(fw_entry);
301 dev_err(dev, "Primary firmware download failed\n");
305 if (!orinoco_cached_fw_get(priv, false)) {
306 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
307 dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
311 fw_entry = orinoco_cached_fw_get(priv, false);
313 /* Load secondary firmware */
314 ret = symbol_dl_image(priv, fw, fw_entry->data,
315 fw_entry->data + fw_entry->size, 1);
316 if (!orinoco_cached_fw_get(priv, false))
317 release_firmware(fw_entry);
319 dev_err(dev, "Secondary firmware download failed\n");
325 int orinoco_download(struct orinoco_private *priv)
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);
336 case FIRMWARE_TYPE_SYMBOL:
337 err = symbol_dl_firmware(priv,
338 &orinoco_fw[priv->firmware_type]);
340 case FIRMWARE_TYPE_INTERSIL:
343 /* TODO: if we fail we probably need to reinitialise
349 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
350 void orinoco_cache_fw(struct orinoco_private *priv, int ap)
352 const struct firmware *fw_entry = NULL;
356 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
358 fw = orinoco_fw[priv->firmware_type].ap_fw;
360 fw = orinoco_fw[priv->firmware_type].sta_fw;
363 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
364 priv->cached_pri_fw = fw_entry;
368 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
369 priv->cached_fw = fw_entry;
373 void orinoco_uncache_fw(struct orinoco_private *priv)
375 if (priv->cached_pri_fw)
376 release_firmware(priv->cached_pri_fw);
378 release_firmware(priv->cached_fw);
380 priv->cached_pri_fw = NULL;
381 priv->cached_fw = NULL;