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 }
31 MODULE_FIRMWARE("agere_sta_fw.bin");
32 MODULE_FIRMWARE("agere_ap_fw.bin");
33 MODULE_FIRMWARE("prism_sta_fw.bin");
34 MODULE_FIRMWARE("prism_ap_fw.bin");
35 MODULE_FIRMWARE("symbol_sp24t_prim_fw");
36 MODULE_FIRMWARE("symbol_sp24t_sec_fw");
38 /* Structure used to access fields in FW
39 * Make sure LE decoding macros are used
41 struct 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));
53 /* Check the range of various header entries. Return a pointer to a
54 * description of the problem, or NULL if everything checks out. */
55 static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
59 if (len < sizeof(*hdr))
60 return "image too small";
61 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
62 return "format not recognised";
64 hdrsize = le16_to_cpu(hdr->headersize);
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";
76 /* TODO: consider adding a checksum or CRC to the firmware format */
80 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
81 static inline const struct firmware *
82 orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
85 return priv->cached_pri_fw;
87 return priv->cached_fw;
90 #define orinoco_cached_fw_get(priv, primary) (NULL)
93 /* Download either STA or AP firmware into the card. */
95 orinoco_dl_firmware(struct orinoco_private *priv,
96 const struct fw_info *fw,
99 /* Plug Data Area (PDA) */
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;
107 const char *firmware;
109 struct device *dev = priv->dev;
112 pda = kzalloc(fw->pda_size, GFP_KERNEL);
117 firmware = fw->ap_fw;
119 firmware = fw->sta_fw;
121 dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
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);
129 if (!orinoco_cached_fw_get(priv, false)) {
130 err = request_firmware(&fw_entry, firmware, priv->dev);
133 dev_err(dev, "Cannot find firmware %s\n", firmware);
138 fw_entry = orinoco_cached_fw_get(priv, false);
140 hdr = (const struct orinoco_fw_header *) fw_entry->data;
142 fw_err = validate_fw(hdr, fw_entry->size);
144 dev_warn(dev, "Invalid firmware image detected (%s). "
145 "Aborting download\n", fw_err);
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);
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;
162 err = hermes_program(hw, first_block, end);
163 dev_dbg(dev, "Program returned %d\n", err);
167 /* Update production data */
168 first_block = (fw_entry->data +
169 le16_to_cpu(hdr->headersize) +
170 le32_to_cpu(hdr->pdr_offset));
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);
178 /* Tell card we've finished */
179 err = hermesi_program_end(hw);
180 dev_dbg(dev, "Program end returned %d\n", err);
184 /* Check if we're running */
185 dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
188 /* If we requested the firmware, release it. */
189 if (!orinoco_cached_fw_get(priv, false))
190 release_firmware(fw_entry);
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.
203 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
204 const unsigned char *image, const void *end,
207 hermes_t *hw = &priv->hw;
209 const unsigned char *ptr;
210 const unsigned char *first_block;
212 /* Plug Data Area (PDA) */
215 /* Binary block begins after the 0x1A marker */
217 while (*ptr++ != TEXT_END);
220 /* Read the PDA from EEPROM */
222 pda = kzalloc(fw->pda_size, GFP_KERNEL);
226 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
231 /* Stop the firmware, so that it can be safely rewritten */
233 ret = priv->stop_fw(priv, 1);
238 /* Program the adapter with new firmware */
239 ret = hermes_program(hw, first_block, end);
243 /* Write the PDA to the adapter */
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)]);
254 /* Run the firmware */
256 ret = priv->stop_fw(priv, 0);
261 /* Reset hermes chip and make sure it responds */
262 ret = hermes_init(hw);
264 /* hermes_reset() should return 0 with the secondary firmware */
265 if (secondary && ret != 0)
268 /* And this should work with any firmware */
269 if (!hermes_present(hw))
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.
285 symbol_dl_firmware(struct orinoco_private *priv,
286 const struct fw_info *fw)
288 struct device *dev = priv->dev;
290 const struct firmware *fw_entry;
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);
298 fw_entry = orinoco_cached_fw_get(priv, true);
300 /* Load primary firmware */
301 ret = symbol_dl_image(priv, fw, fw_entry->data,
302 fw_entry->data + fw_entry->size, 0);
304 if (!orinoco_cached_fw_get(priv, true))
305 release_firmware(fw_entry);
307 dev_err(dev, "Primary firmware download failed\n");
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);
317 fw_entry = orinoco_cached_fw_get(priv, false);
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);
325 dev_err(dev, "Secondary firmware download failed\n");
331 int orinoco_download(struct orinoco_private *priv)
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);
342 case FIRMWARE_TYPE_SYMBOL:
343 err = symbol_dl_firmware(priv,
344 &orinoco_fw[priv->firmware_type]);
346 case FIRMWARE_TYPE_INTERSIL:
349 /* TODO: if we fail we probably need to reinitialise
355 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
356 void orinoco_cache_fw(struct orinoco_private *priv, int ap)
358 const struct firmware *fw_entry = NULL;
362 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
364 fw = orinoco_fw[priv->firmware_type].ap_fw;
366 fw = orinoco_fw[priv->firmware_type].sta_fw;
369 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
370 priv->cached_pri_fw = fw_entry;
374 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
375 priv->cached_fw = fw_entry;
379 void orinoco_uncache_fw(struct orinoco_private *priv)
381 if (priv->cached_pri_fw)
382 release_firmware(priv->cached_pri_fw);
384 release_firmware(priv->cached_fw);
386 priv->cached_pri_fw = NULL;
387 priv->cached_fw = NULL;