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>
9 #include <linux/module.h>
12 #include "hermes_dld.h"
17 /* End markers (for Symbol firmware only) */
18 #define TEXT_END 0x1A /* End of text header */
28 static const struct fw_info orinoco_fw[] = {
29 { NULL, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/", 0x00390000, 1000 },
30 { NULL, "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/", 0, 1024 },
31 { "/*(DEBLOBBED)*/", "/*(DEBLOBBED)*/", NULL, 0x00003100, 512 }
35 /* Structure used to access fields in FW
36 * Make sure LE decoding macros are used
38 struct orinoco_fw_header {
39 char hdr_vers[6]; /* ASCII string for header version */
40 __le16 headersize; /* Total length of header */
41 __le32 entry_point; /* NIC entry point */
42 __le32 blocks; /* Number of blocks to program */
43 __le32 block_offset; /* Offset of block data from eof header */
44 __le32 pdr_offset; /* Offset to PDR data from eof header */
45 __le32 pri_offset; /* Offset to primary plug data */
46 __le32 compat_offset; /* Offset to compatibility data*/
47 char signature[0]; /* FW signature length headersize-20 */
50 /* Check the range of various header entries. Return a pointer to a
51 * description of the problem, or NULL if everything checks out. */
52 static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
56 if (len < sizeof(*hdr))
57 return "image too small";
58 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
59 return "format not recognised";
61 hdrsize = le16_to_cpu(hdr->headersize);
63 return "bad headersize";
64 if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
65 return "bad block offset";
66 if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
67 return "bad PDR offset";
68 if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
69 return "bad PRI offset";
70 if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
71 return "bad compat offset";
73 /* TODO: consider adding a checksum or CRC to the firmware format */
77 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
78 static inline const struct firmware *
79 orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
82 return priv->cached_pri_fw;
84 return priv->cached_fw;
87 #define orinoco_cached_fw_get(priv, primary) (NULL)
90 /* Download either STA or AP firmware into the card. */
92 orinoco_dl_firmware(struct orinoco_private *priv,
93 const struct fw_info *fw,
96 /* Plug Data Area (PDA) */
99 struct hermes *hw = &priv->hw;
100 const struct firmware *fw_entry;
101 const struct orinoco_fw_header *hdr;
102 const unsigned char *first_block;
104 const char *firmware;
106 struct device *dev = priv->dev;
109 pda = kzalloc(fw->pda_size, GFP_KERNEL);
114 firmware = fw->ap_fw;
116 firmware = fw->sta_fw;
118 dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
120 /* Read current plug data */
121 err = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
122 dev_dbg(dev, "Read PDA returned %d\n", err);
126 if (!orinoco_cached_fw_get(priv, false)) {
127 err = reject_firmware(&fw_entry, firmware, priv->dev);
130 dev_err(dev, "Cannot find firmware %s\n", firmware);
135 fw_entry = orinoco_cached_fw_get(priv, false);
137 hdr = (const struct orinoco_fw_header *) fw_entry->data;
139 fw_err = validate_fw(hdr, fw_entry->size);
141 dev_warn(dev, "Invalid firmware image detected (%s). "
142 "Aborting download\n", fw_err);
147 /* Enable aux port to allow programming */
148 err = hw->ops->program_init(hw, le32_to_cpu(hdr->entry_point));
149 dev_dbg(dev, "Program init returned %d\n", err);
154 first_block = (fw_entry->data +
155 le16_to_cpu(hdr->headersize) +
156 le32_to_cpu(hdr->block_offset));
157 end = fw_entry->data + fw_entry->size;
159 err = hermes_program(hw, first_block, end);
160 dev_dbg(dev, "Program returned %d\n", err);
164 /* Update production data */
165 first_block = (fw_entry->data +
166 le16_to_cpu(hdr->headersize) +
167 le32_to_cpu(hdr->pdr_offset));
169 err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
170 &pda[fw->pda_size / sizeof(*pda)]);
171 dev_dbg(dev, "Apply PDA returned %d\n", err);
175 /* Tell card we've finished */
176 err = hw->ops->program_end(hw);
177 dev_dbg(dev, "Program end returned %d\n", err);
181 /* Check if we're running */
182 dev_dbg(dev, "hermes_present returned %d\n", 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 struct hermes *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 = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
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 = hw->ops->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 device *dev = priv->dev;
287 const struct firmware *fw_entry;
289 if (!orinoco_cached_fw_get(priv, true)) {
290 if (reject_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
291 dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
295 fw_entry = orinoco_cached_fw_get(priv, true);
297 /* Load primary firmware */
298 ret = symbol_dl_image(priv, fw, fw_entry->data,
299 fw_entry->data + fw_entry->size, 0);
301 if (!orinoco_cached_fw_get(priv, true))
302 release_firmware(fw_entry);
304 dev_err(dev, "Primary firmware download failed\n");
308 if (!orinoco_cached_fw_get(priv, false)) {
309 if (reject_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
310 dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
314 fw_entry = orinoco_cached_fw_get(priv, false);
316 /* Load secondary firmware */
317 ret = symbol_dl_image(priv, fw, fw_entry->data,
318 fw_entry->data + fw_entry->size, 1);
319 if (!orinoco_cached_fw_get(priv, false))
320 release_firmware(fw_entry);
322 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 release_firmware(priv->cached_pri_fw);
378 release_firmware(priv->cached_fw);
379 priv->cached_pri_fw = NULL;
380 priv->cached_fw = NULL;