fw.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* Firmware file reading and download helpers
  2. *
  3. * See copyright notice in main.c
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/firmware.h>
  7. #include <linux/device.h>
  8. #include "hermes.h"
  9. #include "hermes_dld.h"
  10. #include "orinoco.h"
  11. #include "fw.h"
  12. /* End markers (for Symbol firmware only) */
  13. #define TEXT_END 0x1A /* End of text header */
  14. struct fw_info {
  15. char *pri_fw;
  16. char *sta_fw;
  17. char *ap_fw;
  18. u32 pda_addr;
  19. u16 pda_size;
  20. };
  21. static const struct fw_info orinoco_fw[] = {
  22. { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
  23. { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
  24. { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
  25. };
  26. /* Structure used to access fields in FW
  27. * Make sure LE decoding macros are used
  28. */
  29. struct orinoco_fw_header {
  30. char hdr_vers[6]; /* ASCII string for header version */
  31. __le16 headersize; /* Total length of header */
  32. __le32 entry_point; /* NIC entry point */
  33. __le32 blocks; /* Number of blocks to program */
  34. __le32 block_offset; /* Offset of block data from eof header */
  35. __le32 pdr_offset; /* Offset to PDR data from eof header */
  36. __le32 pri_offset; /* Offset to primary plug data */
  37. __le32 compat_offset; /* Offset to compatibility data*/
  38. char signature[0]; /* FW signature length headersize-20 */
  39. } __attribute__ ((packed));
  40. /* Check the range of various header entries. Return a pointer to a
  41. * description of the problem, or NULL if everything checks out. */
  42. static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
  43. {
  44. u16 hdrsize;
  45. if (len < sizeof(*hdr))
  46. return "image too small";
  47. if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
  48. return "format not recognised";
  49. hdrsize = le16_to_cpu(hdr->headersize);
  50. if (hdrsize > len)
  51. return "bad headersize";
  52. if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
  53. return "bad block offset";
  54. if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
  55. return "bad PDR offset";
  56. if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
  57. return "bad PRI offset";
  58. if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
  59. return "bad compat offset";
  60. /* TODO: consider adding a checksum or CRC to the firmware format */
  61. return NULL;
  62. }
  63. #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
  64. static inline const struct firmware *
  65. orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
  66. {
  67. if (primary)
  68. return priv->cached_pri_fw;
  69. else
  70. return priv->cached_fw;
  71. }
  72. #else
  73. #define orinoco_cached_fw_get(priv, primary) (NULL)
  74. #endif
  75. /* Download either STA or AP firmware into the card. */
  76. static int
  77. orinoco_dl_firmware(struct orinoco_private *priv,
  78. const struct fw_info *fw,
  79. int ap)
  80. {
  81. /* Plug Data Area (PDA) */
  82. __le16 *pda;
  83. hermes_t *hw = &priv->hw;
  84. const struct firmware *fw_entry;
  85. const struct orinoco_fw_header *hdr;
  86. const unsigned char *first_block;
  87. const void *end;
  88. const char *firmware;
  89. const char *fw_err;
  90. struct device *dev = priv->dev;
  91. int err = 0;
  92. pda = kzalloc(fw->pda_size, GFP_KERNEL);
  93. if (!pda)
  94. return -ENOMEM;
  95. if (ap)
  96. firmware = fw->ap_fw;
  97. else
  98. firmware = fw->sta_fw;
  99. dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
  100. /* Read current plug data */
  101. err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
  102. dev_dbg(dev, "Read PDA returned %d\n", err);
  103. if (err)
  104. goto free;
  105. if (!orinoco_cached_fw_get(priv, false)) {
  106. err = request_firmware(&fw_entry, firmware, priv->dev);
  107. if (err) {
  108. dev_err(dev, "Cannot find firmware %s\n", firmware);
  109. err = -ENOENT;
  110. goto free;
  111. }
  112. } else
  113. fw_entry = orinoco_cached_fw_get(priv, false);
  114. hdr = (const struct orinoco_fw_header *) fw_entry->data;
  115. fw_err = validate_fw(hdr, fw_entry->size);
  116. if (fw_err) {
  117. dev_warn(dev, "Invalid firmware image detected (%s). "
  118. "Aborting download\n", fw_err);
  119. err = -EINVAL;
  120. goto abort;
  121. }
  122. /* Enable aux port to allow programming */
  123. err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
  124. dev_dbg(dev, "Program init returned %d\n", err);
  125. if (err != 0)
  126. goto abort;
  127. /* Program data */
  128. first_block = (fw_entry->data +
  129. le16_to_cpu(hdr->headersize) +
  130. le32_to_cpu(hdr->block_offset));
  131. end = fw_entry->data + fw_entry->size;
  132. err = hermes_program(hw, first_block, end);
  133. dev_dbg(dev, "Program returned %d\n", err);
  134. if (err != 0)
  135. goto abort;
  136. /* Update production data */
  137. first_block = (fw_entry->data +
  138. le16_to_cpu(hdr->headersize) +
  139. le32_to_cpu(hdr->pdr_offset));
  140. err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
  141. &pda[fw->pda_size / sizeof(*pda)]);
  142. dev_dbg(dev, "Apply PDA returned %d\n", err);
  143. if (err)
  144. goto abort;
  145. /* Tell card we've finished */
  146. err = hermesi_program_end(hw);
  147. dev_dbg(dev, "Program end returned %d\n", err);
  148. if (err != 0)
  149. goto abort;
  150. /* Check if we're running */
  151. dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
  152. abort:
  153. /* If we requested the firmware, release it. */
  154. if (!orinoco_cached_fw_get(priv, false))
  155. release_firmware(fw_entry);
  156. free:
  157. kfree(pda);
  158. return err;
  159. }
  160. /*
  161. * Process a firmware image - stop the card, load the firmware, reset
  162. * the card and make sure it responds. For the secondary firmware take
  163. * care of the PDA - read it and then write it on top of the firmware.
  164. */
  165. static int
  166. symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
  167. const unsigned char *image, const void *end,
  168. int secondary)
  169. {
  170. hermes_t *hw = &priv->hw;
  171. int ret = 0;
  172. const unsigned char *ptr;
  173. const unsigned char *first_block;
  174. /* Plug Data Area (PDA) */
  175. __le16 *pda = NULL;
  176. /* Binary block begins after the 0x1A marker */
  177. ptr = image;
  178. while (*ptr++ != TEXT_END);
  179. first_block = ptr;
  180. /* Read the PDA from EEPROM */
  181. if (secondary) {
  182. pda = kzalloc(fw->pda_size, GFP_KERNEL);
  183. if (!pda)
  184. return -ENOMEM;
  185. ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
  186. if (ret)
  187. goto free;
  188. }
  189. /* Stop the firmware, so that it can be safely rewritten */
  190. if (priv->stop_fw) {
  191. ret = priv->stop_fw(priv, 1);
  192. if (ret)
  193. goto free;
  194. }
  195. /* Program the adapter with new firmware */
  196. ret = hermes_program(hw, first_block, end);
  197. if (ret)
  198. goto free;
  199. /* Write the PDA to the adapter */
  200. if (secondary) {
  201. size_t len = hermes_blocks_length(first_block, end);
  202. ptr = first_block + len;
  203. ret = hermes_apply_pda(hw, ptr, end, pda,
  204. &pda[fw->pda_size / sizeof(*pda)]);
  205. kfree(pda);
  206. if (ret)
  207. return ret;
  208. }
  209. /* Run the firmware */
  210. if (priv->stop_fw) {
  211. ret = priv->stop_fw(priv, 0);
  212. if (ret)
  213. return ret;
  214. }
  215. /* Reset hermes chip and make sure it responds */
  216. ret = hermes_init(hw);
  217. /* hermes_reset() should return 0 with the secondary firmware */
  218. if (secondary && ret != 0)
  219. return -ENODEV;
  220. /* And this should work with any firmware */
  221. if (!hermes_present(hw))
  222. return -ENODEV;
  223. return 0;
  224. free:
  225. kfree(pda);
  226. return ret;
  227. }
  228. /*
  229. * Download the firmware into the card, this also does a PCMCIA soft
  230. * reset on the card, to make sure it's in a sane state.
  231. */
  232. static int
  233. symbol_dl_firmware(struct orinoco_private *priv,
  234. const struct fw_info *fw)
  235. {
  236. struct device *dev = priv->dev;
  237. int ret;
  238. const struct firmware *fw_entry;
  239. if (!orinoco_cached_fw_get(priv, true)) {
  240. if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
  241. dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
  242. return -ENOENT;
  243. }
  244. } else
  245. fw_entry = orinoco_cached_fw_get(priv, true);
  246. /* Load primary firmware */
  247. ret = symbol_dl_image(priv, fw, fw_entry->data,
  248. fw_entry->data + fw_entry->size, 0);
  249. if (!orinoco_cached_fw_get(priv, true))
  250. release_firmware(fw_entry);
  251. if (ret) {
  252. dev_err(dev, "Primary firmware download failed\n");
  253. return ret;
  254. }
  255. if (!orinoco_cached_fw_get(priv, false)) {
  256. if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
  257. dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
  258. return -ENOENT;
  259. }
  260. } else
  261. fw_entry = orinoco_cached_fw_get(priv, false);
  262. /* Load secondary firmware */
  263. ret = symbol_dl_image(priv, fw, fw_entry->data,
  264. fw_entry->data + fw_entry->size, 1);
  265. if (!orinoco_cached_fw_get(priv, false))
  266. release_firmware(fw_entry);
  267. if (ret) {
  268. dev_err(dev, "Secondary firmware download failed\n");
  269. }
  270. return ret;
  271. }
  272. int orinoco_download(struct orinoco_private *priv)
  273. {
  274. int err = 0;
  275. /* Reload firmware */
  276. switch (priv->firmware_type) {
  277. case FIRMWARE_TYPE_AGERE:
  278. /* case FIRMWARE_TYPE_INTERSIL: */
  279. err = orinoco_dl_firmware(priv,
  280. &orinoco_fw[priv->firmware_type], 0);
  281. break;
  282. case FIRMWARE_TYPE_SYMBOL:
  283. err = symbol_dl_firmware(priv,
  284. &orinoco_fw[priv->firmware_type]);
  285. break;
  286. case FIRMWARE_TYPE_INTERSIL:
  287. break;
  288. }
  289. /* TODO: if we fail we probably need to reinitialise
  290. * the driver */
  291. return err;
  292. }
  293. #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
  294. void orinoco_cache_fw(struct orinoco_private *priv, int ap)
  295. {
  296. const struct firmware *fw_entry = NULL;
  297. const char *pri_fw;
  298. const char *fw;
  299. pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
  300. if (ap)
  301. fw = orinoco_fw[priv->firmware_type].ap_fw;
  302. else
  303. fw = orinoco_fw[priv->firmware_type].sta_fw;
  304. if (pri_fw) {
  305. if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
  306. priv->cached_pri_fw = fw_entry;
  307. }
  308. if (fw) {
  309. if (request_firmware(&fw_entry, fw, priv->dev) == 0)
  310. priv->cached_fw = fw_entry;
  311. }
  312. }
  313. void orinoco_uncache_fw(struct orinoco_private *priv)
  314. {
  315. if (priv->cached_pri_fw)
  316. release_firmware(priv->cached_pri_fw);
  317. if (priv->cached_fw)
  318. release_firmware(priv->cached_fw);
  319. priv->cached_pri_fw = NULL;
  320. priv->cached_fw = NULL;
  321. }
  322. #endif