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