fw.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. dev_err(dev, "Cannot find firmware %s\n", firmware);
  117. err = -ENOENT;
  118. goto free;
  119. }
  120. } else
  121. fw_entry = orinoco_cached_fw_get(priv, false);
  122. hdr = (const struct orinoco_fw_header *) fw_entry->data;
  123. fw_err = validate_fw(hdr, fw_entry->size);
  124. if (fw_err) {
  125. dev_warn(dev, "Invalid firmware image detected (%s). "
  126. "Aborting download\n", fw_err);
  127. err = -EINVAL;
  128. goto abort;
  129. }
  130. /* Enable aux port to allow programming */
  131. err = hw->ops->program_init(hw, le32_to_cpu(hdr->entry_point));
  132. dev_dbg(dev, "Program init returned %d\n", err);
  133. if (err != 0)
  134. goto abort;
  135. /* Program data */
  136. first_block = (fw_entry->data +
  137. le16_to_cpu(hdr->headersize) +
  138. le32_to_cpu(hdr->block_offset));
  139. end = fw_entry->data + fw_entry->size;
  140. err = hermes_program(hw, first_block, end);
  141. dev_dbg(dev, "Program returned %d\n", err);
  142. if (err != 0)
  143. goto abort;
  144. /* Update production data */
  145. first_block = (fw_entry->data +
  146. le16_to_cpu(hdr->headersize) +
  147. le32_to_cpu(hdr->pdr_offset));
  148. err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
  149. &pda[fw->pda_size / sizeof(*pda)]);
  150. dev_dbg(dev, "Apply PDA returned %d\n", err);
  151. if (err)
  152. goto abort;
  153. /* Tell card we've finished */
  154. err = hw->ops->program_end(hw);
  155. dev_dbg(dev, "Program end returned %d\n", err);
  156. if (err != 0)
  157. goto abort;
  158. /* Check if we're running */
  159. dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
  160. abort:
  161. /* If we requested the firmware, release it. */
  162. if (!orinoco_cached_fw_get(priv, false))
  163. release_firmware(fw_entry);
  164. free:
  165. kfree(pda);
  166. return err;
  167. }
  168. /*
  169. * Process a firmware image - stop the card, load the firmware, reset
  170. * the card and make sure it responds. For the secondary firmware take
  171. * care of the PDA - read it and then write it on top of the firmware.
  172. */
  173. static int
  174. symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
  175. const unsigned char *image, const void *end,
  176. int secondary)
  177. {
  178. struct hermes *hw = &priv->hw;
  179. int ret = 0;
  180. const unsigned char *ptr;
  181. const unsigned char *first_block;
  182. /* Plug Data Area (PDA) */
  183. __le16 *pda = NULL;
  184. /* Binary block begins after the 0x1A marker */
  185. ptr = image;
  186. while (*ptr++ != TEXT_END);
  187. first_block = ptr;
  188. /* Read the PDA from EEPROM */
  189. if (secondary) {
  190. pda = kzalloc(fw->pda_size, GFP_KERNEL);
  191. if (!pda)
  192. return -ENOMEM;
  193. ret = hw->ops->read_pda(hw, pda, fw->pda_addr, fw->pda_size);
  194. if (ret)
  195. goto free;
  196. }
  197. /* Stop the firmware, so that it can be safely rewritten */
  198. if (priv->stop_fw) {
  199. ret = priv->stop_fw(priv, 1);
  200. if (ret)
  201. goto free;
  202. }
  203. /* Program the adapter with new firmware */
  204. ret = hermes_program(hw, first_block, end);
  205. if (ret)
  206. goto free;
  207. /* Write the PDA to the adapter */
  208. if (secondary) {
  209. size_t len = hermes_blocks_length(first_block, end);
  210. ptr = first_block + len;
  211. ret = hermes_apply_pda(hw, ptr, end, pda,
  212. &pda[fw->pda_size / sizeof(*pda)]);
  213. kfree(pda);
  214. if (ret)
  215. return ret;
  216. }
  217. /* Run the firmware */
  218. if (priv->stop_fw) {
  219. ret = priv->stop_fw(priv, 0);
  220. if (ret)
  221. return ret;
  222. }
  223. /* Reset hermes chip and make sure it responds */
  224. ret = hw->ops->init(hw);
  225. /* hermes_reset() should return 0 with the secondary firmware */
  226. if (secondary && ret != 0)
  227. return -ENODEV;
  228. /* And this should work with any firmware */
  229. if (!hermes_present(hw))
  230. return -ENODEV;
  231. return 0;
  232. free:
  233. kfree(pda);
  234. return ret;
  235. }
  236. /*
  237. * Download the firmware into the card, this also does a PCMCIA soft
  238. * reset on the card, to make sure it's in a sane state.
  239. */
  240. static int
  241. symbol_dl_firmware(struct orinoco_private *priv,
  242. const struct fw_info *fw)
  243. {
  244. struct device *dev = priv->dev;
  245. int ret;
  246. const struct firmware *fw_entry;
  247. if (!orinoco_cached_fw_get(priv, true)) {
  248. if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
  249. dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
  250. return -ENOENT;
  251. }
  252. } else
  253. fw_entry = orinoco_cached_fw_get(priv, true);
  254. /* Load primary firmware */
  255. ret = symbol_dl_image(priv, fw, fw_entry->data,
  256. fw_entry->data + fw_entry->size, 0);
  257. if (!orinoco_cached_fw_get(priv, true))
  258. release_firmware(fw_entry);
  259. if (ret) {
  260. dev_err(dev, "Primary firmware download failed\n");
  261. return ret;
  262. }
  263. if (!orinoco_cached_fw_get(priv, false)) {
  264. if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
  265. dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
  266. return -ENOENT;
  267. }
  268. } else
  269. fw_entry = orinoco_cached_fw_get(priv, false);
  270. /* Load secondary firmware */
  271. ret = symbol_dl_image(priv, fw, fw_entry->data,
  272. fw_entry->data + fw_entry->size, 1);
  273. if (!orinoco_cached_fw_get(priv, false))
  274. release_firmware(fw_entry);
  275. if (ret)
  276. dev_err(dev, "Secondary firmware download failed\n");
  277. return ret;
  278. }
  279. int orinoco_download(struct orinoco_private *priv)
  280. {
  281. int err = 0;
  282. /* Reload firmware */
  283. switch (priv->firmware_type) {
  284. case FIRMWARE_TYPE_AGERE:
  285. /* case FIRMWARE_TYPE_INTERSIL: */
  286. err = orinoco_dl_firmware(priv,
  287. &orinoco_fw[priv->firmware_type], 0);
  288. break;
  289. case FIRMWARE_TYPE_SYMBOL:
  290. err = symbol_dl_firmware(priv,
  291. &orinoco_fw[priv->firmware_type]);
  292. break;
  293. case FIRMWARE_TYPE_INTERSIL:
  294. break;
  295. }
  296. /* TODO: if we fail we probably need to reinitialise
  297. * the driver */
  298. return err;
  299. }
  300. #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
  301. void orinoco_cache_fw(struct orinoco_private *priv, int ap)
  302. {
  303. const struct firmware *fw_entry = NULL;
  304. const char *pri_fw;
  305. const char *fw;
  306. pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
  307. if (ap)
  308. fw = orinoco_fw[priv->firmware_type].ap_fw;
  309. else
  310. fw = orinoco_fw[priv->firmware_type].sta_fw;
  311. if (pri_fw) {
  312. if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
  313. priv->cached_pri_fw = fw_entry;
  314. }
  315. if (fw) {
  316. if (request_firmware(&fw_entry, fw, priv->dev) == 0)
  317. priv->cached_fw = fw_entry;
  318. }
  319. }
  320. void orinoco_uncache_fw(struct orinoco_private *priv)
  321. {
  322. release_firmware(priv->cached_pri_fw);
  323. release_firmware(priv->cached_fw);
  324. priv->cached_pri_fw = NULL;
  325. priv->cached_fw = NULL;
  326. }
  327. #endif