Merge remote-tracking branch 'pm/linux-next'
[linux-2.6/next.git] / drivers / staging / wlan-ng / prism2fw.c
blob3c40096f0c05c0ec290cdcb1f495934c57e8d264
1 /* from src/prism2/download/prism2dl.c
3 * utility for downloading prism2 images moved into kernelspace
5 * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
6 * --------------------------------------------------------------------
8 * linux-wlan
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/
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.
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.
31 * --------------------------------------------------------------------
33 * Inquiries regarding the linux-wlan Open Source project can be
34 * made directly to:
36 * AbsoluteValue Systems Inc.
37 * info@linux-wlan.com
38 * http://www.linux-wlan.com
40 * --------------------------------------------------------------------
42 * Portions of the development of this software were funded by
43 * Intersil Corporation as part of PRISM(R) chipset product development.
45 * --------------------------------------------------------------------
48 /*================================================================*/
49 /* System Includes */
50 #include <linux/ihex.h>
51 #include <linux/slab.h>
53 /*================================================================*/
54 /* Local Constants */
56 #define PRISM2_USB_FWFILE "prism2_ru.fw"
57 MODULE_FIRMWARE(PRISM2_USB_FWFILE);
59 #define S3DATA_MAX 5000
60 #define S3PLUG_MAX 200
61 #define S3CRC_MAX 200
62 #define S3INFO_MAX 50
64 #define S3ADDR_PLUG (0xff000000UL)
65 #define S3ADDR_CRC (0xff100000UL)
66 #define S3ADDR_INFO (0xff200000UL)
67 #define S3ADDR_START (0xff400000UL)
69 #define CHUNKS_MAX 100
71 #define WRITESIZE_MAX 4096
73 /*================================================================*/
74 /* Local Types */
76 struct s3datarec {
77 u32 len;
78 u32 addr;
79 u8 checksum;
80 u8 *data;
83 struct s3plugrec {
84 u32 itemcode;
85 u32 addr;
86 u32 len;
89 struct s3crcrec {
90 u32 addr;
91 u32 len;
92 unsigned int dowrite;
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;
106 struct pda {
107 u8 buf[HFA384x_PDA_LEN_MAX];
108 hfa384x_pdrec_t *rec[HFA384x_PDA_RECS_MAX];
109 unsigned int nrec;
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;
119 /*================================================================*/
120 /* Local Static Definitions */
122 /*----------------------------------------------------------------*/
123 /* s-record image processing */
125 /* Data records */
126 unsigned int ns3data;
127 struct s3datarec s3data[S3DATA_MAX];
129 /* Plug records */
130 unsigned int ns3plug;
131 struct s3plugrec s3plug[S3PLUG_MAX];
133 /* CRC records */
134 unsigned int ns3crc;
135 struct s3crcrec s3crc[S3CRC_MAX];
137 /* Info records */
138 unsigned int ns3info;
139 struct s3inforec s3info[S3INFO_MAX];
141 /* S7 record (there _better_ be only one) */
142 u32 startaddr;
144 /* Load image chunks */
145 unsigned int nfchunks;
146 struct imgchunk fchunk[CHUNKS_MAX];
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...] */
154 struct pda pda;
155 hfa384x_compident_t nicid;
156 hfa384x_caplevel_t rfid;
157 hfa384x_caplevel_t macid;
158 hfa384x_caplevel_t priid;
160 /*================================================================*/
161 /* Local Function Declarations */
163 static int prism2_fwapply(const struct ihex_binrec *rfptr,
164 wlandevice_t *wlandev);
166 static int read_fwfile(const struct ihex_binrec *rfptr);
168 static int mkimage(struct imgchunk *clist, unsigned int *ccnt);
170 static int read_cardpda(struct pda *pda, wlandevice_t *wlandev);
172 static int mkpdrlist(struct pda *pda);
174 static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
175 struct s3plugrec *s3plug, unsigned int ns3plug, struct pda * pda);
177 static int crcimage(struct imgchunk *fchunk, unsigned int nfchunks,
178 struct s3crcrec *s3crc, unsigned int ns3crc);
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);
184 static void free_srecs(void);
186 static int validate_identity(void);
188 /*================================================================*/
189 /* Function Definitions */
191 /*----------------------------------------------------------------
192 * prism2_fwtry
194 * Try and get firmware into memory
196 * Arguments:
197 * udev usb device structure
198 * wlandev wlan device structure
200 * Returns:
201 * 0 - success
202 * ~0 - failure
203 ----------------------------------------------------------------*/
204 int prism2_fwtry(struct usb_device *udev, wlandevice_t *wlandev)
206 const struct firmware *fw_entry = NULL;
208 printk(KERN_INFO "prism2_usb: Checking for firmware %s\n",
209 PRISM2_USB_FWFILE);
210 if (request_ihex_firmware(&fw_entry, PRISM2_USB_FWFILE, &udev->dev) != 0) {
211 printk(KERN_INFO
212 "prism2_usb: Firmware not available, but not essential\n");
213 printk(KERN_INFO
214 "prism2_usb: can continue to use card anyway.\n");
215 return 1;
218 printk(KERN_INFO "prism2_usb: %s will be processed, size %zu\n",
219 PRISM2_USB_FWFILE, fw_entry->size);
220 prism2_fwapply((const struct ihex_binrec *)fw_entry->data, wlandev);
222 release_firmware(fw_entry);
223 return 0;
226 /*----------------------------------------------------------------
227 * prism2_fwapply
229 * Apply the firmware loaded into memory
231 * Arguments:
232 * rfptr firmware image in kernel memory
233 * wlandev device
235 * Returns:
236 * 0 - success
237 * ~0 - failure
238 ----------------------------------------------------------------*/
239 int prism2_fwapply(const struct ihex_binrec *rfptr, wlandevice_t *wlandev)
241 signed int result = 0;
242 struct p80211msg_dot11req_mibget getmsg;
243 p80211itemd_t *item;
244 u32 *data;
246 /* Initialize the data structures */
247 ns3data = 0;
248 memset(s3data, 0, sizeof(s3data));
249 ns3plug = 0;
250 memset(s3plug, 0, sizeof(s3plug));
251 ns3crc = 0;
252 memset(s3crc, 0, sizeof(s3crc));
253 ns3info = 0;
254 memset(s3info, 0, sizeof(s3info));
255 startaddr = 0;
257 nfchunks = 0;
258 memset(fchunk, 0, sizeof(fchunk));
259 memset(&nicid, 0, sizeof(nicid));
260 memset(&rfid, 0, sizeof(rfid));
261 memset(&macid, 0, sizeof(macid));
262 memset(&priid, 0, sizeof(priid));
264 /* clear the pda and add an initial END record */
265 memset(&pda, 0, sizeof(pda));
266 pda.rec[0] = (hfa384x_pdrec_t *) pda.buf;
267 pda.rec[0]->len = cpu_to_le16(2); /* len in words */
268 pda.rec[0]->code = cpu_to_le16(HFA384x_PDR_END_OF_PDA);
269 pda.nrec = 1;
271 /*-----------------------------------------------------*/
272 /* Put card into fwload state */
273 prism2sta_ifstate(wlandev, P80211ENUM_ifstate_fwload);
275 /* Build the PDA we're going to use. */
276 if (read_cardpda(&pda, wlandev)) {
277 printk(KERN_ERR "load_cardpda failed, exiting.\n");
278 return 1;
281 /* read the card's PRI-SUP */
282 memset(&getmsg, 0, sizeof(getmsg));
283 getmsg.msgcode = DIDmsg_dot11req_mibget;
284 getmsg.msglen = sizeof(getmsg);
285 strcpy(getmsg.devname, wlandev->name);
287 getmsg.mibattribute.did = DIDmsg_dot11req_mibget_mibattribute;
288 getmsg.mibattribute.status = P80211ENUM_msgitem_status_data_ok;
289 getmsg.resultcode.did = DIDmsg_dot11req_mibget_resultcode;
290 getmsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
292 item = (p80211itemd_t *) getmsg.mibattribute.data;
293 item->did = DIDmib_p2_p2NIC_p2PRISupRange;
294 item->status = P80211ENUM_msgitem_status_no_value;
296 data = (u32 *) item->data;
298 /* DIDmsg_dot11req_mibget */
299 prism2mgmt_mibset_mibget(wlandev, &getmsg);
300 if (getmsg.resultcode.data != P80211ENUM_resultcode_success)
301 printk(KERN_ERR "Couldn't fetch PRI-SUP info\n");
303 /* Already in host order */
304 priid.role = *data++;
305 priid.id = *data++;
306 priid.variant = *data++;
307 priid.bottom = *data++;
308 priid.top = *data++;
310 /* Read the S3 file */
311 result = read_fwfile(rfptr);
312 if (result) {
313 printk(KERN_ERR "Failed to read the data exiting.\n");
314 return 1;
317 result = validate_identity();
319 if (result) {
320 printk(KERN_ERR "Incompatible firmware image.\n");
321 return 1;
324 if (startaddr == 0x00000000) {
325 printk(KERN_ERR "Can't RAM download a Flash image!\n");
326 return 1;
329 /* Make the image chunks */
330 result = mkimage(fchunk, &nfchunks);
332 /* Do any plugging */
333 result = plugimage(fchunk, nfchunks, s3plug, ns3plug, &pda);
334 if (result) {
335 printk(KERN_ERR "Failed to plug data.\n");
336 return 1;
339 /* Insert any CRCs */
340 if (crcimage(fchunk, nfchunks, s3crc, ns3crc)) {
341 printk(KERN_ERR "Failed to insert all CRCs\n");
342 return 1;
345 /* Write the image */
346 result = writeimage(wlandev, fchunk, nfchunks);
347 if (result) {
348 printk(KERN_ERR "Failed to ramwrite image data.\n");
349 return 1;
352 /* clear any allocated memory */
353 free_chunks(fchunk, &nfchunks);
354 free_srecs();
356 printk(KERN_INFO "prism2_usb: firmware loading finished.\n");
358 return result;
361 /*----------------------------------------------------------------
362 * crcimage
364 * Adds a CRC16 in the two bytes prior to each block identified by
365 * an S3 CRC record. Currently, we don't actually do a CRC we just
366 * insert the value 0xC0DE in hfa384x order.
368 * Arguments:
369 * fchunk Array of image chunks
370 * nfchunks Number of image chunks
371 * s3crc Array of crc records
372 * ns3crc Number of crc records
374 * Returns:
375 * 0 success
376 * ~0 failure
377 ----------------------------------------------------------------*/
378 int crcimage(struct imgchunk *fchunk, unsigned int nfchunks,
379 struct s3crcrec *s3crc, unsigned int ns3crc)
381 int result = 0;
382 int i;
383 int c;
384 u32 crcstart;
385 u32 crcend;
386 u32 cstart = 0;
387 u32 cend;
388 u8 *dest;
389 u32 chunkoff;
391 for (i = 0; i < ns3crc; i++) {
392 if (!s3crc[i].dowrite)
393 continue;
394 crcstart = s3crc[i].addr;
395 crcend = s3crc[i].addr + s3crc[i].len;
396 /* Find chunk */
397 for (c = 0; c < nfchunks; c++) {
398 cstart = fchunk[c].addr;
399 cend = fchunk[c].addr + fchunk[c].len;
400 /* the line below does an address & len match search */
401 /* unfortunately, I've found that the len fields of */
402 /* some crc records don't match with the length of */
403 /* the actual data, so we're not checking right now */
404 /* if (crcstart-2 >= cstart && crcend <= cend) break; */
406 /* note the -2 below, it's to make sure the chunk has */
407 /* space for the CRC value */
408 if (crcstart - 2 >= cstart && crcstart < cend)
409 break;
411 if (c >= nfchunks) {
412 printk(KERN_ERR
413 "Failed to find chunk for "
414 "crcrec[%d], addr=0x%06x len=%d , "
415 "aborting crc.\n",
416 i, s3crc[i].addr, s3crc[i].len);
417 return 1;
420 /* Insert crc */
421 pr_debug("Adding crc @ 0x%06x\n", s3crc[i].addr - 2);
422 chunkoff = crcstart - cstart - 2;
423 dest = fchunk[c].data + chunkoff;
424 *dest = 0xde;
425 *(dest + 1) = 0xc0;
428 return result;
431 /*----------------------------------------------------------------
432 * free_chunks
434 * Clears the chunklist data structures in preparation for a new file.
436 * Arguments:
437 * none
439 * Returns:
440 * nothing
441 ----------------------------------------------------------------*/
442 void free_chunks(struct imgchunk *fchunk, unsigned int *nfchunks)
444 int i;
445 for (i = 0; i < *nfchunks; i++)
446 kfree(fchunk[i].data);
448 *nfchunks = 0;
449 memset(fchunk, 0, sizeof(*fchunk));
453 /*----------------------------------------------------------------
454 * free_srecs
456 * Clears the srec data structures in preparation for a new file.
458 * Arguments:
459 * none
461 * Returns:
462 * nothing
463 ----------------------------------------------------------------*/
464 void free_srecs(void)
466 ns3data = 0;
467 memset(s3data, 0, sizeof(s3data));
468 ns3plug = 0;
469 memset(s3plug, 0, sizeof(s3plug));
470 ns3crc = 0;
471 memset(s3crc, 0, sizeof(s3crc));
472 ns3info = 0;
473 memset(s3info, 0, sizeof(s3info));
474 startaddr = 0;
477 /*----------------------------------------------------------------
478 * mkimage
480 * Scans the currently loaded set of S records for data residing
481 * in contiguous memory regions. Each contiguous region is then
482 * made into a 'chunk'. This function assumes that we're building
483 * a new chunk list. Assumes the s3data items are in sorted order.
485 * Arguments: none
487 * Returns:
488 * 0 - success
489 * ~0 - failure (probably an errno)
490 ----------------------------------------------------------------*/
491 int mkimage(struct imgchunk *clist, unsigned int *ccnt)
493 int result = 0;
494 int i;
495 int j;
496 int currchunk = 0;
497 u32 nextaddr = 0;
498 u32 s3start;
499 u32 s3end;
500 u32 cstart = 0;
501 u32 cend;
502 u32 coffset;
504 /* There may already be data in the chunklist */
505 *ccnt = 0;
507 /* Establish the location and size of each chunk */
508 for (i = 0; i < ns3data; i++) {
509 if (s3data[i].addr == nextaddr) {
510 /* existing chunk, grow it */
511 clist[currchunk].len += s3data[i].len;
512 nextaddr += s3data[i].len;
513 } else {
514 /* New chunk */
515 (*ccnt)++;
516 currchunk = *ccnt - 1;
517 clist[currchunk].addr = s3data[i].addr;
518 clist[currchunk].len = s3data[i].len;
519 nextaddr = s3data[i].addr + s3data[i].len;
520 /* Expand the chunk if there is a CRC record at */
521 /* their beginning bound */
522 for (j = 0; j < ns3crc; j++) {
523 if (s3crc[j].dowrite &&
524 s3crc[j].addr == clist[currchunk].addr) {
525 clist[currchunk].addr -= 2;
526 clist[currchunk].len += 2;
532 /* We're currently assuming there aren't any overlapping chunks */
533 /* if this proves false, we'll need to add code to coalesce. */
535 /* Allocate buffer space for chunks */
536 for (i = 0; i < *ccnt; i++) {
537 clist[i].data = kzalloc(clist[i].len, GFP_KERNEL);
538 if (clist[i].data == NULL) {
539 printk(KERN_ERR
540 "failed to allocate image space, exitting.\n");
541 return 1;
543 pr_debug("chunk[%d]: addr=0x%06x len=%d\n",
544 i, clist[i].addr, clist[i].len);
547 /* Copy srec data to chunks */
548 for (i = 0; i < ns3data; i++) {
549 s3start = s3data[i].addr;
550 s3end = s3start + s3data[i].len - 1;
551 for (j = 0; j < *ccnt; j++) {
552 cstart = clist[j].addr;
553 cend = cstart + clist[j].len - 1;
554 if (s3start >= cstart && s3end <= cend)
555 break;
557 if (((unsigned int)j) >= (*ccnt)) {
558 printk(KERN_ERR
559 "s3rec(a=0x%06x,l=%d), no chunk match, exiting.\n",
560 s3start, s3data[i].len);
561 return 1;
563 coffset = s3start - cstart;
564 memcpy(clist[j].data + coffset, s3data[i].data, s3data[i].len);
567 return result;
570 /*----------------------------------------------------------------
571 * mkpdrlist
573 * Reads a raw PDA and builds an array of pdrec_t structures.
575 * Arguments:
576 * pda buffer containing raw PDA bytes
577 * pdrec ptr to an array of pdrec_t's. Will be filled on exit.
578 * nrec ptr to a variable that will contain the count of PDRs
580 * Returns:
581 * 0 - success
582 * ~0 - failure (probably an errno)
583 ----------------------------------------------------------------*/
584 int mkpdrlist(struct pda *pda)
586 int result = 0;
587 u16 *pda16 = (u16 *) pda->buf;
588 int curroff; /* in 'words' */
590 pda->nrec = 0;
591 curroff = 0;
592 while (curroff < (HFA384x_PDA_LEN_MAX / 2) &&
593 le16_to_cpu(pda16[curroff + 1]) != HFA384x_PDR_END_OF_PDA) {
594 pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
596 if (le16_to_cpu(pda->rec[pda->nrec]->code) == HFA384x_PDR_NICID) {
597 memcpy(&nicid, &pda->rec[pda->nrec]->data.nicid,
598 sizeof(nicid));
599 nicid.id = le16_to_cpu(nicid.id);
600 nicid.variant = le16_to_cpu(nicid.variant);
601 nicid.major = le16_to_cpu(nicid.major);
602 nicid.minor = le16_to_cpu(nicid.minor);
604 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
605 HFA384x_PDR_MFISUPRANGE) {
606 memcpy(&rfid, &pda->rec[pda->nrec]->data.mfisuprange,
607 sizeof(rfid));
608 rfid.id = le16_to_cpu(rfid.id);
609 rfid.variant = le16_to_cpu(rfid.variant);
610 rfid.bottom = le16_to_cpu(rfid.bottom);
611 rfid.top = le16_to_cpu(rfid.top);
613 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
614 HFA384x_PDR_CFISUPRANGE) {
615 memcpy(&macid, &pda->rec[pda->nrec]->data.cfisuprange,
616 sizeof(macid));
617 macid.id = le16_to_cpu(macid.id);
618 macid.variant = le16_to_cpu(macid.variant);
619 macid.bottom = le16_to_cpu(macid.bottom);
620 macid.top = le16_to_cpu(macid.top);
623 (pda->nrec)++;
624 curroff += le16_to_cpu(pda16[curroff]) + 1;
627 if (curroff >= (HFA384x_PDA_LEN_MAX / 2)) {
628 printk(KERN_ERR
629 "no end record found or invalid lengths in "
630 "PDR data, exiting. %x %d\n", curroff, pda->nrec);
631 return 1;
633 if (le16_to_cpu(pda16[curroff + 1]) == HFA384x_PDR_END_OF_PDA) {
634 pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
635 (pda->nrec)++;
637 return result;
640 /*----------------------------------------------------------------
641 * plugimage
643 * Plugs the given image using the given plug records from the given
644 * PDA and filename.
646 * Arguments:
647 * fchunk Array of image chunks
648 * nfchunks Number of image chunks
649 * s3plug Array of plug records
650 * ns3plug Number of plug records
651 * pda Current pda data
653 * Returns:
654 * 0 success
655 * ~0 failure
656 ----------------------------------------------------------------*/
657 int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
658 struct s3plugrec *s3plug, unsigned int ns3plug, struct pda * pda)
660 int result = 0;
661 int i; /* plug index */
662 int j; /* index of PDR or -1 if fname plug */
663 int c; /* chunk index */
664 u32 pstart;
665 u32 pend;
666 u32 cstart = 0;
667 u32 cend;
668 u32 chunkoff;
669 u8 *dest;
671 /* for each plug record */
672 for (i = 0; i < ns3plug; i++) {
673 pstart = s3plug[i].addr;
674 pend = s3plug[i].addr + s3plug[i].len;
675 /* find the matching PDR (or filename) */
676 if (s3plug[i].itemcode != 0xffffffffUL) { /* not filename */
677 for (j = 0; j < pda->nrec; j++) {
678 if (s3plug[i].itemcode ==
679 le16_to_cpu(pda->rec[j]->code))
680 break;
682 } else {
683 j = -1;
685 if (j >= pda->nrec && j != -1) { /* if no matching PDR, fail */
686 printk(KERN_WARNING
687 "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.
698 result = 1;
699 continue;
700 #endif
703 /* Validate plug len against PDR len */
704 if (j != -1 && s3plug[i].len < le16_to_cpu(pda->rec[j]->len)) {
705 printk(KERN_ERR
706 "error: Plug vs. PDR len mismatch for "
707 "plugrec 0x%04x, abort plugging.\n",
708 s3plug[i].itemcode);
709 result = 1;
710 continue;
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;
720 if (c >= nfchunks) {
721 printk(KERN_ERR
722 "error: Failed to find image chunk for "
723 "plugrec 0x%04x.\n", s3plug[i].itemcode);
724 result = 1;
725 continue;
728 /* Plug data */
729 chunkoff = pstart - cstart;
730 dest = fchunk[c].data + chunkoff;
731 pr_debug("Plugging item 0x%04x @ 0x%06x, len=%d, "
732 "cnum=%d coff=0x%06x\n",
733 s3plug[i].itemcode, pstart, s3plug[i].len,
734 c, chunkoff);
736 if (j == -1) { /* plug the filename */
737 memset(dest, 0, s3plug[i].len);
738 strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
739 } else { /* plug a PDR */
740 memcpy(dest, &(pda->rec[j]->data), s3plug[i].len);
743 return result;
747 /*----------------------------------------------------------------
748 * read_cardpda
750 * Sends the command for the driver to read the pda from the card
751 * named in the device variable. Upon success, the card pda is
752 * stored in the "cardpda" variables. Note that the pda structure
753 * is considered 'well formed' after this function. That means
754 * that the nrecs is valid, the rec array has been set up, and there's
755 * a valid PDAEND record in the raw PDA data.
757 * Arguments:
758 * pda pda structure
759 * wlandev device
761 * Returns:
762 * 0 - success
763 * ~0 - failure (probably an errno)
764 ----------------------------------------------------------------*/
765 int read_cardpda(struct pda *pda, wlandevice_t *wlandev)
767 int result = 0;
768 struct p80211msg_p2req_readpda msg;
770 /* set up the msg */
771 msg.msgcode = DIDmsg_p2req_readpda;
772 msg.msglen = sizeof(msg);
773 strcpy(msg.devname, wlandev->name);
774 msg.pda.did = DIDmsg_p2req_readpda_pda;
775 msg.pda.len = HFA384x_PDA_LEN_MAX;
776 msg.pda.status = P80211ENUM_msgitem_status_no_value;
777 msg.resultcode.did = DIDmsg_p2req_readpda_resultcode;
778 msg.resultcode.len = sizeof(u32);
779 msg.resultcode.status = P80211ENUM_msgitem_status_no_value;
781 if (prism2mgmt_readpda(wlandev, &msg) != 0) {
782 /* prism2mgmt_readpda prints an errno if appropriate */
783 result = -1;
784 } else if (msg.resultcode.data == P80211ENUM_resultcode_success) {
785 memcpy(pda->buf, msg.pda.data, HFA384x_PDA_LEN_MAX);
786 result = mkpdrlist(pda);
787 } else {
788 /* resultcode must've been something other than success */
789 result = -1;
792 return result;
795 /*----------------------------------------------------------------
796 * read_fwfile
798 * Reads the given fw file which should have been compiled from an srec
799 * file. Each record in the fw file will either be a plain data record,
800 * a start address record, or other records used for plugging.
802 * Note that data records are expected to be sorted into
803 * ascending address order in the fw file.
805 * Note also that the start address record, originally an S7 record in
806 * the srec file, is expected in the fw file to be like a data record but
807 * with a certain address to make it identiable.
809 * Here's the SREC format that the fw should have come from:
810 * S[37]nnaaaaaaaaddd...dddcc
812 * nn - number of bytes starting with the address field
813 * aaaaaaaa - address in readable (or big endian) format
814 * dd....dd - 0-245 data bytes (two chars per byte)
815 * cc - checksum
817 * The S7 record's (there should be only one) address value gets
818 * converted to an S3 record with address of 0xff400000, with the
819 * start address being stored as a 4 byte data word. That address is
820 * the start execution address used for RAM downloads.
822 * The S3 records have a collection of subformats indicated by the
823 * value of aaaaaaaa:
824 * 0xff000000 - Plug record, data field format:
825 * xxxxxxxxaaaaaaaassssssss
826 * x - PDR code number (little endian)
827 * a - Address in load image to plug (little endian)
828 * s - Length of plug data area (little endian)
830 * 0xff100000 - CRC16 generation record, data field format:
831 * aaaaaaaassssssssbbbbbbbb
832 * a - Start address for CRC calculation (little endian)
833 * s - Length of data to calculate over (little endian)
834 * b - Boolean, true=write crc, false=don't write
836 * 0xff200000 - Info record, data field format:
837 * ssssttttdd..dd
838 * s - Size in words (little endian)
839 * t - Info type (little endian), see #defines and
840 * struct s3inforec for details about types.
841 * d - (s - 1) little endian words giving the contents of
842 * the given info type.
844 * 0xff400000 - Start address record, data field format:
845 * aaaaaaaa
846 * a - Address in load image to plug (little endian)
848 * Arguments:
849 * record firmware image (ihex record structure) in kernel memory
851 * Returns:
852 * 0 - success
853 * ~0 - failure (probably an errno)
854 ----------------------------------------------------------------*/
855 int read_fwfile(const struct ihex_binrec *record)
857 int i;
858 int rcnt = 0;
859 u16 *tmpinfo;
860 u16 *ptr16;
861 u32 *ptr32, len, addr;
863 pr_debug("Reading fw file ...\n");
865 while (record) {
867 rcnt++;
869 len = be16_to_cpu(record->len);
870 addr = be32_to_cpu(record->addr);
872 /* Point into data for different word lengths */
873 ptr32 = (u32 *) record->data;
874 ptr16 = (u16 *) record->data;
876 /* parse what was an S3 srec and put it in the right array */
877 switch (addr) {
878 case S3ADDR_START:
879 startaddr = *ptr32;
880 pr_debug(" S7 start addr, record=%d "
881 " addr=0x%08x\n",
882 rcnt,
883 startaddr);
884 break;
885 case S3ADDR_PLUG:
886 s3plug[ns3plug].itemcode = *ptr32;
887 s3plug[ns3plug].addr = *(ptr32 + 1);
888 s3plug[ns3plug].len = *(ptr32 + 2);
890 pr_debug(" S3 plugrec, record=%d "
891 "itemcode=0x%08x addr=0x%08x len=%d\n",
892 rcnt,
893 s3plug[ns3plug].itemcode,
894 s3plug[ns3plug].addr,
895 s3plug[ns3plug].len);
897 ns3plug++;
898 if (ns3plug == S3PLUG_MAX) {
899 printk(KERN_ERR "S3 plugrec limit reached - aborting\n");
900 return 1;
902 break;
903 case S3ADDR_CRC:
904 s3crc[ns3crc].addr = *ptr32;
905 s3crc[ns3crc].len = *(ptr32 + 1);
906 s3crc[ns3crc].dowrite = *(ptr32 + 2);
908 pr_debug(" S3 crcrec, record=%d "
909 "addr=0x%08x len=%d write=0x%08x\n",
910 rcnt,
911 s3crc[ns3crc].addr,
912 s3crc[ns3crc].len,
913 s3crc[ns3crc].dowrite);
914 ns3crc++;
915 if (ns3crc == S3CRC_MAX) {
916 printk(KERN_ERR "S3 crcrec limit reached - aborting\n");
917 return 1;
919 break;
920 case S3ADDR_INFO:
921 s3info[ns3info].len = *ptr16;
922 s3info[ns3info].type = *(ptr16 + 1);
924 pr_debug(" S3 inforec, record=%d "
925 "len=0x%04x type=0x%04x\n",
926 rcnt,
927 s3info[ns3info].len,
928 s3info[ns3info].type);
929 if (((s3info[ns3info].len - 1) * sizeof(u16)) > sizeof(s3info[ns3info].info)) {
930 printk(KERN_ERR " S3 inforec length too long - aborting\n");
931 return 1;
934 tmpinfo = (u16 *)&(s3info[ns3info].info.version);
935 pr_debug(" info=");
936 for (i = 0; i < s3info[ns3info].len - 1; i++) {
937 tmpinfo[i] = *(ptr16 + 2 + i);
938 pr_debug("%04x ", tmpinfo[i]);
940 pr_debug("\n");
942 ns3info++;
943 if (ns3info == S3INFO_MAX) {
944 printk(KERN_ERR "S3 inforec limit reached - aborting\n");
945 return 1;
947 break;
948 default: /* Data record */
949 s3data[ns3data].addr = addr;
950 s3data[ns3data].len = len;
951 s3data[ns3data].data = (uint8_t *) record->data;
952 ns3data++;
953 if (ns3data == S3DATA_MAX) {
954 printk(KERN_ERR "S3 datarec limit reached - aborting\n");
955 return 1;
957 break;
959 record = ihex_next_binrec(record);
961 return 0;
964 /*----------------------------------------------------------------
965 * writeimage
967 * Takes the chunks, builds p80211 messages and sends them down
968 * to the driver for writing to the card.
970 * Arguments:
971 * wlandev device
972 * fchunk Array of image chunks
973 * nfchunks Number of image chunks
975 * Returns:
976 * 0 success
977 * ~0 failure
978 ----------------------------------------------------------------*/
979 int writeimage(wlandevice_t *wlandev, struct imgchunk *fchunk,
980 unsigned int nfchunks)
982 int result = 0;
983 struct p80211msg_p2req_ramdl_state rstatemsg;
984 struct p80211msg_p2req_ramdl_write rwritemsg;
985 struct p80211msg *msgp;
986 u32 resultcode;
987 int i;
988 int j;
989 unsigned int nwrites;
990 u32 curroff;
991 u32 currlen;
992 u32 currdaddr;
994 /* Initialize the messages */
995 memset(&rstatemsg, 0, sizeof(rstatemsg));
996 strcpy(rstatemsg.devname, wlandev->name);
997 rstatemsg.msgcode = DIDmsg_p2req_ramdl_state;
998 rstatemsg.msglen = sizeof(rstatemsg);
999 rstatemsg.enable.did = DIDmsg_p2req_ramdl_state_enable;
1000 rstatemsg.exeaddr.did = DIDmsg_p2req_ramdl_state_exeaddr;
1001 rstatemsg.resultcode.did = DIDmsg_p2req_ramdl_state_resultcode;
1002 rstatemsg.enable.status = P80211ENUM_msgitem_status_data_ok;
1003 rstatemsg.exeaddr.status = P80211ENUM_msgitem_status_data_ok;
1004 rstatemsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
1005 rstatemsg.enable.len = sizeof(u32);
1006 rstatemsg.exeaddr.len = sizeof(u32);
1007 rstatemsg.resultcode.len = sizeof(u32);
1009 memset(&rwritemsg, 0, sizeof(rwritemsg));
1010 strcpy(rwritemsg.devname, wlandev->name);
1011 rwritemsg.msgcode = DIDmsg_p2req_ramdl_write;
1012 rwritemsg.msglen = sizeof(rwritemsg);
1013 rwritemsg.addr.did = DIDmsg_p2req_ramdl_write_addr;
1014 rwritemsg.len.did = DIDmsg_p2req_ramdl_write_len;
1015 rwritemsg.data.did = DIDmsg_p2req_ramdl_write_data;
1016 rwritemsg.resultcode.did = DIDmsg_p2req_ramdl_write_resultcode;
1017 rwritemsg.addr.status = P80211ENUM_msgitem_status_data_ok;
1018 rwritemsg.len.status = P80211ENUM_msgitem_status_data_ok;
1019 rwritemsg.data.status = P80211ENUM_msgitem_status_data_ok;
1020 rwritemsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
1021 rwritemsg.addr.len = sizeof(u32);
1022 rwritemsg.len.len = sizeof(u32);
1023 rwritemsg.data.len = WRITESIZE_MAX;
1024 rwritemsg.resultcode.len = sizeof(u32);
1026 /* Send xxx_state(enable) */
1027 pr_debug("Sending dl_state(enable) message.\n");
1028 rstatemsg.enable.data = P80211ENUM_truth_true;
1029 rstatemsg.exeaddr.data = startaddr;
1031 msgp = (struct p80211msg *) &rstatemsg;
1032 result = prism2mgmt_ramdl_state(wlandev, msgp);
1033 if (result) {
1034 printk(KERN_ERR
1035 "writeimage state enable failed w/ result=%d, "
1036 "aborting download\n", result);
1037 return result;
1039 resultcode = rstatemsg.resultcode.data;
1040 if (resultcode != P80211ENUM_resultcode_success) {
1041 printk(KERN_ERR
1042 "writeimage()->xxxdl_state msg indicates failure, "
1043 "w/ resultcode=%d, aborting download.\n", resultcode);
1044 return 1;
1047 /* Now, loop through the data chunks and send WRITESIZE_MAX data */
1048 for (i = 0; i < nfchunks; i++) {
1049 nwrites = fchunk[i].len / WRITESIZE_MAX;
1050 nwrites += (fchunk[i].len % WRITESIZE_MAX) ? 1 : 0;
1051 curroff = 0;
1052 for (j = 0; j < nwrites; j++) {
1053 /* TODO Move this to a separate function */
1054 int lenleft = fchunk[i].len - (WRITESIZE_MAX * j);
1055 if (fchunk[i].len > WRITESIZE_MAX)
1056 currlen = WRITESIZE_MAX;
1057 else
1058 currlen = lenleft;
1059 curroff = j * WRITESIZE_MAX;
1060 currdaddr = fchunk[i].addr + curroff;
1061 /* Setup the message */
1062 rwritemsg.addr.data = currdaddr;
1063 rwritemsg.len.data = currlen;
1064 memcpy(rwritemsg.data.data,
1065 fchunk[i].data + curroff, currlen);
1067 /* Send flashdl_write(pda) */
1068 pr_debug
1069 ("Sending xxxdl_write message addr=%06x len=%d.\n",
1070 currdaddr, currlen);
1072 msgp = (struct p80211msg *) &rwritemsg;
1073 result = prism2mgmt_ramdl_write(wlandev, msgp);
1075 /* Check the results */
1076 if (result) {
1077 printk(KERN_ERR
1078 "writeimage chunk write failed w/ result=%d, "
1079 "aborting download\n", result);
1080 return result;
1082 resultcode = rstatemsg.resultcode.data;
1083 if (resultcode != P80211ENUM_resultcode_success) {
1084 printk(KERN_ERR
1085 "writeimage()->xxxdl_write msg indicates failure, "
1086 "w/ resultcode=%d, aborting download.\n",
1087 resultcode);
1088 return 1;
1094 /* Send xxx_state(disable) */
1095 pr_debug("Sending dl_state(disable) message.\n");
1096 rstatemsg.enable.data = P80211ENUM_truth_false;
1097 rstatemsg.exeaddr.data = 0;
1099 msgp = (struct p80211msg *) &rstatemsg;
1100 result = prism2mgmt_ramdl_state(wlandev, msgp);
1101 if (result) {
1102 printk(KERN_ERR
1103 "writeimage state disable failed w/ result=%d, "
1104 "aborting download\n", result);
1105 return result;
1107 resultcode = rstatemsg.resultcode.data;
1108 if (resultcode != P80211ENUM_resultcode_success) {
1109 printk(KERN_ERR
1110 "writeimage()->xxxdl_state msg indicates failure, "
1111 "w/ resultcode=%d, aborting download.\n", resultcode);
1112 return 1;
1114 return result;
1117 int validate_identity(void)
1119 int i;
1120 int result = 1;
1121 int trump = 0;
1123 pr_debug("NIC ID: %#x v%d.%d.%d\n",
1124 nicid.id, nicid.major, nicid.minor, nicid.variant);
1125 pr_debug("MFI ID: %#x v%d %d->%d\n",
1126 rfid.id, rfid.variant, rfid.bottom, rfid.top);
1127 pr_debug("CFI ID: %#x v%d %d->%d\n",
1128 macid.id, macid.variant, macid.bottom, macid.top);
1129 pr_debug("PRI ID: %#x v%d %d->%d\n",
1130 priid.id, priid.variant, priid.bottom, priid.top);
1132 for (i = 0; i < ns3info; i++) {
1133 switch (s3info[i].type) {
1134 case 1:
1135 pr_debug("Version: ID %#x %d.%d.%d\n",
1136 s3info[i].info.version.id,
1137 s3info[i].info.version.major,
1138 s3info[i].info.version.minor,
1139 s3info[i].info.version.variant);
1140 break;
1141 case 2:
1142 pr_debug("Compat: Role %#x Id %#x v%d %d->%d\n",
1143 s3info[i].info.compat.role,
1144 s3info[i].info.compat.id,
1145 s3info[i].info.compat.variant,
1146 s3info[i].info.compat.bottom,
1147 s3info[i].info.compat.top);
1149 /* MAC compat range */
1150 if ((s3info[i].info.compat.role == 1) &&
1151 (s3info[i].info.compat.id == 2)) {
1152 if (s3info[i].info.compat.variant !=
1153 macid.variant) {
1154 result = 2;
1158 /* PRI compat range */
1159 if ((s3info[i].info.compat.role == 1) &&
1160 (s3info[i].info.compat.id == 3)) {
1161 if ((s3info[i].info.compat.bottom > priid.top)
1162 || (s3info[i].info.compat.top <
1163 priid.bottom)) {
1164 result = 3;
1167 /* SEC compat range */
1168 if ((s3info[i].info.compat.role == 1) &&
1169 (s3info[i].info.compat.id == 4)) {
1170 /* FIXME: isn't something missing here? */
1173 break;
1174 case 3:
1175 pr_debug("Seq: %#x\n", s3info[i].info.buildseq);
1177 break;
1178 case 4:
1179 pr_debug("Platform: ID %#x %d.%d.%d\n",
1180 s3info[i].info.version.id,
1181 s3info[i].info.version.major,
1182 s3info[i].info.version.minor,
1183 s3info[i].info.version.variant);
1185 if (nicid.id != s3info[i].info.version.id)
1186 continue;
1187 if (nicid.major != s3info[i].info.version.major)
1188 continue;
1189 if (nicid.minor != s3info[i].info.version.minor)
1190 continue;
1191 if ((nicid.variant != s3info[i].info.version.variant) &&
1192 (nicid.id != 0x8008))
1193 continue;
1195 trump = 1;
1196 break;
1197 case 0x8001:
1198 pr_debug("name inforec len %d\n", s3info[i].len);
1200 break;
1201 default:
1202 pr_debug("Unknown inforec type %d\n", s3info[i].type);
1205 /* walk through */
1207 if (trump && (result != 2))
1208 result = 0;
1209 return result;