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