1
2
3
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
18#define TEXT_END 0x1A
19
20struct fw_info {
21 char *pri_fw;
22 char *sta_fw;
23 char *ap_fw;
24 u32 pda_addr;
25 u16 pda_size;
26};
27
28static const struct fw_info orinoco_fw[] = {
29 { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
30 { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
31 { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
32};
33MODULE_FIRMWARE("agere_sta_fw.bin");
34MODULE_FIRMWARE("agere_ap_fw.bin");
35MODULE_FIRMWARE("prism_sta_fw.bin");
36MODULE_FIRMWARE("prism_ap_fw.bin");
37MODULE_FIRMWARE("symbol_sp24t_prim_fw");
38MODULE_FIRMWARE("symbol_sp24t_sec_fw");
39
40
41
42
43struct orinoco_fw_header {
44 char hdr_vers[6];
45 __le16 headersize;
46 __le32 entry_point;
47 __le32 blocks;
48 __le32 block_offset;
49 __le32 pdr_offset;
50 __le32 pri_offset;
51 __le32 compat_offset;
52 char signature[0];
53} __packed;
54
55
56
57static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
58{
59 u16 hdrsize;
60
61 if (len < sizeof(*hdr))
62 return "image too small";
63 if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
64 return "format not recognised";
65
66 hdrsize = le16_to_cpu(hdr->headersize);
67 if (hdrsize > len)
68 return "bad headersize";
69 if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
70 return "bad block offset";
71 if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
72 return "bad PDR offset";
73 if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
74 return "bad PRI offset";
75 if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
76 return "bad compat offset";
77
78
79 return NULL;
80}
81
82#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
83static inline const struct firmware *
84orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
85{
86 if (primary)
87 return priv->cached_pri_fw;
88 else
89 return priv->cached_fw;
90}
91#else
92#define orinoco_cached_fw_get(priv, primary) (NULL)
93#endif
94
95
96static int
97orinoco_dl_firmware(struct orinoco_private *priv,
98 const struct fw_info *fw,
99 int ap)
100{
101
102 __le16 *pda;
103
104 struct hermes *hw = &priv->hw;
105 const struct firmware *fw_entry;
106 const struct orinoco_fw_header *hdr;
107 const unsigned char *first_block;
108 const void *end;
109 const char *firmware;
110 const char *fw_err;
111 struct device *dev = priv->dev;
112 int err = 0;
113
114 pda = kzalloc(fw->pda_size, GFP_KERNEL);
115 if (!pda)
116 return -ENOMEM;
117
118 if (ap)
119 firmware = fw->ap_fw;
120 else
121 firmware = fw->sta_fw;
122
123 dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
124
125
126 err = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
127 dev_dbg(dev, "Read PDA returned %d\n", err);
128 if (err)
129 goto free;
130
131 if (!orinoco_cached_fw_get(priv, false)) {
132 err = request_firmware(&fw_entry, firmware, priv->dev);
133
134 if (err) {
135 dev_err(dev, "Cannot find firmware %s\n", firmware);
136 err = -ENOENT;
137 goto free;
138 }
139 } else
140 fw_entry = orinoco_cached_fw_get(priv, false);
141
142 hdr = (const struct orinoco_fw_header *) fw_entry->data;
143
144 fw_err = validate_fw(hdr, fw_entry->size);
145 if (fw_err) {
146 dev_warn(dev, "Invalid firmware image detected (%s). "
147 "Aborting download\n", fw_err);
148 err = -EINVAL;
149 goto abort;
150 }
151
152
153 err = hw->ops->program_init(hw, le32_to_cpu(hdr->entry_point));
154 dev_dbg(dev, "Program init returned %d\n", err);
155 if (err != 0)
156 goto abort;
157
158
159 first_block = (fw_entry->data +
160 le16_to_cpu(hdr->headersize) +
161 le32_to_cpu(hdr->block_offset));
162 end = fw_entry->data + fw_entry->size;
163
164 err = hermes_program(hw, first_block, end);
165 dev_dbg(dev, "Program returned %d\n", err);
166 if (err != 0)
167 goto abort;
168
169
170 first_block = (fw_entry->data +
171 le16_to_cpu(hdr->headersize) +
172 le32_to_cpu(hdr->pdr_offset));
173
174 err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
175 &pda[fw->pda_size / sizeof(*pda)]);
176 dev_dbg(dev, "Apply PDA returned %d\n", err);
177 if (err)
178 goto abort;
179
180
181 err = hw->ops->program_end(hw);
182 dev_dbg(dev, "Program end returned %d\n", err);
183 if (err != 0)
184 goto abort;
185
186
187 dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
188
189abort:
190
191 if (!orinoco_cached_fw_get(priv, false))
192 release_firmware(fw_entry);
193
194free:
195 kfree(pda);
196 return err;
197}
198
199
200
201
202
203
204static int
205symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
206 const unsigned char *image, const void *end,
207 int secondary)
208{
209 struct hermes *hw = &priv->hw;
210 int ret = 0;
211 const unsigned char *ptr;
212 const unsigned char *first_block;
213
214
215 __le16 *pda = NULL;
216
217
218 ptr = image;
219 while (*ptr++ != TEXT_END);
220 first_block = ptr;
221
222
223 if (secondary) {
224 pda = kzalloc(fw->pda_size, GFP_KERNEL);
225 if (!pda)
226 return -ENOMEM;
227
228 ret = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
229 if (ret)
230 goto free;
231 }
232
233
234 if (priv->stop_fw) {
235 ret = priv->stop_fw(priv, 1);
236 if (ret)
237 goto free;
238 }
239
240
241 ret = hermes_program(hw, first_block, end);
242 if (ret)
243 goto free;
244
245
246 if (secondary) {
247 size_t len = hermes_blocks_length(first_block, end);
248 ptr = first_block + len;
249 ret = hermes_apply_pda(hw, ptr, end, pda,
250 &pda[fw->pda_size / sizeof(*pda)]);
251 kfree(pda);
252 if (ret)
253 return ret;
254 }
255
256
257 if (priv->stop_fw) {
258 ret = priv->stop_fw(priv, 0);
259 if (ret)
260 return ret;
261 }
262
263
264 ret = hw->ops->init(hw);
265
266
267 if (secondary && ret != 0)
268 return -ENODEV;
269
270
271 if (!hermes_present(hw))
272 return -ENODEV;
273
274 return 0;
275
276free:
277 kfree(pda);
278 return ret;
279}
280
281
282
283
284
285
286static int
287symbol_dl_firmware(struct orinoco_private *priv,
288 const struct fw_info *fw)
289{
290 struct device *dev = priv->dev;
291 int ret;
292 const struct firmware *fw_entry;
293
294 if (!orinoco_cached_fw_get(priv, true)) {
295 if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
296 dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
297 return -ENOENT;
298 }
299 } else
300 fw_entry = orinoco_cached_fw_get(priv, true);
301
302
303 ret = symbol_dl_image(priv, fw, fw_entry->data,
304 fw_entry->data + fw_entry->size, 0);
305
306 if (!orinoco_cached_fw_get(priv, true))
307 release_firmware(fw_entry);
308 if (ret) {
309 dev_err(dev, "Primary firmware download failed\n");
310 return ret;
311 }
312
313 if (!orinoco_cached_fw_get(priv, false)) {
314 if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
315 dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
316 return -ENOENT;
317 }
318 } else
319 fw_entry = orinoco_cached_fw_get(priv, false);
320
321
322 ret = symbol_dl_image(priv, fw, fw_entry->data,
323 fw_entry->data + fw_entry->size, 1);
324 if (!orinoco_cached_fw_get(priv, false))
325 release_firmware(fw_entry);
326 if (ret)
327 dev_err(dev, "Secondary firmware download failed\n");
328
329 return ret;
330}
331
332int orinoco_download(struct orinoco_private *priv)
333{
334 int err = 0;
335
336 switch (priv->firmware_type) {
337 case FIRMWARE_TYPE_AGERE:
338
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
351
352
353 return err;
354}
355
356#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
357void 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
380void orinoco_uncache_fw(struct orinoco_private *priv)
381{
382 release_firmware(priv->cached_pri_fw);
383 release_firmware(priv->cached_fw);
384 priv->cached_pri_fw = NULL;
385 priv->cached_fw = NULL;
386}
387#endif
388