Add rt2x00-mac80211 snapshot (#1916)
[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/crc-itu-t.h>
33 #include <linux/firmware.h>
34
35 #include "rt2x00.h"
36 #include "rt2x00firmware.h"
37
38 static void rt2x00lib_load_firmware_continued(const struct firmware *fw,
39         void *context)
40 {
41         struct rt2x00_dev *rt2x00dev = context;
42         u16 crc;
43         u16 tmp;
44
45         if (!fw || !fw->size || !fw->data) {
46                 ERROR(rt2x00dev, "Failed to read Firmware.\n");
47                 goto exit_failed;
48         }
49
50         /*
51          * Validate the firmware using 16 bit CRC.
52          * The last 2 bytes of the firmware are the CRC
53          * so substract those 2 bytes from the CRC checksum,
54          * and set those 2 bytes to 0 when calculating CRC.
55          */
56         tmp = 0;
57         crc = crc_itu_t(0, fw->data, fw->size - 2);
58         crc = crc_itu_t(crc, (u8*)&tmp, 2);
59
60         if (crc != (fw->data[fw->size - 2] << 8 | fw->data[fw->size - 1])) {
61                 ERROR(rt2x00dev, "Firmware CRC error.\n");
62                 goto exit_failed;
63         }
64
65         /*
66          * Send firmware to the device.
67          */
68         if (rt2x00dev->ops->lib->load_firmware(rt2x00dev, fw->data, fw->size))
69                 goto exit_failed;
70
71         INFO(rt2x00dev, "Firmware detected - version: %d.%d.\n",
72                 fw->data[fw->size - 4], fw->data[fw->size - 3]);
73
74         __set_bit(FIRMWARE_LOADED, &rt2x00dev->flags);
75
76         return;
77
78 exit_failed:
79         rt2x00debug_deregister(rt2x00dev);
80
81         __set_bit(FIRMWARE_FAILED, &rt2x00dev->flags);
82 }
83
84 int rt2x00lib_load_firmware(struct rt2x00_dev *rt2x00dev)
85 {
86         char *fw_name;
87         int status = -EINVAL;
88
89         /*
90          * Read correct firmware from harddisk.
91          */
92         fw_name = rt2x00dev->ops->lib->get_fw_name(rt2x00dev);
93         BUG_ON(fw_name == NULL);
94
95         INFO(rt2x00dev, "Loading firmware file '%s'.\n", fw_name);
96
97         status = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
98                 fw_name, rt2x00dev->device, rt2x00dev,
99                 &rt2x00lib_load_firmware_continued);
100
101         if (status)
102                 ERROR(rt2x00dev, "Failed to request Firmware.\n");
103
104         return status;
105 }
106
107 int rt2x00lib_load_firmware_wait(struct rt2x00_dev *rt2x00dev)
108 {
109         unsigned int i;
110
111         if (!test_bit(FIRMWARE_REQUIRED, &rt2x00dev->flags))
112                 return 0;
113
114         for (i = 0; i < 150; i++) {
115                 if (test_bit(FIRMWARE_FAILED, &rt2x00dev->flags))
116                         return -EIO;
117                 if (test_bit(FIRMWARE_LOADED, &rt2x00dev->flags))
118                         return 0;
119                 msleep(20);
120         }
121
122         ERROR(rt2x00dev, "Firmware loading timed out.\n");
123         return -ETIMEDOUT;
124 }