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