fix insmod crash when the module is not found
[openwrt.git] / package / rt2x00 / src / rt2x00firmware.c
1 /*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <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 specific routines.
24 Supported chipsets: rt2561, rt2561s, rt2661, rt2571W & rt2671.
25 */
26
27 /*
28 * Set enviroment defines for rt2x00.h
29 */
30 #define DRV_NAME "rt2x00lib"
31
32 #include <linux/delay.h>
33 #include <linux/crc-itu-t.h>
34 #include <linux/firmware.h>
35
36 #include "rt2x00.h"
37 #include "rt2x00lib.h"
38 #include "rt2x00firmware.h"
39
40 static void rt2x00lib_load_firmware_continued(const struct firmware *fw,
41 void *context)
42 {
43 struct rt2x00_dev *rt2x00dev = context;
44 u16 crc;
45 u16 tmp;
46
47 if (!fw || !fw->size || !fw->data) {
48 ERROR(rt2x00dev, "Failed to read Firmware.\n");
49 goto exit_failed;
50 }
51
52 /*
53 * Validate the firmware using 16 bit CRC.
54 * The last 2 bytes of the firmware are the CRC
55 * so substract those 2 bytes from the CRC checksum,
56 * and set those 2 bytes to 0 when calculating CRC.
57 */
58 tmp = 0;
59 crc = crc_itu_t(0, fw->data, fw->size - 2);
60 crc = crc_itu_t(crc, (u8*)&tmp, 2);
61
62 if (crc != (fw->data[fw->size - 2] << 8 | fw->data[fw->size - 1])) {
63 ERROR(rt2x00dev, "Firmware CRC error.\n");
64 goto exit_failed;
65 }
66
67 /*
68 * Send firmware to the device.
69 */
70 if (rt2x00dev->ops->lib->load_firmware(rt2x00dev, fw->data, fw->size))
71 goto exit_failed;
72
73 INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
74 fw->data[fw->size - 4], fw->data[fw->size - 3]);
75
76 __set_bit(FIRMWARE_LOADED, &rt2x00dev->flags);
77
78 return;
79
80 exit_failed:
81 rt2x00debug_deregister(rt2x00dev);
82
83 __set_bit(FIRMWARE_FAILED, &rt2x00dev->flags);
84 }
85
86 int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
87 {
88 char *fw_name;
89 int status = -EINVAL;
90
91 /*
92 * Read correct firmware from harddisk.
93 */
94 fw_name = rt2x00dev->ops->lib->get_fw_name(rt2x00dev);
95 if (!fw_name) {
96 ERROR(rt2x00dev,
97 "Invalid firmware filename.\n"
98 "Please file bug report to %s.\n", DRV_PROJECT);
99 return -EINVAL;
100 }
101
102 INFO(rt2x00dev, "Loading firmware file '%s'.\n", fw_name);
103
104 status = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
105 fw_name, wiphy_dev(rt2x00dev->hw->wiphy), rt2x00dev,
106 &rt2x00lib_load_firmware_continued);
107
108 if (status)
109 ERROR(rt2x00dev, "Failed to request Firmware.\n");
110
111 return status;
112 }
113
114 int rt2x00lib_load_firmware_wait(struct rt2x00_dev *rt2x00dev)
115 {
116 unsigned int i;
117
118 if (!test_bit(FIRMWARE_REQUIRED, &rt2x00dev->flags))
119 return 0;
120
121 for (i = 0; i < 150; i++) {
122 if (test_bit(FIRMWARE_FAILED, &rt2x00dev->flags))
123 return -EIO;
124 if (test_bit(FIRMWARE_LOADED, &rt2x00dev->flags))
125 return 0;
126 msleep(20);
127 }
128
129 ERROR(rt2x00dev, "Firmware loading timed out.\n");
130 return -ETIMEDOUT;
131 }
This page took 0.052607 seconds and 5 git commands to generate.