fw.c 9.6 KB

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