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