rt2x00firmware.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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, write to the
  15. Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /*
  19. Module: rt2x00lib
  20. Abstract: rt2x00 firmware loading routines.
  21. */
  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. /*
  33. * Read correct firmware from harddisk.
  34. */
  35. fw_name = rt2x00dev->ops->lib->get_firmware_name(rt2x00dev);
  36. if (!fw_name) {
  37. ERROR(rt2x00dev,
  38. "Invalid firmware filename.\n"
  39. "Please file bug report to %s.\n", DRV_PROJECT);
  40. return -EINVAL;
  41. }
  42. INFO(rt2x00dev, "Loading firmware file '%s'.\n", fw_name);
  43. retval = request_firmware(&fw, fw_name, device);
  44. if (retval) {
  45. ERROR(rt2x00dev, "Failed to request Firmware.\n");
  46. return retval;
  47. }
  48. if (!fw || !fw->size || !fw->data) {
  49. ERROR(rt2x00dev, "Failed to read Firmware.\n");
  50. release_firmware(fw);
  51. return -ENOENT;
  52. }
  53. INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
  54. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  55. snprintf(rt2x00dev->hw->wiphy->fw_version,
  56. sizeof(rt2x00dev->hw->wiphy->fw_version), "%d.%d",
  57. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  58. retval = rt2x00dev->ops->lib->check_firmware(rt2x00dev, fw->data, fw->size);
  59. switch (retval) {
  60. case FW_OK:
  61. break;
  62. case FW_BAD_CRC:
  63. ERROR(rt2x00dev, "Firmware checksum error.\n");
  64. goto exit;
  65. case FW_BAD_LENGTH:
  66. ERROR(rt2x00dev,
  67. "Invalid firmware file length (len=%zu)\n", fw->size);
  68. goto exit;
  69. case FW_BAD_VERSION:
  70. ERROR(rt2x00dev,
  71. "Current firmware does not support detected chipset.\n");
  72. goto exit;
  73. }
  74. rt2x00dev->fw = fw;
  75. return 0;
  76. exit:
  77. release_firmware(fw);
  78. return -ENOENT;
  79. }
  80. int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
  81. {
  82. int retval;
  83. if (!test_bit(REQUIRE_FIRMWARE, &rt2x00dev->cap_flags))
  84. return 0;
  85. if (!rt2x00dev->fw) {
  86. retval = rt2x00lib_request_firmware(rt2x00dev);
  87. if (retval)
  88. return retval;
  89. }
  90. /*
  91. * Send firmware to the device.
  92. */
  93. retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
  94. rt2x00dev->fw->data,
  95. rt2x00dev->fw->size);
  96. /*
  97. * When the firmware is uploaded to the hardware the LED
  98. * association status might have been triggered, for correct
  99. * LED handling it should now be reset.
  100. */
  101. rt2x00leds_led_assoc(rt2x00dev, false);
  102. return retval;
  103. }
  104. void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
  105. {
  106. release_firmware(rt2x00dev->fw);
  107. rt2x00dev->fw = NULL;
  108. }