fw.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 "hermes.h"
  8. #include "hermes_dld.h"
  9. #include "orinoco.h"
  10. #include "fw.h"
  11. /* End markers (for Symbol firmware only) */
  12. #define TEXT_END 0x1A /* End of text header */
  13. struct fw_info {
  14. char *pri_fw;
  15. char *sta_fw;
  16. char *ap_fw;
  17. u32 pda_addr;
  18. u16 pda_size;
  19. };
  20. static const struct fw_info orinoco_fw[] = {
  21. { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
  22. { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
  23. { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
  24. };
  25. /* Structure used to access fields in FW
  26. * Make sure LE decoding macros are used
  27. */
  28. struct orinoco_fw_header {
  29. char hdr_vers[6]; /* ASCII string for header version */
  30. __le16 headersize; /* Total length of header */
  31. __le32 entry_point; /* NIC entry point */
  32. __le32 blocks; /* Number of blocks to program */
  33. __le32 block_offset; /* Offset of block data from eof header */
  34. __le32 pdr_offset; /* Offset to PDR data from eof header */
  35. __le32 pri_offset; /* Offset to primary plug data */
  36. __le32 compat_offset; /* Offset to compatibility data*/
  37. char signature[0]; /* FW signature length headersize-20 */
  38. } __attribute__ ((packed));
  39. /* Check the range of various header entries. Return a pointer to a
  40. * description of the problem, or NULL if everything checks out. */
  41. static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
  42. {
  43. u16 hdrsize;
  44. if (len < sizeof(*hdr))
  45. return "image too small";
  46. if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
  47. return "format not recognised";
  48. hdrsize = le16_to_cpu(hdr->headersize);
  49. if (hdrsize > len)
  50. return "bad headersize";
  51. if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
  52. return "bad block offset";
  53. if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
  54. return "bad PDR offset";
  55. if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
  56. return "bad PRI offset";
  57. if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
  58. return "bad compat offset";
  59. /* TODO: consider adding a checksum or CRC to the firmware format */
  60. return NULL;
  61. }
  62. #if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
  63. static inline const struct firmware *
  64. orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
  65. {
  66. if (primary)
  67. return priv->cached_pri_fw;
  68. else
  69. return priv->cached_fw;
  70. }
  71. #else
  72. #define orinoco_cached_fw_get(priv, primary) (NULL)
  73. #endif
  74. /* Download either STA or AP firmware into the card. */
  75. static int
  76. orinoco_dl_firmware(struct orinoco_private *priv,
  77. const struct fw_info *fw,
  78. int ap)
  79. {
  80. /* Plug Data Area (PDA) */
  81. __le16 *pda;
  82. hermes_t *hw = &priv->hw;
  83. const struct firmware *fw_entry;
  84. const struct orinoco_fw_header *hdr;
  85. const unsigned char *first_block;
  86. const void *end;
  87. const char *firmware;
  88. const char *fw_err;
  89. struct net_device *dev = priv->ndev;
  90. int err = 0;
  91. pda = kzalloc(fw->pda_size, GFP_KERNEL);
  92. if (!pda)
  93. return -ENOMEM;
  94. if (ap)
  95. firmware = fw->ap_fw;
  96. else
  97. firmware = fw->sta_fw;
  98. printk(KERN_DEBUG "%s: Attempting to download firmware %s\n",
  99. dev->name, firmware);
  100. /* Read current plug data */
  101. err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
  102. printk(KERN_DEBUG "%s: Read PDA returned %d\n", dev->name, 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. printk(KERN_ERR "%s: Cannot find firmware %s\n",
  109. dev->name, firmware);
  110. err = -ENOENT;
  111. goto free;
  112. }
  113. } else
  114. fw_entry = orinoco_cached_fw_get(priv, false);
  115. hdr = (const struct orinoco_fw_header *) fw_entry->data;
  116. fw_err = validate_fw(hdr, fw_entry->size);
  117. if (fw_err) {
  118. printk(KERN_WARNING "%s: Invalid firmware image detected (%s). "
  119. "Aborting download\n",
  120. dev->name, fw_err);
  121. err = -EINVAL;
  122. goto abort;
  123. }
  124. /* Enable aux port to allow programming */
  125. err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
  126. printk(KERN_DEBUG "%s: Program init returned %d\n", dev->name, err);
  127. if (err != 0)
  128. goto abort;
  129. /* Program data */
  130. first_block = (fw_entry->data +
  131. le16_to_cpu(hdr->headersize) +
  132. le32_to_cpu(hdr->block_offset));
  133. end = fw_entry->data + fw_entry->size;
  134. err = hermes_program(hw, first_block, end);
  135. printk(KERN_DEBUG "%s: Program returned %d\n", dev->name, err);
  136. if (err != 0)
  137. goto abort;
  138. /* Update production data */
  139. first_block = (fw_entry->data +
  140. le16_to_cpu(hdr->headersize) +
  141. le32_to_cpu(hdr->pdr_offset));
  142. err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
  143. &pda[fw->pda_size / sizeof(*pda)]);
  144. printk(KERN_DEBUG "%s: Apply PDA returned %d\n", dev->name, err);
  145. if (err)
  146. goto abort;
  147. /* Tell card we've finished */
  148. err = hermesi_program_end(hw);
  149. printk(KERN_DEBUG "%s: Program end returned %d\n", dev->name, err);
  150. if (err != 0)
  151. goto abort;
  152. /* Check if we're running */
  153. printk(KERN_DEBUG "%s: hermes_present returned %d\n",
  154. dev->name, 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. hermes_t *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 = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
  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 = hermes_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 net_device *dev = priv->ndev;
  240. int ret;
  241. const struct firmware *fw_entry;
  242. if (!orinoco_cached_fw_get(priv, true)) {
  243. if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
  244. printk(KERN_ERR "%s: Cannot find firmware: %s\n",
  245. dev->name, fw->pri_fw);
  246. return -ENOENT;
  247. }
  248. } else
  249. fw_entry = orinoco_cached_fw_get(priv, true);
  250. /* Load primary firmware */
  251. ret = symbol_dl_image(priv, fw, fw_entry->data,
  252. fw_entry->data + fw_entry->size, 0);
  253. if (!orinoco_cached_fw_get(priv, true))
  254. release_firmware(fw_entry);
  255. if (ret) {
  256. printk(KERN_ERR "%s: Primary firmware download failed\n",
  257. dev->name);
  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. printk(KERN_ERR "%s: Cannot find firmware: %s\n",
  263. dev->name, 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. printk(KERN_ERR "%s: Secondary firmware download failed\n",
  275. dev->name);
  276. }
  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. if (priv->cached_pri_fw)
  323. release_firmware(priv->cached_pri_fw);
  324. if (priv->cached_fw)
  325. release_firmware(priv->cached_fw);
  326. priv->cached_pri_fw = NULL;
  327. priv->cached_fw = NULL;
  328. }
  329. #endif