blob: fd5ddb29436c381d8d8deccc26fecb304996c5e1 [file] [log] [blame]
Karl Relton76e3e7c2009-04-17 10:15:34 +01001/* 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 */
Karl Reltond8950592009-08-19 08:06:39 +010050#include <linux/ihex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Karl Relton76e3e7c2009-04-17 10:15:34 +010052
Karl Relton76e3e7c2009-04-17 10:15:34 +010053/*================================================================*/
54/* Local Constants */
55
Karl Reltond8950592009-08-19 08:06:39 +010056#define PRISM2_USB_FWFILE "prism2_ru.fw"
Ben Hutchings5d929a72010-01-13 23:36:09 +000057MODULE_FIRMWARE(PRISM2_USB_FWFILE);
Karl Relton76e3e7c2009-04-17 10:15:34 +010058
59#define S3DATA_MAX 5000
60#define S3PLUG_MAX 200
61#define S3CRC_MAX 200
62#define S3INFO_MAX 50
Karl Relton76e3e7c2009-04-17 10:15:34 +010063
64#define S3ADDR_PLUG (0xff000000UL)
65#define S3ADDR_CRC (0xff100000UL)
66#define S3ADDR_INFO (0xff200000UL)
Karl Reltond8950592009-08-19 08:06:39 +010067#define S3ADDR_START (0xff400000UL)
Karl Relton76e3e7c2009-04-17 10:15:34 +010068
69#define CHUNKS_MAX 100
70
71#define WRITESIZE_MAX 4096
72
73/*================================================================*/
Karl Relton76e3e7c2009-04-17 10:15:34 +010074/* Local Types */
75
Edgardo Hamese0264412010-07-21 22:31:56 -030076struct s3datarec {
Mithlesh Thukral75f49e02009-05-25 19:06:16 +053077 u32 len;
78 u32 addr;
79 u8 checksum;
80 u8 *data;
Edgardo Hamese0264412010-07-21 22:31:56 -030081};
Karl Relton76e3e7c2009-04-17 10:15:34 +010082
Edgardo Hamese0264412010-07-21 22:31:56 -030083struct s3plugrec {
Mithlesh Thukral75f49e02009-05-25 19:06:16 +053084 u32 itemcode;
85 u32 addr;
86 u32 len;
Edgardo Hamese0264412010-07-21 22:31:56 -030087};
Karl Relton76e3e7c2009-04-17 10:15:34 +010088
Edgardo Hamese0264412010-07-21 22:31:56 -030089struct s3crcrec {
Mithlesh Thukral75f49e02009-05-25 19:06:16 +053090 u32 addr;
91 u32 len;
92 unsigned int dowrite;
Edgardo Hamese0264412010-07-21 22:31:56 -030093};
Karl Relton76e3e7c2009-04-17 10:15:34 +010094
Edgardo Hamese0264412010-07-21 22:31:56 -030095struct s3inforec {
Mithlesh Thukral75f49e02009-05-25 19:06:16 +053096 u16 len;
97 u16 type;
Karl Relton76e3e7c2009-04-17 10:15:34 +010098 union {
Mithlesh Thukral75f49e02009-05-25 19:06:16 +053099 hfa384x_compident_t version;
100 hfa384x_caplevel_t compat;
101 u16 buildseq;
102 hfa384x_compident_t platform;
103 } info;
Edgardo Hamese0264412010-07-21 22:31:56 -0300104};
Karl Relton76e3e7c2009-04-17 10:15:34 +0100105
Edgardo Hamese0264412010-07-21 22:31:56 -0300106struct pda {
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530107 u8 buf[HFA384x_PDA_LEN_MAX];
108 hfa384x_pdrec_t *rec[HFA384x_PDA_RECS_MAX];
109 unsigned int nrec;
Edgardo Hamese0264412010-07-21 22:31:56 -0300110};
Karl Relton76e3e7c2009-04-17 10:15:34 +0100111
Edgardo Hamese0264412010-07-21 22:31:56 -0300112struct imgchunk {
Andrew Elwellef1a0ed2010-02-18 23:56:12 +0100113 u32 addr; /* start address */
114 u32 len; /* in bytes */
115 u16 crc; /* CRC value (if it falls at a chunk boundary) */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530116 u8 *data;
Edgardo Hamese0264412010-07-21 22:31:56 -0300117};
Karl Relton76e3e7c2009-04-17 10:15:34 +0100118
119/*================================================================*/
120/* Local Static Definitions */
121
Karl Relton76e3e7c2009-04-17 10:15:34 +0100122/*----------------------------------------------------------------*/
123/* s-record image processing */
124
125/* Data records */
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530126unsigned int ns3data;
Edgardo Hamese0264412010-07-21 22:31:56 -0300127struct s3datarec s3data[S3DATA_MAX];
Karl Relton76e3e7c2009-04-17 10:15:34 +0100128
129/* Plug records */
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530130unsigned int ns3plug;
Edgardo Hamese0264412010-07-21 22:31:56 -0300131struct s3plugrec s3plug[S3PLUG_MAX];
Karl Relton76e3e7c2009-04-17 10:15:34 +0100132
133/* CRC records */
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530134unsigned int ns3crc;
Edgardo Hamese0264412010-07-21 22:31:56 -0300135struct s3crcrec s3crc[S3CRC_MAX];
Karl Relton76e3e7c2009-04-17 10:15:34 +0100136
137/* Info records */
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530138unsigned int ns3info;
Edgardo Hamese0264412010-07-21 22:31:56 -0300139struct s3inforec s3info[S3INFO_MAX];
Karl Relton76e3e7c2009-04-17 10:15:34 +0100140
141/* S7 record (there _better_ be only one) */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530142u32 startaddr;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100143
144/* Load image chunks */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530145unsigned int nfchunks;
Edgardo Hamese0264412010-07-21 22:31:56 -0300146struct imgchunk fchunk[CHUNKS_MAX];
Karl Relton76e3e7c2009-04-17 10:15:34 +0100147
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
Edgardo Hamese0264412010-07-21 22:31:56 -0300154struct pda pda;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100155hfa384x_compident_t nicid;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530156hfa384x_caplevel_t rfid;
157hfa384x_caplevel_t macid;
158hfa384x_caplevel_t priid;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100159
160/*================================================================*/
161/* Local Function Declarations */
162
Zachary Richeya05d08c2010-05-11 14:16:41 -0400163static int prism2_fwapply(const struct ihex_binrec *rfptr,
164wlandevice_t *wlandev);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100165
Zachary Richeya05d08c2010-05-11 14:16:41 -0400166static int read_fwfile(const struct ihex_binrec *rfptr);
167
Edgardo Hamese0264412010-07-21 22:31:56 -0300168static int mkimage(struct imgchunk *clist, unsigned int *ccnt);
Zachary Richeya05d08c2010-05-11 14:16:41 -0400169
Edgardo Hamese0264412010-07-21 22:31:56 -0300170static int read_cardpda(struct pda *pda, wlandevice_t *wlandev);
Zachary Richeya05d08c2010-05-11 14:16:41 -0400171
Edgardo Hamese0264412010-07-21 22:31:56 -0300172static int mkpdrlist(struct pda *pda);
Zachary Richeya05d08c2010-05-11 14:16:41 -0400173
Edgardo Hamese0264412010-07-21 22:31:56 -0300174static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
175 struct s3plugrec *s3plug, unsigned int ns3plug, struct pda * pda);
Zachary Richeya05d08c2010-05-11 14:16:41 -0400176
Edgardo Hamese0264412010-07-21 22:31:56 -0300177static int crcimage(struct imgchunk *fchunk, unsigned int nfchunks,
178 struct s3crcrec *s3crc, unsigned int ns3crc);
Zachary Richeya05d08c2010-05-11 14:16:41 -0400179
Edgardo Hamese0264412010-07-21 22:31:56 -0300180static int writeimage(wlandevice_t *wlandev, struct imgchunk *fchunk,
Zachary Richeya05d08c2010-05-11 14:16:41 -0400181 unsigned int nfchunks);
Edgardo Hamese0264412010-07-21 22:31:56 -0300182static void free_chunks(struct imgchunk *fchunk, unsigned int *nfchunks);
Zachary Richeya05d08c2010-05-11 14:16:41 -0400183
184static void free_srecs(void);
185
186static int validate_identity(void);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100187
188/*================================================================*/
189/* Function Definitions */
190
Karl Relton76e3e7c2009-04-17 10:15:34 +0100191/*----------------------------------------------------------------
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----------------------------------------------------------------*/
Mithlesh Thukral297f06c2009-06-10 19:36:11 +0530204int prism2_fwtry(struct usb_device *udev, wlandevice_t *wlandev)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100205{
206 const struct firmware *fw_entry = NULL;
207
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530208 printk(KERN_INFO "prism2_usb: Checking for firmware %s\n",
209 PRISM2_USB_FWFILE);
Karl Reltond8950592009-08-19 08:06:39 +0100210 if (request_ihex_firmware(&fw_entry, PRISM2_USB_FWFILE, &udev->dev) != 0) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100211 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
Randy Dunlap83a0f9b2010-02-25 09:55:03 -0800218 printk(KERN_INFO "prism2_usb: %s will be processed, size %zu\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530219 PRISM2_USB_FWFILE, fw_entry->size);
Karl Reltond8950592009-08-19 08:06:39 +0100220 prism2_fwapply((const struct ihex_binrec *)fw_entry->data, wlandev);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100221
222 release_firmware(fw_entry);
223 return 0;
224}
225
Karl Relton76e3e7c2009-04-17 10:15:34 +0100226/*----------------------------------------------------------------
227* prism2_fwapply
228*
229* Apply the firmware loaded into memory
230*
231* Arguments:
232* rfptr firmware image in kernel memory
Karl Relton76e3e7c2009-04-17 10:15:34 +0100233* wlandev device
234*
235* Returns:
236* 0 - success
237* ~0 - failure
238----------------------------------------------------------------*/
Karl Reltond8950592009-08-19 08:06:39 +0100239int prism2_fwapply(const struct ihex_binrec *rfptr, wlandevice_t *wlandev)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100240{
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530241 signed int result = 0;
Edgardo Hamesb6bb56e2010-08-02 16:20:39 -0300242 struct p80211msg_dot11req_mibget getmsg;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100243 p80211itemd_t *item;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530244 u32 *data;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100245
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;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530258 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));
Karl Relton76e3e7c2009-04-17 10:15:34 +0100263
264 /* clear the pda and add an initial END record */
265 memset(&pda, 0, sizeof(pda));
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530266 pda.rec[0] = (hfa384x_pdrec_t *) pda.buf;
Zachary Richeya05d08c2010-05-11 14:16:41 -0400267 pda.rec[0]->len = cpu_to_le16(2); /* len in words */
Karl Relton76e3e7c2009-04-17 10:15:34 +0100268 pda.rec[0]->code = cpu_to_le16(HFA384x_PDR_END_OF_PDA);
269 pda.nrec = 1;
270
Karl Relton76e3e7c2009-04-17 10:15:34 +0100271 /*-----------------------------------------------------*/
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");
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100278 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100279 }
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
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530296 data = (u32 *) item->data;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100297
298 /* DIDmsg_dot11req_mibget */
299 prism2mgmt_mibset_mibget(wlandev, &getmsg);
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100300 if (getmsg.resultcode.data != P80211ENUM_resultcode_success)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100301 printk(KERN_ERR "Couldn't fetch PRI-SUP info\n");
Karl Relton76e3e7c2009-04-17 10:15:34 +0100302
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
Karl Relton76e3e7c2009-04-17 10:15:34 +0100310 /* Read the S3 file */
Karl Reltond8950592009-08-19 08:06:39 +0100311 result = read_fwfile(rfptr);
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530312 if (result) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100313 printk(KERN_ERR "Failed to read the data exiting.\n");
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100314 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100315 }
Karl Relton76e3e7c2009-04-17 10:15:34 +0100316
317 result = validate_identity();
318
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530319 if (result) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100320 printk(KERN_ERR "Incompatible firmware image.\n");
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100321 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100322 }
323
324 if (startaddr == 0x00000000) {
325 printk(KERN_ERR "Can't RAM download a Flash image!\n");
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100326 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100327 }
328
329 /* Make the image chunks */
330 result = mkimage(fchunk, &nfchunks);
331
332 /* Do any plugging */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530333 result = plugimage(fchunk, nfchunks, s3plug, ns3plug, &pda);
334 if (result) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100335 printk(KERN_ERR "Failed to plug data.\n");
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100336 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100337 }
338
339 /* Insert any CRCs */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530340 if (crcimage(fchunk, nfchunks, s3crc, ns3crc)) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100341 printk(KERN_ERR "Failed to insert all CRCs\n");
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100342 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100343 }
344
345 /* Write the image */
346 result = writeimage(wlandev, fchunk, nfchunks);
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530347 if (result) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100348 printk(KERN_ERR "Failed to ramwrite image data.\n");
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100349 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100350 }
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
Karl Relton76e3e7c2009-04-17 10:15:34 +0100361/*----------------------------------------------------------------
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----------------------------------------------------------------*/
Edgardo Hamese0264412010-07-21 22:31:56 -0300378int crcimage(struct imgchunk *fchunk, unsigned int nfchunks,
379 struct s3crcrec *s3crc, unsigned int ns3crc)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100380{
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530381 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;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100390
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530391 for (i = 0; i < ns3crc; i++) {
392 if (!s3crc[i].dowrite)
393 continue;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100394 crcstart = s3crc[i].addr;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530395 crcend = s3crc[i].addr + s3crc[i].len;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100396 /* Find chunk */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530397 for (c = 0; c < nfchunks; c++) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100398 cstart = fchunk[c].addr;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530399 cend = fchunk[c].addr + fchunk[c].len;
Edgardo Hamese0264412010-07-21 22:31:56 -0300400 /* 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; */
Karl Relton76e3e7c2009-04-17 10:15:34 +0100405
406 /* note the -2 below, it's to make sure the chunk has */
Edgardo Hamese0264412010-07-21 22:31:56 -0300407 /* space for the CRC value */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530408 if (crcstart - 2 >= cstart && crcstart < cend)
409 break;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100410 }
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530411 if (c >= nfchunks) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100412 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530413 "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);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100417 return 1;
418 }
419
420 /* Insert crc */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530421 pr_debug("Adding crc @ 0x%06x\n", s3crc[i].addr - 2);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100422 chunkoff = crcstart - cstart - 2;
423 dest = fchunk[c].data + chunkoff;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530424 *dest = 0xde;
425 *(dest + 1) = 0xc0;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100426
427 }
428 return result;
429}
430
Karl Relton76e3e7c2009-04-17 10:15:34 +0100431/*----------------------------------------------------------------
432* free_chunks
433*
434* Clears the chunklist data structures in preparation for a new file.
435*
436* Arguments:
437* none
438*
439* Returns:
440* nothing
441----------------------------------------------------------------*/
Edgardo Hamese0264412010-07-21 22:31:56 -0300442void free_chunks(struct imgchunk *fchunk, unsigned int *nfchunks)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100443{
444 int i;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530445 for (i = 0; i < *nfchunks; i++) {
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100446 if (fchunk[i].data != NULL)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100447 kfree(fchunk[i].data);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100448 }
449 *nfchunks = 0;
Julia Lawall4068fe82009-12-09 20:25:04 +0100450 memset(fchunk, 0, sizeof(*fchunk));
Karl Relton76e3e7c2009-04-17 10:15:34 +0100451
452}
453
Karl Relton76e3e7c2009-04-17 10:15:34 +0100454/*----------------------------------------------------------------
455* free_srecs
456*
457* Clears the srec data structures in preparation for a new file.
458*
459* Arguments:
460* none
461*
462* Returns:
463* nothing
464----------------------------------------------------------------*/
465void free_srecs(void)
466{
Karl Relton76e3e7c2009-04-17 10:15:34 +0100467 ns3data = 0;
468 memset(s3data, 0, sizeof(s3data));
469 ns3plug = 0;
470 memset(s3plug, 0, sizeof(s3plug));
471 ns3crc = 0;
472 memset(s3crc, 0, sizeof(s3crc));
473 ns3info = 0;
474 memset(s3info, 0, sizeof(s3info));
475 startaddr = 0;
476}
477
Karl Relton76e3e7c2009-04-17 10:15:34 +0100478/*----------------------------------------------------------------
479* mkimage
480*
481* Scans the currently loaded set of S records for data residing
482* in contiguous memory regions. Each contiguous region is then
483* made into a 'chunk'. This function assumes that we're building
484* a new chunk list. Assumes the s3data items are in sorted order.
485*
486* Arguments: none
487*
488* Returns:
489* 0 - success
490* ~0 - failure (probably an errno)
491----------------------------------------------------------------*/
Edgardo Hamese0264412010-07-21 22:31:56 -0300492int mkimage(struct imgchunk *clist, unsigned int *ccnt)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100493{
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530494 int result = 0;
495 int i;
496 int j;
497 int currchunk = 0;
498 u32 nextaddr = 0;
499 u32 s3start;
500 u32 s3end;
501 u32 cstart = 0;
502 u32 cend;
503 u32 coffset;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100504
505 /* There may already be data in the chunklist */
506 *ccnt = 0;
507
508 /* Establish the location and size of each chunk */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530509 for (i = 0; i < ns3data; i++) {
510 if (s3data[i].addr == nextaddr) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100511 /* existing chunk, grow it */
512 clist[currchunk].len += s3data[i].len;
513 nextaddr += s3data[i].len;
514 } else {
515 /* New chunk */
516 (*ccnt)++;
517 currchunk = *ccnt - 1;
518 clist[currchunk].addr = s3data[i].addr;
519 clist[currchunk].len = s3data[i].len;
520 nextaddr = s3data[i].addr + s3data[i].len;
521 /* Expand the chunk if there is a CRC record at */
522 /* their beginning bound */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530523 for (j = 0; j < ns3crc; j++) {
524 if (s3crc[j].dowrite &&
525 s3crc[j].addr == clist[currchunk].addr) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100526 clist[currchunk].addr -= 2;
527 clist[currchunk].len += 2;
528 }
529 }
530 }
531 }
532
533 /* We're currently assuming there aren't any overlapping chunks */
534 /* if this proves false, we'll need to add code to coalesce. */
535
536 /* Allocate buffer space for chunks */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530537 for (i = 0; i < *ccnt; i++) {
Julia Lawall7a6cb0d2010-05-13 22:00:05 +0200538 clist[i].data = kzalloc(clist[i].len, GFP_KERNEL);
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530539 if (clist[i].data == NULL) {
540 printk(KERN_ERR
541 "failed to allocate image space, exitting.\n");
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100542 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100543 }
Karl Relton76e3e7c2009-04-17 10:15:34 +0100544 pr_debug("chunk[%d]: addr=0x%06x len=%d\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530545 i, clist[i].addr, clist[i].len);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100546 }
547
548 /* Copy srec data to chunks */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530549 for (i = 0; i < ns3data; i++) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100550 s3start = s3data[i].addr;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530551 s3end = s3start + s3data[i].len - 1;
552 for (j = 0; j < *ccnt; j++) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100553 cstart = clist[j].addr;
554 cend = cstart + clist[j].len - 1;
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100555 if (s3start >= cstart && s3end <= cend)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100556 break;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100557 }
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530558 if (((unsigned int)j) >= (*ccnt)) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100559 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530560 "s3rec(a=0x%06x,l=%d), no chunk match, exiting.\n",
561 s3start, s3data[i].len);
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100562 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100563 }
564 coffset = s3start - cstart;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530565 memcpy(clist[j].data + coffset, s3data[i].data, s3data[i].len);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100566 }
567
568 return result;
569}
570
571/*----------------------------------------------------------------
572* mkpdrlist
573*
574* Reads a raw PDA and builds an array of pdrec_t structures.
575*
576* Arguments:
577* pda buffer containing raw PDA bytes
578* pdrec ptr to an array of pdrec_t's. Will be filled on exit.
579* nrec ptr to a variable that will contain the count of PDRs
580*
581* Returns:
582* 0 - success
583* ~0 - failure (probably an errno)
584----------------------------------------------------------------*/
Edgardo Hamese0264412010-07-21 22:31:56 -0300585int mkpdrlist(struct pda *pda)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100586{
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530587 int result = 0;
588 u16 *pda16 = (u16 *) pda->buf;
589 int curroff; /* in 'words' */
Karl Relton76e3e7c2009-04-17 10:15:34 +0100590
591 pda->nrec = 0;
592 curroff = 0;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530593 while (curroff < (HFA384x_PDA_LEN_MAX / 2) &&
594 le16_to_cpu(pda16[curroff + 1]) != HFA384x_PDR_END_OF_PDA) {
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100595 pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100596
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530597 if (le16_to_cpu(pda->rec[pda->nrec]->code) == HFA384x_PDR_NICID) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100598 memcpy(&nicid, &pda->rec[pda->nrec]->data.nicid,
599 sizeof(nicid));
600 nicid.id = le16_to_cpu(nicid.id);
601 nicid.variant = le16_to_cpu(nicid.variant);
602 nicid.major = le16_to_cpu(nicid.major);
603 nicid.minor = le16_to_cpu(nicid.minor);
604 }
605 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530606 HFA384x_PDR_MFISUPRANGE) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100607 memcpy(&rfid, &pda->rec[pda->nrec]->data.mfisuprange,
608 sizeof(rfid));
609 rfid.id = le16_to_cpu(rfid.id);
610 rfid.variant = le16_to_cpu(rfid.variant);
611 rfid.bottom = le16_to_cpu(rfid.bottom);
612 rfid.top = le16_to_cpu(rfid.top);
613 }
614 if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530615 HFA384x_PDR_CFISUPRANGE) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100616 memcpy(&macid, &pda->rec[pda->nrec]->data.cfisuprange,
617 sizeof(macid));
618 macid.id = le16_to_cpu(macid.id);
619 macid.variant = le16_to_cpu(macid.variant);
620 macid.bottom = le16_to_cpu(macid.bottom);
621 macid.top = le16_to_cpu(macid.top);
622 }
623
624 (pda->nrec)++;
625 curroff += le16_to_cpu(pda16[curroff]) + 1;
626
627 }
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530628 if (curroff >= (HFA384x_PDA_LEN_MAX / 2)) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100629 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530630 "no end record found or invalid lengths in "
631 "PDR data, exiting. %x %d\n", curroff, pda->nrec);
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100632 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100633 }
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530634 if (le16_to_cpu(pda16[curroff + 1]) == HFA384x_PDR_END_OF_PDA) {
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100635 pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100636 (pda->nrec)++;
637 }
638 return result;
639}
640
Karl Relton76e3e7c2009-04-17 10:15:34 +0100641/*----------------------------------------------------------------
642* plugimage
643*
644* Plugs the given image using the given plug records from the given
645* PDA and filename.
646*
647* Arguments:
648* fchunk Array of image chunks
649* nfchunks Number of image chunks
650* s3plug Array of plug records
651* ns3plug Number of plug records
652* pda Current pda data
653*
654* Returns:
655* 0 success
656* ~0 failure
657----------------------------------------------------------------*/
Edgardo Hamese0264412010-07-21 22:31:56 -0300658int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
659 struct s3plugrec *s3plug, unsigned int ns3plug, struct pda * pda)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100660{
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530661 int result = 0;
662 int i; /* plug index */
663 int j; /* index of PDR or -1 if fname plug */
664 int c; /* chunk index */
665 u32 pstart;
666 u32 pend;
667 u32 cstart = 0;
668 u32 cend;
669 u32 chunkoff;
670 u8 *dest;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100671
672 /* for each plug record */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530673 for (i = 0; i < ns3plug; i++) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100674 pstart = s3plug[i].addr;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530675 pend = s3plug[i].addr + s3plug[i].len;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100676 /* find the matching PDR (or filename) */
Edgardo Hamese0264412010-07-21 22:31:56 -0300677 if (s3plug[i].itemcode != 0xffffffffUL) { /* not filename */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530678 for (j = 0; j < pda->nrec; j++) {
679 if (s3plug[i].itemcode ==
680 le16_to_cpu(pda->rec[j]->code))
681 break;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100682 }
683 } else {
684 j = -1;
685 }
Edgardo Hamese0264412010-07-21 22:31:56 -0300686 if (j >= pda->nrec && j != -1) { /* if no matching PDR, fail */
Karl Relton76e3e7c2009-04-17 10:15:34 +0100687 printk(KERN_WARNING
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530688 "warning: Failed to find PDR for "
689 "plugrec 0x%04x.\n", s3plug[i].itemcode);
690 continue; /* and move on to the next PDR */
Karl Relton76e3e7c2009-04-17 10:15:34 +0100691#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 */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530705 if (j != -1 && s3plug[i].len < le16_to_cpu(pda->rec[j]->len)) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100706 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530707 "error: Plug vs. PDR len mismatch for "
708 "plugrec 0x%04x, abort plugging.\n",
709 s3plug[i].itemcode);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100710 result = 1;
711 continue;
712 }
713
714 /* Validate plug address against chunk data and identify chunk */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530715 for (c = 0; c < nfchunks; c++) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100716 cstart = fchunk[c].addr;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530717 cend = fchunk[c].addr + fchunk[c].len;
718 if (pstart >= cstart && pend <= cend)
719 break;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100720 }
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530721 if (c >= nfchunks) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100722 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530723 "error: Failed to find image chunk for "
724 "plugrec 0x%04x.\n", s3plug[i].itemcode);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100725 result = 1;
726 continue;
727 }
728
729 /* Plug data */
730 chunkoff = pstart - cstart;
731 dest = fchunk[c].data + chunkoff;
732 pr_debug("Plugging item 0x%04x @ 0x%06x, len=%d, "
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530733 "cnum=%d coff=0x%06x\n",
734 s3plug[i].itemcode, pstart, s3plug[i].len,
735 c, chunkoff);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100736
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530737 if (j == -1) { /* plug the filename */
Karl Relton76e3e7c2009-04-17 10:15:34 +0100738 memset(dest, 0, s3plug[i].len);
739 strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530740 } else { /* plug a PDR */
741 memcpy(dest, &(pda->rec[j]->data), s3plug[i].len);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100742 }
743 }
744 return result;
745
746}
747
Karl Relton76e3e7c2009-04-17 10:15:34 +0100748/*----------------------------------------------------------------
749* read_cardpda
750*
751* Sends the command for the driver to read the pda from the card
752* named in the device variable. Upon success, the card pda is
753* stored in the "cardpda" variables. Note that the pda structure
754* is considered 'well formed' after this function. That means
755* that the nrecs is valid, the rec array has been set up, and there's
756* a valid PDAEND record in the raw PDA data.
757*
758* Arguments:
759* pda pda structure
760* wlandev device
761*
762* Returns:
763* 0 - success
764* ~0 - failure (probably an errno)
765----------------------------------------------------------------*/
Edgardo Hamese0264412010-07-21 22:31:56 -0300766int read_cardpda(struct pda *pda, wlandevice_t *wlandev)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100767{
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530768 int result = 0;
Edgardo Hamesb6bb56e2010-08-02 16:20:39 -0300769 struct p80211msg_p2req_readpda msg;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100770
771 /* set up the msg */
772 msg.msgcode = DIDmsg_p2req_readpda;
773 msg.msglen = sizeof(msg);
774 strcpy(msg.devname, wlandev->name);
775 msg.pda.did = DIDmsg_p2req_readpda_pda;
776 msg.pda.len = HFA384x_PDA_LEN_MAX;
777 msg.pda.status = P80211ENUM_msgitem_status_no_value;
778 msg.resultcode.did = DIDmsg_p2req_readpda_resultcode;
779 msg.resultcode.len = sizeof(u32);
780 msg.resultcode.status = P80211ENUM_msgitem_status_no_value;
781
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530782 if (prism2mgmt_readpda(wlandev, &msg) != 0) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100783 /* prism2mgmt_readpda prints an errno if appropriate */
784 result = -1;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530785 } else if (msg.resultcode.data == P80211ENUM_resultcode_success) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100786 memcpy(pda->buf, msg.pda.data, HFA384x_PDA_LEN_MAX);
787 result = mkpdrlist(pda);
788 } else {
789 /* resultcode must've been something other than success */
790 result = -1;
791 }
792
793 return result;
794}
795
Karl Relton76e3e7c2009-04-17 10:15:34 +0100796/*----------------------------------------------------------------
Karl Reltond8950592009-08-19 08:06:39 +0100797* read_fwfile
Karl Relton76e3e7c2009-04-17 10:15:34 +0100798*
Karl Reltond8950592009-08-19 08:06:39 +0100799* Reads the given fw file which should have been compiled from an srec
800* file. Each record in the fw file will either be a plain data record,
801* a start address record, or other records used for plugging.
Karl Relton76e3e7c2009-04-17 10:15:34 +0100802*
Karl Reltond8950592009-08-19 08:06:39 +0100803* Note that data records are expected to be sorted into
804* ascending address order in the fw file.
Karl Relton76e3e7c2009-04-17 10:15:34 +0100805*
Karl Reltond8950592009-08-19 08:06:39 +0100806* Note also that the start address record, originally an S7 record in
807* the srec file, is expected in the fw file to be like a data record but
808* with a certain address to make it identiable.
Karl Relton76e3e7c2009-04-17 10:15:34 +0100809*
Karl Reltond8950592009-08-19 08:06:39 +0100810* Here's the SREC format that the fw should have come from:
Karl Relton76e3e7c2009-04-17 10:15:34 +0100811* S[37]nnaaaaaaaaddd...dddcc
812*
813* nn - number of bytes starting with the address field
814* aaaaaaaa - address in readable (or big endian) format
815* dd....dd - 0-245 data bytes (two chars per byte)
816* cc - checksum
817*
818* The S7 record's (there should be only one) address value gets
Karl Reltond8950592009-08-19 08:06:39 +0100819* converted to an S3 record with address of 0xff400000, with the
820* start address being stored as a 4 byte data word. That address is
821* the start execution address used for RAM downloads.
Karl Relton76e3e7c2009-04-17 10:15:34 +0100822*
823* The S3 records have a collection of subformats indicated by the
824* value of aaaaaaaa:
825* 0xff000000 - Plug record, data field format:
826* xxxxxxxxaaaaaaaassssssss
827* x - PDR code number (little endian)
828* a - Address in load image to plug (little endian)
829* s - Length of plug data area (little endian)
830*
831* 0xff100000 - CRC16 generation record, data field format:
832* aaaaaaaassssssssbbbbbbbb
833* a - Start address for CRC calculation (little endian)
834* s - Length of data to calculate over (little endian)
835* b - Boolean, true=write crc, false=don't write
836*
837* 0xff200000 - Info record, data field format:
838* ssssttttdd..dd
839* s - Size in words (little endian)
840* t - Info type (little endian), see #defines and
Edgardo Hamese0264412010-07-21 22:31:56 -0300841* struct s3inforec for details about types.
Karl Relton76e3e7c2009-04-17 10:15:34 +0100842* d - (s - 1) little endian words giving the contents of
843* the given info type.
844*
Karl Reltond8950592009-08-19 08:06:39 +0100845* 0xff400000 - Start address record, data field format:
846* aaaaaaaa
847* a - Address in load image to plug (little endian)
848*
Karl Relton76e3e7c2009-04-17 10:15:34 +0100849* Arguments:
Karl Reltond8950592009-08-19 08:06:39 +0100850* record firmware image (ihex record structure) in kernel memory
Karl Relton76e3e7c2009-04-17 10:15:34 +0100851*
852* Returns:
853* 0 - success
854* ~0 - failure (probably an errno)
855----------------------------------------------------------------*/
Karl Reltond8950592009-08-19 08:06:39 +0100856int read_fwfile(const struct ihex_binrec *record)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100857{
Karl Reltond8950592009-08-19 08:06:39 +0100858 int i;
859 int rcnt = 0;
860 u16 *tmpinfo;
861 u16 *ptr16;
862 u32 *ptr32, len, addr;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100863
Karl Reltond8950592009-08-19 08:06:39 +0100864 pr_debug("Reading fw file ...\n");
Karl Relton76e3e7c2009-04-17 10:15:34 +0100865
Karl Reltond8950592009-08-19 08:06:39 +0100866 while (record) {
Karl Relton76e3e7c2009-04-17 10:15:34 +0100867
Karl Reltond8950592009-08-19 08:06:39 +0100868 rcnt++;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100869
Karl Reltond8950592009-08-19 08:06:39 +0100870 len = be16_to_cpu(record->len);
871 addr = be32_to_cpu(record->addr);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100872
Karl Reltond8950592009-08-19 08:06:39 +0100873 /* Point into data for different word lengths */
874 ptr32 = (u32 *) record->data;
875 ptr16 = (u16 *) record->data;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100876
Karl Reltond8950592009-08-19 08:06:39 +0100877 /* parse what was an S3 srec and put it in the right array */
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100878 switch (addr) {
Karl Reltond8950592009-08-19 08:06:39 +0100879 case S3ADDR_START:
880 startaddr = *ptr32;
881 pr_debug(" S7 start addr, record=%d "
882 " addr=0x%08x\n",
883 rcnt,
884 startaddr);
885 break;
886 case S3ADDR_PLUG:
887 s3plug[ns3plug].itemcode = *ptr32;
888 s3plug[ns3plug].addr = *(ptr32 + 1);
889 s3plug[ns3plug].len = *(ptr32 + 2);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100890
Karl Reltond8950592009-08-19 08:06:39 +0100891 pr_debug(" S3 plugrec, record=%d "
892 "itemcode=0x%08x addr=0x%08x len=%d\n",
893 rcnt,
894 s3plug[ns3plug].itemcode,
895 s3plug[ns3plug].addr,
896 s3plug[ns3plug].len);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100897
Karl Reltond8950592009-08-19 08:06:39 +0100898 ns3plug++;
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100899 if (ns3plug == S3PLUG_MAX) {
Karl Reltond8950592009-08-19 08:06:39 +0100900 printk(KERN_ERR "S3 plugrec limit reached - aborting\n");
901 return 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100902 }
Karl Reltond8950592009-08-19 08:06:39 +0100903 break;
904 case S3ADDR_CRC:
905 s3crc[ns3crc].addr = *ptr32;
906 s3crc[ns3crc].len = *(ptr32 + 1);
907 s3crc[ns3crc].dowrite = *(ptr32 + 2);
Karl Relton76e3e7c2009-04-17 10:15:34 +0100908
Karl Reltond8950592009-08-19 08:06:39 +0100909 pr_debug(" S3 crcrec, record=%d "
910 "addr=0x%08x len=%d write=0x%08x\n",
911 rcnt,
912 s3crc[ns3crc].addr,
913 s3crc[ns3crc].len,
914 s3crc[ns3crc].dowrite);
915 ns3crc++;
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100916 if (ns3crc == S3CRC_MAX) {
Karl Reltond8950592009-08-19 08:06:39 +0100917 printk(KERN_ERR "S3 crcrec limit reached - aborting\n");
918 return 1;
919 }
920 break;
921 case S3ADDR_INFO:
922 s3info[ns3info].len = *ptr16;
923 s3info[ns3info].type = *(ptr16 + 1);
924
925 pr_debug(" S3 inforec, record=%d "
926 "len=0x%04x type=0x%04x\n",
927 rcnt,
928 s3info[ns3info].len,
929 s3info[ns3info].type);
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100930 if (((s3info[ns3info].len - 1) * sizeof(u16)) > sizeof(s3info[ns3info].info)) {
Karl Reltond8950592009-08-19 08:06:39 +0100931 printk(KERN_ERR " S3 inforec length too long - aborting\n");
932 return 1;
933 }
934
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100935 tmpinfo = (u16 *)&(s3info[ns3info].info.version);
Karl Reltond8950592009-08-19 08:06:39 +0100936 pr_debug(" info=");
937 for (i = 0; i < s3info[ns3info].len - 1; i++) {
938 tmpinfo[i] = *(ptr16 + 2 + i);
939 pr_debug("%04x ", tmpinfo[i]);
940 }
941 pr_debug("\n");
942
943 ns3info++;
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100944 if (ns3info == S3INFO_MAX) {
Karl Reltond8950592009-08-19 08:06:39 +0100945 printk(KERN_ERR "S3 inforec limit reached - aborting\n");
946 return 1;
947 }
948 break;
949 default: /* Data record */
950 s3data[ns3data].addr = addr;
951 s3data[ns3data].len = len;
952 s3data[ns3data].data = (uint8_t *) record->data;
953 ns3data++;
Svenne Krap5dd8acc2010-02-14 18:59:00 +0100954 if (ns3data == S3DATA_MAX) {
Karl Reltond8950592009-08-19 08:06:39 +0100955 printk(KERN_ERR "S3 datarec limit reached - aborting\n");
956 return 1;
957 }
958 break;
959 }
960 record = ihex_next_binrec(record);
961 }
962 return 0;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100963}
964
Karl Relton76e3e7c2009-04-17 10:15:34 +0100965/*----------------------------------------------------------------
966* writeimage
967*
968* Takes the chunks, builds p80211 messages and sends them down
969* to the driver for writing to the card.
970*
971* Arguments:
972* wlandev device
973* fchunk Array of image chunks
974* nfchunks Number of image chunks
975*
976* Returns:
977* 0 success
978* ~0 failure
979----------------------------------------------------------------*/
Edgardo Hamese0264412010-07-21 22:31:56 -0300980int writeimage(wlandevice_t *wlandev, struct imgchunk *fchunk,
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530981 unsigned int nfchunks)
Karl Relton76e3e7c2009-04-17 10:15:34 +0100982{
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530983 int result = 0;
Edgardo Hamesb6bb56e2010-08-02 16:20:39 -0300984 struct p80211msg_p2req_ramdl_state rstatemsg;
985 struct p80211msg_p2req_ramdl_write rwritemsg;
Edgardo Hames3d049432010-08-02 17:17:17 -0300986 struct p80211msg *msgp;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530987 u32 resultcode;
988 int i;
989 int j;
990 unsigned int nwrites;
991 u32 curroff;
992 u32 currlen;
993 u32 currdaddr;
Karl Relton76e3e7c2009-04-17 10:15:34 +0100994
995 /* Initialize the messages */
996 memset(&rstatemsg, 0, sizeof(rstatemsg));
997 strcpy(rstatemsg.devname, wlandev->name);
Mithlesh Thukral75f49e02009-05-25 19:06:16 +0530998 rstatemsg.msgcode = DIDmsg_p2req_ramdl_state;
999 rstatemsg.msglen = sizeof(rstatemsg);
1000 rstatemsg.enable.did = DIDmsg_p2req_ramdl_state_enable;
1001 rstatemsg.exeaddr.did = DIDmsg_p2req_ramdl_state_exeaddr;
1002 rstatemsg.resultcode.did = DIDmsg_p2req_ramdl_state_resultcode;
1003 rstatemsg.enable.status = P80211ENUM_msgitem_status_data_ok;
1004 rstatemsg.exeaddr.status = P80211ENUM_msgitem_status_data_ok;
1005 rstatemsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
1006 rstatemsg.enable.len = sizeof(u32);
1007 rstatemsg.exeaddr.len = sizeof(u32);
1008 rstatemsg.resultcode.len = sizeof(u32);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001009
1010 memset(&rwritemsg, 0, sizeof(rwritemsg));
1011 strcpy(rwritemsg.devname, wlandev->name);
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301012 rwritemsg.msgcode = DIDmsg_p2req_ramdl_write;
1013 rwritemsg.msglen = sizeof(rwritemsg);
1014 rwritemsg.addr.did = DIDmsg_p2req_ramdl_write_addr;
1015 rwritemsg.len.did = DIDmsg_p2req_ramdl_write_len;
1016 rwritemsg.data.did = DIDmsg_p2req_ramdl_write_data;
1017 rwritemsg.resultcode.did = DIDmsg_p2req_ramdl_write_resultcode;
1018 rwritemsg.addr.status = P80211ENUM_msgitem_status_data_ok;
1019 rwritemsg.len.status = P80211ENUM_msgitem_status_data_ok;
1020 rwritemsg.data.status = P80211ENUM_msgitem_status_data_ok;
1021 rwritemsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
1022 rwritemsg.addr.len = sizeof(u32);
1023 rwritemsg.len.len = sizeof(u32);
1024 rwritemsg.data.len = WRITESIZE_MAX;
1025 rwritemsg.resultcode.len = sizeof(u32);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001026
1027 /* Send xxx_state(enable) */
1028 pr_debug("Sending dl_state(enable) message.\n");
1029 rstatemsg.enable.data = P80211ENUM_truth_true;
1030 rstatemsg.exeaddr.data = startaddr;
1031
Edgardo Hames3d049432010-08-02 17:17:17 -03001032 msgp = (struct p80211msg *) &rstatemsg;
Karl Relton76e3e7c2009-04-17 10:15:34 +01001033 result = prism2mgmt_ramdl_state(wlandev, msgp);
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301034 if (result) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001035 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301036 "writeimage state enable failed w/ result=%d, "
1037 "aborting download\n", result);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001038 return result;
1039 }
1040 resultcode = rstatemsg.resultcode.data;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301041 if (resultcode != P80211ENUM_resultcode_success) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001042 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301043 "writeimage()->xxxdl_state msg indicates failure, "
1044 "w/ resultcode=%d, aborting download.\n", resultcode);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001045 return 1;
1046 }
1047
1048 /* Now, loop through the data chunks and send WRITESIZE_MAX data */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301049 for (i = 0; i < nfchunks; i++) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001050 nwrites = fchunk[i].len / WRITESIZE_MAX;
1051 nwrites += (fchunk[i].len % WRITESIZE_MAX) ? 1 : 0;
1052 curroff = 0;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301053 for (j = 0; j < nwrites; j++) {
Edgardo Hamese0264412010-07-21 22:31:56 -03001054 /* TODO Move this to a separate function */
1055 int lenleft = fchunk[i].len - (WRITESIZE_MAX * j);
1056 if (fchunk[i].len > WRITESIZE_MAX)
1057 currlen = WRITESIZE_MAX;
1058 else
1059 currlen = lenleft;
Karl Relton76e3e7c2009-04-17 10:15:34 +01001060 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,
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301066 fchunk[i].data + curroff, currlen);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001067
1068 /* Send flashdl_write(pda) */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301069 pr_debug
1070 ("Sending xxxdl_write message addr=%06x len=%d.\n",
1071 currdaddr, currlen);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001072
Edgardo Hames3d049432010-08-02 17:17:17 -03001073 msgp = (struct p80211msg *) &rwritemsg;
Karl Relton76e3e7c2009-04-17 10:15:34 +01001074 result = prism2mgmt_ramdl_write(wlandev, msgp);
1075
1076 /* Check the results */
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301077 if (result) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001078 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301079 "writeimage chunk write failed w/ result=%d, "
1080 "aborting download\n", result);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001081 return result;
1082 }
1083 resultcode = rstatemsg.resultcode.data;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301084 if (resultcode != P80211ENUM_resultcode_success) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001085 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301086 "writeimage()->xxxdl_write msg indicates failure, "
1087 "w/ resultcode=%d, aborting download.\n",
1088 resultcode);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001089 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
Edgardo Hames3d049432010-08-02 17:17:17 -03001100 msgp = (struct p80211msg *) &rstatemsg;
Karl Relton76e3e7c2009-04-17 10:15:34 +01001101 result = prism2mgmt_ramdl_state(wlandev, msgp);
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301102 if (result) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001103 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301104 "writeimage state disable failed w/ result=%d, "
1105 "aborting download\n", result);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001106 return result;
1107 }
1108 resultcode = rstatemsg.resultcode.data;
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301109 if (resultcode != P80211ENUM_resultcode_success) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001110 printk(KERN_ERR
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301111 "writeimage()->xxxdl_state msg indicates failure, "
1112 "w/ resultcode=%d, aborting download.\n", resultcode);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001113 return 1;
1114 }
1115 return result;
1116}
1117
Karl Relton76e3e7c2009-04-17 10:15:34 +01001118int validate_identity(void)
1119{
1120 int i;
1121 int result = 1;
Karl Reltond8950592009-08-19 08:06:39 +01001122 int trump = 0;
Karl Relton76e3e7c2009-04-17 10:15:34 +01001123
1124 pr_debug("NIC ID: %#x v%d.%d.%d\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301125 nicid.id, nicid.major, nicid.minor, nicid.variant);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001126 pr_debug("MFI ID: %#x v%d %d->%d\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301127 rfid.id, rfid.variant, rfid.bottom, rfid.top);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001128 pr_debug("CFI ID: %#x v%d %d->%d\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301129 macid.id, macid.variant, macid.bottom, macid.top);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001130 pr_debug("PRI ID: %#x v%d %d->%d\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301131 priid.id, priid.variant, priid.bottom, priid.top);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001132
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301133 for (i = 0; i < ns3info; i++) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001134 switch (s3info[i].type) {
1135 case 1:
1136 pr_debug("Version: ID %#x %d.%d.%d\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301137 s3info[i].info.version.id,
1138 s3info[i].info.version.major,
1139 s3info[i].info.version.minor,
1140 s3info[i].info.version.variant);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001141 break;
1142 case 2:
1143 pr_debug("Compat: Role %#x Id %#x v%d %d->%d\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301144 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);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001149
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)) {
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301162 if ((s3info[i].info.compat.bottom > priid.top)
1163 || (s3info[i].info.compat.top <
1164 priid.bottom)) {
Karl Relton76e3e7c2009-04-17 10:15:34 +01001165 result = 3;
1166 }
1167 }
1168 /* SEC compat range */
1169 if ((s3info[i].info.compat.role == 1) &&
1170 (s3info[i].info.compat.id == 4)) {
Greg Kroah-Hartmanb02957d2010-03-04 08:14:54 -08001171 /* FIXME: isn't something missing here? */
Karl Relton76e3e7c2009-04-17 10:15:34 +01001172 }
1173
1174 break;
1175 case 3:
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301176 pr_debug("Seq: %#x\n", s3info[i].info.buildseq);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001177
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301178 break;
Karl Relton76e3e7c2009-04-17 10:15:34 +01001179 case 4:
1180 pr_debug("Platform: ID %#x %d.%d.%d\n",
Mithlesh Thukral75f49e02009-05-25 19:06:16 +05301181 s3info[i].info.version.id,
1182 s3info[i].info.version.major,
1183 s3info[i].info.version.minor,
1184 s3info[i].info.version.variant);
Karl Relton76e3e7c2009-04-17 10:15:34 +01001185
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
Karl Reltond8950592009-08-19 08:06:39 +01001196 trump = 1;
Karl Relton76e3e7c2009-04-17 10:15:34 +01001197 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 }
Svenne Krap5dd8acc2010-02-14 18:59:00 +01001206 /* walk through */
Karl Relton76e3e7c2009-04-17 10:15:34 +01001207
Svenne Krap5dd8acc2010-02-14 18:59:00 +01001208 if (trump && (result != 2))
1209 result = 0;
Karl Relton76e3e7c2009-04-17 10:15:34 +01001210 return result;
1211}