rt2x00firmware.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00lib
  19. Abstract: rt2x00 firmware loading routines.
  20. */
  21. #include <linux/crc-itu-t.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include "rt2x00.h"
  25. #include "rt2x00lib.h"
  26. static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev)
  27. {
  28. struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
  29. const struct firmware *fw;
  30. char *fw_name;
  31. int retval;
  32. u16 crc;
  33. u16 tmp;
  34. /*
  35. * Read correct firmware from harddisk.
  36. */
  37. fw_name = rt2x00dev->ops->lib->get_firmware_name(rt2x00dev);
  38. if (!fw_name) {
  39. ERROR(rt2x00dev,
  40. "Invalid firmware filename.\n"
  41. "Please file bug report to %s.\n", DRV_PROJECT);
  42. return -EINVAL;
  43. }
  44. INFO(rt2x00dev, "Loading firmware file '%s'.\n", fw_name);
  45. retval = request_firmware(&fw, fw_name, device);
  46. if (retval) {
  47. ERROR(rt2x00dev, "Failed to request Firmware.\n");
  48. return retval;
  49. }
  50. if (!fw || !fw->size || !fw->data) {
  51. ERROR(rt2x00dev, "Failed to read Firmware.\n");
  52. return -ENOENT;
  53. }
  54. /*
  55. * Validate the firmware using 16 bit CRC.
  56. * The last 2 bytes of the firmware are the CRC
  57. * so substract those 2 bytes from the CRC checksum,
  58. * and set those 2 bytes to 0 when calculating CRC.
  59. */
  60. tmp = 0;
  61. crc = crc_itu_t(0, fw->data, fw->size - 2);
  62. crc = crc_itu_t(crc, (u8 *)&tmp, 2);
  63. if (crc != (fw->data[fw->size - 2] << 8 | fw->data[fw->size - 1])) {
  64. ERROR(rt2x00dev, "Firmware CRC error.\n");
  65. retval = -ENOENT;
  66. goto exit;
  67. }
  68. INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
  69. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  70. rt2x00dev->fw = fw;
  71. return 0;
  72. exit:
  73. release_firmware(fw);
  74. return retval;
  75. }
  76. int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
  77. {
  78. int retval;
  79. if (!rt2x00dev->fw) {
  80. retval = rt2x00lib_request_firmware(rt2x00dev);
  81. if (retval)
  82. return retval;
  83. }
  84. /*
  85. * Send firmware to the device.
  86. */
  87. retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
  88. rt2x00dev->fw->data,
  89. rt2x00dev->fw->size);
  90. return retval;
  91. }
  92. void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
  93. {
  94. release_firmware(rt2x00dev->fw);
  95. rt2x00dev->fw = NULL;
  96. }