blob: b971bc6e7ee260d9f3ca282e806004aeecae2faf [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn811aa9c2008-02-03 15:42:53 +01002 Copyright (C) 2004 - 2008 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;
38 u16 crc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070039
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 Doorna7f3a062008-03-09 22:44:54 +010064 crc = rt2x00dev->ops->lib->get_firmware_crc(fw->data, fw->size);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070065 if (crc != (fw->data[fw->size - 2] << 8 | fw->data[fw->size - 1])) {
Ivo van Doorn9404ef32008-02-03 15:48:38 +010066 ERROR(rt2x00dev, "Firmware checksum error.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -070067 retval = -ENOENT;
68 goto exit;
69 }
70
71 INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
72 fw->data[fw->size - 4], fw->data[fw->size - 3]);
73
74 rt2x00dev->fw = fw;
75
76 return 0;
77
78exit:
79 release_firmware(fw);
80
81 return retval;
82}
83
84int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
85{
86 int retval;
87
Ivo van Doorn9404ef32008-02-03 15:48:38 +010088 if (!test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags))
89 return 0;
90
Ivo van Doorn95ea3622007-09-25 17:57:13 -070091 if (!rt2x00dev->fw) {
92 retval = rt2x00lib_request_firmware(rt2x00dev);
93 if (retval)
94 return retval;
95 }
96
97 /*
98 * Send firmware to the device.
99 */
100 retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
101 rt2x00dev->fw->data,
102 rt2x00dev->fw->size);
103 return retval;
104}
105
106void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
107{
108 release_firmware(rt2x00dev->fw);
109 rt2x00dev->fw = NULL;
110}