Merge commit 'jwb/next' into next
[GitHub/mt8127/android_kernel_alcatel_ttab.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 typedef struct s3datarec {
77 u32 len;
78 u32 addr;
79 u8 checksum;
80 u8 *data;
81 } s3datarec_t;
82
83 typedef struct s3plugrec {
84 u32 itemcode;
85 u32 addr;
86 u32 len;
87 } s3plugrec_t;
88
89 typedef struct s3crcrec {
90 u32 addr;
91 u32 len;
92 unsigned int dowrite;
93 } s3crcrec_t;
94
95 typedef 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 } s3inforec_t;
105
106 typedef struct pda {
107 u8 buf[HFA384x_PDA_LEN_MAX];
108 hfa384x_pdrec_t *rec[HFA384x_PDA_RECS_MAX];
109 unsigned int nrec;
110 } pda_t;
111
112 typedef 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 } imgchunk_t;
118
119 /*================================================================*/
120 /* Local Static Definitions */
121
122 /*----------------------------------------------------------------*/
123 /* s-record image processing */
124
125 /* Data records */
126 unsigned int ns3data;
127 s3datarec_t s3data[S3DATA_MAX];
128
129 /* Plug records */
130 unsigned int ns3plug;
131 s3plugrec_t s3plug[S3PLUG_MAX];
132
133 /* CRC records */
134 unsigned int ns3crc;
135 s3crcrec_t s3crc[S3CRC_MAX];
136
137 /* Info records */
138 unsigned int ns3info;
139 s3inforec_t s3info[S3INFO_MAX];
140
141 /* S7 record (there _better_ be only one) */
142 u32 startaddr;
143
144 /* Load image chunks */
145 unsigned int nfchunks;
146 imgchunk_t 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 pda_t pda;
155 hfa384x_compident_t nicid;
156 hfa384x_caplevel_t rfid;
157 hfa384x_caplevel_t macid;
158 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(imgchunk_t *clist, unsigned int *ccnt);
169
170 static int read_cardpda(pda_t *pda, wlandevice_t *wlandev);
171
172 static int mkpdrlist(pda_t *pda);
173
174 static int plugimage(imgchunk_t *fchunk, unsigned int nfchunks,
175 s3plugrec_t *s3plug, unsigned int ns3plug, pda_t * pda);
176
177 static int crcimage(imgchunk_t *fchunk, unsigned int nfchunks,
178 s3crcrec_t *s3crc, unsigned int ns3crc);
179
180 static int writeimage(wlandevice_t *wlandev, imgchunk_t *fchunk,
181 unsigned int nfchunks);
182 static void free_chunks(imgchunk_t *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 int prism2_fwtry(struct usb_device *udev, wlandevice_t *wlandev)
205 {
206 const struct firmware *fw_entry = NULL;
207
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;
216 }
217
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);
221
222 release_firmware(fw_entry);
223 return 0;
224 }
225
226 /*----------------------------------------------------------------
227 * prism2_fwapply
228 *
229 * Apply the firmware loaded into memory
230 *
231 * Arguments:
232 * rfptr firmware image in kernel memory
233 * wlandev device
234 *
235 * Returns:
236 * 0 - success
237 * ~0 - failure
238 ----------------------------------------------------------------*/
239 int prism2_fwapply(const struct ihex_binrec *rfptr, wlandevice_t *wlandev)
240 {
241 signed int result = 0;
242 p80211msg_dot11req_mibget_t getmsg;
243 p80211itemd_t *item;
244 u32 *data;
245
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;
256
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));
263
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;
270
271 /*-----------------------------------------------------*/
272 /* Put card into fwload state */
273 prism2sta_ifstate(wlandev, P80211ENUM_ifstate_fwload);
274
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;
279 }
280
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);
286
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;
291
292 item = (p80211itemd_t *) getmsg.mibattribute.data;
293 item->did = DIDmib_p2_p2NIC_p2PRISupRange;
294 item->status = P80211ENUM_msgitem_status_no_value;
295
296 data = (u32 *) item->data;
297
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");
302
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++;
309
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;
315 }
316
317 result = validate_identity();
318
319 if (result) {
320 printk(KERN_ERR "Incompatible firmware image.\n");
321 return 1;
322 }
323
324 if (startaddr == 0x00000000) {
325 printk(KERN_ERR "Can't RAM download a Flash image!\n");
326 return 1;
327 }
328
329 /* Make the image chunks */
330 result = mkimage(fchunk, &nfchunks);
331
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;
337 }
338
339 /* Insert any CRCs */
340 if (crcimage(fchunk, nfchunks, s3crc, ns3crc)) {
341 printk(KERN_ERR "Failed to insert all CRCs\n");
342 return 1;
343 }
344
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;
350 }
351
352 /* clear any allocated memory */
353 free_chunks(fchunk, &nfchunks);
354 free_srecs();
355
356 printk(KERN_INFO "prism2_usb: firmware loading finished.\n");
357
358 return result;
359 }
360
361 /*----------------------------------------------------------------
362 * crcimage
363 *
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.
367 *
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
373 *
374 * Returns:
375 * 0 success
376 * ~0 failure
377 ----------------------------------------------------------------*/
378 int crcimage(imgchunk_t *fchunk, unsigned int nfchunks, s3crcrec_t *s3crc,
379 unsigned int ns3crc)
380 {
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;
390
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 */
404 /* now */
405 /* if ( crcstart-2 >= cstart && crcend <= cend ) break; */
406
407 /* note the -2 below, it's to make sure the chunk has */
408 /* space for the CRC value */
409 if (crcstart - 2 >= cstart && crcstart < cend)
410 break;
411 }
412 if (c >= nfchunks) {
413 printk(KERN_ERR
414 "Failed to find chunk for "
415 "crcrec[%d], addr=0x%06x len=%d , "
416 "aborting crc.\n",
417 i, s3crc[i].addr, s3crc[i].len);
418 return 1;
419 }
420
421 /* Insert crc */
422 pr_debug("Adding crc @ 0x%06x\n", s3crc[i].addr - 2);
423 chunkoff = crcstart - cstart - 2;
424 dest = fchunk[c].data + chunkoff;
425 *dest = 0xde;
426 *(dest + 1) = 0xc0;
427
428 }
429 return result;
430 }
431
432 /*----------------------------------------------------------------
433 * free_chunks
434 *
435 * Clears the chunklist data structures in preparation for a new file.
436 *
437 * Arguments:
438 * none
439 *
440 * Returns:
441 * nothing
442 ----------------------------------------------------------------*/
443 void free_chunks(imgchunk_t *fchunk, unsigned int *nfchunks)
444 {
445 int i;
446 for (i = 0; i < *nfchunks; i++) {
447 if (fchunk[i].data != NULL)
448 kfree(fchunk[i].data);
449 }
450 *nfchunks = 0;
451 memset(fchunk, 0, sizeof(*fchunk));
452
453 }
454
455 /*----------------------------------------------------------------
456 * free_srecs
457 *
458 * Clears the srec data structures in preparation for a new file.
459 *
460 * Arguments:
461 * none
462 *
463 * Returns:
464 * nothing
465 ----------------------------------------------------------------*/
466 void free_srecs(void)
467 {
468 ns3data = 0;
469 memset(s3data, 0, sizeof(s3data));
470 ns3plug = 0;
471 memset(s3plug, 0, sizeof(s3plug));
472 ns3crc = 0;
473 memset(s3crc, 0, sizeof(s3crc));
474 ns3info = 0;
475 memset(s3info, 0, sizeof(s3info));
476 startaddr = 0;
477 }
478
479 /*----------------------------------------------------------------
480 * mkimage
481 *
482 * Scans the currently loaded set of S records for data residing
483 * in contiguous memory regions. Each contiguous region is then
484 * made into a 'chunk'. This function assumes that we're building
485 * a new chunk list. Assumes the s3data items are in sorted order.
486 *
487 * Arguments: none
488 *
489 * Returns:
490 * 0 - success
491 * ~0 - failure (probably an errno)
492 ----------------------------------------------------------------*/
493 int mkimage(imgchunk_t *clist, unsigned int *ccnt)
494 {
495 int result = 0;
496 int i;
497 int j;
498 int currchunk = 0;
499 u32 nextaddr = 0;
500 u32 s3start;
501 u32 s3end;
502 u32 cstart = 0;
503 u32 cend;
504 u32 coffset;
505
506 /* There may already be data in the chunklist */
507 *ccnt = 0;
508
509 /* Establish the location and size of each chunk */
510 for (i = 0; i < ns3data; i++) {
511 if (s3data[i].addr == nextaddr) {
512 /* existing chunk, grow it */
513 clist[currchunk].len += s3data[i].len;
514 nextaddr += s3data[i].len;
515 } else {
516 /* New chunk */
517 (*ccnt)++;
518 currchunk = *ccnt - 1;
519 clist[currchunk].addr = s3data[i].addr;
520 clist[currchunk].len = s3data[i].len;
521 nextaddr = s3data[i].addr + s3data[i].len;
522 /* Expand the chunk if there is a CRC record at */
523 /* their beginning bound */
524 for (j = 0; j < ns3crc; j++) {
525 if (s3crc[j].dowrite &&
526 s3crc[j].addr == clist[currchunk].addr) {
527 clist[currchunk].addr -= 2;
528 clist[currchunk].len += 2;
529 }
530 }
531 }
532 }
533
534 /* We're currently assuming there aren't any overlapping chunks */
535 /* if this proves false, we'll need to add code to coalesce. */
536
537 /* Allocate buffer space for chunks */
538 for (i = 0; i < *ccnt; i++) {
539 clist[i].data = kzalloc(clist[i].len, GFP_KERNEL);
540 if (clist[i].data == NULL) {
541 printk(KERN_ERR
542 "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 printk(KERN_ERR
561 "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 int mkpdrlist(pda_t *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) == 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 printk(KERN_ERR
631 "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 int plugimage(imgchunk_t *fchunk, unsigned int nfchunks,
660 s3plugrec_t *s3plug, unsigned int ns3plug, pda_t * 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 printk(KERN_WARNING
689 "warning: Failed to find PDR for "
690 "plugrec 0x%04x.\n", s3plug[i].itemcode);
691 continue; /* and move on to the next PDR */
692 #if 0
693 /* MSM: They swear that unless it's the MAC address,
694 * the serial number, or the TX calibration records,
695 * then there's reasonable defaults in the f/w
696 * image. Therefore, missing PDRs in the card
697 * should only be a warning, not fatal.
698 * TODO: add fatals for the PDRs mentioned above.
699 */
700 result = 1;
701 continue;
702 #endif
703 }
704
705 /* Validate plug len against PDR len */
706 if (j != -1 && s3plug[i].len < le16_to_cpu(pda->rec[j]->len)) {
707 printk(KERN_ERR
708 "error: Plug vs. PDR len mismatch for "
709 "plugrec 0x%04x, abort plugging.\n",
710 s3plug[i].itemcode);
711 result = 1;
712 continue;
713 }
714
715 /* Validate plug address against chunk data and identify chunk */
716 for (c = 0; c < nfchunks; c++) {
717 cstart = fchunk[c].addr;
718 cend = fchunk[c].addr + fchunk[c].len;
719 if (pstart >= cstart && pend <= cend)
720 break;
721 }
722 if (c >= nfchunks) {
723 printk(KERN_ERR
724 "error: Failed to find image chunk for "
725 "plugrec 0x%04x.\n", s3plug[i].itemcode);
726 result = 1;
727 continue;
728 }
729
730 /* Plug data */
731 chunkoff = pstart - cstart;
732 dest = fchunk[c].data + chunkoff;
733 pr_debug("Plugging item 0x%04x @ 0x%06x, len=%d, "
734 "cnum=%d coff=0x%06x\n",
735 s3plug[i].itemcode, pstart, s3plug[i].len,
736 c, chunkoff);
737
738 if (j == -1) { /* plug the filename */
739 memset(dest, 0, s3plug[i].len);
740 strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
741 } else { /* plug a PDR */
742 memcpy(dest, &(pda->rec[j]->data), s3plug[i].len);
743 }
744 }
745 return result;
746
747 }
748
749 /*----------------------------------------------------------------
750 * read_cardpda
751 *
752 * Sends the command for the driver to read the pda from the card
753 * named in the device variable. Upon success, the card pda is
754 * stored in the "cardpda" variables. Note that the pda structure
755 * is considered 'well formed' after this function. That means
756 * that the nrecs is valid, the rec array has been set up, and there's
757 * a valid PDAEND record in the raw PDA data.
758 *
759 * Arguments:
760 * pda pda structure
761 * wlandev device
762 *
763 * Returns:
764 * 0 - success
765 * ~0 - failure (probably an errno)
766 ----------------------------------------------------------------*/
767 int read_cardpda(pda_t *pda, wlandevice_t *wlandev)
768 {
769 int result = 0;
770 p80211msg_p2req_readpda_t msg;
771
772 /* set up the msg */
773 msg.msgcode = DIDmsg_p2req_readpda;
774 msg.msglen = sizeof(msg);
775 strcpy(msg.devname, wlandev->name);
776 msg.pda.did = DIDmsg_p2req_readpda_pda;
777 msg.pda.len = HFA384x_PDA_LEN_MAX;
778 msg.pda.status = P80211ENUM_msgitem_status_no_value;
779 msg.resultcode.did = DIDmsg_p2req_readpda_resultcode;
780 msg.resultcode.len = sizeof(u32);
781 msg.resultcode.status = P80211ENUM_msgitem_status_no_value;
782
783 if (prism2mgmt_readpda(wlandev, &msg) != 0) {
784 /* prism2mgmt_readpda prints an errno if appropriate */
785 result = -1;
786 } else if (msg.resultcode.data == P80211ENUM_resultcode_success) {
787 memcpy(pda->buf, msg.pda.data, HFA384x_PDA_LEN_MAX);
788 result = mkpdrlist(pda);
789 } else {
790 /* resultcode must've been something other than success */
791 result = -1;
792 }
793
794 return result;
795 }
796
797 /*----------------------------------------------------------------
798 * read_fwfile
799 *
800 * Reads the given fw file which should have been compiled from an srec
801 * file. Each record in the fw file will either be a plain data record,
802 * a start address record, or other records used for plugging.
803 *
804 * Note that data records are expected to be sorted into
805 * ascending address order in the fw file.
806 *
807 * Note also that the start address record, originally an S7 record in
808 * the srec file, is expected in the fw file to be like a data record but
809 * with a certain address to make it identiable.
810 *
811 * Here's the SREC format that the fw should have come from:
812 * S[37]nnaaaaaaaaddd...dddcc
813 *
814 * nn - number of bytes starting with the address field
815 * aaaaaaaa - address in readable (or big endian) format
816 * dd....dd - 0-245 data bytes (two chars per byte)
817 * cc - checksum
818 *
819 * The S7 record's (there should be only one) address value gets
820 * converted to an S3 record with address of 0xff400000, with the
821 * start address being stored as a 4 byte data word. That address is
822 * the start execution address used for RAM downloads.
823 *
824 * The S3 records have a collection of subformats indicated by the
825 * value of aaaaaaaa:
826 * 0xff000000 - Plug record, data field format:
827 * xxxxxxxxaaaaaaaassssssss
828 * x - PDR code number (little endian)
829 * a - Address in load image to plug (little endian)
830 * s - Length of plug data area (little endian)
831 *
832 * 0xff100000 - CRC16 generation record, data field format:
833 * aaaaaaaassssssssbbbbbbbb
834 * a - Start address for CRC calculation (little endian)
835 * s - Length of data to calculate over (little endian)
836 * b - Boolean, true=write crc, false=don't write
837 *
838 * 0xff200000 - Info record, data field format:
839 * ssssttttdd..dd
840 * s - Size in words (little endian)
841 * t - Info type (little endian), see #defines and
842 * s3inforec_t for details about types.
843 * d - (s - 1) little endian words giving the contents of
844 * the given info type.
845 *
846 * 0xff400000 - Start address record, data field format:
847 * aaaaaaaa
848 * a - Address in load image to plug (little endian)
849 *
850 * Arguments:
851 * record firmware image (ihex record structure) in kernel memory
852 *
853 * Returns:
854 * 0 - success
855 * ~0 - failure (probably an errno)
856 ----------------------------------------------------------------*/
857 int read_fwfile(const struct ihex_binrec *record)
858 {
859 int i;
860 int rcnt = 0;
861 u16 *tmpinfo;
862 u16 *ptr16;
863 u32 *ptr32, len, addr;
864
865 pr_debug("Reading fw file ...\n");
866
867 while (record) {
868
869 rcnt++;
870
871 len = be16_to_cpu(record->len);
872 addr = be32_to_cpu(record->addr);
873
874 /* Point into data for different word lengths */
875 ptr32 = (u32 *) record->data;
876 ptr16 = (u16 *) record->data;
877
878 /* parse what was an S3 srec and put it in the right array */
879 switch (addr) {
880 case S3ADDR_START:
881 startaddr = *ptr32;
882 pr_debug(" S7 start addr, record=%d "
883 " addr=0x%08x\n",
884 rcnt,
885 startaddr);
886 break;
887 case S3ADDR_PLUG:
888 s3plug[ns3plug].itemcode = *ptr32;
889 s3plug[ns3plug].addr = *(ptr32 + 1);
890 s3plug[ns3plug].len = *(ptr32 + 2);
891
892 pr_debug(" S3 plugrec, record=%d "
893 "itemcode=0x%08x addr=0x%08x len=%d\n",
894 rcnt,
895 s3plug[ns3plug].itemcode,
896 s3plug[ns3plug].addr,
897 s3plug[ns3plug].len);
898
899 ns3plug++;
900 if (ns3plug == S3PLUG_MAX) {
901 printk(KERN_ERR "S3 plugrec limit reached - aborting\n");
902 return 1;
903 }
904 break;
905 case S3ADDR_CRC:
906 s3crc[ns3crc].addr = *ptr32;
907 s3crc[ns3crc].len = *(ptr32 + 1);
908 s3crc[ns3crc].dowrite = *(ptr32 + 2);
909
910 pr_debug(" S3 crcrec, record=%d "
911 "addr=0x%08x len=%d write=0x%08x\n",
912 rcnt,
913 s3crc[ns3crc].addr,
914 s3crc[ns3crc].len,
915 s3crc[ns3crc].dowrite);
916 ns3crc++;
917 if (ns3crc == S3CRC_MAX) {
918 printk(KERN_ERR "S3 crcrec limit reached - aborting\n");
919 return 1;
920 }
921 break;
922 case S3ADDR_INFO:
923 s3info[ns3info].len = *ptr16;
924 s3info[ns3info].type = *(ptr16 + 1);
925
926 pr_debug(" S3 inforec, record=%d "
927 "len=0x%04x type=0x%04x\n",
928 rcnt,
929 s3info[ns3info].len,
930 s3info[ns3info].type);
931 if (((s3info[ns3info].len - 1) * sizeof(u16)) > sizeof(s3info[ns3info].info)) {
932 printk(KERN_ERR " S3 inforec length too long - aborting\n");
933 return 1;
934 }
935
936 tmpinfo = (u16 *)&(s3info[ns3info].info.version);
937 pr_debug(" info=");
938 for (i = 0; i < s3info[ns3info].len - 1; i++) {
939 tmpinfo[i] = *(ptr16 + 2 + i);
940 pr_debug("%04x ", tmpinfo[i]);
941 }
942 pr_debug("\n");
943
944 ns3info++;
945 if (ns3info == S3INFO_MAX) {
946 printk(KERN_ERR "S3 inforec limit reached - aborting\n");
947 return 1;
948 }
949 break;
950 default: /* Data record */
951 s3data[ns3data].addr = addr;
952 s3data[ns3data].len = len;
953 s3data[ns3data].data = (uint8_t *) record->data;
954 ns3data++;
955 if (ns3data == S3DATA_MAX) {
956 printk(KERN_ERR "S3 datarec limit reached - aborting\n");
957 return 1;
958 }
959 break;
960 }
961 record = ihex_next_binrec(record);
962 }
963 return 0;
964 }
965
966 /*----------------------------------------------------------------
967 * writeimage
968 *
969 * Takes the chunks, builds p80211 messages and sends them down
970 * to the driver for writing to the card.
971 *
972 * Arguments:
973 * wlandev device
974 * fchunk Array of image chunks
975 * nfchunks Number of image chunks
976 *
977 * Returns:
978 * 0 success
979 * ~0 failure
980 ----------------------------------------------------------------*/
981 int writeimage(wlandevice_t *wlandev, imgchunk_t *fchunk,
982 unsigned int nfchunks)
983 {
984 int result = 0;
985 p80211msg_p2req_ramdl_state_t rstatemsg;
986 p80211msg_p2req_ramdl_write_t rwritemsg;
987 p80211msg_t *msgp;
988 u32 resultcode;
989 int i;
990 int j;
991 unsigned int nwrites;
992 u32 curroff;
993 u32 currlen;
994 u32 currdaddr;
995
996 /* Initialize the messages */
997 memset(&rstatemsg, 0, sizeof(rstatemsg));
998 strcpy(rstatemsg.devname, wlandev->name);
999 rstatemsg.msgcode = DIDmsg_p2req_ramdl_state;
1000 rstatemsg.msglen = sizeof(rstatemsg);
1001 rstatemsg.enable.did = DIDmsg_p2req_ramdl_state_enable;
1002 rstatemsg.exeaddr.did = DIDmsg_p2req_ramdl_state_exeaddr;
1003 rstatemsg.resultcode.did = DIDmsg_p2req_ramdl_state_resultcode;
1004 rstatemsg.enable.status = P80211ENUM_msgitem_status_data_ok;
1005 rstatemsg.exeaddr.status = P80211ENUM_msgitem_status_data_ok;
1006 rstatemsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
1007 rstatemsg.enable.len = sizeof(u32);
1008 rstatemsg.exeaddr.len = sizeof(u32);
1009 rstatemsg.resultcode.len = sizeof(u32);
1010
1011 memset(&rwritemsg, 0, sizeof(rwritemsg));
1012 strcpy(rwritemsg.devname, wlandev->name);
1013 rwritemsg.msgcode = DIDmsg_p2req_ramdl_write;
1014 rwritemsg.msglen = sizeof(rwritemsg);
1015 rwritemsg.addr.did = DIDmsg_p2req_ramdl_write_addr;
1016 rwritemsg.len.did = DIDmsg_p2req_ramdl_write_len;
1017 rwritemsg.data.did = DIDmsg_p2req_ramdl_write_data;
1018 rwritemsg.resultcode.did = DIDmsg_p2req_ramdl_write_resultcode;
1019 rwritemsg.addr.status = P80211ENUM_msgitem_status_data_ok;
1020 rwritemsg.len.status = P80211ENUM_msgitem_status_data_ok;
1021 rwritemsg.data.status = P80211ENUM_msgitem_status_data_ok;
1022 rwritemsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
1023 rwritemsg.addr.len = sizeof(u32);
1024 rwritemsg.len.len = sizeof(u32);
1025 rwritemsg.data.len = WRITESIZE_MAX;
1026 rwritemsg.resultcode.len = sizeof(u32);
1027
1028 /* Send xxx_state(enable) */
1029 pr_debug("Sending dl_state(enable) message.\n");
1030 rstatemsg.enable.data = P80211ENUM_truth_true;
1031 rstatemsg.exeaddr.data = startaddr;
1032
1033 msgp = (p80211msg_t *) &rstatemsg;
1034 result = prism2mgmt_ramdl_state(wlandev, msgp);
1035 if (result) {
1036 printk(KERN_ERR
1037 "writeimage state enable failed w/ result=%d, "
1038 "aborting download\n", result);
1039 return result;
1040 }
1041 resultcode = rstatemsg.resultcode.data;
1042 if (resultcode != P80211ENUM_resultcode_success) {
1043 printk(KERN_ERR
1044 "writeimage()->xxxdl_state msg indicates failure, "
1045 "w/ resultcode=%d, aborting download.\n", resultcode);
1046 return 1;
1047 }
1048
1049 /* Now, loop through the data chunks and send WRITESIZE_MAX data */
1050 for (i = 0; i < nfchunks; i++) {
1051 nwrites = fchunk[i].len / WRITESIZE_MAX;
1052 nwrites += (fchunk[i].len % WRITESIZE_MAX) ? 1 : 0;
1053 curroff = 0;
1054 for (j = 0; j < nwrites; j++) {
1055 currlen =
1056 (fchunk[i].len - (WRITESIZE_MAX * j)) >
1057 WRITESIZE_MAX ? WRITESIZE_MAX : (fchunk[i].len -
1058 (WRITESIZE_MAX *
1059 j));
1060 curroff = j * WRITESIZE_MAX;
1061 currdaddr = fchunk[i].addr + curroff;
1062 /* Setup the message */
1063 rwritemsg.addr.data = currdaddr;
1064 rwritemsg.len.data = currlen;
1065 memcpy(rwritemsg.data.data,
1066 fchunk[i].data + curroff, currlen);
1067
1068 /* Send flashdl_write(pda) */
1069 pr_debug
1070 ("Sending xxxdl_write message addr=%06x len=%d.\n",
1071 currdaddr, currlen);
1072
1073 msgp = (p80211msg_t *) &rwritemsg;
1074 result = prism2mgmt_ramdl_write(wlandev, msgp);
1075
1076 /* Check the results */
1077 if (result) {
1078 printk(KERN_ERR
1079 "writeimage chunk write failed w/ result=%d, "
1080 "aborting download\n", result);
1081 return result;
1082 }
1083 resultcode = rstatemsg.resultcode.data;
1084 if (resultcode != P80211ENUM_resultcode_success) {
1085 printk(KERN_ERR
1086 "writeimage()->xxxdl_write msg indicates failure, "
1087 "w/ resultcode=%d, aborting download.\n",
1088 resultcode);
1089 return 1;
1090 }
1091
1092 }
1093 }
1094
1095 /* Send xxx_state(disable) */
1096 pr_debug("Sending dl_state(disable) message.\n");
1097 rstatemsg.enable.data = P80211ENUM_truth_false;
1098 rstatemsg.exeaddr.data = 0;
1099
1100 msgp = (p80211msg_t *) &rstatemsg;
1101 result = prism2mgmt_ramdl_state(wlandev, msgp);
1102 if (result) {
1103 printk(KERN_ERR
1104 "writeimage state disable failed w/ result=%d, "
1105 "aborting download\n", result);
1106 return result;
1107 }
1108 resultcode = rstatemsg.resultcode.data;
1109 if (resultcode != P80211ENUM_resultcode_success) {
1110 printk(KERN_ERR
1111 "writeimage()->xxxdl_state msg indicates failure, "
1112 "w/ resultcode=%d, aborting download.\n", resultcode);
1113 return 1;
1114 }
1115 return result;
1116 }
1117
1118 int validate_identity(void)
1119 {
1120 int i;
1121 int result = 1;
1122 int trump = 0;
1123
1124 pr_debug("NIC ID: %#x v%d.%d.%d\n",
1125 nicid.id, nicid.major, nicid.minor, nicid.variant);
1126 pr_debug("MFI ID: %#x v%d %d->%d\n",
1127 rfid.id, rfid.variant, rfid.bottom, rfid.top);
1128 pr_debug("CFI ID: %#x v%d %d->%d\n",
1129 macid.id, macid.variant, macid.bottom, macid.top);
1130 pr_debug("PRI ID: %#x v%d %d->%d\n",
1131 priid.id, priid.variant, priid.bottom, priid.top);
1132
1133 for (i = 0; i < ns3info; i++) {
1134 switch (s3info[i].type) {
1135 case 1:
1136 pr_debug("Version: ID %#x %d.%d.%d\n",
1137 s3info[i].info.version.id,
1138 s3info[i].info.version.major,
1139 s3info[i].info.version.minor,
1140 s3info[i].info.version.variant);
1141 break;
1142 case 2:
1143 pr_debug("Compat: Role %#x Id %#x v%d %d->%d\n",
1144 s3info[i].info.compat.role,
1145 s3info[i].info.compat.id,
1146 s3info[i].info.compat.variant,
1147 s3info[i].info.compat.bottom,
1148 s3info[i].info.compat.top);
1149
1150 /* MAC compat range */
1151 if ((s3info[i].info.compat.role == 1) &&
1152 (s3info[i].info.compat.id == 2)) {
1153 if (s3info[i].info.compat.variant !=
1154 macid.variant) {
1155 result = 2;
1156 }
1157 }
1158
1159 /* PRI compat range */
1160 if ((s3info[i].info.compat.role == 1) &&
1161 (s3info[i].info.compat.id == 3)) {
1162 if ((s3info[i].info.compat.bottom > priid.top)
1163 || (s3info[i].info.compat.top <
1164 priid.bottom)) {
1165 result = 3;
1166 }
1167 }
1168 /* SEC compat range */
1169 if ((s3info[i].info.compat.role == 1) &&
1170 (s3info[i].info.compat.id == 4)) {
1171 /* FIXME: isn't something missing here? */
1172 }
1173
1174 break;
1175 case 3:
1176 pr_debug("Seq: %#x\n", s3info[i].info.buildseq);
1177
1178 break;
1179 case 4:
1180 pr_debug("Platform: ID %#x %d.%d.%d\n",
1181 s3info[i].info.version.id,
1182 s3info[i].info.version.major,
1183 s3info[i].info.version.minor,
1184 s3info[i].info.version.variant);
1185
1186 if (nicid.id != s3info[i].info.version.id)
1187 continue;
1188 if (nicid.major != s3info[i].info.version.major)
1189 continue;
1190 if (nicid.minor != s3info[i].info.version.minor)
1191 continue;
1192 if ((nicid.variant != s3info[i].info.version.variant) &&
1193 (nicid.id != 0x8008))
1194 continue;
1195
1196 trump = 1;
1197 break;
1198 case 0x8001:
1199 pr_debug("name inforec len %d\n", s3info[i].len);
1200
1201 break;
1202 default:
1203 pr_debug("Unknown inforec type %d\n", s3info[i].type);
1204 }
1205 }
1206 /* walk through */
1207
1208 if (trump && (result != 2))
1209 result = 0;
1210 return result;
1211 }