blob: d2deea2f2679423952dd19b3324e94042857713c [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn4e54c712009-01-17 20:42:32 +01002 Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <http://rt2x00.serialmonkey.com>
4
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
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21/*
22 Module: rt2x00lib
23 Abstract: rt2x00 firmware loading routines.
24 */
25
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#include <linux/kernel.h>
27#include <linux/module.h>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
31
32static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev)
33{
34 struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
35 const struct firmware *fw;
36 char *fw_name;
37 int retval;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070038
39 /*
40 * Read correct firmware from harddisk.
41 */
42 fw_name = rt2x00dev->ops->lib->get_firmware_name(rt2x00dev);
43 if (!fw_name) {
44 ERROR(rt2x00dev,
45 "Invalid firmware filename.\n"
46 "Please file bug report to %s.\n", DRV_PROJECT);
47 return -EINVAL;
48 }
49
50 INFO(rt2x00dev, "Loading firmware file '%s'.\n", fw_name);
51
52 retval = request_firmware(&fw, fw_name, device);
53 if (retval) {
54 ERROR(rt2x00dev, "Failed to request Firmware.\n");
55 return retval;
56 }
57
58 if (!fw || !fw->size || !fw->data) {
59 ERROR(rt2x00dev, "Failed to read Firmware.\n");
60 return -ENOENT;
61 }
62
Ivo van Doorn95ea3622007-09-25 17:57:13 -070063 INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
64 fw->data[fw->size - 4], fw->data[fw->size - 3]);
65
Ivo van Doorn0cbe0062009-01-28 00:33:47 +010066 retval = rt2x00dev->ops->lib->check_firmware(rt2x00dev, fw->data, fw->size);
67 switch (retval) {
68 case FW_OK:
69 break;
70 case FW_BAD_CRC:
71 ERROR(rt2x00dev, "Firmware checksum error.\n");
72 goto exit;
73 case FW_BAD_LENGTH:
74 ERROR(rt2x00dev,
75 "Invalid firmware file length (len=%zu)\n", fw->size);
76 goto exit;
77 case FW_BAD_VERSION:
78 ERROR(rt2x00dev,
79 "Current firmware does not support detected chipset.\n");
80 goto exit;
81 };
82
Ivo van Doorn95ea3622007-09-25 17:57:13 -070083 rt2x00dev->fw = fw;
84
85 return 0;
86
87exit:
88 release_firmware(fw);
89
Ivo van Doorn0cbe0062009-01-28 00:33:47 +010090 return -ENOENT;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070091}
92
93int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
94{
95 int retval;
96
Ivo van Doorn9404ef32008-02-03 15:48:38 +010097 if (!test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags))
98 return 0;
99
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700100 if (!rt2x00dev->fw) {
101 retval = rt2x00lib_request_firmware(rt2x00dev);
102 if (retval)
103 return retval;
104 }
105
106 /*
107 * Send firmware to the device.
108 */
109 retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
110 rt2x00dev->fw->data,
111 rt2x00dev->fw->size);
Ivo van Doorn4c9adaf2008-07-13 10:07:48 +0200112
113 /*
114 * When the firmware is uploaded to the hardware the LED
115 * association status might have been triggered, for correct
116 * LED handling it should now be reset.
117 */
118 rt2x00leds_led_assoc(rt2x00dev, false);
119
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700120 return retval;
121}
122
123void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
124{
125 release_firmware(rt2x00dev->fw);
126 rt2x00dev->fw = NULL;
127}