1 /* Firmware file reading and download helpers
3 * See copyright notice in main.c
5 #include <linux/kernel.h>
6 #include <linux/firmware.h>
9 #include "hermes_dld.h"
14 /* End markers (for Symbol firmware only) */
15 #define TEXT_END 0x1A /* End of text header */
25 static const struct fw_info orinoco_fw[] = {
26 { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
27 { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
28 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
31 /* Structure used to access fields in FW
32 * Make sure LE decoding macros are used
34 struct orinoco_fw_header {
35 char hdr_vers[6]; /* ASCII string for header version */
36 __le16 headersize; /* Total length of header */
37 __le32 entry_point; /* NIC entry point */
38 __le32 blocks; /* Number of blocks to program */
39 __le32 block_offset; /* Offset of block data from eof header */
40 __le32 pdr_offset; /* Offset to PDR data from eof header */
41 __le32 pri_offset; /* Offset to primary plug data */
42 __le32 compat_offset; /* Offset to compatibility data*/
43 char signature[0]; /* FW signature length headersize-20 */
44 } __attribute__ ((packed));
46 /* Check the range of various header entries. Return a pointer to a
47 * description of the problem, or NULL if everything checks out. */
48 static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
52 if (len < sizeof(*hdr))
53 return "image too small";
54 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
55 return "format not recognised";
57 hdrsize = le16_to_cpu(hdr->headersize);
59 return "bad headersize";
60 if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
61 return "bad block offset";
62 if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
63 return "bad PDR offset";
64 if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
65 return "bad PRI offset";
66 if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
67 return "bad compat offset";
69 /* TODO: consider adding a checksum or CRC to the firmware format */
73 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
74 static inline const struct firmware *
75 orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
78 return priv->cached_pri_fw;
80 return priv->cached_fw;
83 #define orinoco_cached_fw_get(priv, primary) (NULL)
86 /* Download either STA or AP firmware into the card. */
88 orinoco_dl_firmware(struct orinoco_private *priv,
89 const struct fw_info *fw,
92 /* Plug Data Area (PDA) */
95 hermes_t *hw = &priv->hw;
96 const struct firmware *fw_entry;
97 const struct orinoco_fw_header *hdr;
98 const unsigned char *first_block;
100 const char *firmware;
102 struct net_device *dev = priv->ndev;
105 pda = kzalloc(fw->pda_size, GFP_KERNEL);
110 firmware = fw->ap_fw;
112 firmware = fw->sta_fw;
114 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
115 dev->name, firmware);
117 /* Read current plug data */
118 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
119 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
123 if (!orinoco_cached_fw_get(priv, false)) {
124 err = request_firmware(&fw_entry, firmware, priv->dev);
127 printk(KERN_ERR "%s: Cannot find firmware %s\n",
128 dev->name, firmware);
133 fw_entry = orinoco_cached_fw_get(priv, false);
135 hdr = (const struct orinoco_fw_header *) fw_entry->data;
137 fw_err = validate_fw(hdr, fw_entry->size);
139 printk(KERN_WARNING "%s: Invalid firmware image detected (%s). "
140 "Aborting download\n",
146 /* Enable aux port to allow programming */
147 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
148 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
153 first_block = (fw_entry->data +
154 le16_to_cpu(hdr->headersize) +
155 le32_to_cpu(hdr->block_offset));
156 end = fw_entry->data + fw_entry->size;
158 err = hermes_program(hw, first_block, end);
159 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
163 /* Update production data */
164 first_block = (fw_entry->data +
165 le16_to_cpu(hdr->headersize) +
166 le32_to_cpu(hdr->pdr_offset));
168 err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
169 &pda[fw->pda_size / sizeof(*pda)]);
170 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
174 /* Tell card we've finished */
175 err = hermesi_program_end(hw);
176 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
180 /* Check if we're running */
181 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
182 dev->name, hermes_present(hw));
185 /* If we requested the firmware, release it. */
186 if (!orinoco_cached_fw_get(priv, false))
187 release_firmware(fw_entry);
195 * Process a firmware image - stop the card, load the firmware, reset
196 * the card and make sure it responds. For the secondary firmware take
197 * care of the PDA - read it and then write it on top of the firmware.
200 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
201 const unsigned char *image, const void *end,
204 hermes_t *hw = &priv->hw;
206 const unsigned char *ptr;
207 const unsigned char *first_block;
209 /* Plug Data Area (PDA) */
212 /* Binary block begins after the 0x1A marker */
214 while (*ptr++ != TEXT_END);
217 /* Read the PDA from EEPROM */
219 pda = kzalloc(fw->pda_size, GFP_KERNEL);
223 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
228 /* Stop the firmware, so that it can be safely rewritten */
230 ret = priv->stop_fw(priv, 1);
235 /* Program the adapter with new firmware */
236 ret = hermes_program(hw, first_block, end);
240 /* Write the PDA to the adapter */
242 size_t len = hermes_blocks_length(first_block, end);
243 ptr = first_block + len;
244 ret = hermes_apply_pda(hw, ptr, end, pda,
245 &pda[fw->pda_size / sizeof(*pda)]);
251 /* Run the firmware */
253 ret = priv->stop_fw(priv, 0);
258 /* Reset hermes chip and make sure it responds */
259 ret = hermes_init(hw);
261 /* hermes_reset() should return 0 with the secondary firmware */
262 if (secondary && ret != 0)
265 /* And this should work with any firmware */
266 if (!hermes_present(hw))
278 * Download the firmware into the card, this also does a PCMCIA soft
279 * reset on the card, to make sure it's in a sane state.
282 symbol_dl_firmware(struct orinoco_private *priv,
283 const struct fw_info *fw)
285 struct net_device *dev = priv->ndev;
287 const struct firmware *fw_entry;
289 if (!orinoco_cached_fw_get(priv, true)) {
290 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
291 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
292 dev->name, fw->pri_fw);
296 fw_entry = orinoco_cached_fw_get(priv, true);
298 /* Load primary firmware */
299 ret = symbol_dl_image(priv, fw, fw_entry->data,
300 fw_entry->data + fw_entry->size, 0);
302 if (!orinoco_cached_fw_get(priv, true))
303 release_firmware(fw_entry);
305 printk(KERN_ERR "%s: Primary firmware download failed\n",
310 if (!orinoco_cached_fw_get(priv, false)) {
311 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
312 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
313 dev->name, 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 printk(KERN_ERR "%s: Secondary firmware download failed\n",
332 int orinoco_download(struct orinoco_private *priv)
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);
343 case FIRMWARE_TYPE_SYMBOL:
344 err = symbol_dl_firmware(priv,
345 &orinoco_fw[priv->firmware_type]);
347 case FIRMWARE_TYPE_INTERSIL:
350 /* TODO: if we fail we probably need to reinitialise
356 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
357 void orinoco_cache_fw(struct orinoco_private *priv, int ap)
359 const struct firmware *fw_entry = NULL;
363 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
365 fw = orinoco_fw[priv->firmware_type].ap_fw;
367 fw = orinoco_fw[priv->firmware_type].sta_fw;
370 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
371 priv->cached_pri_fw = fw_entry;
375 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
376 priv->cached_fw = fw_entry;
380 void orinoco_uncache_fw(struct orinoco_private *priv)
382 if (priv->cached_pri_fw)
383 release_firmware(priv->cached_pri_fw);
385 release_firmware(priv->cached_fw);
387 priv->cached_pri_fw = NULL;
388 priv->cached_fw = NULL;