Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1 | /* |
Gertjan van Wingerde | 9c9a0d1 | 2009-11-08 16:39:55 +0100 | [diff] [blame] | 2 | Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com> |
| 3 | Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com> |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 4 | <http://rt2x00.serialmonkey.com> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the |
| 18 | Free Software Foundation, Inc., |
| 19 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | Module: rt2x00lib |
| 24 | Abstract: rt2x00 firmware loading routines. |
| 25 | */ |
| 26 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | |
| 30 | #include "rt2x00.h" |
| 31 | #include "rt2x00lib.h" |
| 32 | |
| 33 | static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev) |
| 34 | { |
| 35 | struct device *device = wiphy_dev(rt2x00dev->hw->wiphy); |
| 36 | const struct firmware *fw; |
| 37 | char *fw_name; |
| 38 | int retval; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Read correct firmware from harddisk. |
| 42 | */ |
| 43 | fw_name = rt2x00dev->ops->lib->get_firmware_name(rt2x00dev); |
| 44 | if (!fw_name) { |
| 45 | ERROR(rt2x00dev, |
| 46 | "Invalid firmware filename.\n" |
| 47 | "Please file bug report to %s.\n", DRV_PROJECT); |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | |
| 51 | INFO(rt2x00dev, "Loading firmware file '%s'.\n", fw_name); |
| 52 | |
| 53 | retval = request_firmware(&fw, fw_name, device); |
| 54 | if (retval) { |
| 55 | ERROR(rt2x00dev, "Failed to request Firmware.\n"); |
| 56 | return retval; |
| 57 | } |
| 58 | |
| 59 | if (!fw || !fw->size || !fw->data) { |
| 60 | ERROR(rt2x00dev, "Failed to read Firmware.\n"); |
| 61 | return -ENOENT; |
| 62 | } |
| 63 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 64 | INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n", |
| 65 | fw->data[fw->size - 4], fw->data[fw->size - 3]); |
John W. Linville | dd358c9 | 2010-07-29 13:50:39 -0400 | [diff] [blame] | 66 | snprintf(rt2x00dev->hw->wiphy->fw_version, |
| 67 | sizeof(rt2x00dev->hw->wiphy->fw_version), "%d.%d", |
| 68 | fw->data[fw->size - 4], fw->data[fw->size - 3]); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 69 | |
Ivo van Doorn | 0cbe006 | 2009-01-28 00:33:47 +0100 | [diff] [blame] | 70 | retval = rt2x00dev->ops->lib->check_firmware(rt2x00dev, fw->data, fw->size); |
| 71 | switch (retval) { |
| 72 | case FW_OK: |
| 73 | break; |
| 74 | case FW_BAD_CRC: |
| 75 | ERROR(rt2x00dev, "Firmware checksum error.\n"); |
| 76 | goto exit; |
| 77 | case FW_BAD_LENGTH: |
| 78 | ERROR(rt2x00dev, |
| 79 | "Invalid firmware file length (len=%zu)\n", fw->size); |
| 80 | goto exit; |
| 81 | case FW_BAD_VERSION: |
| 82 | ERROR(rt2x00dev, |
| 83 | "Current firmware does not support detected chipset.\n"); |
| 84 | goto exit; |
Joe Perches | ee289b6 | 2010-05-17 22:47:34 -0700 | [diff] [blame] | 85 | } |
Ivo van Doorn | 0cbe006 | 2009-01-28 00:33:47 +0100 | [diff] [blame] | 86 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 87 | rt2x00dev->fw = fw; |
| 88 | |
| 89 | return 0; |
| 90 | |
| 91 | exit: |
| 92 | release_firmware(fw); |
| 93 | |
Ivo van Doorn | 0cbe006 | 2009-01-28 00:33:47 +0100 | [diff] [blame] | 94 | return -ENOENT; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev) |
| 98 | { |
| 99 | int retval; |
| 100 | |
Ivo van Doorn | 9404ef3 | 2008-02-03 15:48:38 +0100 | [diff] [blame] | 101 | if (!test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) |
| 102 | return 0; |
| 103 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 104 | if (!rt2x00dev->fw) { |
| 105 | retval = rt2x00lib_request_firmware(rt2x00dev); |
| 106 | if (retval) |
| 107 | return retval; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Send firmware to the device. |
| 112 | */ |
| 113 | retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev, |
| 114 | rt2x00dev->fw->data, |
| 115 | rt2x00dev->fw->size); |
Ivo van Doorn | 4c9adaf | 2008-07-13 10:07:48 +0200 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * When the firmware is uploaded to the hardware the LED |
| 119 | * association status might have been triggered, for correct |
| 120 | * LED handling it should now be reset. |
| 121 | */ |
| 122 | rt2x00leds_led_assoc(rt2x00dev, false); |
| 123 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 124 | return retval; |
| 125 | } |
| 126 | |
| 127 | void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev) |
| 128 | { |
| 129 | release_firmware(rt2x00dev->fw); |
| 130 | rt2x00dev->fw = NULL; |
| 131 | } |