Merge branch 'reiserfs-updates' from Jeff Mahoney
[pandora-kernel.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/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
17 struct fw_info {
18         char *pri_fw;
19         char *sta_fw;
20         char *ap_fw;
21         u32 pda_addr;
22         u16 pda_size;
23 };
24
25 static const struct fw_info orinoco_fw[] = {
26         { 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  */
34 struct 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
46 /* Check the range of various header entries. Return a pointer to a
47  * description of the problem, or NULL if everything checks out. */
48 static 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
73 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
74 static inline const struct firmware *
75 orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
76 {
77         if (primary)
78                 return priv->cached_pri_fw;
79         else
80                 return priv->cached_fw;
81 }
82 #else
83 #define orinoco_cached_fw_get(priv, primary) (NULL)
84 #endif
85
86 /* Download either STA or AP firmware into the card. */
87 static int
88 orinoco_dl_firmware(struct orinoco_private *priv,
89                     const struct fw_info *fw,
90                     int ap)
91 {
92         /* Plug Data Area (PDA) */
93         __le16 *pda;
94
95         hermes_t *hw = &priv->hw;
96         const struct firmware *fw_entry;
97         const struct orinoco_fw_header *hdr;
98         const unsigned char *first_block;
99         const void *end;
100         const char *firmware;
101         const char *fw_err;
102         struct net_device *dev = priv->ndev;
103         int err = 0;
104
105         pda = kzalloc(fw->pda_size, GFP_KERNEL);
106         if (!pda)
107                 return -ENOMEM;
108
109         if (ap)
110                 firmware = fw->ap_fw;
111         else
112                 firmware = fw->sta_fw;
113
114         printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
115                dev->name, firmware);
116
117         /* Read current plug data */
118         err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
119         printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, err);
120         if (err)
121                 goto free;
122
123         if (!orinoco_cached_fw_get(priv, false)) {
124                 err = request_firmware(&fw_entry, firmware, priv->dev);
125
126                 if (err) {
127                         printk(KERN_ERR "%s: Cannot find firmware %s\n",
128                                dev->name, firmware);
129                         err = -ENOENT;
130                         goto free;
131                 }
132         } else
133                 fw_entry = orinoco_cached_fw_get(priv, false);
134
135         hdr = (const struct orinoco_fw_header *) fw_entry->data;
136
137         fw_err = validate_fw(hdr, fw_entry->size);
138         if (fw_err) {
139                 printk(KERN_WARNING "%s: Invalid firmware image detected (%s). "
140                        "Aborting download\n",
141                        dev->name, fw_err);
142                 err = -EINVAL;
143                 goto abort;
144         }
145
146         /* Enable aux port to allow programming */
147         err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
148         printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, 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         printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, 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         printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
171         if (err)
172                 goto abort;
173
174         /* Tell card we've finished */
175         err = hermesi_program_end(hw);
176         printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
177         if (err != 0)
178                 goto abort;
179
180         /* Check if we're running */
181         printk(KERN_DEBUG "%s: hermes_present returned %d\n",
182                dev->name, 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         hermes_t *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 = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
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 = hermes_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 net_device *dev = priv->ndev;
286         int ret;
287         const struct firmware *fw_entry;
288
289         if (!orinoco_cached_fw_get(priv, true)) {
290                 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
291                         printk(KERN_ERR "%s: Cannot find firmware: %s\n",
292                                dev->name, fw->pri_fw);
293                         return -ENOENT;
294                 }
295         } else
296                 fw_entry = orinoco_cached_fw_get(priv, true);
297
298         /* Load primary firmware */
299         ret = symbol_dl_image(priv, fw, fw_entry->data,
300                               fw_entry->data + fw_entry->size, 0);
301
302         if (!orinoco_cached_fw_get(priv, true))
303                 release_firmware(fw_entry);
304         if (ret) {
305                 printk(KERN_ERR "%s: Primary firmware download failed\n",
306                        dev->name);
307                 return ret;
308         }
309
310         if (!orinoco_cached_fw_get(priv, false)) {
311                 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
312                         printk(KERN_ERR "%s: Cannot find firmware: %s\n",
313                                dev->name, fw->sta_fw);
314                         return -ENOENT;
315                 }
316         } else
317                 fw_entry = orinoco_cached_fw_get(priv, false);
318
319         /* Load secondary firmware */
320         ret = symbol_dl_image(priv, fw, fw_entry->data,
321                               fw_entry->data + fw_entry->size, 1);
322         if (!orinoco_cached_fw_get(priv, false))
323                 release_firmware(fw_entry);
324         if (ret) {
325                 printk(KERN_ERR "%s: Secondary firmware download failed\n",
326                        dev->name);
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
356 #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
357 void orinoco_cache_fw(struct orinoco_private *priv, int ap)
358 {
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         }
378 }
379
380 void orinoco_uncache_fw(struct orinoco_private *priv)
381 {
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;
389 }
390 #endif