rt2x00firmware.c 3.1 KB

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