1*1ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
233aca94dSKalle Valo /*
333aca94dSKalle Valo Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
433aca94dSKalle Valo Copyright (C) 2004 - 2009 Gertjan van Wingerde <gwingerde@gmail.com>
533aca94dSKalle Valo <http://rt2x00.serialmonkey.com>
633aca94dSKalle Valo
733aca94dSKalle Valo */
833aca94dSKalle Valo
933aca94dSKalle Valo /*
1033aca94dSKalle Valo Module: rt2x00lib
1133aca94dSKalle Valo Abstract: rt2x00 firmware loading routines.
1233aca94dSKalle Valo */
1333aca94dSKalle Valo
1433aca94dSKalle Valo #include <linux/kernel.h>
1533aca94dSKalle Valo #include <linux/module.h>
1633aca94dSKalle Valo
1733aca94dSKalle Valo #include "rt2x00.h"
1833aca94dSKalle Valo #include "rt2x00lib.h"
1933aca94dSKalle Valo
rt2x00lib_request_firmware(struct rt2x00_dev * rt2x00dev)2033aca94dSKalle Valo static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev)
2133aca94dSKalle Valo {
2233aca94dSKalle Valo struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
2333aca94dSKalle Valo const struct firmware *fw;
2433aca94dSKalle Valo char *fw_name;
2533aca94dSKalle Valo int retval;
2633aca94dSKalle Valo
2733aca94dSKalle Valo /*
2833aca94dSKalle Valo * Read correct firmware from harddisk.
2933aca94dSKalle Valo */
3033aca94dSKalle Valo fw_name = rt2x00dev->ops->lib->get_firmware_name(rt2x00dev);
3133aca94dSKalle Valo if (!fw_name) {
3233aca94dSKalle Valo rt2x00_err(rt2x00dev,
3333aca94dSKalle Valo "Invalid firmware filename\n"
3433aca94dSKalle Valo "Please file bug report to %s\n", DRV_PROJECT);
3533aca94dSKalle Valo return -EINVAL;
3633aca94dSKalle Valo }
3733aca94dSKalle Valo
3833aca94dSKalle Valo rt2x00_info(rt2x00dev, "Loading firmware file '%s'\n", fw_name);
3933aca94dSKalle Valo
4033aca94dSKalle Valo retval = request_firmware(&fw, fw_name, device);
4133aca94dSKalle Valo if (retval) {
4233aca94dSKalle Valo rt2x00_err(rt2x00dev, "Failed to request Firmware\n");
4333aca94dSKalle Valo return retval;
4433aca94dSKalle Valo }
4533aca94dSKalle Valo
4633aca94dSKalle Valo if (!fw || !fw->size || !fw->data) {
4733aca94dSKalle Valo rt2x00_err(rt2x00dev, "Failed to read Firmware\n");
4833aca94dSKalle Valo release_firmware(fw);
4933aca94dSKalle Valo return -ENOENT;
5033aca94dSKalle Valo }
5133aca94dSKalle Valo
5233aca94dSKalle Valo rt2x00_info(rt2x00dev, "Firmware detected - version: %d.%d\n",
5333aca94dSKalle Valo fw->data[fw->size - 4], fw->data[fw->size - 3]);
5433aca94dSKalle Valo snprintf(rt2x00dev->hw->wiphy->fw_version,
5533aca94dSKalle Valo sizeof(rt2x00dev->hw->wiphy->fw_version), "%d.%d",
5633aca94dSKalle Valo fw->data[fw->size - 4], fw->data[fw->size - 3]);
5733aca94dSKalle Valo
5833aca94dSKalle Valo retval = rt2x00dev->ops->lib->check_firmware(rt2x00dev, fw->data, fw->size);
5933aca94dSKalle Valo switch (retval) {
6033aca94dSKalle Valo case FW_OK:
6133aca94dSKalle Valo break;
6233aca94dSKalle Valo case FW_BAD_CRC:
6333aca94dSKalle Valo rt2x00_err(rt2x00dev, "Firmware checksum error\n");
6433aca94dSKalle Valo goto exit;
6533aca94dSKalle Valo case FW_BAD_LENGTH:
6633aca94dSKalle Valo rt2x00_err(rt2x00dev, "Invalid firmware file length (len=%zu)\n",
6733aca94dSKalle Valo fw->size);
6833aca94dSKalle Valo goto exit;
6933aca94dSKalle Valo case FW_BAD_VERSION:
7033aca94dSKalle Valo rt2x00_err(rt2x00dev, "Current firmware does not support detected chipset\n");
7133aca94dSKalle Valo goto exit;
7233aca94dSKalle Valo }
7333aca94dSKalle Valo
7433aca94dSKalle Valo rt2x00dev->fw = fw;
7533aca94dSKalle Valo
7633aca94dSKalle Valo return 0;
7733aca94dSKalle Valo
7833aca94dSKalle Valo exit:
7933aca94dSKalle Valo release_firmware(fw);
8033aca94dSKalle Valo
8133aca94dSKalle Valo return -ENOENT;
8233aca94dSKalle Valo }
8333aca94dSKalle Valo
rt2x00lib_load_firmware(struct rt2x00_dev * rt2x00dev)8433aca94dSKalle Valo int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
8533aca94dSKalle Valo {
8633aca94dSKalle Valo int retval;
8733aca94dSKalle Valo
8833aca94dSKalle Valo if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_FIRMWARE))
8933aca94dSKalle Valo return 0;
9033aca94dSKalle Valo
9133aca94dSKalle Valo if (!rt2x00dev->fw) {
9233aca94dSKalle Valo retval = rt2x00lib_request_firmware(rt2x00dev);
9333aca94dSKalle Valo if (retval)
9433aca94dSKalle Valo return retval;
9533aca94dSKalle Valo }
9633aca94dSKalle Valo
9733aca94dSKalle Valo /*
9833aca94dSKalle Valo * Send firmware to the device.
9933aca94dSKalle Valo */
10033aca94dSKalle Valo retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
10133aca94dSKalle Valo rt2x00dev->fw->data,
10233aca94dSKalle Valo rt2x00dev->fw->size);
10333aca94dSKalle Valo
10433aca94dSKalle Valo /*
10533aca94dSKalle Valo * When the firmware is uploaded to the hardware the LED
10633aca94dSKalle Valo * association status might have been triggered, for correct
10733aca94dSKalle Valo * LED handling it should now be reset.
10833aca94dSKalle Valo */
10933aca94dSKalle Valo rt2x00leds_led_assoc(rt2x00dev, false);
11033aca94dSKalle Valo
11133aca94dSKalle Valo return retval;
11233aca94dSKalle Valo }
11333aca94dSKalle Valo
rt2x00lib_free_firmware(struct rt2x00_dev * rt2x00dev)11433aca94dSKalle Valo void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
11533aca94dSKalle Valo {
11633aca94dSKalle Valo release_firmware(rt2x00dev->fw);
11733aca94dSKalle Valo rt2x00dev->fw = NULL;
11833aca94dSKalle Valo }
119