rt2x00firmware.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /*
  22. * Set enviroment defines for rt2x00.h
  23. */
  24. #define DRV_NAME "rt2x00lib"
  25. #include <linux/crc-itu-t.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include "rt2x00.h"
  29. #include "rt2x00lib.h"
  30. static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev)
  31. {
  32. struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
  33. const struct firmware *fw;
  34. char *fw_name;
  35. int retval;
  36. u16 crc;
  37. u16 tmp;
  38. /*
  39. * Read correct firmware from harddisk.
  40. */
  41. fw_name = rt2x00dev->ops->lib->get_firmware_name(rt2x00dev);
  42. if (!fw_name) {
  43. ERROR(rt2x00dev,
  44. "Invalid firmware filename.\n"
  45. "Please file bug report to %s.\n", DRV_PROJECT);
  46. return -EINVAL;
  47. }
  48. INFO(rt2x00dev, "Loading firmware file '%s'.\n", fw_name);
  49. retval = request_firmware(&fw, fw_name, device);
  50. if (retval) {
  51. ERROR(rt2x00dev, "Failed to request Firmware.\n");
  52. return retval;
  53. }
  54. if (!fw || !fw->size || !fw->data) {
  55. ERROR(rt2x00dev, "Failed to read Firmware.\n");
  56. return -ENOENT;
  57. }
  58. /*
  59. * Validate the firmware using 16 bit CRC.
  60. * The last 2 bytes of the firmware are the CRC
  61. * so substract those 2 bytes from the CRC checksum,
  62. * and set those 2 bytes to 0 when calculating CRC.
  63. */
  64. tmp = 0;
  65. crc = crc_itu_t(0, fw->data, fw->size - 2);
  66. crc = crc_itu_t(crc, (u8 *)&tmp, 2);
  67. if (crc != (fw->data[fw->size - 2] << 8 | fw->data[fw->size - 1])) {
  68. ERROR(rt2x00dev, "Firmware CRC error.\n");
  69. retval = -ENOENT;
  70. goto exit;
  71. }
  72. INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
  73. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  74. rt2x00dev->fw = fw;
  75. return 0;
  76. exit:
  77. release_firmware(fw);
  78. return retval;
  79. }
  80. int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
  81. {
  82. int retval;
  83. if (!rt2x00dev->fw) {
  84. retval = rt2x00lib_request_firmware(rt2x00dev);
  85. if (retval)
  86. return retval;
  87. }
  88. /*
  89. * Send firmware to the device.
  90. */
  91. retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
  92. rt2x00dev->fw->data,
  93. rt2x00dev->fw->size);
  94. return retval;
  95. }
  96. void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
  97. {
  98. release_firmware(rt2x00dev->fw);
  99. rt2x00dev->fw = NULL;
  100. }