rt2x00firmware.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. return -ENOENT;
  51. }
  52. INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
  53. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  54. snprintf(rt2x00dev->hw->wiphy->fw_version,
  55. sizeof(rt2x00dev->hw->wiphy->fw_version), "%d.%d",
  56. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  57. retval = rt2x00dev->ops->lib->check_firmware(rt2x00dev, fw->data, fw->size);
  58. switch (retval) {
  59. case FW_OK:
  60. break;
  61. case FW_BAD_CRC:
  62. ERROR(rt2x00dev, "Firmware checksum error.\n");
  63. goto exit;
  64. case FW_BAD_LENGTH:
  65. ERROR(rt2x00dev,
  66. "Invalid firmware file length (len=%zu)\n", fw->size);
  67. goto exit;
  68. case FW_BAD_VERSION:
  69. ERROR(rt2x00dev,
  70. "Current firmware does not support detected chipset.\n");
  71. goto exit;
  72. }
  73. rt2x00dev->fw = fw;
  74. return 0;
  75. exit:
  76. release_firmware(fw);
  77. return -ENOENT;
  78. }
  79. int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
  80. {
  81. int retval;
  82. if (!test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags))
  83. return 0;
  84. if (!rt2x00dev->fw) {
  85. retval = rt2x00lib_request_firmware(rt2x00dev);
  86. if (retval)
  87. return retval;
  88. }
  89. /*
  90. * Send firmware to the device.
  91. */
  92. retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
  93. rt2x00dev->fw->data,
  94. rt2x00dev->fw->size);
  95. /*
  96. * When the firmware is uploaded to the hardware the LED
  97. * association status might have been triggered, for correct
  98. * LED handling it should now be reset.
  99. */
  100. rt2x00leds_led_assoc(rt2x00dev, false);
  101. return retval;
  102. }
  103. void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
  104. {
  105. release_firmware(rt2x00dev->fw);
  106. rt2x00dev->fw = NULL;
  107. }