blob: 4f9fe56f4f2e42689ea1ddf07d686fcd680a4fde [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 Doorn9404ef32008-02-03 15:48:38 +010026#include <linux/crc-ccitt.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070027#include <linux/crc-itu-t.h>
28#include <linux/kernel.h>
29#include <linux/module.h>
30
31#include "rt2x00.h"
32#include "rt2x00lib.h"
33
34static int rt2x00lib_request_firmware(struct rt2x00_dev *rt2x00dev)
35{
36 struct device *device = wiphy_dev(rt2x00dev->hw->wiphy);
37 const struct firmware *fw;
38 char *fw_name;
39 int retval;
40 u16 crc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070041
42 /*
43 * Read correct firmware from harddisk.
44 */
45 fw_name = rt2x00dev->ops->lib->get_firmware_name(rt2x00dev);
46 if (!fw_name) {
47 ERROR(rt2x00dev,
48 "Invalid firmware filename.\n"
49 "Please file bug report to %s.\n", DRV_PROJECT);
50 return -EINVAL;
51 }
52
53 INFO(rt2x00dev, "Loading firmware file '%s'.\n", fw_name);
54
55 retval = request_firmware(&fw, fw_name, device);
56 if (retval) {
57 ERROR(rt2x00dev, "Failed to request Firmware.\n");
58 return retval;
59 }
60
61 if (!fw || !fw->size || !fw->data) {
62 ERROR(rt2x00dev, "Failed to read Firmware.\n");
63 return -ENOENT;
64 }
65
66 /*
Ivo van Doorn9404ef32008-02-03 15:48:38 +010067 * Perform crc validation on the firmware.
68 * The last 2 bytes in the firmware array are the crc checksum itself,
69 * this means that we should never pass those 2 bytes to the crc
70 * algorithm.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070071 */
Ivo van Doorn9404ef32008-02-03 15:48:38 +010072 if (test_bit(DRIVER_REQUIRE_FIRMWARE_CRC_ITU_T, &rt2x00dev->flags)) {
73 /*
74 * Use the crc itu-t algorithm.
75 * Use 0 for the last 2 bytes to complete the checksum.
76 */
77 crc = crc_itu_t(0, fw->data, fw->size - 2);
78 crc = crc_itu_t_byte(crc, 0);
79 crc = crc_itu_t_byte(crc, 0);
80 } else if (test_bit(DRIVER_REQUIRE_FIRMWARE_CCITT, &rt2x00dev->flags)) {
81 /*
82 * Use the crc ccitt algorithm.
83 * This will return the same value as the legacy driver which
84 * used bit ordering reversion on the both the firmware bytes
85 * before input input as well as on the final output.
86 * Obviously using crc ccitt directly is much more efficient.
87 */
88 crc = crc_ccitt(~0, fw->data, fw->size - 2);
89 } else {
90 ERROR(rt2x00dev, "No checksum algorithm selected "
91 "for firmware validation.\n");
92 retval = -ENOENT;
93 goto exit;
94 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -070095
96 if (crc != (fw->data[fw->size - 2] << 8 | fw->data[fw->size - 1])) {
Ivo van Doorn9404ef32008-02-03 15:48:38 +010097 ERROR(rt2x00dev, "Firmware checksum error.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -070098 retval = -ENOENT;
99 goto exit;
100 }
101
102 INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
103 fw->data[fw->size - 4], fw->data[fw->size - 3]);
104
105 rt2x00dev->fw = fw;
106
107 return 0;
108
109exit:
110 release_firmware(fw);
111
112 return retval;
113}
114
115int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
116{
117 int retval;
118
Ivo van Doorn9404ef32008-02-03 15:48:38 +0100119 if (!test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags))
120 return 0;
121
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700122 if (!rt2x00dev->fw) {
123 retval = rt2x00lib_request_firmware(rt2x00dev);
124 if (retval)
125 return retval;
126 }
127
128 /*
129 * Send firmware to the device.
130 */
131 retval = rt2x00dev->ops->lib->load_firmware(rt2x00dev,
132 rt2x00dev->fw->data,
133 rt2x00dev->fw->size);
134 return retval;
135}
136
137void rt2x00lib_free_firmware(struct rt2x00_dev *rt2x00dev)
138{
139 release_firmware(rt2x00dev->fw);
140 rt2x00dev->fw = NULL;
141}
142