Merge branch 'for-linus' into for-3.18/core
[firefly-linux-kernel-4.4.55.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
449         for (i = 0; i < *nfchunks; i++)
450                 kfree(fchunk[i].data);
451
452         *nfchunks = 0;
453         memset(fchunk, 0, sizeof(*fchunk));
454
455 }
456
457 /*----------------------------------------------------------------
458 * free_srecs
459 *
460 * Clears the srec data structures in preparation for a new file.
461 *
462 * Arguments:
463 *       none
464 *
465 * Returns:
466 *       nothing
467 ----------------------------------------------------------------*/
468 static void free_srecs(void)
469 {
470         ns3data = 0;
471         memset(s3data, 0, sizeof(s3data));
472         ns3plug = 0;
473         memset(s3plug, 0, sizeof(s3plug));
474         ns3crc = 0;
475         memset(s3crc, 0, sizeof(s3crc));
476         ns3info = 0;
477         memset(s3info, 0, sizeof(s3info));
478         startaddr = 0;
479 }
480
481 /*----------------------------------------------------------------
482 * mkimage
483 *
484 * Scans the currently loaded set of S records for data residing
485 * in contiguous memory regions.  Each contiguous region is then
486 * made into a 'chunk'.  This function assumes that we're building
487 * a new chunk list.  Assumes the s3data items are in sorted order.
488 *
489 * Arguments:    none
490 *
491 * Returns:
492 *       0       - success
493 *       ~0      - failure (probably an errno)
494 ----------------------------------------------------------------*/
495 static int mkimage(struct imgchunk *clist, unsigned int *ccnt)
496 {
497         int result = 0;
498         int i;
499         int j;
500         int currchunk = 0;
501         u32 nextaddr = 0;
502         u32 s3start;
503         u32 s3end;
504         u32 cstart = 0;
505         u32 cend;
506         u32 coffset;
507
508         /* There may already be data in the chunklist */
509         *ccnt = 0;
510
511         /* Establish the location and size of each chunk */
512         for (i = 0; i < ns3data; i++) {
513                 if (s3data[i].addr == nextaddr) {
514                         /* existing chunk, grow it */
515                         clist[currchunk].len += s3data[i].len;
516                         nextaddr += s3data[i].len;
517                 } else {
518                         /* New chunk */
519                         (*ccnt)++;
520                         currchunk = *ccnt - 1;
521                         clist[currchunk].addr = s3data[i].addr;
522                         clist[currchunk].len = s3data[i].len;
523                         nextaddr = s3data[i].addr + s3data[i].len;
524                         /* Expand the chunk if there is a CRC record at */
525                         /* their beginning bound */
526                         for (j = 0; j < ns3crc; j++) {
527                                 if (s3crc[j].dowrite &&
528                                     s3crc[j].addr == clist[currchunk].addr) {
529                                         clist[currchunk].addr -= 2;
530                                         clist[currchunk].len += 2;
531                                 }
532                         }
533                 }
534         }
535
536         /* We're currently assuming there aren't any overlapping chunks */
537         /*  if this proves false, we'll need to add code to coalesce. */
538
539         /* Allocate buffer space for chunks */
540         for (i = 0; i < *ccnt; i++) {
541                 clist[i].data = kzalloc(clist[i].len, GFP_KERNEL);
542                 if (clist[i].data == NULL) {
543                         pr_err("failed to allocate image space, exitting.\n");
544                         return 1;
545                 }
546                 pr_debug("chunk[%d]: addr=0x%06x len=%d\n",
547                          i, clist[i].addr, clist[i].len);
548         }
549
550         /* Copy srec data to chunks */
551         for (i = 0; i < ns3data; i++) {
552                 s3start = s3data[i].addr;
553                 s3end = s3start + s3data[i].len - 1;
554                 for (j = 0; j < *ccnt; j++) {
555                         cstart = clist[j].addr;
556                         cend = cstart + clist[j].len - 1;
557                         if (s3start >= cstart && s3end <= cend)
558                                 break;
559                 }
560                 if (((unsigned int)j) >= (*ccnt)) {
561                         pr_err("s3rec(a=0x%06x,l=%d), no chunk match, exiting.\n",
562                                s3start, s3data[i].len);
563                         return 1;
564                 }
565                 coffset = s3start - cstart;
566                 memcpy(clist[j].data + coffset, s3data[i].data, s3data[i].len);
567         }
568
569         return result;
570 }
571
572 /*----------------------------------------------------------------
573 * mkpdrlist
574 *
575 * Reads a raw PDA and builds an array of pdrec_t structures.
576 *
577 * Arguments:
578 *       pda     buffer containing raw PDA bytes
579 *       pdrec   ptr to an array of pdrec_t's.  Will be filled on exit.
580 *       nrec    ptr to a variable that will contain the count of PDRs
581 *
582 * Returns:
583 *       0       - success
584 *       ~0      - failure (probably an errno)
585 ----------------------------------------------------------------*/
586 static int mkpdrlist(struct pda *pda)
587 {
588         int result = 0;
589         u16 *pda16 = (u16 *) pda->buf;
590         int curroff;            /* in 'words' */
591
592         pda->nrec = 0;
593         curroff = 0;
594         while (curroff < (HFA384x_PDA_LEN_MAX / 2) &&
595                le16_to_cpu(pda16[curroff + 1]) != HFA384x_PDR_END_OF_PDA) {
596                 pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
597
598                 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
599                     HFA384x_PDR_NICID) {
600                         memcpy(&nicid, &pda->rec[pda->nrec]->data.nicid,
601                                sizeof(nicid));
602                         nicid.id = le16_to_cpu(nicid.id);
603                         nicid.variant = le16_to_cpu(nicid.variant);
604                         nicid.major = le16_to_cpu(nicid.major);
605                         nicid.minor = le16_to_cpu(nicid.minor);
606                 }
607                 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
608                     HFA384x_PDR_MFISUPRANGE) {
609                         memcpy(&rfid, &pda->rec[pda->nrec]->data.mfisuprange,
610                                sizeof(rfid));
611                         rfid.id = le16_to_cpu(rfid.id);
612                         rfid.variant = le16_to_cpu(rfid.variant);
613                         rfid.bottom = le16_to_cpu(rfid.bottom);
614                         rfid.top = le16_to_cpu(rfid.top);
615                 }
616                 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
617                     HFA384x_PDR_CFISUPRANGE) {
618                         memcpy(&macid, &pda->rec[pda->nrec]->data.cfisuprange,
619                                sizeof(macid));
620                         macid.id = le16_to_cpu(macid.id);
621                         macid.variant = le16_to_cpu(macid.variant);
622                         macid.bottom = le16_to_cpu(macid.bottom);
623                         macid.top = le16_to_cpu(macid.top);
624                 }
625
626                 (pda->nrec)++;
627                 curroff += le16_to_cpu(pda16[curroff]) + 1;
628
629         }
630         if (curroff >= (HFA384x_PDA_LEN_MAX / 2)) {
631                 pr_err("no end record found or invalid lengths in "
632                        "PDR data, exiting. %x %d\n", curroff, pda->nrec);
633                 return 1;
634         }
635         if (le16_to_cpu(pda16[curroff + 1]) == HFA384x_PDR_END_OF_PDA) {
636                 pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
637                 (pda->nrec)++;
638         }
639         return result;
640 }
641
642 /*----------------------------------------------------------------
643 * plugimage
644 *
645 * Plugs the given image using the given plug records from the given
646 * PDA and filename.
647 *
648 * Arguments:
649 *       fchunk          Array of image chunks
650 *       nfchunks        Number of image chunks
651 *       s3plug          Array of plug records
652 *       ns3plug         Number of plug records
653 *       pda             Current pda data
654 *
655 * Returns:
656 *       0       success
657 *       ~0      failure
658 ----------------------------------------------------------------*/
659 static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
660               struct s3plugrec *s3plug, unsigned int ns3plug, struct pda *pda)
661 {
662         int result = 0;
663         int i;                  /* plug index */
664         int j;                  /* index of PDR or -1 if fname plug */
665         int c;                  /* chunk index */
666         u32 pstart;
667         u32 pend;
668         u32 cstart = 0;
669         u32 cend;
670         u32 chunkoff;
671         u8 *dest;
672
673         /* for each plug record */
674         for (i = 0; i < ns3plug; i++) {
675                 pstart = s3plug[i].addr;
676                 pend = s3plug[i].addr + s3plug[i].len;
677                 /* find the matching PDR (or filename) */
678                 if (s3plug[i].itemcode != 0xffffffffUL) { /* not filename */
679                         for (j = 0; j < pda->nrec; j++) {
680                                 if (s3plug[i].itemcode ==
681                                     le16_to_cpu(pda->rec[j]->code))
682                                         break;
683                         }
684                 } else {
685                         j = -1;
686                 }
687                 if (j >= pda->nrec && j != -1) { /*  if no matching PDR, fail */
688                         pr_warn("warning: Failed to find PDR for "
689                                "plugrec 0x%04x.\n", s3plug[i].itemcode);
690                         continue;       /* and move on to the next PDR */
691 #if 0
692                         /* MSM: They swear that unless it's the MAC address,
693                          * the serial number, or the TX calibration records,
694                          * then there's reasonable defaults in the f/w
695                          * image.  Therefore, missing PDRs in the card
696                          * should only be a warning, not fatal.
697                          * TODO: add fatals for the PDRs mentioned above.
698                          */
699                         result = 1;
700                         continue;
701 #endif
702                 }
703
704                 /* Validate plug len against PDR len */
705                 if (j != -1 && s3plug[i].len < le16_to_cpu(pda->rec[j]->len)) {
706                         pr_err("error: Plug vs. PDR len mismatch for "
707                                "plugrec 0x%04x, abort plugging.\n",
708                                s3plug[i].itemcode);
709                         result = 1;
710                         continue;
711                 }
712
713                 /* Validate plug address against chunk data and identify chunk */
714                 for (c = 0; c < nfchunks; c++) {
715                         cstart = fchunk[c].addr;
716                         cend = fchunk[c].addr + fchunk[c].len;
717                         if (pstart >= cstart && pend <= cend)
718                                 break;
719                 }
720                 if (c >= nfchunks) {
721                         pr_err("error: Failed to find image chunk for "
722                                "plugrec 0x%04x.\n", s3plug[i].itemcode);
723                         result = 1;
724                         continue;
725                 }
726
727                 /* Plug data */
728                 chunkoff = pstart - cstart;
729                 dest = fchunk[c].data + chunkoff;
730                 pr_debug("Plugging item 0x%04x @ 0x%06x, len=%d, "
731                          "cnum=%d coff=0x%06x\n",
732                          s3plug[i].itemcode, pstart, s3plug[i].len,
733                          c, chunkoff);
734
735                 if (j == -1) {  /* plug the filename */
736                         memset(dest, 0, s3plug[i].len);
737                         strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
738                 } else {        /* plug a PDR */
739                         memcpy(dest, &(pda->rec[j]->data), s3plug[i].len);
740                 }
741         }
742         return result;
743
744 }
745
746 /*----------------------------------------------------------------
747 * read_cardpda
748 *
749 * Sends the command for the driver to read the pda from the card
750 * named in the device variable.  Upon success, the card pda is
751 * stored in the "cardpda" variables.  Note that the pda structure
752 * is considered 'well formed' after this function.  That means
753 * that the nrecs is valid, the rec array has been set up, and there's
754 * a valid PDAEND record in the raw PDA data.
755 *
756 * Arguments:
757 *       pda             pda structure
758 *       wlandev         device
759 *
760 * Returns:
761 *       0       - success
762 *       ~0      - failure (probably an errno)
763 ----------------------------------------------------------------*/
764 static int read_cardpda(struct pda *pda, wlandevice_t *wlandev)
765 {
766         int result = 0;
767         struct p80211msg_p2req_readpda msg;
768
769         /* set up the msg */
770         msg.msgcode = DIDmsg_p2req_readpda;
771         msg.msglen = sizeof(msg);
772         strcpy(msg.devname, wlandev->name);
773         msg.pda.did = DIDmsg_p2req_readpda_pda;
774         msg.pda.len = HFA384x_PDA_LEN_MAX;
775         msg.pda.status = P80211ENUM_msgitem_status_no_value;
776         msg.resultcode.did = DIDmsg_p2req_readpda_resultcode;
777         msg.resultcode.len = sizeof(u32);
778         msg.resultcode.status = P80211ENUM_msgitem_status_no_value;
779
780         if (prism2mgmt_readpda(wlandev, &msg) != 0) {
781                 /* prism2mgmt_readpda prints an errno if appropriate */
782                 result = -1;
783         } else if (msg.resultcode.data == P80211ENUM_resultcode_success) {
784                 memcpy(pda->buf, msg.pda.data, HFA384x_PDA_LEN_MAX);
785                 result = mkpdrlist(pda);
786         } else {
787                 /* resultcode must've been something other than success */
788                 result = -1;
789         }
790
791         return result;
792 }
793
794 /*----------------------------------------------------------------
795 * read_fwfile
796 *
797 * Reads the given fw file which should have been compiled from an srec
798 * file. Each record in the fw file will either be a plain data record,
799 * a start address record, or other records used for plugging.
800 *
801 * Note that data records are expected to be sorted into
802 * ascending address order in the fw file.
803 *
804 * Note also that the start address record, originally an S7 record in
805 * the srec file, is expected in the fw file to be like a data record but
806 * with a certain address to make it identifiable.
807 *
808 * Here's the SREC format that the fw should have come from:
809 * S[37]nnaaaaaaaaddd...dddcc
810 *
811 *       nn - number of bytes starting with the address field
812 * aaaaaaaa - address in readable (or big endian) format
813 * dd....dd - 0-245 data bytes (two chars per byte)
814 *       cc - checksum
815 *
816 * The S7 record's (there should be only one) address value gets
817 * converted to an S3 record with address of 0xff400000, with the
818 * start address being stored as a 4 byte data word. That address is
819 * the start execution address used for RAM downloads.
820 *
821 * The S3 records have a collection of subformats indicated by the
822 * value of aaaaaaaa:
823 *   0xff000000 - Plug record, data field format:
824 *                xxxxxxxxaaaaaaaassssssss
825 *                x - PDR code number (little endian)
826 *                a - Address in load image to plug (little endian)
827 *                s - Length of plug data area (little endian)
828 *
829 *   0xff100000 - CRC16 generation record, data field format:
830 *                aaaaaaaassssssssbbbbbbbb
831 *                a - Start address for CRC calculation (little endian)
832 *                s - Length of data to  calculate over (little endian)
833 *                b - Boolean, true=write crc, false=don't write
834 *
835 *   0xff200000 - Info record, data field format:
836 *                ssssttttdd..dd
837 *                s - Size in words (little endian)
838 *                t - Info type (little endian), see #defines and
839 *                    struct s3inforec for details about types.
840 *                d - (s - 1) little endian words giving the contents of
841 *                    the given info type.
842 *
843 *   0xff400000 - Start address record, data field format:
844 *                aaaaaaaa
845 *                a - Address in load image to plug (little endian)
846 *
847 * Arguments:
848 *       record  firmware image (ihex record structure) in kernel memory
849 *
850 * Returns:
851 *       0       - success
852 *       ~0      - failure (probably an errno)
853 ----------------------------------------------------------------*/
854 static int read_fwfile(const struct ihex_binrec *record)
855 {
856         int             i;
857         int             rcnt = 0;
858         u16             *tmpinfo;
859         u16             *ptr16;
860         u32             *ptr32, len, addr;
861
862         pr_debug("Reading fw file ...\n");
863
864         while (record) {
865
866                 rcnt++;
867
868                 len = be16_to_cpu(record->len);
869                 addr = be32_to_cpu(record->addr);
870
871                 /* Point into data for different word lengths */
872                 ptr32 = (u32 *) record->data;
873                 ptr16 = (u16 *) record->data;
874
875                 /* parse what was an S3 srec and put it in the right array */
876                 switch (addr) {
877                 case S3ADDR_START:
878                         startaddr = *ptr32;
879                         pr_debug("  S7 start addr, record=%d "
880                                       " addr=0x%08x\n",
881                                       rcnt,
882                                       startaddr);
883                         break;
884                 case S3ADDR_PLUG:
885                         s3plug[ns3plug].itemcode = *ptr32;
886                         s3plug[ns3plug].addr = *(ptr32 + 1);
887                         s3plug[ns3plug].len = *(ptr32 + 2);
888
889                         pr_debug("  S3 plugrec, record=%d "
890                                       "itemcode=0x%08x addr=0x%08x len=%d\n",
891                                       rcnt,
892                                       s3plug[ns3plug].itemcode,
893                                       s3plug[ns3plug].addr,
894                                       s3plug[ns3plug].len);
895
896                         ns3plug++;
897                         if (ns3plug == S3PLUG_MAX) {
898                                 pr_err("S3 plugrec limit reached - aborting\n");
899                                 return 1;
900                         }
901                         break;
902                 case S3ADDR_CRC:
903                         s3crc[ns3crc].addr = *ptr32;
904                         s3crc[ns3crc].len = *(ptr32 + 1);
905                         s3crc[ns3crc].dowrite = *(ptr32 + 2);
906
907                         pr_debug("  S3 crcrec, record=%d "
908                                       "addr=0x%08x len=%d write=0x%08x\n",
909                                       rcnt,
910                                       s3crc[ns3crc].addr,
911                                       s3crc[ns3crc].len,
912                                       s3crc[ns3crc].dowrite);
913                         ns3crc++;
914                         if (ns3crc == S3CRC_MAX) {
915                                 pr_err("S3 crcrec limit reached - aborting\n");
916                                 return 1;
917                         }
918                         break;
919                 case S3ADDR_INFO:
920                         s3info[ns3info].len = *ptr16;
921                         s3info[ns3info].type = *(ptr16 + 1);
922
923                         pr_debug("  S3 inforec, record=%d "
924                               "len=0x%04x type=0x%04x\n",
925                                       rcnt,
926                                       s3info[ns3info].len,
927                                       s3info[ns3info].type);
928                         if (((s3info[ns3info].len - 1) * sizeof(u16)) > sizeof(s3info[ns3info].info)) {
929                                 pr_err("S3 inforec length too long - aborting\n");
930                                 return 1;
931                         }
932
933                         tmpinfo = (u16 *)&(s3info[ns3info].info.version);
934                         pr_debug("            info=");
935                         for (i = 0; i < s3info[ns3info].len - 1; i++) {
936                                 tmpinfo[i] = *(ptr16 + 2 + i);
937                                 pr_debug("%04x ", tmpinfo[i]);
938                         }
939                         pr_debug("\n");
940
941                         ns3info++;
942                         if (ns3info == S3INFO_MAX) {
943                                 pr_err("S3 inforec limit reached - aborting\n");
944                                 return 1;
945                         }
946                         break;
947                 default:        /* Data record */
948                         s3data[ns3data].addr = addr;
949                         s3data[ns3data].len = len;
950                         s3data[ns3data].data = (uint8_t *) record->data;
951                         ns3data++;
952                         if (ns3data == S3DATA_MAX) {
953                                 pr_err("S3 datarec limit reached - aborting\n");
954                                 return 1;
955                         }
956                         break;
957                 }
958                 record = ihex_next_binrec(record);
959         }
960         return 0;
961 }
962
963 /*----------------------------------------------------------------
964 * writeimage
965 *
966 * Takes the chunks, builds p80211 messages and sends them down
967 * to the driver for writing to the card.
968 *
969 * Arguments:
970 *       wlandev         device
971 *       fchunk          Array of image chunks
972 *       nfchunks        Number of image chunks
973 *
974 * Returns:
975 *       0       success
976 *       ~0      failure
977 ----------------------------------------------------------------*/
978 static int writeimage(wlandevice_t *wlandev, struct imgchunk *fchunk,
979                unsigned int nfchunks)
980 {
981         int result = 0;
982         struct p80211msg_p2req_ramdl_state *rstmsg;
983         struct p80211msg_p2req_ramdl_write *rwrmsg;
984         u32 resultcode;
985         int i;
986         int j;
987         unsigned int nwrites;
988         u32 curroff;
989         u32 currlen;
990         u32 currdaddr;
991
992         rstmsg = kmalloc(sizeof(*rstmsg), GFP_KERNEL);
993         rwrmsg = kmalloc(sizeof(*rwrmsg), GFP_KERNEL);
994         if (!rstmsg || !rwrmsg) {
995                 kfree(rstmsg);
996                 kfree(rwrmsg);
997                 netdev_err(wlandev->netdev,
998                            "writeimage: no memory for firmware download, "
999                            "aborting download\n");
1000                 return -ENOMEM;
1001         }
1002
1003         /* Initialize the messages */
1004         memset(rstmsg, 0, sizeof(*rstmsg));
1005         strcpy(rstmsg->devname, wlandev->name);
1006         rstmsg->msgcode = DIDmsg_p2req_ramdl_state;
1007         rstmsg->msglen = sizeof(*rstmsg);
1008         rstmsg->enable.did = DIDmsg_p2req_ramdl_state_enable;
1009         rstmsg->exeaddr.did = DIDmsg_p2req_ramdl_state_exeaddr;
1010         rstmsg->resultcode.did = DIDmsg_p2req_ramdl_state_resultcode;
1011         rstmsg->enable.status = P80211ENUM_msgitem_status_data_ok;
1012         rstmsg->exeaddr.status = P80211ENUM_msgitem_status_data_ok;
1013         rstmsg->resultcode.status = P80211ENUM_msgitem_status_no_value;
1014         rstmsg->enable.len = sizeof(u32);
1015         rstmsg->exeaddr.len = sizeof(u32);
1016         rstmsg->resultcode.len = sizeof(u32);
1017
1018         memset(rwrmsg, 0, sizeof(*rwrmsg));
1019         strcpy(rwrmsg->devname, wlandev->name);
1020         rwrmsg->msgcode = DIDmsg_p2req_ramdl_write;
1021         rwrmsg->msglen = sizeof(*rwrmsg);
1022         rwrmsg->addr.did = DIDmsg_p2req_ramdl_write_addr;
1023         rwrmsg->len.did = DIDmsg_p2req_ramdl_write_len;
1024         rwrmsg->data.did = DIDmsg_p2req_ramdl_write_data;
1025         rwrmsg->resultcode.did = DIDmsg_p2req_ramdl_write_resultcode;
1026         rwrmsg->addr.status = P80211ENUM_msgitem_status_data_ok;
1027         rwrmsg->len.status = P80211ENUM_msgitem_status_data_ok;
1028         rwrmsg->data.status = P80211ENUM_msgitem_status_data_ok;
1029         rwrmsg->resultcode.status = P80211ENUM_msgitem_status_no_value;
1030         rwrmsg->addr.len = sizeof(u32);
1031         rwrmsg->len.len = sizeof(u32);
1032         rwrmsg->data.len = WRITESIZE_MAX;
1033         rwrmsg->resultcode.len = sizeof(u32);
1034
1035         /* Send xxx_state(enable) */
1036         pr_debug("Sending dl_state(enable) message.\n");
1037         rstmsg->enable.data = P80211ENUM_truth_true;
1038         rstmsg->exeaddr.data = startaddr;
1039
1040         result = prism2mgmt_ramdl_state(wlandev, rstmsg);
1041         if (result) {
1042                 netdev_err(wlandev->netdev,
1043                            "writeimage state enable failed w/ result=%d, "
1044                            "aborting download\n", result);
1045                 goto free_result;
1046         }
1047         resultcode = rstmsg->resultcode.data;
1048         if (resultcode != P80211ENUM_resultcode_success) {
1049                 netdev_err(wlandev->netdev,
1050                            "writeimage()->xxxdl_state msg indicates failure, "
1051                            "w/ resultcode=%d, aborting download.\n", resultcode);
1052                 result = 1;
1053                 goto free_result;
1054         }
1055
1056         /* Now, loop through the data chunks and send WRITESIZE_MAX data */
1057         for (i = 0; i < nfchunks; i++) {
1058                 nwrites = fchunk[i].len / WRITESIZE_MAX;
1059                 nwrites += (fchunk[i].len % WRITESIZE_MAX) ? 1 : 0;
1060                 curroff = 0;
1061                 for (j = 0; j < nwrites; j++) {
1062                         /* TODO Move this to a separate function */
1063                         int lenleft = fchunk[i].len - (WRITESIZE_MAX * j);
1064
1065                         if (fchunk[i].len > WRITESIZE_MAX)
1066                                 currlen = WRITESIZE_MAX;
1067                         else
1068                                 currlen = lenleft;
1069                         curroff = j * WRITESIZE_MAX;
1070                         currdaddr = fchunk[i].addr + curroff;
1071                         /* Setup the message */
1072                         rwrmsg->addr.data = currdaddr;
1073                         rwrmsg->len.data = currlen;
1074                         memcpy(rwrmsg->data.data,
1075                                fchunk[i].data + curroff, currlen);
1076
1077                         /* Send flashdl_write(pda) */
1078                         pr_debug
1079                             ("Sending xxxdl_write message addr=%06x len=%d.\n",
1080                              currdaddr, currlen);
1081
1082                         result = prism2mgmt_ramdl_write(wlandev, rwrmsg);
1083
1084                         /* Check the results */
1085                         if (result) {
1086                                 netdev_err(wlandev->netdev,
1087                                            "writeimage chunk write failed w/ "
1088                                            "result=%d, aborting download\n", result);
1089                                 goto free_result;
1090                         }
1091                         resultcode = rstmsg->resultcode.data;
1092                         if (resultcode != P80211ENUM_resultcode_success) {
1093                                 pr_err("writeimage()->xxxdl_write msg indicates failure, "
1094                                        "w/ resultcode=%d, aborting download.\n",
1095                                        resultcode);
1096                                 result = 1;
1097                                 goto free_result;
1098                         }
1099
1100                 }
1101         }
1102
1103         /* Send xxx_state(disable) */
1104         pr_debug("Sending dl_state(disable) message.\n");
1105         rstmsg->enable.data = P80211ENUM_truth_false;
1106         rstmsg->exeaddr.data = 0;
1107
1108         result = prism2mgmt_ramdl_state(wlandev, rstmsg);
1109         if (result) {
1110                 netdev_err(wlandev->netdev,
1111                            "writeimage state disable failed w/ result=%d, "
1112                            "aborting download\n", result);
1113                 goto free_result;
1114         }
1115         resultcode = rstmsg->resultcode.data;
1116         if (resultcode != P80211ENUM_resultcode_success) {
1117                 netdev_err(wlandev->netdev,
1118                            "writeimage()->xxxdl_state msg indicates failure, "
1119                            "w/ resultcode=%d, aborting download.\n", resultcode);
1120                 result = 1;
1121                 goto free_result;
1122         }
1123
1124 free_result:
1125         kfree(rstmsg);
1126         kfree(rwrmsg);
1127         return result;
1128 }
1129
1130 static int validate_identity(void)
1131 {
1132         int i;
1133         int result = 1;
1134         int trump = 0;
1135
1136         pr_debug("NIC ID: %#x v%d.%d.%d\n",
1137                  nicid.id, nicid.major, nicid.minor, nicid.variant);
1138         pr_debug("MFI ID: %#x v%d %d->%d\n",
1139                  rfid.id, rfid.variant, rfid.bottom, rfid.top);
1140         pr_debug("CFI ID: %#x v%d %d->%d\n",
1141                  macid.id, macid.variant, macid.bottom, macid.top);
1142         pr_debug("PRI ID: %#x v%d %d->%d\n",
1143                  priid.id, priid.variant, priid.bottom, priid.top);
1144
1145         for (i = 0; i < ns3info; i++) {
1146                 switch (s3info[i].type) {
1147                 case 1:
1148                         pr_debug("Version:  ID %#x %d.%d.%d\n",
1149                                  s3info[i].info.version.id,
1150                                  s3info[i].info.version.major,
1151                                  s3info[i].info.version.minor,
1152                                  s3info[i].info.version.variant);
1153                         break;
1154                 case 2:
1155                         pr_debug("Compat: Role %#x Id %#x v%d %d->%d\n",
1156                                  s3info[i].info.compat.role,
1157                                  s3info[i].info.compat.id,
1158                                  s3info[i].info.compat.variant,
1159                                  s3info[i].info.compat.bottom,
1160                                  s3info[i].info.compat.top);
1161
1162                         /* MAC compat range */
1163                         if ((s3info[i].info.compat.role == 1) &&
1164                             (s3info[i].info.compat.id == 2)) {
1165                                 if (s3info[i].info.compat.variant !=
1166                                     macid.variant) {
1167                                         result = 2;
1168                                 }
1169                         }
1170
1171                         /* PRI compat range */
1172                         if ((s3info[i].info.compat.role == 1) &&
1173                             (s3info[i].info.compat.id == 3)) {
1174                                 if ((s3info[i].info.compat.bottom > priid.top)
1175                                     || (s3info[i].info.compat.top <
1176                                         priid.bottom)) {
1177                                         result = 3;
1178                                 }
1179                         }
1180                         /* SEC compat range */
1181                         if ((s3info[i].info.compat.role == 1) &&
1182                             (s3info[i].info.compat.id == 4)) {
1183                                 /* FIXME: isn't something missing here? */
1184                         }
1185
1186                         break;
1187                 case 3:
1188                         pr_debug("Seq: %#x\n", s3info[i].info.buildseq);
1189
1190                         break;
1191                 case 4:
1192                         pr_debug("Platform:  ID %#x %d.%d.%d\n",
1193                                  s3info[i].info.version.id,
1194                                  s3info[i].info.version.major,
1195                                  s3info[i].info.version.minor,
1196                                  s3info[i].info.version.variant);
1197
1198                         if (nicid.id != s3info[i].info.version.id)
1199                                 continue;
1200                         if (nicid.major != s3info[i].info.version.major)
1201                                 continue;
1202                         if (nicid.minor != s3info[i].info.version.minor)
1203                                 continue;
1204                         if ((nicid.variant != s3info[i].info.version.variant) &&
1205                             (nicid.id != 0x8008))
1206                                 continue;
1207
1208                         trump = 1;
1209                         break;
1210                 case 0x8001:
1211                         pr_debug("name inforec len %d\n", s3info[i].len);
1212
1213                         break;
1214                 default:
1215                         pr_debug("Unknown inforec type %d\n", s3info[i].type);
1216                 }
1217         }
1218         /* walk through */
1219
1220         if (trump && (result != 2))
1221                 result = 0;
1222         return result;
1223 }