rt2x00firmware.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright (C) 2004 - 2008 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-ccitt.h>
  22. #include <linux/crc-itu-t.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include "rt2x00.h"
  26. #include "rt2x00lib.h"
  27. static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev)
  28. {
  29. struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
  30. const struct firmware *fw;
  31. char *fw_name;
  32. int retval;
  33. u16 crc;
  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. * Perform crc validation on the firmware.
  56. * The last 2 bytes in the firmware array are the crc checksum itself,
  57. * this means that we should never pass those 2 bytes to the crc
  58. * algorithm.
  59. */
  60. if (test_bit(DRIVER_REQUIRE_FIRMWARE_CRC_ITU_T, &rt2x00dev->flags)) {
  61. /*
  62. * Use the crc itu-t algorithm.
  63. * Use 0 for the last 2 bytes to complete the checksum.
  64. */
  65. crc = crc_itu_t(0, fw->data, fw->size - 2);
  66. crc = crc_itu_t_byte(crc, 0);
  67. crc = crc_itu_t_byte(crc, 0);
  68. } else if (test_bit(DRIVER_REQUIRE_FIRMWARE_CCITT, &rt2x00dev->flags)) {
  69. /*
  70. * Use the crc ccitt algorithm.
  71. * This will return the same value as the legacy driver which
  72. * used bit ordering reversion on the both the firmware bytes
  73. * before input input as well as on the final output.
  74. * Obviously using crc ccitt directly is much more efficient.
  75. */
  76. crc = crc_ccitt(~0, fw->data, fw->size - 2);
  77. } else {
  78. ERROR(rt2x00dev, "No checksum algorithm selected "
  79. "for firmware validation.\n");
  80. retval = -ENOENT;
  81. goto exit;
  82. }
  83. if (crc != (fw->data[fw->size - 2] << 8 | fw->data[fw->size - 1])) {
  84. ERROR(rt2x00dev, "Firmware checksum error.\n");
  85. retval = -ENOENT;
  86. goto exit;
  87. }
  88. INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
  89. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  90. rt2x00dev->fw = fw;
  91. return 0;
  92. exit:
  93. release_firmware(fw);
  94. return retval;
  95. }
  96. int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
  97. {
  98. int retval;
  99. if (!test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags))
  100. return 0;
  101. if (!rt2x00dev->fw) {
  102. retval = rt2x00lib_request_firmware(rt2x00dev);
  103. if (retval)
  104. return retval;
  105. }
  106. /*
  107. * Send firmware to the device.
  108. */
  109. retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
  110. rt2x00dev->fw->data,
  111. rt2x00dev->fw->size);
  112. return retval;
  113. }
  114. void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
  115. {
  116. release_firmware(rt2x00dev->fw);
  117. rt2x00dev->fw = NULL;
  118. }