blob: e7abb45d6753bc5cad4aba31604d529f7caf95ee [file] [log] [blame]
David Kilroy37a2e562009-02-04 23:05:52 +00001/* Firmware file reading and download helpers
2 *
3 * See copyright notice in main.c
4 */
5#include <linux/kernel.h>
6#include <linux/firmware.h>
7
8#include "hermes.h"
9#include "hermes_dld.h"
10#include "orinoco.h"
11
12#include "fw.h"
13
14/* End markers (for Symbol firmware only) */
15#define TEXT_END 0x1A /* End of text header */
16
17struct fw_info {
18 char *pri_fw;
19 char *sta_fw;
20 char *ap_fw;
21 u32 pda_addr;
22 u16 pda_size;
23};
24
Tobias Klauserf733ded2009-02-09 23:06:53 +010025static const struct fw_info orinoco_fw[] = {
David Kilroy37a2e562009-02-04 23:05:52 +000026 { 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 }
29};
30
31/* Structure used to access fields in FW
32 * Make sure LE decoding macros are used
33 */
34struct 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));
45
David Kilroy7e578112009-02-21 16:52:53 +000046/* Check the range of various header entries. Return a pointer to a
47 * description of the problem, or NULL if everything checks out. */
48static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
49{
50 u16 hdrsize;
51
52 if (len < sizeof(*hdr))
53 return "image too small";
54 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
55 return "format not recognised";
56
57 hdrsize = le16_to_cpu(hdr->headersize);
58 if (hdrsize > len)
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";
68
69 /* TODO: consider adding a checksum or CRC to the firmware format */
70 return NULL;
71}
72
David Kilroy37a2e562009-02-04 23:05:52 +000073/* Download either STA or AP firmware into the card. */
74static int
75orinoco_dl_firmware(struct orinoco_private *priv,
76 const struct fw_info *fw,
77 int ap)
78{
79 /* Plug Data Area (PDA) */
80 __le16 *pda;
81
82 hermes_t *hw = &priv->hw;
83 const struct firmware *fw_entry;
84 const struct orinoco_fw_header *hdr;
85 const unsigned char *first_block;
David Kilroy3faa19c2009-02-21 16:52:54 +000086 const void *end;
David Kilroy37a2e562009-02-04 23:05:52 +000087 const char *firmware;
David Kilroy7e578112009-02-21 16:52:53 +000088 const char *fw_err;
David Kilroy37a2e562009-02-04 23:05:52 +000089 struct net_device *dev = priv->ndev;
90 int err = 0;
91
92 pda = kzalloc(fw->pda_size, GFP_KERNEL);
93 if (!pda)
94 return -ENOMEM;
95
96 if (ap)
97 firmware = fw->ap_fw;
98 else
99 firmware = fw->sta_fw;
100
101 printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
102 dev->name, firmware);
103
104 /* Read current plug data */
105 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
106 printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
107 if (err)
108 goto free;
109
110 if (!priv->cached_fw) {
111 err = request_firmware(&fw_entry, firmware, priv->dev);
112
113 if (err) {
114 printk(KERN_ERR "%s: Cannot find firmware %s\n",
115 dev->name, firmware);
116 err = -ENOENT;
117 goto free;
118 }
119 } else
120 fw_entry = priv->cached_fw;
121
122 hdr = (const struct orinoco_fw_header *) fw_entry->data;
123
David Kilroy7e578112009-02-21 16:52:53 +0000124 fw_err = validate_fw(hdr, fw_entry->size);
125 if (fw_err) {
126 printk(KERN_WARNING "%s: Invalid firmware image detected (%s). "
127 "Aborting download\n",
128 dev->name, fw_err);
129 err = -EINVAL;
130 goto abort;
131 }
132
David Kilroy37a2e562009-02-04 23:05:52 +0000133 /* Enable aux port to allow programming */
134 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
135 printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
136 if (err != 0)
137 goto abort;
138
139 /* Program data */
140 first_block = (fw_entry->data +
141 le16_to_cpu(hdr->headersize) +
142 le32_to_cpu(hdr->block_offset));
143 end = fw_entry->data + fw_entry->size;
144
145 err = hermes_program(hw, first_block, end);
146 printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
147 if (err != 0)
148 goto abort;
149
150 /* Update production data */
151 first_block = (fw_entry->data +
152 le16_to_cpu(hdr->headersize) +
153 le32_to_cpu(hdr->pdr_offset));
154
David Kilroy3faa19c2009-02-21 16:52:54 +0000155 err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
156 &pda[fw->pda_size / sizeof(*pda)]);
David Kilroy37a2e562009-02-04 23:05:52 +0000157 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
158 if (err)
159 goto abort;
160
161 /* Tell card we've finished */
162 err = hermesi_program_end(hw);
163 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
164 if (err != 0)
165 goto abort;
166
167 /* Check if we're running */
168 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
169 dev->name, hermes_present(hw));
170
171abort:
172 /* If we requested the firmware, release it. */
173 if (!priv->cached_fw)
174 release_firmware(fw_entry);
175
176free:
177 kfree(pda);
178 return err;
179}
180
181/*
182 * Process a firmware image - stop the card, load the firmware, reset
183 * the card and make sure it responds. For the secondary firmware take
184 * care of the PDA - read it and then write it on top of the firmware.
185 */
186static int
187symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
David Kilroy3faa19c2009-02-21 16:52:54 +0000188 const unsigned char *image, const void *end,
David Kilroy37a2e562009-02-04 23:05:52 +0000189 int secondary)
190{
191 hermes_t *hw = &priv->hw;
192 int ret = 0;
193 const unsigned char *ptr;
194 const unsigned char *first_block;
195
196 /* Plug Data Area (PDA) */
197 __le16 *pda = NULL;
198
199 /* Binary block begins after the 0x1A marker */
200 ptr = image;
201 while (*ptr++ != TEXT_END);
202 first_block = ptr;
203
204 /* Read the PDA from EEPROM */
205 if (secondary) {
206 pda = kzalloc(fw->pda_size, GFP_KERNEL);
207 if (!pda)
208 return -ENOMEM;
209
210 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
211 if (ret)
212 goto free;
213 }
214
215 /* Stop the firmware, so that it can be safely rewritten */
216 if (priv->stop_fw) {
217 ret = priv->stop_fw(priv, 1);
218 if (ret)
219 goto free;
220 }
221
222 /* Program the adapter with new firmware */
223 ret = hermes_program(hw, first_block, end);
224 if (ret)
225 goto free;
226
227 /* Write the PDA to the adapter */
228 if (secondary) {
David Kilroy3faa19c2009-02-21 16:52:54 +0000229 size_t len = hermes_blocks_length(first_block, end);
David Kilroy37a2e562009-02-04 23:05:52 +0000230 ptr = first_block + len;
David Kilroy3faa19c2009-02-21 16:52:54 +0000231 ret = hermes_apply_pda(hw, ptr, end, pda,
232 &pda[fw->pda_size / sizeof(*pda)]);
David Kilroy37a2e562009-02-04 23:05:52 +0000233 kfree(pda);
234 if (ret)
235 return ret;
236 }
237
238 /* Run the firmware */
239 if (priv->stop_fw) {
240 ret = priv->stop_fw(priv, 0);
241 if (ret)
242 return ret;
243 }
244
245 /* Reset hermes chip and make sure it responds */
246 ret = hermes_init(hw);
247
248 /* hermes_reset() should return 0 with the secondary firmware */
249 if (secondary && ret != 0)
250 return -ENODEV;
251
252 /* And this should work with any firmware */
253 if (!hermes_present(hw))
254 return -ENODEV;
255
256 return 0;
257
258free:
259 kfree(pda);
260 return ret;
261}
262
263
264/*
265 * Download the firmware into the card, this also does a PCMCIA soft
266 * reset on the card, to make sure it's in a sane state.
267 */
268static int
269symbol_dl_firmware(struct orinoco_private *priv,
270 const struct fw_info *fw)
271{
272 struct net_device *dev = priv->ndev;
273 int ret;
274 const struct firmware *fw_entry;
275
276 if (!priv->cached_pri_fw) {
277 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
278 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
279 dev->name, fw->pri_fw);
280 return -ENOENT;
281 }
282 } else
283 fw_entry = priv->cached_pri_fw;
284
285 /* Load primary firmware */
286 ret = symbol_dl_image(priv, fw, fw_entry->data,
287 fw_entry->data + fw_entry->size, 0);
288
289 if (!priv->cached_pri_fw)
290 release_firmware(fw_entry);
291 if (ret) {
292 printk(KERN_ERR "%s: Primary firmware download failed\n",
293 dev->name);
294 return ret;
295 }
296
297 if (!priv->cached_fw) {
298 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
299 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
300 dev->name, fw->sta_fw);
301 return -ENOENT;
302 }
303 } else
304 fw_entry = priv->cached_fw;
305
306 /* Load secondary firmware */
307 ret = symbol_dl_image(priv, fw, fw_entry->data,
308 fw_entry->data + fw_entry->size, 1);
309 if (!priv->cached_fw)
310 release_firmware(fw_entry);
311 if (ret) {
312 printk(KERN_ERR "%s: Secondary firmware download failed\n",
313 dev->name);
314 }
315
316 return ret;
317}
318
319int orinoco_download(struct orinoco_private *priv)
320{
321 int err = 0;
322 /* Reload firmware */
323 switch (priv->firmware_type) {
324 case FIRMWARE_TYPE_AGERE:
325 /* case FIRMWARE_TYPE_INTERSIL: */
326 err = orinoco_dl_firmware(priv,
327 &orinoco_fw[priv->firmware_type], 0);
328 break;
329
330 case FIRMWARE_TYPE_SYMBOL:
331 err = symbol_dl_firmware(priv,
332 &orinoco_fw[priv->firmware_type]);
333 break;
334 case FIRMWARE_TYPE_INTERSIL:
335 break;
336 }
337 /* TODO: if we fail we probably need to reinitialise
338 * the driver */
339
340 return err;
341}
342
343void orinoco_cache_fw(struct orinoco_private *priv, int ap)
344{
345#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
346 const struct firmware *fw_entry = NULL;
347 const char *pri_fw;
348 const char *fw;
349
350 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
351 if (ap)
352 fw = orinoco_fw[priv->firmware_type].ap_fw;
353 else
354 fw = orinoco_fw[priv->firmware_type].sta_fw;
355
356 if (pri_fw) {
357 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
358 priv->cached_pri_fw = fw_entry;
359 }
360
361 if (fw) {
362 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
363 priv->cached_fw = fw_entry;
364 }
365#endif
366}
367
368void orinoco_uncache_fw(struct orinoco_private *priv)
369{
370#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
371 if (priv->cached_pri_fw)
372 release_firmware(priv->cached_pri_fw);
373 if (priv->cached_fw)
374 release_firmware(priv->cached_fw);
375
376 priv->cached_pri_fw = NULL;
377 priv->cached_fw = NULL;
378#endif
379}