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