[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / net / wireless / orinoco / fw.c
blob1257250a1e22c3822fb3372dd09eca23b740f76e
1 /* Firmware file reading and download helpers
3 * See copyright notice in main.c
4 */
5 #include <linux/kernel.h>
6 #include <linux/firmware.h>
7 #include <linux/device.h>
9 #include "hermes.h"
10 #include "hermes_dld.h"
11 #include "orinoco.h"
13 #include "fw.h"
15 /* End markers (for Symbol firmware only) */
16 #define TEXT_END 0x1A /* End of text header */
18 struct fw_info {
19 char *pri_fw;
20 char *sta_fw;
21 char *ap_fw;
22 u32 pda_addr;
23 u16 pda_size;
26 static const struct fw_info orinoco_fw[] = {
27 { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
28 { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
29 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
32 /* Structure used to access fields in FW
33 * Make sure LE decoding macros are used
35 struct orinoco_fw_header {
36 char hdr_vers[6]; /* ASCII string for header version */
37 __le16 headersize; /* Total length of header */
38 __le32 entry_point; /* NIC entry point */
39 __le32 blocks; /* Number of blocks to program */
40 __le32 block_offset; /* Offset of block data from eof header */
41 __le32 pdr_offset; /* Offset to PDR data from eof header */
42 __le32 pri_offset; /* Offset to primary plug data */
43 __le32 compat_offset; /* Offset to compatibility data*/
44 char signature[0]; /* FW signature length headersize-20 */
45 } __attribute__ ((packed));
47 /* Check the range of various header entries. Return a pointer to a
48 * description of the problem, or NULL if everything checks out. */
49 static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
51 u16 hdrsize;
53 if (len < sizeof(*hdr))
54 return "image too small";
55 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
56 return "format not recognised";
58 hdrsize = le16_to_cpu(hdr->headersize);
59 if (hdrsize > len)
60 return "bad headersize";
61 if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
62 return "bad block offset";
63 if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
64 return "bad PDR offset";
65 if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
66 return "bad PRI offset";
67 if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
68 return "bad compat offset";
70 /* TODO: consider adding a checksum or CRC to the firmware format */
71 return NULL;
74 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
75 static inline const struct firmware *
76 orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
78 if (primary)
79 return priv->cached_pri_fw;
80 else
81 return priv->cached_fw;
83 #else
84 #define orinoco_cached_fw_get(priv, primary) (NULL)
85 #endif
87 /* Download either STA or AP firmware into the card. */
88 static int
89 orinoco_dl_firmware(struct orinoco_private *priv,
90 const struct fw_info *fw,
91 int ap)
93 /* Plug Data Area (PDA) */
94 __le16 *pda;
96 hermes_t *hw = &priv->hw;
97 const struct firmware *fw_entry;
98 const struct orinoco_fw_header *hdr;
99 const unsigned char *first_block;
100 const void *end;
101 const char *firmware;
102 const char *fw_err;
103 struct device *dev = priv->dev;
104 int err = 0;
106 pda = kzalloc(fw->pda_size, GFP_KERNEL);
107 if (!pda)
108 return -ENOMEM;
110 if (ap)
111 firmware = fw->ap_fw;
112 else
113 firmware = fw->sta_fw;
115 dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
117 /* Read current plug data */
118 err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
119 dev_dbg(dev, "Read PDA returned %d\n", err);
120 if (err)
121 goto free;
123 if (!orinoco_cached_fw_get(priv, false)) {
124 err = request_firmware(&fw_entry, firmware, priv->dev);
126 if (err) {
127 dev_err(dev, "Cannot find firmware %s\n", firmware);
128 err = -ENOENT;
129 goto free;
131 } else
132 fw_entry = orinoco_cached_fw_get(priv, false);
134 hdr = (const struct orinoco_fw_header *) fw_entry->data;
136 fw_err = validate_fw(hdr, fw_entry->size);
137 if (fw_err) {
138 dev_warn(dev, "Invalid firmware image detected (%s). "
139 "Aborting download\n", fw_err);
140 err = -EINVAL;
141 goto abort;
144 /* Enable aux port to allow programming */
145 err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
146 dev_dbg(dev, "Program init returned %d\n", err);
147 if (err != 0)
148 goto abort;
150 /* Program data */
151 first_block = (fw_entry->data +
152 le16_to_cpu(hdr->headersize) +
153 le32_to_cpu(hdr->block_offset));
154 end = fw_entry->data + fw_entry->size;
156 err = hermes_program(hw, first_block, end);
157 dev_dbg(dev, "Program returned %d\n", err);
158 if (err != 0)
159 goto abort;
161 /* Update production data */
162 first_block = (fw_entry->data +
163 le16_to_cpu(hdr->headersize) +
164 le32_to_cpu(hdr->pdr_offset));
166 err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
167 &pda[fw->pda_size / sizeof(*pda)]);
168 dev_dbg(dev, "Apply PDA returned %d\n", err);
169 if (err)
170 goto abort;
172 /* Tell card we've finished */
173 err = hermesi_program_end(hw);
174 dev_dbg(dev, "Program end returned %d\n", err);
175 if (err != 0)
176 goto abort;
178 /* Check if we're running */
179 dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
181 abort:
182 /* If we requested the firmware, release it. */
183 if (!orinoco_cached_fw_get(priv, false))
184 release_firmware(fw_entry);
186 free:
187 kfree(pda);
188 return err;
192 * Process a firmware image - stop the card, load the firmware, reset
193 * the card and make sure it responds. For the secondary firmware take
194 * care of the PDA - read it and then write it on top of the firmware.
196 static int
197 symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
198 const unsigned char *image, const void *end,
199 int secondary)
201 hermes_t *hw = &priv->hw;
202 int ret = 0;
203 const unsigned char *ptr;
204 const unsigned char *first_block;
206 /* Plug Data Area (PDA) */
207 __le16 *pda = NULL;
209 /* Binary block begins after the 0x1A marker */
210 ptr = image;
211 while (*ptr++ != TEXT_END);
212 first_block = ptr;
214 /* Read the PDA from EEPROM */
215 if (secondary) {
216 pda = kzalloc(fw->pda_size, GFP_KERNEL);
217 if (!pda)
218 return -ENOMEM;
220 ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
221 if (ret)
222 goto free;
225 /* Stop the firmware, so that it can be safely rewritten */
226 if (priv->stop_fw) {
227 ret = priv->stop_fw(priv, 1);
228 if (ret)
229 goto free;
232 /* Program the adapter with new firmware */
233 ret = hermes_program(hw, first_block, end);
234 if (ret)
235 goto free;
237 /* Write the PDA to the adapter */
238 if (secondary) {
239 size_t len = hermes_blocks_length(first_block, end);
240 ptr = first_block + len;
241 ret = hermes_apply_pda(hw, ptr, end, pda,
242 &pda[fw->pda_size / sizeof(*pda)]);
243 kfree(pda);
244 if (ret)
245 return ret;
248 /* Run the firmware */
249 if (priv->stop_fw) {
250 ret = priv->stop_fw(priv, 0);
251 if (ret)
252 return ret;
255 /* Reset hermes chip and make sure it responds */
256 ret = hermes_init(hw);
258 /* hermes_reset() should return 0 with the secondary firmware */
259 if (secondary && ret != 0)
260 return -ENODEV;
262 /* And this should work with any firmware */
263 if (!hermes_present(hw))
264 return -ENODEV;
266 return 0;
268 free:
269 kfree(pda);
270 return ret;
275 * Download the firmware into the card, this also does a PCMCIA soft
276 * reset on the card, to make sure it's in a sane state.
278 static int
279 symbol_dl_firmware(struct orinoco_private *priv,
280 const struct fw_info *fw)
282 struct device *dev = priv->dev;
283 int ret;
284 const struct firmware *fw_entry;
286 if (!orinoco_cached_fw_get(priv, true)) {
287 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
288 dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
289 return -ENOENT;
291 } else
292 fw_entry = orinoco_cached_fw_get(priv, true);
294 /* Load primary firmware */
295 ret = symbol_dl_image(priv, fw, fw_entry->data,
296 fw_entry->data + fw_entry->size, 0);
298 if (!orinoco_cached_fw_get(priv, true))
299 release_firmware(fw_entry);
300 if (ret) {
301 dev_err(dev, "Primary firmware download failed\n");
302 return ret;
305 if (!orinoco_cached_fw_get(priv, false)) {
306 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
307 dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
308 return -ENOENT;
310 } else
311 fw_entry = orinoco_cached_fw_get(priv, false);
313 /* Load secondary firmware */
314 ret = symbol_dl_image(priv, fw, fw_entry->data,
315 fw_entry->data + fw_entry->size, 1);
316 if (!orinoco_cached_fw_get(priv, false))
317 release_firmware(fw_entry);
318 if (ret) {
319 dev_err(dev, "Secondary firmware download failed\n");
322 return ret;
325 int orinoco_download(struct orinoco_private *priv)
327 int err = 0;
328 /* Reload firmware */
329 switch (priv->firmware_type) {
330 case FIRMWARE_TYPE_AGERE:
331 /* case FIRMWARE_TYPE_INTERSIL: */
332 err = orinoco_dl_firmware(priv,
333 &orinoco_fw[priv->firmware_type], 0);
334 break;
336 case FIRMWARE_TYPE_SYMBOL:
337 err = symbol_dl_firmware(priv,
338 &orinoco_fw[priv->firmware_type]);
339 break;
340 case FIRMWARE_TYPE_INTERSIL:
341 break;
343 /* TODO: if we fail we probably need to reinitialise
344 * the driver */
346 return err;
349 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
350 void orinoco_cache_fw(struct orinoco_private *priv, int ap)
352 const struct firmware *fw_entry = NULL;
353 const char *pri_fw;
354 const char *fw;
356 pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
357 if (ap)
358 fw = orinoco_fw[priv->firmware_type].ap_fw;
359 else
360 fw = orinoco_fw[priv->firmware_type].sta_fw;
362 if (pri_fw) {
363 if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
364 priv->cached_pri_fw = fw_entry;
367 if (fw) {
368 if (request_firmware(&fw_entry, fw, priv->dev) == 0)
369 priv->cached_fw = fw_entry;
373 void orinoco_uncache_fw(struct orinoco_private *priv)
375 if (priv->cached_pri_fw)
376 release_firmware(priv->cached_pri_fw);
377 if (priv->cached_fw)
378 release_firmware(priv->cached_fw);
380 priv->cached_pri_fw = NULL;
381 priv->cached_fw = NULL;
383 #endif