blob: 9f163feb9362d3d06dda1d3c0a6c7686c47fb995 [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;
86 const unsigned char *end;
87 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
155 err = hermes_apply_pda_with_defaults(hw, first_block, pda);
156 printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
157 if (err)
158 goto abort;
159
160 /* Tell card we've finished */
161 err = hermesi_program_end(hw);
162 printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
163 if (err != 0)
164 goto abort;
165
166 /* Check if we're running */
167 printk(KERN_DEBUG "%s: hermes_present returned %d\n",
168 dev->name, hermes_present(hw));
169
170abort:
171 /* If we requested the firmware, release it. */
172 if (!priv->cached_fw)
173 release_firmware(fw_entry);
174
175free:
176 kfree(pda);
177 return err;
178}
179
180/*
181 * Process a firmware image - stop the card, load the firmware, reset
182 * the card and make sure it responds. For the secondary firmware take
183 * care of the PDA - read it and then write it on top of the firmware.
184 */
185static int
186symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
187 const unsigned char *image, const unsigned char *end,
188 int secondary)
189{
190 hermes_t *hw = &priv->hw;
191 int ret = 0;
192 const unsigned char *ptr;
193 const unsigned char *first_block;
194
195 /* Plug Data Area (PDA) */
196 __le16 *pda = NULL;
197
198 /* Binary block begins after the 0x1A marker */
199 ptr = image;
200 while (*ptr++ != TEXT_END);
201 first_block = ptr;
202
203 /* Read the PDA from EEPROM */
204 if (secondary) {
205 pda = kzalloc(fw->pda_size, GFP_KERNEL);
206 if (!pda)
207 return -ENOMEM;
208
209 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
210 if (ret)
211 goto free;
212 }
213
214 /* Stop the firmware, so that it can be safely rewritten */
215 if (priv->stop_fw) {
216 ret = priv->stop_fw(priv, 1);
217 if (ret)
218 goto free;
219 }
220
221 /* Program the adapter with new firmware */
222 ret = hermes_program(hw, first_block, end);
223 if (ret)
224 goto free;
225
226 /* Write the PDA to the adapter */
227 if (secondary) {
228 size_t len = hermes_blocks_length(first_block);
229 ptr = first_block + len;
230 ret = hermes_apply_pda(hw, ptr, pda);
231 kfree(pda);
232 if (ret)
233 return ret;
234 }
235
236 /* Run the firmware */
237 if (priv->stop_fw) {
238 ret = priv->stop_fw(priv, 0);
239 if (ret)
240 return ret;
241 }
242
243 /* Reset hermes chip and make sure it responds */
244 ret = hermes_init(hw);
245
246 /* hermes_reset() should return 0 with the secondary firmware */
247 if (secondary && ret != 0)
248 return -ENODEV;
249
250 /* And this should work with any firmware */
251 if (!hermes_present(hw))
252 return -ENODEV;
253
254 return 0;
255
256free:
257 kfree(pda);
258 return ret;
259}
260
261
262/*
263 * Download the firmware into the card, this also does a PCMCIA soft
264 * reset on the card, to make sure it's in a sane state.
265 */
266static int
267symbol_dl_firmware(struct orinoco_private *priv,
268 const struct fw_info *fw)
269{
270 struct net_device *dev = priv->ndev;
271 int ret;
272 const struct firmware *fw_entry;
273
274 if (!priv->cached_pri_fw) {
275 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
276 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
277 dev->name, fw->pri_fw);
278 return -ENOENT;
279 }
280 } else
281 fw_entry = priv->cached_pri_fw;
282
283 /* Load primary firmware */
284 ret = symbol_dl_image(priv, fw, fw_entry->data,
285 fw_entry->data + fw_entry->size, 0);
286
287 if (!priv->cached_pri_fw)
288 release_firmware(fw_entry);
289 if (ret) {
290 printk(KERN_ERR "%s: Primary firmware download failed\n",
291 dev->name);
292 return ret;
293 }
294
295 if (!priv->cached_fw) {
296 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
297 printk(KERN_ERR "%s: Cannot find firmware: %s\n",
298 dev->name, fw->sta_fw);
299 return -ENOENT;
300 }
301 } else
302 fw_entry = priv->cached_fw;
303
304 /* Load secondary firmware */
305 ret = symbol_dl_image(priv, fw, fw_entry->data,
306 fw_entry->data + fw_entry->size, 1);
307 if (!priv->cached_fw)
308 release_firmware(fw_entry);
309 if (ret) {
310 printk(KERN_ERR "%s: Secondary firmware download failed\n",
311 dev->name);
312 }
313
314 return ret;
315}
316
317int orinoco_download(struct orinoco_private *priv)
318{
319 int err = 0;
320 /* Reload firmware */
321 switch (priv->firmware_type) {
322 case FIRMWARE_TYPE_AGERE:
323 /* case FIRMWARE_TYPE_INTERSIL: */
324 err = orinoco_dl_firmware(priv,
325 &orinoco_fw[priv->firmware_type], 0);
326 break;
327
328 case FIRMWARE_TYPE_SYMBOL:
329 err = symbol_dl_firmware(priv,
330 &orinoco_fw[priv->firmware_type]);
331 break;
332 case FIRMWARE_TYPE_INTERSIL:
333 break;
334 }
335 /* TODO: if we fail we probably need to reinitialise
336 * the driver */
337
338 return err;
339}
340
341void orinoco_cache_fw(struct orinoco_private *priv, int ap)
342{
343#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
344 const struct firmware *fw_entry = NULL;
345 const char *pri_fw;
346 const char *fw;
347
348 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
349 if (ap)
350 fw = orinoco_fw[priv->firmware_type].ap_fw;
351 else
352 fw = orinoco_fw[priv->firmware_type].sta_fw;
353
354 if (pri_fw) {
355 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
356 priv->cached_pri_fw = fw_entry;
357 }
358
359 if (fw) {
360 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
361 priv->cached_fw = fw_entry;
362 }
363#endif
364}
365
366void orinoco_uncache_fw(struct orinoco_private *priv)
367{
368#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
369 if (priv->cached_pri_fw)
370 release_firmware(priv->cached_pri_fw);
371 if (priv->cached_fw)
372 release_firmware(priv->cached_fw);
373
374 priv->cached_pri_fw = NULL;
375 priv->cached_fw = NULL;
376#endif
377}