rt2x00firmware.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com>
  4. <http://rt2x00.serialmonkey.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. Module: rt2x00lib
  18. Abstract: rt2x00 firmware loading routines.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include "rt2x00.h"
  23. #include "rt2x00lib.h"
  24. static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev)
  25. {
  26. struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
  27. const struct firmware *fw;
  28. char *fw_name;
  29. int retval;
  30. /*
  31. * Read correct firmware from harddisk.
  32. */
  33. fw_name = rt2x00dev->ops->lib->get_firmware_name(rt2x00dev);
  34. if (!fw_name) {
  35. rt2x00_err(rt2x00dev,
  36. "Invalid firmware filename\n"
  37. "Please file bug report to %s\n", DRV_PROJECT);
  38. return -EINVAL;
  39. }
  40. rt2x00_info(rt2x00dev, "Loading firmware file '%s'\n", fw_name);
  41. retval = request_firmware(&fw, fw_name, device);
  42. if (retval) {
  43. rt2x00_err(rt2x00dev, "Failed to request Firmware\n");
  44. return retval;
  45. }
  46. if (!fw || !fw->size || !fw->data) {
  47. rt2x00_err(rt2x00dev, "Failed to read Firmware\n");
  48. release_firmware(fw);
  49. return -ENOENT;
  50. }
  51. rt2x00_info(rt2x00dev, "Firmware detected - version: %d.%d\n",
  52. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  53. snprintf(rt2x00dev->hw->wiphy->fw_version,
  54. sizeof(rt2x00dev->hw->wiphy->fw_version), "%d.%d",
  55. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  56. retval = rt2x00dev->ops->lib->check_firmware(rt2x00dev, fw->data, fw->size);
  57. switch (retval) {
  58. case FW_OK:
  59. break;
  60. case FW_BAD_CRC:
  61. rt2x00_err(rt2x00dev, "Firmware checksum error\n");
  62. goto exit;
  63. case FW_BAD_LENGTH:
  64. rt2x00_err(rt2x00dev, "Invalid firmware file length (len=%zu)\n",
  65. fw->size);
  66. goto exit;
  67. case FW_BAD_VERSION:
  68. rt2x00_err(rt2x00dev, "Current firmware does not support detected chipset\n");
  69. goto exit;
  70. }
  71. rt2x00dev->fw = fw;
  72. return 0;
  73. exit:
  74. release_firmware(fw);
  75. return -ENOENT;
  76. }
  77. int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
  78. {
  79. int retval;
  80. if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_FIRMWARE))
  81. return 0;
  82. if (!rt2x00dev->fw) {
  83. retval = rt2x00lib_request_firmware(rt2x00dev);
  84. if (retval)
  85. return retval;
  86. }
  87. /*
  88. * Send firmware to the device.
  89. */
  90. retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
  91. rt2x00dev->fw->data,
  92. rt2x00dev->fw->size);
  93. /*
  94. * When the firmware is uploaded to the hardware the LED
  95. * association status might have been triggered, for correct
  96. * LED handling it should now be reset.
  97. */
  98. rt2x00leds_led_assoc(rt2x00dev, false);
  99. return retval;
  100. }
  101. void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
  102. {
  103. release_firmware(rt2x00dev->fw);
  104. rt2x00dev->fw = NULL;
  105. }