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 /* Download either STA or AP firmware into the card. */
48 orinoco_dl_firmware(struct orinoco_private *priv,
49 const struct fw_info *fw,
52 /* Plug Data Area (PDA) */
55 hermes_t *hw = &priv->hw;
56 const struct firmware *fw_entry;
57 const struct orinoco_fw_header *hdr;
58 const unsigned char *first_block;
59 const unsigned char *end;
61 struct net_device *dev = priv->ndev;
64 pda = kzalloc(fw->pda_size, GFP_KERNEL);
71 firmware = fw->sta_fw;
73 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
76 /* Read current plug data */
77 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
78 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
82 if (!priv->cached_fw) {
83 err = request_firmware(&fw_entry, firmware, priv->dev);
86 printk(KERN_ERR "%s: Cannot find firmware %s\n",
92 fw_entry = priv->cached_fw;
94 hdr = (const struct orinoco_fw_header *) fw_entry->data;
96 /* Enable aux port to allow programming */
97 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
98 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
103 first_block = (fw_entry->data +
104 le16_to_cpu(hdr->headersize) +
105 le32_to_cpu(hdr->block_offset));
106 end = fw_entry->data + fw_entry->size;
108 err = hermes_program(hw, first_block, end);
109 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
113 /* Update production data */
114 first_block = (fw_entry->data +
115 le16_to_cpu(hdr->headersize) +
116 le32_to_cpu(hdr->pdr_offset));
118 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
119 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
123 /* Tell card we've finished */
124 err = hermesi_program_end(hw);
125 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
129 /* Check if we're running */
130 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
131 dev->name, hermes_present(hw));
134 /* If we requested the firmware, release it. */
135 if (!priv->cached_fw)
136 release_firmware(fw_entry);
144 * Process a firmware image - stop the card, load the firmware, reset
145 * the card and make sure it responds. For the secondary firmware take
146 * care of the PDA - read it and then write it on top of the firmware.
149 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
150 const unsigned char *image, const unsigned char *end,
153 hermes_t *hw = &priv->hw;
155 const unsigned char *ptr;
156 const unsigned char *first_block;
158 /* Plug Data Area (PDA) */
161 /* Binary block begins after the 0x1A marker */
163 while (*ptr++ != TEXT_END);
166 /* Read the PDA from EEPROM */
168 pda = kzalloc(fw->pda_size, GFP_KERNEL);
172 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
177 /* Stop the firmware, so that it can be safely rewritten */
179 ret = priv->stop_fw(priv, 1);
184 /* Program the adapter with new firmware */
185 ret = hermes_program(hw, first_block, end);
189 /* Write the PDA to the adapter */
191 size_t len = hermes_blocks_length(first_block);
192 ptr = first_block + len;
193 ret = hermes_apply_pda(hw, ptr, pda);
199 /* Run the firmware */
201 ret = priv->stop_fw(priv, 0);
206 /* Reset hermes chip and make sure it responds */
207 ret = hermes_init(hw);
209 /* hermes_reset() should return 0 with the secondary firmware */
210 if (secondary && ret != 0)
213 /* And this should work with any firmware */
214 if (!hermes_present(hw))
226 * Download the firmware into the card, this also does a PCMCIA soft
227 * reset on the card, to make sure it's in a sane state.
230 symbol_dl_firmware(struct orinoco_private *priv,
231 const struct fw_info *fw)
233 struct net_device *dev = priv->ndev;
235 const struct firmware *fw_entry;
237 if (!priv->cached_pri_fw) {
238 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
239 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
240 dev->name, fw->pri_fw);
244 fw_entry = priv->cached_pri_fw;
246 /* Load primary firmware */
247 ret = symbol_dl_image(priv, fw, fw_entry->data,
248 fw_entry->data + fw_entry->size, 0);
250 if (!priv->cached_pri_fw)
251 release_firmware(fw_entry);
253 printk(KERN_ERR "%s: Primary firmware download failed\n",
258 if (!priv->cached_fw) {
259 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
260 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
261 dev->name, fw->sta_fw);
265 fw_entry = priv->cached_fw;
267 /* Load secondary firmware */
268 ret = symbol_dl_image(priv, fw, fw_entry->data,
269 fw_entry->data + fw_entry->size, 1);
270 if (!priv->cached_fw)
271 release_firmware(fw_entry);
273 printk(KERN_ERR "%s: Secondary firmware download failed\n",
280 int orinoco_download(struct orinoco_private *priv)
283 /* Reload firmware */
284 switch (priv->firmware_type) {
285 case FIRMWARE_TYPE_AGERE:
286 /* case FIRMWARE_TYPE_INTERSIL: */
287 err = orinoco_dl_firmware(priv,
288 &orinoco_fw[priv->firmware_type], 0);
291 case FIRMWARE_TYPE_SYMBOL:
292 err = symbol_dl_firmware(priv,
293 &orinoco_fw[priv->firmware_type]);
295 case FIRMWARE_TYPE_INTERSIL:
298 /* TODO: if we fail we probably need to reinitialise
304 void orinoco_cache_fw(struct orinoco_private *priv, int ap)
306 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
307 const struct firmware *fw_entry = NULL;
311 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
313 fw = orinoco_fw[priv->firmware_type].ap_fw;
315 fw = orinoco_fw[priv->firmware_type].sta_fw;
318 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
319 priv->cached_pri_fw = fw_entry;
323 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
324 priv->cached_fw = fw_entry;
329 void orinoco_uncache_fw(struct orinoco_private *priv)
331 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
332 if (priv->cached_pri_fw)
333 release_firmware(priv->cached_pri_fw);
335 release_firmware(priv->cached_fw);
337 priv->cached_pri_fw = NULL;
338 priv->cached_fw = NULL;