rt2x00firmware.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev)
  26. {
  27. struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
  28. const struct firmware *fw;
  29. char *fw_name;
  30. int retval;
  31. u16 crc;
  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. crc = rt2x00dev->ops->lib->get_firmware_crc(fw->data, fw->size);
  53. if (crc != (fw->data[fw->size - 2] << 8 | fw->data[fw->size - 1])) {
  54. ERROR(rt2x00dev, "Firmware checksum error.\n");
  55. retval = -ENOENT;
  56. goto exit;
  57. }
  58. INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
  59. fw->data[fw->size - 4], fw->data[fw->size - 3]);
  60. rt2x00dev->fw = fw;
  61. return 0;
  62. exit:
  63. release_firmware(fw);
  64. return retval;
  65. }
  66. int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
  67. {
  68. int retval;
  69. if (!test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags))
  70. return 0;
  71. if (!rt2x00dev->fw) {
  72. retval = rt2x00lib_request_firmware(rt2x00dev);
  73. if (retval)
  74. return retval;
  75. }
  76. /*
  77. * Send firmware to the device.
  78. */
  79. retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
  80. rt2x00dev->fw->data,
  81. rt2x00dev->fw->size);
  82. /*
  83. * When the firmware is uploaded to the hardware the LED
  84. * association status might have been triggered, for correct
  85. * LED handling it should now be reset.
  86. */
  87. rt2x00leds_led_assoc(rt2x00dev, false);
  88. return retval;
  89. }
  90. void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
  91. {
  92. release_firmware(rt2x00dev->fw);
  93. rt2x00dev->fw = NULL;
  94. }