Merge tag 'pwm/for-3.16-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[deliverable/linux.git] / drivers / staging / wlan-ng / prism2fw.c
1 /* from src/prism2/download/prism2dl.c
2 *
3 * utility for downloading prism2 images moved into kernelspace
4 *
5 * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
6 * --------------------------------------------------------------------
7 *
8 * linux-wlan
9 *
10 * The contents of this file are subject to the Mozilla Public
11 * License Version 1.1 (the "License"); you may not use this file
12 * except in compliance with the License. You may obtain a copy of
13 * the License at http://www.mozilla.org/MPL/
14 *
15 * Software distributed under the License is distributed on an "AS
16 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
19 *
20 * Alternatively, the contents of this file may be used under the
21 * terms of the GNU Public License version 2 (the "GPL"), in which
22 * case the provisions of the GPL are applicable instead of the
23 * above. If you wish to allow the use of your version of this file
24 * only under the terms of the GPL and not to allow others to use
25 * your version of this file under the MPL, indicate your decision
26 * by deleting the provisions above and replace them with the notice
27 * and other provisions required by the GPL. If you do not delete
28 * the provisions above, a recipient may use your version of this
29 * file under either the MPL or the GPL.
30 *
31 * --------------------------------------------------------------------
32 *
33 * Inquiries regarding the linux-wlan Open Source project can be
34 * made directly to:
35 *
36 * AbsoluteValue Systems Inc.
37 * info@linux-wlan.com
38 * http://www.linux-wlan.com
39 *
40 * --------------------------------------------------------------------
41 *
42 * Portions of the development of this software were funded by
43 * Intersil Corporation as part of PRISM(R) chipset product development.
44 *
45 * --------------------------------------------------------------------
46 */
47
48 /*================================================================*/
49 /* System Includes */
50 #include <linux/ihex.h>
51 #include <linux/slab.h>
52
53 /*================================================================*/
54 /* Local Constants */
55
56 #define PRISM2_USB_FWFILE "prism2_ru.fw"
57 MODULE_FIRMWARE(PRISM2_USB_FWFILE);
58
59 #define S3DATA_MAX 5000
60 #define S3PLUG_MAX 200
61 #define S3CRC_MAX 200
62 #define S3INFO_MAX 50
63
64 #define S3ADDR_PLUG (0xff000000UL)
65 #define S3ADDR_CRC (0xff100000UL)
66 #define S3ADDR_INFO (0xff200000UL)
67 #define S3ADDR_START (0xff400000UL)
68
69 #define CHUNKS_MAX 100
70
71 #define WRITESIZE_MAX 4096
72
73 /*================================================================*/
74 /* Local Types */
75
76 struct s3datarec {
77 u32 len;
78 u32 addr;
79 u8 checksum;
80 u8 *data;
81 };
82
83 struct s3plugrec {
84 u32 itemcode;
85 u32 addr;
86 u32 len;
87 };
88
89 struct s3crcrec {
90 u32 addr;
91 u32 len;
92 unsigned int dowrite;
93 };
94
95 struct s3inforec {
96 u16 len;
97 u16 type;
98 union {
99 hfa384x_compident_t version;
100 hfa384x_caplevel_t compat;
101 u16 buildseq;
102 hfa384x_compident_t platform;
103 } info;
104 };
105
106 struct pda {
107 u8 buf[HFA384x_PDA_LEN_MAX];
108 hfa384x_pdrec_t *rec[HFA384x_PDA_RECS_MAX];
109 unsigned int nrec;
110 };
111
112 struct imgchunk {
113 u32 addr; /* start address */
114 u32 len; /* in bytes */
115 u16 crc; /* CRC value (if it falls at a chunk boundary) */
116 u8 *data;
117 };
118
119 /*================================================================*/
120 /* Local Static Definitions */
121
122 /*----------------------------------------------------------------*/
123 /* s-record image processing */
124
125 /* Data records */
126 static unsigned int ns3data;
127 static struct s3datarec s3data[S3DATA_MAX];
128
129 /* Plug records */
130 static unsigned int ns3plug;
131 static struct s3plugrec s3plug[S3PLUG_MAX];
132
133 /* CRC records */
134 static unsigned int ns3crc;
135 static struct s3crcrec s3crc[S3CRC_MAX];
136
137 /* Info records */
138 static unsigned int ns3info;
139 static struct s3inforec s3info[S3INFO_MAX];
140
141 /* S7 record (there _better_ be only one) */
142 static u32 startaddr;
143
144 /* Load image chunks */
145 static unsigned int nfchunks;
146 static struct imgchunk fchunk[CHUNKS_MAX];
147
148 /* Note that for the following pdrec_t arrays, the len and code */
149 /* fields are stored in HOST byte order. The mkpdrlist() function */
150 /* does the conversion. */
151 /*----------------------------------------------------------------*/
152 /* PDA, built from [card|newfile]+[addfile1+addfile2...] */
153
154 static struct pda pda;
155 static hfa384x_compident_t nicid;
156 static hfa384x_caplevel_t rfid;
157 static hfa384x_caplevel_t macid;
158 static hfa384x_caplevel_t priid;
159
160 /*================================================================*/
161 /* Local Function Declarations */
162
163 static int prism2_fwapply(const struct ihex_binrec *rfptr,
164 wlandevice_t *wlandev);
165
166 static int read_fwfile(const struct ihex_binrec *rfptr);
167
168 static int mkimage(struct imgchunk *clist, unsigned int *ccnt);
169
170 static int read_cardpda(struct pda *pda, wlandevice_t *wlandev);
171
172 static int mkpdrlist(struct pda *pda);
173
174 static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
175 struct s3plugrec *s3plug, unsigned int ns3plug, struct pda *pda);
176
177 static int crcimage(struct imgchunk *fchunk, unsigned int nfchunks,
178 struct s3crcrec *s3crc, unsigned int ns3crc);
179
180 static int writeimage(wlandevice_t *wlandev, struct imgchunk *fchunk,
181 unsigned int nfchunks);
182 static void free_chunks(struct imgchunk *fchunk, unsigned int *nfchunks);
183
184 static void free_srecs(void);
185
186 static int validate_identity(void);
187
188 /*================================================================*/
189 /* Function Definitions */
190
191 /*----------------------------------------------------------------
192 * prism2_fwtry
193 *
194 * Try and get firmware into memory
195 *
196 * Arguments:
197 * udev usb device structure
198 * wlandev wlan device structure
199 *
200 * Returns:
201 * 0 - success
202 * ~0 - failure
203 ----------------------------------------------------------------*/
204 static int prism2_fwtry(struct usb_device *udev, wlandevice_t *wlandev)
205 {
206 const struct firmware *fw_entry = NULL;
207
208 netdev_info(wlandev->netdev, "prism2_usb: Checking for firmware %s\n",
209 PRISM2_USB_FWFILE);
210 if (request_ihex_firmware(&fw_entry,
211 PRISM2_USB_FWFILE, &udev->dev) != 0) {
212 netdev_info(wlandev->netdev,
213 "prism2_usb: Firmware not available, but not essential\n");
214 netdev_info(wlandev->netdev,
215 "prism2_usb: can continue to use card anyway.\n");
216 return 1;
217 }
218
219 netdev_info(wlandev->netdev,
220 "prism2_usb: %s will be processed, size %zu\n",
221 PRISM2_USB_FWFILE, fw_entry->size);
222 prism2_fwapply((const struct ihex_binrec *)fw_entry->data, wlandev);
223
224 release_firmware(fw_entry);
225 return 0;
226 }
227
228 /*----------------------------------------------------------------
229 * prism2_fwapply
230 *
231 * Apply the firmware loaded into memory
232 *
233 * Arguments:
234 * rfptr firmware image in kernel memory
235 * wlandev device
236 *
237 * Returns:
238 * 0 - success
239 * ~0 - failure
240 ----------------------------------------------------------------*/
241 static int prism2_fwapply(const struct ihex_binrec *rfptr, wlandevice_t *wlandev)
242 {
243 signed int result = 0;
244 struct p80211msg_dot11req_mibget getmsg;
245 p80211itemd_t *item;
246 u32 *data;
247
248 /* Initialize the data structures */
249 ns3data = 0;
250 memset(s3data, 0, sizeof(s3data));
251 ns3plug = 0;
252 memset(s3plug, 0, sizeof(s3plug));
253 ns3crc = 0;
254 memset(s3crc, 0, sizeof(s3crc));
255 ns3info = 0;
256 memset(s3info, 0, sizeof(s3info));
257 startaddr = 0;
258
259 nfchunks = 0;
260 memset(fchunk, 0, sizeof(fchunk));
261 memset(&nicid, 0, sizeof(nicid));
262 memset(&rfid, 0, sizeof(rfid));
263 memset(&macid, 0, sizeof(macid));
264 memset(&priid, 0, sizeof(priid));
265
266 /* clear the pda and add an initial END record */
267 memset(&pda, 0, sizeof(pda));
268 pda.rec[0] = (hfa384x_pdrec_t *) pda.buf;
269 pda.rec[0]->len = cpu_to_le16(2); /* len in words */
270 pda.rec[0]->code = cpu_to_le16(HFA384x_PDR_END_OF_PDA);
271 pda.nrec = 1;
272
273 /*-----------------------------------------------------*/
274 /* Put card into fwload state */
275 prism2sta_ifstate(wlandev, P80211ENUM_ifstate_fwload);
276
277 /* Build the PDA we're going to use. */
278 if (read_cardpda(&pda, wlandev)) {
279 netdev_err(wlandev->netdev, "load_cardpda failed, exiting.\n");
280 return 1;
281 }
282
283 /* read the card's PRI-SUP */
284 memset(&getmsg, 0, sizeof(getmsg));
285 getmsg.msgcode = DIDmsg_dot11req_mibget;
286 getmsg.msglen = sizeof(getmsg);
287 strcpy(getmsg.devname, wlandev->name);
288
289 getmsg.mibattribute.did = DIDmsg_dot11req_mibget_mibattribute;
290 getmsg.mibattribute.status = P80211ENUM_msgitem_status_data_ok;
291 getmsg.resultcode.did = DIDmsg_dot11req_mibget_resultcode;
292 getmsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
293
294 item = (p80211itemd_t *) getmsg.mibattribute.data;
295 item->did = DIDmib_p2_p2NIC_p2PRISupRange;
296 item->status = P80211ENUM_msgitem_status_no_value;
297
298 data = (u32 *) item->data;
299
300 /* DIDmsg_dot11req_mibget */
301 prism2mgmt_mibset_mibget(wlandev, &getmsg);
302 if (getmsg.resultcode.data != P80211ENUM_resultcode_success)
303 netdev_err(wlandev->netdev, "Couldn't fetch PRI-SUP info\n");
304
305 /* Already in host order */
306 priid.role = *data++;
307 priid.id = *data++;
308 priid.variant = *data++;
309 priid.bottom = *data++;
310 priid.top = *data++;
311
312 /* Read the S3 file */
313 result = read_fwfile(rfptr);
314 if (result) {
315 netdev_err(wlandev->netdev,
316 "Failed to read the data exiting.\n");
317 return 1;
318 }
319
320 result = validate_identity();
321
322 if (result) {
323 netdev_err(wlandev->netdev, "Incompatible firmware image.\n");
324 return 1;
325 }
326
327 if (startaddr == 0x00000000) {
328 netdev_err(wlandev->netdev,
329 "Can't RAM download a Flash image!\n");
330 return 1;
331 }
332
333 /* Make the image chunks */
334 result = mkimage(fchunk, &nfchunks);
335
336 /* Do any plugging */
337 result = plugimage(fchunk, nfchunks, s3plug, ns3plug, &pda);
338 if (result) {
339 netdev_err(wlandev->netdev, "Failed to plug data.\n");
340 return 1;
341 }
342
343 /* Insert any CRCs */
344 if (crcimage(fchunk, nfchunks, s3crc, ns3crc)) {
345 netdev_err(wlandev->netdev, "Failed to insert all CRCs\n");
346 return 1;
347 }
348
349 /* Write the image */
350 result = writeimage(wlandev, fchunk, nfchunks);
351 if (result) {
352 netdev_err(wlandev->netdev, "Failed to ramwrite image data.\n");
353 return 1;
354 }
355
356 /* clear any allocated memory */
357 free_chunks(fchunk, &nfchunks);
358 free_srecs();
359
360 netdev_info(wlandev->netdev, "prism2_usb: firmware loading finished.\n");
361
362 return result;
363 }
364
365 /*----------------------------------------------------------------
366 * crcimage
367 *
368 * Adds a CRC16 in the two bytes prior to each block identified by
369 * an S3 CRC record. Currently, we don't actually do a CRC we just
370 * insert the value 0xC0DE in hfa384x order.
371 *
372 * Arguments:
373 * fchunk Array of image chunks
374 * nfchunks Number of image chunks
375 * s3crc Array of crc records
376 * ns3crc Number of crc records
377 *
378 * Returns:
379 * 0 success
380 * ~0 failure
381 ----------------------------------------------------------------*/
382 static int crcimage(struct imgchunk *fchunk, unsigned int nfchunks,
383 struct s3crcrec *s3crc, unsigned int ns3crc)
384 {
385 int result = 0;
386 int i;
387 int c;
388 u32 crcstart;
389 u32 crcend;
390 u32 cstart = 0;
391 u32 cend;
392 u8 *dest;
393 u32 chunkoff;
394
395 for (i = 0; i < ns3crc; i++) {
396 if (!s3crc[i].dowrite)
397 continue;
398 crcstart = s3crc[i].addr;
399 crcend = s3crc[i].addr + s3crc[i].len;
400 /* Find chunk */
401 for (c = 0; c < nfchunks; c++) {
402 cstart = fchunk[c].addr;
403 cend = fchunk[c].addr + fchunk[c].len;
404 /* the line below does an address & len match search */
405 /* unfortunately, I've found that the len fields of */
406 /* some crc records don't match with the length of */
407 /* the actual data, so we're not checking right now */
408 /* if (crcstart-2 >= cstart && crcend <= cend) break; */
409
410 /* note the -2 below, it's to make sure the chunk has */
411 /* space for the CRC value */
412 if (crcstart - 2 >= cstart && crcstart < cend)
413 break;
414 }
415 if (c >= nfchunks) {
416 pr_err("Failed to find chunk for "
417 "crcrec[%d], addr=0x%06x len=%d , "
418 "aborting crc.\n",
419 i, s3crc[i].addr, s3crc[i].len);
420 return 1;
421 }
422
423 /* Insert crc */
424 pr_debug("Adding crc @ 0x%06x\n", s3crc[i].addr - 2);
425 chunkoff = crcstart - cstart - 2;
426 dest = fchunk[c].data + chunkoff;
427 *dest = 0xde;
428 *(dest + 1) = 0xc0;
429
430 }
431 return result;
432 }
433
434 /*----------------------------------------------------------------
435 * free_chunks
436 *
437 * Clears the chunklist data structures in preparation for a new file.
438 *
439 * Arguments:
440 * none
441 *
442 * Returns:
443 * nothing
444 ----------------------------------------------------------------*/
445 static void free_chunks(struct imgchunk *fchunk, unsigned int *nfchunks)
446 {
447 int i;
448 for (i = 0; i < *nfchunks; i++)
449 kfree(fchunk[i].data);
450
451 *nfchunks = 0;
452 memset(fchunk, 0, sizeof(*fchunk));
453
454 }
455
456 /*----------------------------------------------------------------
457 * free_srecs
458 *
459 * Clears the srec data structures in preparation for a new file.
460 *
461 * Arguments:
462 * none
463 *
464 * Returns:
465 * nothing
466 ----------------------------------------------------------------*/
467 static void free_srecs(void)
468 {
469 ns3data = 0;
470 memset(s3data, 0, sizeof(s3data));
471 ns3plug = 0;
472 memset(s3plug, 0, sizeof(s3plug));
473 ns3crc = 0;
474 memset(s3crc, 0, sizeof(s3crc));
475 ns3info = 0;
476 memset(s3info, 0, sizeof(s3info));
477 startaddr = 0;
478 }
479
480 /*----------------------------------------------------------------
481 * mkimage
482 *
483 * Scans the currently loaded set of S records for data residing
484 * in contiguous memory regions. Each contiguous region is then
485 * made into a 'chunk'. This function assumes that we're building
486 * a new chunk list. Assumes the s3data items are in sorted order.
487 *
488 * Arguments: none
489 *
490 * Returns:
491 * 0 - success
492 * ~0 - failure (probably an errno)
493 ----------------------------------------------------------------*/
494 static int mkimage(struct imgchunk *clist, unsigned int *ccnt)
495 {
496 int result = 0;
497 int i;
498 int j;
499 int currchunk = 0;
500 u32 nextaddr = 0;
501 u32 s3start;
502 u32 s3end;
503 u32 cstart = 0;
504 u32 cend;
505 u32 coffset;
506
507 /* There may already be data in the chunklist */
508 *ccnt = 0;
509
510 /* Establish the location and size of each chunk */
511 for (i = 0; i < ns3data; i++) {
512 if (s3data[i].addr == nextaddr) {
513 /* existing chunk, grow it */
514 clist[currchunk].len += s3data[i].len;
515 nextaddr += s3data[i].len;
516 } else {
517 /* New chunk */
518 (*ccnt)++;
519 currchunk = *ccnt - 1;
520 clist[currchunk].addr = s3data[i].addr;
521 clist[currchunk].len = s3data[i].len;
522 nextaddr = s3data[i].addr + s3data[i].len;
523 /* Expand the chunk if there is a CRC record at */
524 /* their beginning bound */
525 for (j = 0; j < ns3crc; j++) {
526 if (s3crc[j].dowrite &&
527 s3crc[j].addr == clist[currchunk].addr) {
528 clist[currchunk].addr -= 2;
529 clist[currchunk].len += 2;
530 }
531 }
532 }
533 }
534
535 /* We're currently assuming there aren't any overlapping chunks */
536 /* if this proves false, we'll need to add code to coalesce. */
537
538 /* Allocate buffer space for chunks */
539 for (i = 0; i < *ccnt; i++) {
540 clist[i].data = kzalloc(clist[i].len, GFP_KERNEL);
541 if (clist[i].data == NULL) {
542 pr_err("failed to allocate image space, exitting.\n");
543 return 1;
544 }
545 pr_debug("chunk[%d]: addr=0x%06x len=%d\n",
546 i, clist[i].addr, clist[i].len);
547 }
548
549 /* Copy srec data to chunks */
550 for (i = 0; i < ns3data; i++) {
551 s3start = s3data[i].addr;
552 s3end = s3start + s3data[i].len - 1;
553 for (j = 0; j < *ccnt; j++) {
554 cstart = clist[j].addr;
555 cend = cstart + clist[j].len - 1;
556 if (s3start >= cstart && s3end <= cend)
557 break;
558 }
559 if (((unsigned int)j) >= (*ccnt)) {
560 pr_err("s3rec(a=0x%06x,l=%d), no chunk match, exiting.\n",
561 s3start, s3data[i].len);
562 return 1;
563 }
564 coffset = s3start - cstart;
565 memcpy(clist[j].data + coffset, s3data[i].data, s3data[i].len);
566 }
567
568 return result;
569 }
570
571 /*----------------------------------------------------------------
572 * mkpdrlist
573 *
574 * Reads a raw PDA and builds an array of pdrec_t structures.
575 *
576 * Arguments:
577 * pda buffer containing raw PDA bytes
578 * pdrec ptr to an array of pdrec_t's. Will be filled on exit.
579 * nrec ptr to a variable that will contain the count of PDRs
580 *
581 * Returns:
582 * 0 - success
583 * ~0 - failure (probably an errno)
584 ----------------------------------------------------------------*/
585 static int mkpdrlist(struct pda *pda)
586 {
587 int result = 0;
588 u16 *pda16 = (u16 *) pda->buf;
589 int curroff; /* in 'words' */
590
591 pda->nrec = 0;
592 curroff = 0;
593 while (curroff < (HFA384x_PDA_LEN_MAX / 2) &&
594 le16_to_cpu(pda16[curroff + 1]) != HFA384x_PDR_END_OF_PDA) {
595 pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
596
597 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
598 HFA384x_PDR_NICID) {
599 memcpy(&nicid, &pda->rec[pda->nrec]->data.nicid,
600 sizeof(nicid));
601 nicid.id = le16_to_cpu(nicid.id);
602 nicid.variant = le16_to_cpu(nicid.variant);
603 nicid.major = le16_to_cpu(nicid.major);
604 nicid.minor = le16_to_cpu(nicid.minor);
605 }
606 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
607 HFA384x_PDR_MFISUPRANGE) {
608 memcpy(&rfid, &pda->rec[pda->nrec]->data.mfisuprange,
609 sizeof(rfid));
610 rfid.id = le16_to_cpu(rfid.id);
611 rfid.variant = le16_to_cpu(rfid.variant);
612 rfid.bottom = le16_to_cpu(rfid.bottom);
613 rfid.top = le16_to_cpu(rfid.top);
614 }
615 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
616 HFA384x_PDR_CFISUPRANGE) {
617 memcpy(&macid, &pda->rec[pda->nrec]->data.cfisuprange,
618 sizeof(macid));
619 macid.id = le16_to_cpu(macid.id);
620 macid.variant = le16_to_cpu(macid.variant);
621 macid.bottom = le16_to_cpu(macid.bottom);
622 macid.top = le16_to_cpu(macid.top);
623 }
624
625 (pda->nrec)++;
626 curroff += le16_to_cpu(pda16[curroff]) + 1;
627
628 }
629 if (curroff >= (HFA384x_PDA_LEN_MAX / 2)) {
630 pr_err("no end record found or invalid lengths in "
631 "PDR data, exiting. %x %d\n", curroff, pda->nrec);
632 return 1;
633 }
634 if (le16_to_cpu(pda16[curroff + 1]) == HFA384x_PDR_END_OF_PDA) {
635 pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
636 (pda->nrec)++;
637 }
638 return result;
639 }
640
641 /*----------------------------------------------------------------
642 * plugimage
643 *
644 * Plugs the given image using the given plug records from the given
645 * PDA and filename.
646 *
647 * Arguments:
648 * fchunk Array of image chunks
649 * nfchunks Number of image chunks
650 * s3plug Array of plug records
651 * ns3plug Number of plug records
652 * pda Current pda data
653 *
654 * Returns:
655 * 0 success
656 * ~0 failure
657 ----------------------------------------------------------------*/
658 static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
659 struct s3plugrec *s3plug, unsigned int ns3plug, struct pda *pda)
660 {
661 int result = 0;
662 int i; /* plug index */
663 int j; /* index of PDR or -1 if fname plug */
664 int c; /* chunk index */
665 u32 pstart;
666 u32 pend;
667 u32 cstart = 0;
668 u32 cend;
669 u32 chunkoff;
670 u8 *dest;
671
672 /* for each plug record */
673 for (i = 0; i < ns3plug; i++) {
674 pstart = s3plug[i].addr;
675 pend = s3plug[i].addr + s3plug[i].len;
676 /* find the matching PDR (or filename) */
677 if (s3plug[i].itemcode != 0xffffffffUL) { /* not filename */
678 for (j = 0; j < pda->nrec; j++) {
679 if (s3plug[i].itemcode ==
680 le16_to_cpu(pda->rec[j]->code))
681 break;
682 }
683 } else {
684 j = -1;
685 }
686 if (j >= pda->nrec && j != -1) { /* if no matching PDR, fail */
687 pr_warn("warning: Failed to find PDR for "
688 "plugrec 0x%04x.\n", s3plug[i].itemcode);
689 continue; /* and move on to the next PDR */
690 #if 0
691 /* MSM: They swear that unless it's the MAC address,
692 * the serial number, or the TX calibration records,
693 * then there's reasonable defaults in the f/w
694 * image. Therefore, missing PDRs in the card
695 * should only be a warning, not fatal.
696 * TODO: add fatals for the PDRs mentioned above.
697 */
698 result = 1;
699 continue;
700 #endif
701 }
702
703 /* Validate plug len against PDR len */
704 if (j != -1 && s3plug[i].len < le16_to_cpu(pda->rec[j]->len)) {
705 pr_err("error: Plug vs. PDR len mismatch for "
706 "plugrec 0x%04x, abort plugging.\n",
707 s3plug[i].itemcode);
708 result = 1;
709 continue;
710 }
711
712 /* Validate plug address against chunk data and identify chunk */
713 for (c = 0; c < nfchunks; c++) {
714 cstart = fchunk[c].addr;
715 cend = fchunk[c].addr + fchunk[c].len;
716 if (pstart >= cstart && pend <= cend)
717 break;
718 }
719 if (c >= nfchunks) {
720 pr_err("error: Failed to find image chunk for "
721 "plugrec 0x%04x.\n", s3plug[i].itemcode);
722 result = 1;
723 continue;
724 }
725
726 /* Plug data */
727 chunkoff = pstart - cstart;
728 dest = fchunk[c].data + chunkoff;
729 pr_debug("Plugging item 0x%04x @ 0x%06x, len=%d, "
730 "cnum=%d coff=0x%06x\n",
731 s3plug[i].itemcode, pstart, s3plug[i].len,
732 c, chunkoff);
733
734 if (j == -1) { /* plug the filename */
735 memset(dest, 0, s3plug[i].len);
736 strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
737 } else { /* plug a PDR */
738 memcpy(dest, &(pda->rec[j]->data), s3plug[i].len);
739 }
740 }
741 return result;
742
743 }
744
745 /*----------------------------------------------------------------
746 * read_cardpda
747 *
748 * Sends the command for the driver to read the pda from the card
749 * named in the device variable. Upon success, the card pda is
750 * stored in the "cardpda" variables. Note that the pda structure
751 * is considered 'well formed' after this function. That means
752 * that the nrecs is valid, the rec array has been set up, and there's
753 * a valid PDAEND record in the raw PDA data.
754 *
755 * Arguments:
756 * pda pda structure
757 * wlandev device
758 *
759 * Returns:
760 * 0 - success
761 * ~0 - failure (probably an errno)
762 ----------------------------------------------------------------*/
763 static int read_cardpda(struct pda *pda, wlandevice_t *wlandev)
764 {
765 int result = 0;
766 struct p80211msg_p2req_readpda msg;
767
768 /* set up the msg */
769 msg.msgcode = DIDmsg_p2req_readpda;
770 msg.msglen = sizeof(msg);
771 strcpy(msg.devname, wlandev->name);
772 msg.pda.did = DIDmsg_p2req_readpda_pda;
773 msg.pda.len = HFA384x_PDA_LEN_MAX;
774 msg.pda.status = P80211ENUM_msgitem_status_no_value;
775 msg.resultcode.did = DIDmsg_p2req_readpda_resultcode;
776 msg.resultcode.len = sizeof(u32);
777 msg.resultcode.status = P80211ENUM_msgitem_status_no_value;
778
779 if (prism2mgmt_readpda(wlandev, &msg) != 0) {
780 /* prism2mgmt_readpda prints an errno if appropriate */
781 result = -1;
782 } else if (msg.resultcode.data == P80211ENUM_resultcode_success) {
783 memcpy(pda->buf, msg.pda.data, HFA384x_PDA_LEN_MAX);
784 result = mkpdrlist(pda);
785 } else {
786 /* resultcode must've been something other than success */
787 result = -1;
788 }
789
790 return result;
791 }
792
793 /*----------------------------------------------------------------
794 * read_fwfile
795 *
796 * Reads the given fw file which should have been compiled from an srec
797 * file. Each record in the fw file will either be a plain data record,
798 * a start address record, or other records used for plugging.
799 *
800 * Note that data records are expected to be sorted into
801 * ascending address order in the fw file.
802 *
803 * Note also that the start address record, originally an S7 record in
804 * the srec file, is expected in the fw file to be like a data record but
805 * with a certain address to make it identifiable.
806 *
807 * Here's the SREC format that the fw should have come from:
808 * S[37]nnaaaaaaaaddd...dddcc
809 *
810 * nn - number of bytes starting with the address field
811 * aaaaaaaa - address in readable (or big endian) format
812 * dd....dd - 0-245 data bytes (two chars per byte)
813 * cc - checksum
814 *
815 * The S7 record's (there should be only one) address value gets
816 * converted to an S3 record with address of 0xff400000, with the
817 * start address being stored as a 4 byte data word. That address is
818 * the start execution address used for RAM downloads.
819 *
820 * The S3 records have a collection of subformats indicated by the
821 * value of aaaaaaaa:
822 * 0xff000000 - Plug record, data field format:
823 * xxxxxxxxaaaaaaaassssssss
824 * x - PDR code number (little endian)
825 * a - Address in load image to plug (little endian)
826 * s - Length of plug data area (little endian)
827 *
828 * 0xff100000 - CRC16 generation record, data field format:
829 * aaaaaaaassssssssbbbbbbbb
830 * a - Start address for CRC calculation (little endian)
831 * s - Length of data to calculate over (little endian)
832 * b - Boolean, true=write crc, false=don't write
833 *
834 * 0xff200000 - Info record, data field format:
835 * ssssttttdd..dd
836 * s - Size in words (little endian)
837 * t - Info type (little endian), see #defines and
838 * struct s3inforec for details about types.
839 * d - (s - 1) little endian words giving the contents of
840 * the given info type.
841 *
842 * 0xff400000 - Start address record, data field format:
843 * aaaaaaaa
844 * a - Address in load image to plug (little endian)
845 *
846 * Arguments:
847 * record firmware image (ihex record structure) in kernel memory
848 *
849 * Returns:
850 * 0 - success
851 * ~0 - failure (probably an errno)
852 ----------------------------------------------------------------*/
853 static int read_fwfile(const struct ihex_binrec *record)
854 {
855 int i;
856 int rcnt = 0;
857 u16 *tmpinfo;
858 u16 *ptr16;
859 u32 *ptr32, len, addr;
860
861 pr_debug("Reading fw file ...\n");
862
863 while (record) {
864
865 rcnt++;
866
867 len = be16_to_cpu(record->len);
868 addr = be32_to_cpu(record->addr);
869
870 /* Point into data for different word lengths */
871 ptr32 = (u32 *) record->data;
872 ptr16 = (u16 *) record->data;
873
874 /* parse what was an S3 srec and put it in the right array */
875 switch (addr) {
876 case S3ADDR_START:
877 startaddr = *ptr32;
878 pr_debug(" S7 start addr, record=%d "
879 " addr=0x%08x\n",
880 rcnt,
881 startaddr);
882 break;
883 case S3ADDR_PLUG:
884 s3plug[ns3plug].itemcode = *ptr32;
885 s3plug[ns3plug].addr = *(ptr32 + 1);
886 s3plug[ns3plug].len = *(ptr32 + 2);
887
888 pr_debug(" S3 plugrec, record=%d "
889 "itemcode=0x%08x addr=0x%08x len=%d\n",
890 rcnt,
891 s3plug[ns3plug].itemcode,
892 s3plug[ns3plug].addr,
893 s3plug[ns3plug].len);
894
895 ns3plug++;
896 if (ns3plug == S3PLUG_MAX) {
897 pr_err("S3 plugrec limit reached - aborting\n");
898 return 1;
899 }
900 break;
901 case S3ADDR_CRC:
902 s3crc[ns3crc].addr = *ptr32;
903 s3crc[ns3crc].len = *(ptr32 + 1);
904 s3crc[ns3crc].dowrite = *(ptr32 + 2);
905
906 pr_debug(" S3 crcrec, record=%d "
907 "addr=0x%08x len=%d write=0x%08x\n",
908 rcnt,
909 s3crc[ns3crc].addr,
910 s3crc[ns3crc].len,
911 s3crc[ns3crc].dowrite);
912 ns3crc++;
913 if (ns3crc == S3CRC_MAX) {
914 pr_err("S3 crcrec limit reached - aborting\n");
915 return 1;
916 }
917 break;
918 case S3ADDR_INFO:
919 s3info[ns3info].len = *ptr16;
920 s3info[ns3info].type = *(ptr16 + 1);
921
922 pr_debug(" S3 inforec, record=%d "
923 "len=0x%04x type=0x%04x\n",
924 rcnt,
925 s3info[ns3info].len,
926 s3info[ns3info].type);
927 if (((s3info[ns3info].len - 1) * sizeof(u16)) > sizeof(s3info[ns3info].info)) {
928 pr_err("S3 inforec length too long - aborting\n");
929 return 1;
930 }
931
932 tmpinfo = (u16 *)&(s3info[ns3info].info.version);
933 pr_debug(" info=");
934 for (i = 0; i < s3info[ns3info].len - 1; i++) {
935 tmpinfo[i] = *(ptr16 + 2 + i);
936 pr_debug("%04x ", tmpinfo[i]);
937 }
938 pr_debug("\n");
939
940 ns3info++;
941 if (ns3info == S3INFO_MAX) {
942 pr_err("S3 inforec limit reached - aborting\n");
943 return 1;
944 }
945 break;
946 default: /* Data record */
947 s3data[ns3data].addr = addr;
948 s3data[ns3data].len = len;
949 s3data[ns3data].data = (uint8_t *) record->data;
950 ns3data++;
951 if (ns3data == S3DATA_MAX) {
952 pr_err("S3 datarec limit reached - aborting\n");
953 return 1;
954 }
955 break;
956 }
957 record = ihex_next_binrec(record);
958 }
959 return 0;
960 }
961
962 /*----------------------------------------------------------------
963 * writeimage
964 *
965 * Takes the chunks, builds p80211 messages and sends them down
966 * to the driver for writing to the card.
967 *
968 * Arguments:
969 * wlandev device
970 * fchunk Array of image chunks
971 * nfchunks Number of image chunks
972 *
973 * Returns:
974 * 0 success
975 * ~0 failure
976 ----------------------------------------------------------------*/
977 static int writeimage(wlandevice_t *wlandev, struct imgchunk *fchunk,
978 unsigned int nfchunks)
979 {
980 int result = 0;
981 struct p80211msg_p2req_ramdl_state *rstmsg;
982 struct p80211msg_p2req_ramdl_write *rwrmsg;
983 u32 resultcode;
984 int i;
985 int j;
986 unsigned int nwrites;
987 u32 curroff;
988 u32 currlen;
989 u32 currdaddr;
990
991 rstmsg = kmalloc(sizeof(*rstmsg), GFP_KERNEL);
992 rwrmsg = kmalloc(sizeof(*rwrmsg), GFP_KERNEL);
993 if (!rstmsg || !rwrmsg) {
994 kfree(rstmsg);
995 kfree(rwrmsg);
996 netdev_err(wlandev->netdev,
997 "writeimage: no memory for firmware download, "
998 "aborting download\n");
999 return -ENOMEM;
1000 }
1001
1002 /* Initialize the messages */
1003 memset(rstmsg, 0, sizeof(*rstmsg));
1004 strcpy(rstmsg->devname, wlandev->name);
1005 rstmsg->msgcode = DIDmsg_p2req_ramdl_state;
1006 rstmsg->msglen = sizeof(*rstmsg);
1007 rstmsg->enable.did = DIDmsg_p2req_ramdl_state_enable;
1008 rstmsg->exeaddr.did = DIDmsg_p2req_ramdl_state_exeaddr;
1009 rstmsg->resultcode.did = DIDmsg_p2req_ramdl_state_resultcode;
1010 rstmsg->enable.status = P80211ENUM_msgitem_status_data_ok;
1011 rstmsg->exeaddr.status = P80211ENUM_msgitem_status_data_ok;
1012 rstmsg->resultcode.status = P80211ENUM_msgitem_status_no_value;
1013 rstmsg->enable.len = sizeof(u32);
1014 rstmsg->exeaddr.len = sizeof(u32);
1015 rstmsg->resultcode.len = sizeof(u32);
1016
1017 memset(rwrmsg, 0, sizeof(*rwrmsg));
1018 strcpy(rwrmsg->devname, wlandev->name);
1019 rwrmsg->msgcode = DIDmsg_p2req_ramdl_write;
1020 rwrmsg->msglen = sizeof(*rwrmsg);
1021 rwrmsg->addr.did = DIDmsg_p2req_ramdl_write_addr;
1022 rwrmsg->len.did = DIDmsg_p2req_ramdl_write_len;
1023 rwrmsg->data.did = DIDmsg_p2req_ramdl_write_data;
1024 rwrmsg->resultcode.did = DIDmsg_p2req_ramdl_write_resultcode;
1025 rwrmsg->addr.status = P80211ENUM_msgitem_status_data_ok;
1026 rwrmsg->len.status = P80211ENUM_msgitem_status_data_ok;
1027 rwrmsg->data.status = P80211ENUM_msgitem_status_data_ok;
1028 rwrmsg->resultcode.status = P80211ENUM_msgitem_status_no_value;
1029 rwrmsg->addr.len = sizeof(u32);
1030 rwrmsg->len.len = sizeof(u32);
1031 rwrmsg->data.len = WRITESIZE_MAX;
1032 rwrmsg->resultcode.len = sizeof(u32);
1033
1034 /* Send xxx_state(enable) */
1035 pr_debug("Sending dl_state(enable) message.\n");
1036 rstmsg->enable.data = P80211ENUM_truth_true;
1037 rstmsg->exeaddr.data = startaddr;
1038
1039 result = prism2mgmt_ramdl_state(wlandev, rstmsg);
1040 if (result) {
1041 netdev_err(wlandev->netdev,
1042 "writeimage state enable failed w/ result=%d, "
1043 "aborting download\n", result);
1044 goto free_result;
1045 }
1046 resultcode = rstmsg->resultcode.data;
1047 if (resultcode != P80211ENUM_resultcode_success) {
1048 netdev_err(wlandev->netdev,
1049 "writeimage()->xxxdl_state msg indicates failure, "
1050 "w/ resultcode=%d, aborting download.\n", resultcode);
1051 result = 1;
1052 goto free_result;
1053 }
1054
1055 /* Now, loop through the data chunks and send WRITESIZE_MAX data */
1056 for (i = 0; i < nfchunks; i++) {
1057 nwrites = fchunk[i].len / WRITESIZE_MAX;
1058 nwrites += (fchunk[i].len % WRITESIZE_MAX) ? 1 : 0;
1059 curroff = 0;
1060 for (j = 0; j < nwrites; j++) {
1061 /* TODO Move this to a separate function */
1062 int lenleft = fchunk[i].len - (WRITESIZE_MAX * j);
1063 if (fchunk[i].len > WRITESIZE_MAX)
1064 currlen = WRITESIZE_MAX;
1065 else
1066 currlen = lenleft;
1067 curroff = j * WRITESIZE_MAX;
1068 currdaddr = fchunk[i].addr + curroff;
1069 /* Setup the message */
1070 rwrmsg->addr.data = currdaddr;
1071 rwrmsg->len.data = currlen;
1072 memcpy(rwrmsg->data.data,
1073 fchunk[i].data + curroff, currlen);
1074
1075 /* Send flashdl_write(pda) */
1076 pr_debug
1077 ("Sending xxxdl_write message addr=%06x len=%d.\n",
1078 currdaddr, currlen);
1079
1080 result = prism2mgmt_ramdl_write(wlandev, rwrmsg);
1081
1082 /* Check the results */
1083 if (result) {
1084 netdev_err(wlandev->netdev,
1085 "writeimage chunk write failed w/ "
1086 "result=%d, aborting download\n", result);
1087 goto free_result;
1088 }
1089 resultcode = rstmsg->resultcode.data;
1090 if (resultcode != P80211ENUM_resultcode_success) {
1091 pr_err("writeimage()->xxxdl_write msg indicates failure, "
1092 "w/ resultcode=%d, aborting download.\n",
1093 resultcode);
1094 result = 1;
1095 goto free_result;
1096 }
1097
1098 }
1099 }
1100
1101 /* Send xxx_state(disable) */
1102 pr_debug("Sending dl_state(disable) message.\n");
1103 rstmsg->enable.data = P80211ENUM_truth_false;
1104 rstmsg->exeaddr.data = 0;
1105
1106 result = prism2mgmt_ramdl_state(wlandev, rstmsg);
1107 if (result) {
1108 netdev_err(wlandev->netdev,
1109 "writeimage state disable failed w/ result=%d, "
1110 "aborting download\n", result);
1111 goto free_result;
1112 }
1113 resultcode = rstmsg->resultcode.data;
1114 if (resultcode != P80211ENUM_resultcode_success) {
1115 netdev_err(wlandev->netdev,
1116 "writeimage()->xxxdl_state msg indicates failure, "
1117 "w/ resultcode=%d, aborting download.\n", resultcode);
1118 result = 1;
1119 goto free_result;
1120 }
1121
1122 free_result:
1123 kfree(rstmsg);
1124 kfree(rwrmsg);
1125 return result;
1126 }
1127
1128 static int validate_identity(void)
1129 {
1130 int i;
1131 int result = 1;
1132 int trump = 0;
1133
1134 pr_debug("NIC ID: %#x v%d.%d.%d\n",
1135 nicid.id, nicid.major, nicid.minor, nicid.variant);
1136 pr_debug("MFI ID: %#x v%d %d->%d\n",
1137 rfid.id, rfid.variant, rfid.bottom, rfid.top);
1138 pr_debug("CFI ID: %#x v%d %d->%d\n",
1139 macid.id, macid.variant, macid.bottom, macid.top);
1140 pr_debug("PRI ID: %#x v%d %d->%d\n",
1141 priid.id, priid.variant, priid.bottom, priid.top);
1142
1143 for (i = 0; i < ns3info; i++) {
1144 switch (s3info[i].type) {
1145 case 1:
1146 pr_debug("Version: ID %#x %d.%d.%d\n",
1147 s3info[i].info.version.id,
1148 s3info[i].info.version.major,
1149 s3info[i].info.version.minor,
1150 s3info[i].info.version.variant);
1151 break;
1152 case 2:
1153 pr_debug("Compat: Role %#x Id %#x v%d %d->%d\n",
1154 s3info[i].info.compat.role,
1155 s3info[i].info.compat.id,
1156 s3info[i].info.compat.variant,
1157 s3info[i].info.compat.bottom,
1158 s3info[i].info.compat.top);
1159
1160 /* MAC compat range */
1161 if ((s3info[i].info.compat.role == 1) &&
1162 (s3info[i].info.compat.id == 2)) {
1163 if (s3info[i].info.compat.variant !=
1164 macid.variant) {
1165 result = 2;
1166 }
1167 }
1168
1169 /* PRI compat range */
1170 if ((s3info[i].info.compat.role == 1) &&
1171 (s3info[i].info.compat.id == 3)) {
1172 if ((s3info[i].info.compat.bottom > priid.top)
1173 || (s3info[i].info.compat.top <
1174 priid.bottom)) {
1175 result = 3;
1176 }
1177 }
1178 /* SEC compat range */
1179 if ((s3info[i].info.compat.role == 1) &&
1180 (s3info[i].info.compat.id == 4)) {
1181 /* FIXME: isn't something missing here? */
1182 }
1183
1184 break;
1185 case 3:
1186 pr_debug("Seq: %#x\n", s3info[i].info.buildseq);
1187
1188 break;
1189 case 4:
1190 pr_debug("Platform: ID %#x %d.%d.%d\n",
1191 s3info[i].info.version.id,
1192 s3info[i].info.version.major,
1193 s3info[i].info.version.minor,
1194 s3info[i].info.version.variant);
1195
1196 if (nicid.id != s3info[i].info.version.id)
1197 continue;
1198 if (nicid.major != s3info[i].info.version.major)
1199 continue;
1200 if (nicid.minor != s3info[i].info.version.minor)
1201 continue;
1202 if ((nicid.variant != s3info[i].info.version.variant) &&
1203 (nicid.id != 0x8008))
1204 continue;
1205
1206 trump = 1;
1207 break;
1208 case 0x8001:
1209 pr_debug("name inforec len %d\n", s3info[i].len);
1210
1211 break;
1212 default:
1213 pr_debug("Unknown inforec type %d\n", s3info[i].type);
1214 }
1215 }
1216 /* walk through */
1217
1218 if (trump && (result != 2))
1219 result = 0;
1220 return result;
1221 }
This page took 0.097365 seconds and 5 git commands to generate.