1 /* Firmware file reading and download helpers
3 * See copyright notice in main.c
5 #include <linux/kernel.h>
6 #include <linux/slab.h>
7 #include <linux/firmware.h>
8 #include <linux/device.h>
11 #include "hermes_dld.h"
16 /* End markers (for Symbol firmware only) */
17 #define TEXT_END 0x1A /* End of text header */
27 static const struct fw_info orinoco_fw[] = {
28 { NULL, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/", 0x00390000, 1000 },
29 { NULL, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/", 0, 1024 },
30 { "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/", NULL, 0x00003100, 512 }
34 /* Structure used to access fields in FW
35 * Make sure LE decoding macros are used
37 struct orinoco_fw_header {
38 char hdr_vers[6]; /* ASCII string for header version */
39 __le16 headersize; /* Total length of header */
40 __le32 entry_point; /* NIC entry point */
41 __le32 blocks; /* Number of blocks to program */
42 __le32 block_offset; /* Offset of block data from eof header */
43 __le32 pdr_offset; /* Offset to PDR data from eof header */
44 __le32 pri_offset; /* Offset to primary plug data */
45 __le32 compat_offset; /* Offset to compatibility data*/
46 char signature[0]; /* FW signature length headersize-20 */
49 /* Check the range of various header entries. Return a pointer to a
50 * description of the problem, or NULL if everything checks out. */
51 static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
55 if (len < sizeof(*hdr))
56 return "image too small";
57 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
58 return "format not recognised";
60 hdrsize = le16_to_cpu(hdr->headersize);
62 return "bad headersize";
63 if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
64 return "bad block offset";
65 if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
66 return "bad PDR offset";
67 if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
68 return "bad PRI offset";
69 if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
70 return "bad compat offset";
72 /* TODO: consider adding a checksum or CRC to the firmware format */
76 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
77 static inline const struct firmware *
78 orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
81 return priv->cached_pri_fw;
83 return priv->cached_fw;
86 #define orinoco_cached_fw_get(priv, primary) (NULL)
89 /* Download either STA or AP firmware into the card. */
91 orinoco_dl_firmware(struct orinoco_private *priv,
92 const struct fw_info *fw,
95 /* Plug Data Area (PDA) */
98 hermes_t *hw = &priv->hw;
99 const struct firmware *fw_entry;
100 const struct orinoco_fw_header *hdr;
101 const unsigned char *first_block;
103 const char *firmware;
105 struct device *dev = priv->dev;
108 pda = kzalloc(fw->pda_size, GFP_KERNEL);
113 firmware = fw->ap_fw;
115 firmware = fw->sta_fw;
117 dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
119 /* Read current plug data */
120 err = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
121 dev_dbg(dev, "Read PDA returned %d\n", err);
125 if (!orinoco_cached_fw_get(priv, false)) {
126 err = reject_firmware(&fw_entry, firmware, priv->dev);
129 dev_err(dev, "Cannot find firmware %s\n", firmware);
134 fw_entry = orinoco_cached_fw_get(priv, false);
136 hdr = (const struct orinoco_fw_header *) fw_entry->data;
138 fw_err = validate_fw(hdr, fw_entry->size);
140 dev_warn(dev, "Invalid firmware image detected (%s). "
141 "Aborting download\n", fw_err);
146 /* Enable aux port to allow programming */
147 err = hw->ops->program_init(hw, le32_to_cpu(hdr->entry_point));
148 dev_dbg(dev, "Program init returned %d\n", 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 dev_dbg(dev, "Program returned %d\n", 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 dev_dbg(dev, "Apply PDA returned %d\n", err);
174 /* Tell card we've finished */
175 err = hw->ops->program_end(hw);
176 dev_dbg(dev, "Program end returned %d\n", err);
180 /* Check if we're running */
181 dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
184 /* If we requested the firmware, release it. */
185 if (!orinoco_cached_fw_get(priv, false))
186 release_firmware(fw_entry);
194 * Process a firmware image - stop the card, load the firmware, reset
195 * the card and make sure it responds. For the secondary firmware take
196 * care of the PDA - read it and then write it on top of the firmware.
199 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
200 const unsigned char *image, const void *end,
203 hermes_t *hw = &priv->hw;
205 const unsigned char *ptr;
206 const unsigned char *first_block;
208 /* Plug Data Area (PDA) */
211 /* Binary block begins after the 0x1A marker */
213 while (*ptr++ != TEXT_END);
216 /* Read the PDA from EEPROM */
218 pda = kzalloc(fw->pda_size, GFP_KERNEL);
222 ret = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
227 /* Stop the firmware, so that it can be safely rewritten */
229 ret = priv->stop_fw(priv, 1);
234 /* Program the adapter with new firmware */
235 ret = hermes_program(hw, first_block, end);
239 /* Write the PDA to the adapter */
241 size_t len = hermes_blocks_length(first_block, end);
242 ptr = first_block + len;
243 ret = hermes_apply_pda(hw, ptr, end, pda,
244 &pda[fw->pda_size / sizeof(*pda)]);
250 /* Run the firmware */
252 ret = priv->stop_fw(priv, 0);
257 /* Reset hermes chip and make sure it responds */
258 ret = hw->ops->init(hw);
260 /* hermes_reset() should return 0 with the secondary firmware */
261 if (secondary && ret != 0)
264 /* And this should work with any firmware */
265 if (!hermes_present(hw))
277 * Download the firmware into the card, this also does a PCMCIA soft
278 * reset on the card, to make sure it's in a sane state.
281 symbol_dl_firmware(struct orinoco_private *priv,
282 const struct fw_info *fw)
284 struct device *dev = priv->dev;
286 const struct firmware *fw_entry;
288 if (!orinoco_cached_fw_get(priv, true)) {
289 if (reject_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
290 dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
294 fw_entry = orinoco_cached_fw_get(priv, true);
296 /* Load primary firmware */
297 ret = symbol_dl_image(priv, fw, fw_entry->data,
298 fw_entry->data + fw_entry->size, 0);
300 if (!orinoco_cached_fw_get(priv, true))
301 release_firmware(fw_entry);
303 dev_err(dev, "Primary firmware download failed\n");
307 if (!orinoco_cached_fw_get(priv, false)) {
308 if (reject_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
309 dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
313 fw_entry = orinoco_cached_fw_get(priv, false);
315 /* Load secondary firmware */
316 ret = symbol_dl_image(priv, fw, fw_entry->data,
317 fw_entry->data + fw_entry->size, 1);
318 if (!orinoco_cached_fw_get(priv, false))
319 release_firmware(fw_entry);
321 dev_err(dev, "Secondary firmware download failed\n");
327 int orinoco_download(struct orinoco_private *priv)
330 /* Reload firmware */
331 switch (priv->firmware_type) {
332 case FIRMWARE_TYPE_AGERE:
333 /* case FIRMWARE_TYPE_INTERSIL: */
334 err = orinoco_dl_firmware(priv,
335 &orinoco_fw[priv->firmware_type], 0);
338 case FIRMWARE_TYPE_SYMBOL:
339 err = symbol_dl_firmware(priv,
340 &orinoco_fw[priv->firmware_type]);
342 case FIRMWARE_TYPE_INTERSIL:
345 /* TODO: if we fail we probably need to reinitialise
351 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
352 void orinoco_cache_fw(struct orinoco_private *priv, int ap)
354 const struct firmware *fw_entry = NULL;
358 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
360 fw = orinoco_fw[priv->firmware_type].ap_fw;
362 fw = orinoco_fw[priv->firmware_type].sta_fw;
365 if (reject_firmware(&fw_entry, pri_fw, priv->dev) == 0)
366 priv->cached_pri_fw = fw_entry;
370 if (reject_firmware(&fw_entry, fw, priv->dev) == 0)
371 priv->cached_fw = fw_entry;
375 void orinoco_uncache_fw(struct orinoco_private *priv)
377 if (priv->cached_pri_fw)
378 release_firmware(priv->cached_pri_fw);
380 release_firmware(priv->cached_fw);
382 priv->cached_pri_fw = NULL;
383 priv->cached_fw = NULL;