prism2fw.c 34 KB

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