blob: a3ec97a4c7443f56ebea763898adacd4137ad8cb [file] [log] [blame]
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001/*
2 Copyright (C) 2004 - 2009 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: rt2800usb
23 Abstract: rt2800usb device specific routines.
24 Supported chipsets: RT2800U.
25 */
26
27#include <linux/crc-ccitt.h>
28#include <linux/delay.h>
29#include <linux/etherdevice.h>
30#include <linux/init.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/usb.h>
34
35#include "rt2x00.h"
36#include "rt2x00usb.h"
37#include "rt2800usb.h"
38
39/*
40 * Allow hardware encryption to be disabled.
41 */
42static int modparam_nohwcrypt = 1;
43module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
44MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
45
46/*
47 * Register access.
48 * All access to the CSR registers will go through the methods
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +010049 * rt2800_register_read and rt2800_register_write.
Ivo van Doornd53d9e62009-04-26 15:47:48 +020050 * BBP and RF register require indirect register access,
51 * and use the CSR registers BBPCSR and RFCSR to achieve this.
52 * These indirect registers work with busy bits,
53 * and we will try maximal REGISTER_BUSY_COUNT times to access
54 * the register while taking a REGISTER_BUSY_DELAY us delay
55 * between each attampt. When the busy bit is still set at that time,
56 * the access attempt is considered to have failed,
57 * and we will print an error.
58 * The _lock versions must be used if you already hold the csr_mutex
59 */
60#define WAIT_FOR_BBP(__dev, __reg) \
Bartlomiej Zolnierkiewiczab209b92009-11-04 18:33:34 +010061 rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
Ivo van Doornd53d9e62009-04-26 15:47:48 +020062#define WAIT_FOR_RFCSR(__dev, __reg) \
Bartlomiej Zolnierkiewiczab209b92009-11-04 18:33:34 +010063 rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
Ivo van Doornd53d9e62009-04-26 15:47:48 +020064#define WAIT_FOR_RF(__dev, __reg) \
Bartlomiej Zolnierkiewiczab209b92009-11-04 18:33:34 +010065 rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
Ivo van Doornd53d9e62009-04-26 15:47:48 +020066#define WAIT_FOR_MCU(__dev, __reg) \
Bartlomiej Zolnierkiewiczab209b92009-11-04 18:33:34 +010067 rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
68 H2M_MAILBOX_CSR_OWNER, (__reg))
Ivo van Doornd53d9e62009-04-26 15:47:48 +020069
70static void rt2800usb_bbp_write(struct rt2x00_dev *rt2x00dev,
71 const unsigned int word, const u8 value)
72{
73 u32 reg;
74
75 mutex_lock(&rt2x00dev->csr_mutex);
76
77 /*
78 * Wait until the BBP becomes available, afterwards we
79 * can safely write the new data into the register.
80 */
81 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
82 reg = 0;
83 rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
84 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
85 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
86 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
87
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +010088 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +020089 }
90
91 mutex_unlock(&rt2x00dev->csr_mutex);
92}
93
94static void rt2800usb_bbp_read(struct rt2x00_dev *rt2x00dev,
95 const unsigned int word, u8 *value)
96{
97 u32 reg;
98
99 mutex_lock(&rt2x00dev->csr_mutex);
100
101 /*
102 * Wait until the BBP becomes available, afterwards we
103 * can safely write the read request into the register.
104 * After the data has been written, we wait until hardware
105 * returns the correct value, if at any time the register
106 * doesn't become available in time, reg will be 0xffffffff
107 * which means we return 0xff to the caller.
108 */
109 if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
110 reg = 0;
111 rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
112 rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
113 rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
114
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100115 rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200116
117 WAIT_FOR_BBP(rt2x00dev, &reg);
118 }
119
120 *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
121
122 mutex_unlock(&rt2x00dev->csr_mutex);
123}
124
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100125static inline void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
126 const unsigned int word, const u8 value)
127{
128 rt2800usb_bbp_write(rt2x00dev, word, value);
129}
130
131static inline void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
132 const unsigned int word, u8 *value)
133{
134 rt2800usb_bbp_read(rt2x00dev, word, value);
135}
136
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200137static void rt2800usb_rfcsr_write(struct rt2x00_dev *rt2x00dev,
138 const unsigned int word, const u8 value)
139{
140 u32 reg;
141
142 mutex_lock(&rt2x00dev->csr_mutex);
143
144 /*
145 * Wait until the RFCSR becomes available, afterwards we
146 * can safely write the new data into the register.
147 */
148 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
149 reg = 0;
150 rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
151 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
152 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
153 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
154
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100155 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200156 }
157
158 mutex_unlock(&rt2x00dev->csr_mutex);
159}
160
161static void rt2800usb_rfcsr_read(struct rt2x00_dev *rt2x00dev,
162 const unsigned int word, u8 *value)
163{
164 u32 reg;
165
166 mutex_lock(&rt2x00dev->csr_mutex);
167
168 /*
169 * Wait until the RFCSR becomes available, afterwards we
170 * can safely write the read request into the register.
171 * After the data has been written, we wait until hardware
172 * returns the correct value, if at any time the register
173 * doesn't become available in time, reg will be 0xffffffff
174 * which means we return 0xff to the caller.
175 */
176 if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
177 reg = 0;
178 rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
179 rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
180 rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
181
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100182 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200183
184 WAIT_FOR_RFCSR(rt2x00dev, &reg);
185 }
186
187 *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
188
189 mutex_unlock(&rt2x00dev->csr_mutex);
190}
191
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100192static inline void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
193 const unsigned int word, const u8 value)
194{
195 rt2800usb_rfcsr_write(rt2x00dev, word, value);
196}
197
198static inline void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
199 const unsigned int word, u8 *value)
200{
201 rt2800usb_rfcsr_read(rt2x00dev, word, value);
202}
203
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200204static void rt2800usb_rf_write(struct rt2x00_dev *rt2x00dev,
205 const unsigned int word, const u32 value)
206{
207 u32 reg;
208
209 mutex_lock(&rt2x00dev->csr_mutex);
210
211 /*
212 * Wait until the RF becomes available, afterwards we
213 * can safely write the new data into the register.
214 */
215 if (WAIT_FOR_RF(rt2x00dev, &reg)) {
216 reg = 0;
217 rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
218 rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
219 rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
220 rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
221
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100222 rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200223 rt2x00_rf_write(rt2x00dev, word, value);
224 }
225
226 mutex_unlock(&rt2x00dev->csr_mutex);
227}
228
Bartlomiej Zolnierkiewicz5c70e5b2009-11-04 18:34:18 +0100229static inline void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
230 const unsigned int word, const u32 value)
231{
232 rt2800usb_rf_write(rt2x00dev, word, value);
233}
234
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200235static void rt2800usb_mcu_request(struct rt2x00_dev *rt2x00dev,
236 const u8 command, const u8 token,
237 const u8 arg0, const u8 arg1)
238{
239 u32 reg;
240
241 mutex_lock(&rt2x00dev->csr_mutex);
242
243 /*
244 * Wait until the MCU becomes available, afterwards we
245 * can safely write the new data into the register.
246 */
247 if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
248 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
249 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
250 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
251 rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100252 rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200253
254 reg = 0;
255 rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100256 rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200257 }
258
259 mutex_unlock(&rt2x00dev->csr_mutex);
260}
261
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +0100262static inline void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
263 const u8 command, const u8 token,
264 const u8 arg0, const u8 arg1)
265{
266 rt2800usb_mcu_request(rt2x00dev, command, token, arg0, arg1);
267}
268
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200269#ifdef CONFIG_RT2X00_LIB_DEBUGFS
270static const struct rt2x00debug rt2800usb_rt2x00debug = {
271 .owner = THIS_MODULE,
272 .csr = {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100273 .read = rt2800_register_read,
274 .write = rt2800_register_write,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200275 .flags = RT2X00DEBUGFS_OFFSET,
276 .word_base = CSR_REG_BASE,
277 .word_size = sizeof(u32),
278 .word_count = CSR_REG_SIZE / sizeof(u32),
279 },
280 .eeprom = {
281 .read = rt2x00_eeprom_read,
282 .write = rt2x00_eeprom_write,
283 .word_base = EEPROM_BASE,
284 .word_size = sizeof(u16),
285 .word_count = EEPROM_SIZE / sizeof(u16),
286 },
287 .bbp = {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100288 .read = rt2800_bbp_read,
289 .write = rt2800_bbp_write,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200290 .word_base = BBP_BASE,
291 .word_size = sizeof(u8),
292 .word_count = BBP_SIZE / sizeof(u8),
293 },
294 .rf = {
295 .read = rt2x00_rf_read,
Bartlomiej Zolnierkiewicz5c70e5b2009-11-04 18:34:18 +0100296 .write = rt2800_rf_write,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200297 .word_base = RF_BASE,
298 .word_size = sizeof(u32),
299 .word_count = RF_SIZE / sizeof(u32),
300 },
301};
302#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
303
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200304static int rt2800usb_rfkill_poll(struct rt2x00_dev *rt2x00dev)
305{
306 u32 reg;
307
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100308 rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200309 return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
310}
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200311
312#ifdef CONFIG_RT2X00_LIB_LEDS
313static void rt2800usb_brightness_set(struct led_classdev *led_cdev,
314 enum led_brightness brightness)
315{
316 struct rt2x00_led *led =
317 container_of(led_cdev, struct rt2x00_led, led_dev);
318 unsigned int enabled = brightness != LED_OFF;
319 unsigned int bg_mode =
320 (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
321 unsigned int polarity =
322 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
323 EEPROM_FREQ_LED_POLARITY);
324 unsigned int ledmode =
325 rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
326 EEPROM_FREQ_LED_MODE);
327
328 if (led->type == LED_TYPE_RADIO) {
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +0100329 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200330 enabled ? 0x20 : 0);
331 } else if (led->type == LED_TYPE_ASSOC) {
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +0100332 rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200333 enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
334 } else if (led->type == LED_TYPE_QUALITY) {
335 /*
336 * The brightness is divided into 6 levels (0 - 5),
337 * The specs tell us the following levels:
338 * 0, 1 ,3, 7, 15, 31
339 * to determine the level in a simple way we can simply
340 * work with bitshifting:
341 * (1 << level) - 1
342 */
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +0100343 rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200344 (1 << brightness / (LED_FULL / 6)) - 1,
345 polarity);
346 }
347}
348
349static int rt2800usb_blink_set(struct led_classdev *led_cdev,
350 unsigned long *delay_on,
351 unsigned long *delay_off)
352{
353 struct rt2x00_led *led =
354 container_of(led_cdev, struct rt2x00_led, led_dev);
355 u32 reg;
356
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100357 rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200358 rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
359 rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
360 rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
361 rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
362 rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 12);
363 rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
364 rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100365 rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200366
367 return 0;
368}
369
370static void rt2800usb_init_led(struct rt2x00_dev *rt2x00dev,
371 struct rt2x00_led *led,
372 enum led_type type)
373{
374 led->rt2x00dev = rt2x00dev;
375 led->type = type;
376 led->led_dev.brightness_set = rt2800usb_brightness_set;
377 led->led_dev.blink_set = rt2800usb_blink_set;
378 led->flags = LED_INITIALIZED;
379}
380#endif /* CONFIG_RT2X00_LIB_LEDS */
381
382/*
383 * Configuration handlers.
384 */
385static void rt2800usb_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
386 struct rt2x00lib_crypto *crypto,
387 struct ieee80211_key_conf *key)
388{
389 struct mac_wcid_entry wcid_entry;
390 struct mac_iveiv_entry iveiv_entry;
391 u32 offset;
392 u32 reg;
393
394 offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
395
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100396 rt2800_register_read(rt2x00dev, offset, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200397 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
398 !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
399 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
400 (crypto->cmd == SET_KEY) * crypto->cipher);
401 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
402 (crypto->cmd == SET_KEY) * crypto->bssidx);
403 rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100404 rt2800_register_write(rt2x00dev, offset, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200405
406 offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
407
408 memset(&iveiv_entry, 0, sizeof(iveiv_entry));
409 if ((crypto->cipher == CIPHER_TKIP) ||
410 (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
411 (crypto->cipher == CIPHER_AES))
412 iveiv_entry.iv[3] |= 0x20;
413 iveiv_entry.iv[3] |= key->keyidx << 6;
Bartlomiej Zolnierkiewicz678b4ee2009-11-04 18:33:20 +0100414 rt2800_register_multiwrite(rt2x00dev, offset,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200415 &iveiv_entry, sizeof(iveiv_entry));
416
417 offset = MAC_WCID_ENTRY(key->hw_key_idx);
418
419 memset(&wcid_entry, 0, sizeof(wcid_entry));
420 if (crypto->cmd == SET_KEY)
421 memcpy(&wcid_entry, crypto->address, ETH_ALEN);
Bartlomiej Zolnierkiewicz678b4ee2009-11-04 18:33:20 +0100422 rt2800_register_multiwrite(rt2x00dev, offset,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200423 &wcid_entry, sizeof(wcid_entry));
424}
425
426static int rt2800usb_config_shared_key(struct rt2x00_dev *rt2x00dev,
427 struct rt2x00lib_crypto *crypto,
428 struct ieee80211_key_conf *key)
429{
430 struct hw_key_entry key_entry;
431 struct rt2x00_field32 field;
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200432 u32 offset;
433 u32 reg;
434
435 if (crypto->cmd == SET_KEY) {
436 key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
437
438 memcpy(key_entry.key, crypto->key,
439 sizeof(key_entry.key));
440 memcpy(key_entry.tx_mic, crypto->tx_mic,
441 sizeof(key_entry.tx_mic));
442 memcpy(key_entry.rx_mic, crypto->rx_mic,
443 sizeof(key_entry.rx_mic));
444
445 offset = SHARED_KEY_ENTRY(key->hw_key_idx);
Bartlomiej Zolnierkiewicz678b4ee2009-11-04 18:33:20 +0100446 rt2800_register_multiwrite(rt2x00dev, offset,
Bartlomiej Zolnierkiewicz3306ef62009-11-04 18:32:58 +0100447 &key_entry, sizeof(key_entry));
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200448 }
449
450 /*
451 * The cipher types are stored over multiple registers
452 * starting with SHARED_KEY_MODE_BASE each word will have
453 * 32 bits and contains the cipher types for 2 bssidx each.
454 * Using the correct defines correctly will cause overhead,
455 * so just calculate the correct offset.
456 */
457 field.bit_offset = 4 * (key->hw_key_idx % 8);
458 field.bit_mask = 0x7 << field.bit_offset;
459
460 offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
461
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100462 rt2800_register_read(rt2x00dev, offset, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200463 rt2x00_set_field32(&reg, field,
464 (crypto->cmd == SET_KEY) * crypto->cipher);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100465 rt2800_register_write(rt2x00dev, offset, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200466
467 /*
468 * Update WCID information
469 */
470 rt2800usb_config_wcid_attr(rt2x00dev, crypto, key);
471
472 return 0;
473}
474
475static int rt2800usb_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
476 struct rt2x00lib_crypto *crypto,
477 struct ieee80211_key_conf *key)
478{
479 struct hw_key_entry key_entry;
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200480 u32 offset;
481
482 if (crypto->cmd == SET_KEY) {
483 /*
484 * 1 pairwise key is possible per AID, this means that the AID
485 * equals our hw_key_idx. Make sure the WCID starts _after_ the
486 * last possible shared key entry.
487 */
488 if (crypto->aid > (256 - 32))
489 return -ENOSPC;
490
491 key->hw_key_idx = 32 + crypto->aid;
492
493 memcpy(key_entry.key, crypto->key,
494 sizeof(key_entry.key));
495 memcpy(key_entry.tx_mic, crypto->tx_mic,
496 sizeof(key_entry.tx_mic));
497 memcpy(key_entry.rx_mic, crypto->rx_mic,
498 sizeof(key_entry.rx_mic));
499
500 offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
Bartlomiej Zolnierkiewicz678b4ee2009-11-04 18:33:20 +0100501 rt2800_register_multiwrite(rt2x00dev, offset,
Bartlomiej Zolnierkiewicz3306ef62009-11-04 18:32:58 +0100502 &key_entry, sizeof(key_entry));
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200503 }
504
505 /*
506 * Update WCID information
507 */
508 rt2800usb_config_wcid_attr(rt2x00dev, crypto, key);
509
510 return 0;
511}
512
513static void rt2800usb_config_filter(struct rt2x00_dev *rt2x00dev,
514 const unsigned int filter_flags)
515{
516 u32 reg;
517
518 /*
519 * Start configuration steps.
520 * Note that the version error will always be dropped
521 * and broadcast frames will always be accepted since
522 * there is no filter for it at this time.
523 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100524 rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200525 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
526 !(filter_flags & FIF_FCSFAIL));
527 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
528 !(filter_flags & FIF_PLCPFAIL));
529 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
530 !(filter_flags & FIF_PROMISC_IN_BSS));
531 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
532 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
533 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
534 !(filter_flags & FIF_ALLMULTI));
535 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
536 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
537 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
538 !(filter_flags & FIF_CONTROL));
539 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
540 !(filter_flags & FIF_CONTROL));
541 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
542 !(filter_flags & FIF_CONTROL));
543 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
544 !(filter_flags & FIF_CONTROL));
545 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
546 !(filter_flags & FIF_CONTROL));
547 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
Igor Perminov1afcfd542009-08-08 23:55:55 +0200548 !(filter_flags & FIF_PSPOLL));
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200549 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
550 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
551 rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
552 !(filter_flags & FIF_CONTROL));
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100553 rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200554}
555
556static void rt2800usb_config_intf(struct rt2x00_dev *rt2x00dev,
557 struct rt2x00_intf *intf,
558 struct rt2x00intf_conf *conf,
559 const unsigned int flags)
560{
561 unsigned int beacon_base;
562 u32 reg;
563
564 if (flags & CONFIG_UPDATE_TYPE) {
565 /*
566 * Clear current synchronisation setup.
567 * For the Beacon base registers we only need to clear
568 * the first byte since that byte contains the VALID and OWNER
569 * bits which (when set to 0) will invalidate the entire beacon.
570 */
571 beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100572 rt2800_register_write(rt2x00dev, beacon_base, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200573
574 /*
575 * Enable synchronisation.
576 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100577 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200578 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
579 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
580 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100581 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200582 }
583
584 if (flags & CONFIG_UPDATE_MAC) {
585 reg = le32_to_cpu(conf->mac[1]);
586 rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
587 conf->mac[1] = cpu_to_le32(reg);
588
Bartlomiej Zolnierkiewicz678b4ee2009-11-04 18:33:20 +0100589 rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200590 conf->mac, sizeof(conf->mac));
591 }
592
593 if (flags & CONFIG_UPDATE_BSSID) {
594 reg = le32_to_cpu(conf->bssid[1]);
595 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 0);
596 rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
597 conf->bssid[1] = cpu_to_le32(reg);
598
Bartlomiej Zolnierkiewicz678b4ee2009-11-04 18:33:20 +0100599 rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200600 conf->bssid, sizeof(conf->bssid));
601 }
602}
603
604static void rt2800usb_config_erp(struct rt2x00_dev *rt2x00dev,
605 struct rt2x00lib_erp *erp)
606{
607 u32 reg;
608
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100609 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
Ivo van Doorn47896662009-09-06 15:14:23 +0200610 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 0x20);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100611 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200612
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100613 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200614 rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
615 !!erp->short_preamble);
616 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
617 !!erp->short_preamble);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100618 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200619
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100620 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200621 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
622 erp->cts_protection ? 2 : 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100623 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200624
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100625 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200626 erp->basic_rates);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100627 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200628
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100629 rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200630 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
631 rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100632 rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200633
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100634 rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200635 rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, erp->sifs);
636 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, erp->sifs);
637 rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
638 rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
639 rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100640 rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
Ivo van Doorn8a566af2009-05-21 19:16:46 +0200641
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100642 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Ivo van Doorn8a566af2009-05-21 19:16:46 +0200643 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
644 erp->beacon_int * 16);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100645 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200646}
647
648static void rt2800usb_config_ant(struct rt2x00_dev *rt2x00dev,
649 struct antenna_setup *ant)
650{
651 u8 r1;
652 u8 r3;
653
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100654 rt2800_bbp_read(rt2x00dev, 1, &r1);
655 rt2800_bbp_read(rt2x00dev, 3, &r3);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200656
657 /*
658 * Configure the TX antenna.
659 */
660 switch ((int)ant->tx) {
661 case 1:
662 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
663 break;
664 case 2:
665 rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
666 break;
667 case 3:
668 /* Do nothing */
669 break;
670 }
671
672 /*
673 * Configure the RX antenna.
674 */
675 switch ((int)ant->rx) {
676 case 1:
677 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
678 break;
679 case 2:
680 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
681 break;
682 case 3:
683 rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
684 break;
685 }
686
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100687 rt2800_bbp_write(rt2x00dev, 3, r3);
688 rt2800_bbp_write(rt2x00dev, 1, r1);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200689}
690
691static void rt2800usb_config_lna_gain(struct rt2x00_dev *rt2x00dev,
692 struct rt2x00lib_conf *libconf)
693{
694 u16 eeprom;
695 short lna_gain;
696
697 if (libconf->rf.channel <= 14) {
698 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
699 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
700 } else if (libconf->rf.channel <= 64) {
701 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
702 lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
703 } else if (libconf->rf.channel <= 128) {
704 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
705 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
706 } else {
707 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
708 lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
709 }
710
711 rt2x00dev->lna_gain = lna_gain;
712}
713
714static void rt2800usb_config_channel_rt2x(struct rt2x00_dev *rt2x00dev,
715 struct ieee80211_conf *conf,
716 struct rf_channel *rf,
717 struct channel_info *info)
718{
719 rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
720
721 if (rt2x00dev->default_ant.tx == 1)
722 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
723
724 if (rt2x00dev->default_ant.rx == 1) {
725 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
726 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
727 } else if (rt2x00dev->default_ant.rx == 2)
728 rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
729
730 if (rf->channel > 14) {
731 /*
732 * When TX power is below 0, we should increase it by 7 to
733 * make it a positive value (Minumum value is -7).
734 * However this means that values between 0 and 7 have
735 * double meaning, and we should set a 7DBm boost flag.
736 */
737 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
738 (info->tx_power1 >= 0));
739
740 if (info->tx_power1 < 0)
741 info->tx_power1 += 7;
742
743 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
744 TXPOWER_A_TO_DEV(info->tx_power1));
745
746 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
747 (info->tx_power2 >= 0));
748
749 if (info->tx_power2 < 0)
750 info->tx_power2 += 7;
751
752 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
753 TXPOWER_A_TO_DEV(info->tx_power2));
754 } else {
755 rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
756 TXPOWER_G_TO_DEV(info->tx_power1));
757 rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
758 TXPOWER_G_TO_DEV(info->tx_power2));
759 }
760
761 rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
762
Bartlomiej Zolnierkiewicz5c70e5b2009-11-04 18:34:18 +0100763 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
764 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
765 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
766 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200767
768 udelay(200);
769
Bartlomiej Zolnierkiewicz5c70e5b2009-11-04 18:34:18 +0100770 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
771 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
772 rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
773 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200774
775 udelay(200);
776
Bartlomiej Zolnierkiewicz5c70e5b2009-11-04 18:34:18 +0100777 rt2800_rf_write(rt2x00dev, 1, rf->rf1);
778 rt2800_rf_write(rt2x00dev, 2, rf->rf2);
779 rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
780 rt2800_rf_write(rt2x00dev, 4, rf->rf4);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200781}
782
783static void rt2800usb_config_channel_rt3x(struct rt2x00_dev *rt2x00dev,
784 struct ieee80211_conf *conf,
785 struct rf_channel *rf,
786 struct channel_info *info)
787{
788 u8 rfcsr;
789
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100790 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
791 rt2800_rfcsr_write(rt2x00dev, 2, rf->rf3);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200792
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100793 rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200794 rt2x00_set_field8(&rfcsr, RFCSR6_R, rf->rf2);
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100795 rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200796
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100797 rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200798 rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
799 TXPOWER_G_TO_DEV(info->tx_power1));
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100800 rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200801
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100802 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200803 rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100804 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200805
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100806 rt2800_rfcsr_write(rt2x00dev, 24,
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200807 rt2x00dev->calibration[conf_is_ht40(conf)]);
808
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100809 rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200810 rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +0100811 rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200812}
813
814static void rt2800usb_config_channel(struct rt2x00_dev *rt2x00dev,
815 struct ieee80211_conf *conf,
816 struct rf_channel *rf,
817 struct channel_info *info)
818{
819 u32 reg;
820 unsigned int tx_pin;
821 u8 bbp;
822
823 if (rt2x00_rev(&rt2x00dev->chip) != RT3070_VERSION)
824 rt2800usb_config_channel_rt2x(rt2x00dev, conf, rf, info);
825 else
826 rt2800usb_config_channel_rt3x(rt2x00dev, conf, rf, info);
827
828 /*
829 * Change BBP settings
830 */
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100831 rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
832 rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
833 rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
834 rt2800_bbp_write(rt2x00dev, 86, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200835
836 if (rf->channel <= 14) {
837 if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100838 rt2800_bbp_write(rt2x00dev, 82, 0x62);
839 rt2800_bbp_write(rt2x00dev, 75, 0x46);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200840 } else {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100841 rt2800_bbp_write(rt2x00dev, 82, 0x84);
842 rt2800_bbp_write(rt2x00dev, 75, 0x50);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200843 }
844 } else {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100845 rt2800_bbp_write(rt2x00dev, 82, 0xf2);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200846
847 if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100848 rt2800_bbp_write(rt2x00dev, 75, 0x46);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200849 else
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100850 rt2800_bbp_write(rt2x00dev, 75, 0x50);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200851 }
852
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100853 rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200854 rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_PLUS, conf_is_ht40_plus(conf));
855 rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
856 rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100857 rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200858
859 tx_pin = 0;
860
861 /* Turn on unused PA or LNA when not using 1T or 1R */
862 if (rt2x00dev->default_ant.tx != 1) {
863 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
864 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
865 }
866
867 /* Turn on unused PA or LNA when not using 1T or 1R */
868 if (rt2x00dev->default_ant.rx != 1) {
869 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
870 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
871 }
872
873 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
874 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
875 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
876 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
877 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
878 rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
879
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100880 rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200881
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100882 rt2800_bbp_read(rt2x00dev, 4, &bbp);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200883 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100884 rt2800_bbp_write(rt2x00dev, 4, bbp);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200885
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100886 rt2800_bbp_read(rt2x00dev, 3, &bbp);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200887 rt2x00_set_field8(&bbp, BBP3_HT40_PLUS, conf_is_ht40_plus(conf));
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100888 rt2800_bbp_write(rt2x00dev, 3, bbp);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200889
890 if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
891 if (conf_is_ht40(conf)) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100892 rt2800_bbp_write(rt2x00dev, 69, 0x1a);
893 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
894 rt2800_bbp_write(rt2x00dev, 73, 0x16);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200895 } else {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100896 rt2800_bbp_write(rt2x00dev, 69, 0x16);
897 rt2800_bbp_write(rt2x00dev, 70, 0x08);
898 rt2800_bbp_write(rt2x00dev, 73, 0x11);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200899 }
900 }
901
902 msleep(1);
903}
904
905static void rt2800usb_config_txpower(struct rt2x00_dev *rt2x00dev,
906 const int txpower)
907{
908 u32 reg;
909 u32 value = TXPOWER_G_TO_DEV(txpower);
910 u8 r1;
911
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100912 rt2800_bbp_read(rt2x00dev, 1, &r1);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200913 rt2x00_set_field8(&reg, BBP1_TX_POWER, 0);
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +0100914 rt2800_bbp_write(rt2x00dev, 1, r1);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200915
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100916 rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200917 rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
918 rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
919 rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
920 rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
921 rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
922 rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
923 rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
924 rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100925 rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200926
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100927 rt2800_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200928 rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
929 rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
930 rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
931 rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
932 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
933 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
934 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
935 rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100936 rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200937
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100938 rt2800_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200939 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
940 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
941 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
942 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
943 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
944 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
945 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
946 rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100947 rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200948
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100949 rt2800_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200950 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
951 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
952 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
953 rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
954 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
955 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
956 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
957 rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100958 rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200959
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100960 rt2800_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200961 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
962 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
963 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
964 rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100965 rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200966}
967
968static void rt2800usb_config_retry_limit(struct rt2x00_dev *rt2x00dev,
969 struct rt2x00lib_conf *libconf)
970{
971 u32 reg;
972
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100973 rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200974 rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
975 libconf->conf->short_frame_max_tx_count);
976 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
977 libconf->conf->long_frame_max_tx_count);
978 rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
979 rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
980 rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
981 rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100982 rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200983}
984
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200985static void rt2800usb_config_ps(struct rt2x00_dev *rt2x00dev,
986 struct rt2x00lib_conf *libconf)
987{
988 enum dev_state state =
989 (libconf->conf->flags & IEEE80211_CONF_PS) ?
990 STATE_SLEEP : STATE_AWAKE;
991 u32 reg;
992
993 if (state == STATE_SLEEP) {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100994 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200995
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +0100996 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +0200997 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
998 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
999 libconf->conf->listen_interval - 1);
1000 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001001 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001002
Ivo van Doorn15e46922009-04-28 20:14:58 +02001003 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001004 } else {
Ivo van Doorn15e46922009-04-28 20:14:58 +02001005 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001006
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001007 rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001008 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1009 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1010 rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001011 rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001012 }
1013}
1014
1015static void rt2800usb_config(struct rt2x00_dev *rt2x00dev,
1016 struct rt2x00lib_conf *libconf,
1017 const unsigned int flags)
1018{
1019 /* Always recalculate LNA gain before changing configuration */
1020 rt2800usb_config_lna_gain(rt2x00dev, libconf);
1021
1022 if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1023 rt2800usb_config_channel(rt2x00dev, libconf->conf,
1024 &libconf->rf, &libconf->channel);
1025 if (flags & IEEE80211_CONF_CHANGE_POWER)
1026 rt2800usb_config_txpower(rt2x00dev, libconf->conf->power_level);
1027 if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1028 rt2800usb_config_retry_limit(rt2x00dev, libconf);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001029 if (flags & IEEE80211_CONF_CHANGE_PS)
1030 rt2800usb_config_ps(rt2x00dev, libconf);
1031}
1032
1033/*
1034 * Link tuning
1035 */
1036static void rt2800usb_link_stats(struct rt2x00_dev *rt2x00dev,
1037 struct link_qual *qual)
1038{
1039 u32 reg;
1040
1041 /*
1042 * Update FCS error count from register.
1043 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001044 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001045 qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1046}
1047
1048static u8 rt2800usb_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1049{
1050 if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
1051 if (rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION)
1052 return 0x1c + (2 * rt2x00dev->lna_gain);
1053 else
1054 return 0x2e + rt2x00dev->lna_gain;
1055 }
1056
1057 if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1058 return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1059 else
1060 return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1061}
1062
1063static inline void rt2800usb_set_vgc(struct rt2x00_dev *rt2x00dev,
1064 struct link_qual *qual, u8 vgc_level)
1065{
1066 if (qual->vgc_level != vgc_level) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001067 rt2800_bbp_write(rt2x00dev, 66, vgc_level);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001068 qual->vgc_level = vgc_level;
1069 qual->vgc_level_reg = vgc_level;
1070 }
1071}
1072
1073static void rt2800usb_reset_tuner(struct rt2x00_dev *rt2x00dev,
1074 struct link_qual *qual)
1075{
1076 rt2800usb_set_vgc(rt2x00dev, qual,
1077 rt2800usb_get_default_vgc(rt2x00dev));
1078}
1079
1080static void rt2800usb_link_tuner(struct rt2x00_dev *rt2x00dev,
1081 struct link_qual *qual, const u32 count)
1082{
1083 if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION)
1084 return;
1085
1086 /*
1087 * When RSSI is better then -80 increase VGC level with 0x10
1088 */
1089 rt2800usb_set_vgc(rt2x00dev, qual,
1090 rt2800usb_get_default_vgc(rt2x00dev) +
1091 ((qual->rssi > -80) * 0x10));
1092}
1093
1094/*
1095 * Firmware functions
1096 */
1097static char *rt2800usb_get_firmware_name(struct rt2x00_dev *rt2x00dev)
1098{
1099 return FIRMWARE_RT2870;
1100}
1101
1102static bool rt2800usb_check_crc(const u8 *data, const size_t len)
1103{
1104 u16 fw_crc;
1105 u16 crc;
1106
1107 /*
1108 * The last 2 bytes in the firmware array are the crc checksum itself,
1109 * this means that we should never pass those 2 bytes to the crc
1110 * algorithm.
1111 */
1112 fw_crc = (data[len - 2] << 8 | data[len - 1]);
1113
1114 /*
1115 * Use the crc ccitt algorithm.
1116 * This will return the same value as the legacy driver which
1117 * used bit ordering reversion on the both the firmware bytes
1118 * before input input as well as on the final output.
1119 * Obviously using crc ccitt directly is much more efficient.
1120 */
1121 crc = crc_ccitt(~0, data, len - 2);
1122
1123 /*
1124 * There is a small difference between the crc-itu-t + bitrev and
1125 * the crc-ccitt crc calculation. In the latter method the 2 bytes
1126 * will be swapped, use swab16 to convert the crc to the correct
1127 * value.
1128 */
1129 crc = swab16(crc);
1130
1131 return fw_crc == crc;
1132}
1133
1134static int rt2800usb_check_firmware(struct rt2x00_dev *rt2x00dev,
1135 const u8 *data, const size_t len)
1136{
1137 u16 chipset = (rt2x00_rev(&rt2x00dev->chip) >> 16) & 0xffff;
1138 size_t offset = 0;
1139
1140 /*
1141 * Firmware files:
1142 * There are 2 variations of the rt2870 firmware.
1143 * a) size: 4kb
1144 * b) size: 8kb
1145 * Note that (b) contains 2 seperate firmware blobs of 4k
1146 * within the file. The first blob is the same firmware as (a),
1147 * but the second blob is for the additional chipsets.
1148 */
1149 if (len != 4096 && len != 8192)
1150 return FW_BAD_LENGTH;
1151
1152 /*
1153 * Check if we need the upper 4kb firmware data or not.
1154 */
1155 if ((len == 4096) &&
1156 (chipset != 0x2860) &&
1157 (chipset != 0x2872) &&
1158 (chipset != 0x3070))
1159 return FW_BAD_VERSION;
1160
1161 /*
1162 * 8kb firmware files must be checked as if it were
1163 * 2 seperate firmware files.
1164 */
1165 while (offset < len) {
1166 if (!rt2800usb_check_crc(data + offset, 4096))
1167 return FW_BAD_CRC;
1168
1169 offset += 4096;
1170 }
1171
1172 return FW_OK;
1173}
1174
1175static int rt2800usb_load_firmware(struct rt2x00_dev *rt2x00dev,
1176 const u8 *data, const size_t len)
1177{
1178 unsigned int i;
1179 int status;
1180 u32 reg;
1181 u32 offset;
1182 u32 length;
1183 u16 chipset = (rt2x00_rev(&rt2x00dev->chip) >> 16) & 0xffff;
1184
1185 /*
1186 * Check which section of the firmware we need.
1187 */
Ivo van Doorn15e46922009-04-28 20:14:58 +02001188 if ((chipset == 0x2860) ||
1189 (chipset == 0x2872) ||
1190 (chipset == 0x3070)) {
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001191 offset = 0;
1192 length = 4096;
1193 } else {
1194 offset = 4096;
1195 length = 4096;
1196 }
1197
1198 /*
1199 * Wait for stable hardware.
1200 */
1201 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001202 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001203 if (reg && reg != ~0)
1204 break;
1205 msleep(1);
1206 }
1207
1208 if (i == REGISTER_BUSY_COUNT) {
1209 ERROR(rt2x00dev, "Unstable hardware.\n");
1210 return -EBUSY;
1211 }
1212
1213 /*
1214 * Write firmware to device.
1215 */
1216 rt2x00usb_vendor_request_large_buff(rt2x00dev, USB_MULTI_WRITE,
1217 USB_VENDOR_REQUEST_OUT,
1218 FIRMWARE_IMAGE_BASE,
1219 data + offset, length,
1220 REGISTER_TIMEOUT32(length));
1221
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001222 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
1223 rt2800_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001224
1225 /*
1226 * Send firmware request to device to load firmware,
1227 * we need to specify a long timeout time.
1228 */
1229 status = rt2x00usb_vendor_request_sw(rt2x00dev, USB_DEVICE_MODE,
1230 0, USB_MODE_FIRMWARE,
1231 REGISTER_TIMEOUT_FIRMWARE);
1232 if (status < 0) {
1233 ERROR(rt2x00dev, "Failed to write Firmware to device.\n");
1234 return status;
1235 }
1236
Ivo van Doorn15e46922009-04-28 20:14:58 +02001237 msleep(10);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001238 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
Ivo van Doorn15e46922009-04-28 20:14:58 +02001239
1240 /*
1241 * Send signal to firmware during boot time.
1242 */
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +01001243 rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0xff, 0, 0);
Ivo van Doorn15e46922009-04-28 20:14:58 +02001244
1245 if ((chipset == 0x3070) ||
1246 (chipset == 0x3071) ||
1247 (chipset == 0x3572)) {
1248 udelay(200);
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +01001249 rt2800_mcu_request(rt2x00dev, MCU_CURRENT, 0, 0, 0);
Ivo van Doorn15e46922009-04-28 20:14:58 +02001250 udelay(10);
1251 }
1252
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001253 /*
1254 * Wait for device to stabilize.
1255 */
1256 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001257 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001258 if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
1259 break;
1260 msleep(1);
1261 }
1262
1263 if (i == REGISTER_BUSY_COUNT) {
1264 ERROR(rt2x00dev, "PBF system register not ready.\n");
1265 return -EBUSY;
1266 }
1267
1268 /*
1269 * Initialize firmware.
1270 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001271 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
1272 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001273 msleep(1);
1274
1275 return 0;
1276}
1277
1278/*
1279 * Initialization functions.
1280 */
1281static int rt2800usb_init_registers(struct rt2x00_dev *rt2x00dev)
1282{
1283 u32 reg;
1284 unsigned int i;
1285
1286 /*
1287 * Wait untill BBP and RF are ready.
1288 */
1289 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001290 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001291 if (reg && reg != ~0)
1292 break;
1293 msleep(1);
1294 }
1295
1296 if (i == REGISTER_BUSY_COUNT) {
1297 ERROR(rt2x00dev, "Unstable hardware.\n");
1298 return -EBUSY;
1299 }
1300
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001301 rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
1302 rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, reg & ~0x00002000);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001303
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001304 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001305 rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_CSR, 1);
1306 rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_BBP, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001307 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001308
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001309 rt2800_register_write(rt2x00dev, USB_DMA_CFG, 0x00000000);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001310
1311 rt2x00usb_vendor_request_sw(rt2x00dev, USB_DEVICE_MODE, 0,
1312 USB_MODE_RESET, REGISTER_TIMEOUT);
1313
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001314 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001315
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001316 rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001317 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1318 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1319 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1320 rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001321 rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001322
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001323 rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001324 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1325 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1326 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1327 rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001328 rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001329
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001330 rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1331 rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001332
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001333 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001334
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001335 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001336 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
1337 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1338 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1339 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1340 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1341 rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001342 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001343
1344 if (rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION) {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001345 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1346 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1347 rt2800_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001348 } else {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001349 rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1350 rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001351 }
1352
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001353 rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001354 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1355 rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1356 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1357 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1358 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1359 rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1360 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1361 rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001362 rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001363
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001364 rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001365 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
1366 rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001367 rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001368
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001369 rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001370 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
1371 if (rt2x00_rev(&rt2x00dev->chip) >= RT2880E_VERSION &&
1372 rt2x00_rev(&rt2x00dev->chip) < RT3070_VERSION)
1373 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1374 else
1375 rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1376 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1377 rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001378 rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001379
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001380 rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001381
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001382 rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001383 rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
1384 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1385 rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
1386 rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1387 rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001388 rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001389
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001390 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001391 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 8);
1392 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1393 rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1394 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1395 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1396 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1397 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1398 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1399 rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001400 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001401
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001402 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001403 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 8);
1404 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1405 rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1406 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1407 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1408 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1409 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1410 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1411 rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001412 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001413
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001414 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001415 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1416 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1417 rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1418 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1419 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1420 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1421 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1422 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1423 rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001424 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001425
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001426 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001427 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
1428 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
1429 rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1430 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1431 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1432 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1433 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1434 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1435 rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001436 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001437
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001438 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001439 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1440 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1441 rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1442 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1443 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1444 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1445 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1446 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1447 rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001448 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001449
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001450 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001451 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1452 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1453 rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1454 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1455 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1456 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1457 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1458 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1459 rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001460 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001461
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001462 rt2800_register_write(rt2x00dev, PBF_CFG, 0xf40006);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001463
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001464 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001465 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1466 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1467 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1468 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1469 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1470 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1471 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1472 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1473 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001474 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001475
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001476 rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1477 rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001478
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001479 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001480 rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1481 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1482 IEEE80211_MAX_RTS_THRESHOLD);
1483 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001484 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001485
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001486 rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
1487 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001488
1489 /*
1490 * ASIC will keep garbage value after boot, clear encryption keys.
1491 */
Ivo van Doorn1738c9e2009-08-17 18:53:57 +02001492 for (i = 0; i < 4; i++)
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001493 rt2800_register_write(rt2x00dev,
Ivo van Doorn1738c9e2009-08-17 18:53:57 +02001494 SHARED_KEY_MODE_ENTRY(i), 0);
1495
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001496 for (i = 0; i < 256; i++) {
1497 u32 wcid[2] = { 0xffffffff, 0x00ffffff };
Bartlomiej Zolnierkiewicz678b4ee2009-11-04 18:33:20 +01001498 rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001499 wcid, sizeof(wcid));
1500
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001501 rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1502 rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001503 }
1504
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001505 /*
1506 * Clear all beacons
1507 * For the Beacon base registers we only need to clear
1508 * the first byte since that byte contains the VALID and OWNER
1509 * bits which (when set to 0) will invalidate the entire beacon.
1510 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001511 rt2800_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
1512 rt2800_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
1513 rt2800_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
1514 rt2800_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
1515 rt2800_register_write(rt2x00dev, HW_BEACON_BASE4, 0);
1516 rt2800_register_write(rt2x00dev, HW_BEACON_BASE5, 0);
1517 rt2800_register_write(rt2x00dev, HW_BEACON_BASE6, 0);
1518 rt2800_register_write(rt2x00dev, HW_BEACON_BASE7, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001519
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001520 rt2800_register_read(rt2x00dev, USB_CYC_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001521 rt2x00_set_field32(&reg, USB_CYC_CFG_CLOCK_CYCLE, 30);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001522 rt2800_register_write(rt2x00dev, USB_CYC_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001523
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001524 rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001525 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1526 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1527 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1528 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1529 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1530 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1531 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1532 rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001533 rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001534
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001535 rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001536 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1537 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1538 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1539 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1540 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1541 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1542 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1543 rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001544 rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001545
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001546 rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001547 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1548 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
Ivo van Doorncd80b682009-08-17 18:55:40 +02001549 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001550 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1551 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1552 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1553 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
1554 rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001555 rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001556
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001557 rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001558 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
1559 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
1560 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
1561 rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001562 rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001563
1564 /*
1565 * We must clear the error counters.
1566 * These registers are cleared on read,
1567 * so we may pass a useless variable to store the value.
1568 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001569 rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1570 rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
1571 rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
1572 rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
1573 rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
1574 rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001575
1576 return 0;
1577}
1578
1579static int rt2800usb_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
1580{
1581 unsigned int i;
1582 u32 reg;
1583
1584 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001585 rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001586 if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
1587 return 0;
1588
1589 udelay(REGISTER_BUSY_DELAY);
1590 }
1591
1592 ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
1593 return -EACCES;
1594}
1595
1596static int rt2800usb_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
1597{
1598 unsigned int i;
1599 u8 value;
1600
Ivo van Doorn15e46922009-04-28 20:14:58 +02001601 /*
1602 * BBP was enabled after firmware was loaded,
1603 * but we need to reactivate it now.
1604 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001605 rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
1606 rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
Ivo van Doorn15e46922009-04-28 20:14:58 +02001607 msleep(1);
1608
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001609 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001610 rt2800_bbp_read(rt2x00dev, 0, &value);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001611 if ((value != 0xff) && (value != 0x00))
1612 return 0;
1613 udelay(REGISTER_BUSY_DELAY);
1614 }
1615
1616 ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
1617 return -EACCES;
1618}
1619
1620static int rt2800usb_init_bbp(struct rt2x00_dev *rt2x00dev)
1621{
1622 unsigned int i;
1623 u16 eeprom;
1624 u8 reg_id;
1625 u8 value;
1626
1627 if (unlikely(rt2800usb_wait_bbp_rf_ready(rt2x00dev) ||
1628 rt2800usb_wait_bbp_ready(rt2x00dev)))
1629 return -EACCES;
1630
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001631 rt2800_bbp_write(rt2x00dev, 65, 0x2c);
1632 rt2800_bbp_write(rt2x00dev, 66, 0x38);
1633 rt2800_bbp_write(rt2x00dev, 69, 0x12);
1634 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1635 rt2800_bbp_write(rt2x00dev, 73, 0x10);
1636 rt2800_bbp_write(rt2x00dev, 81, 0x37);
1637 rt2800_bbp_write(rt2x00dev, 82, 0x62);
1638 rt2800_bbp_write(rt2x00dev, 83, 0x6a);
1639 rt2800_bbp_write(rt2x00dev, 84, 0x99);
1640 rt2800_bbp_write(rt2x00dev, 86, 0x00);
1641 rt2800_bbp_write(rt2x00dev, 91, 0x04);
1642 rt2800_bbp_write(rt2x00dev, 92, 0x00);
1643 rt2800_bbp_write(rt2x00dev, 103, 0x00);
1644 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001645
1646 if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001647 rt2800_bbp_write(rt2x00dev, 69, 0x16);
1648 rt2800_bbp_write(rt2x00dev, 73, 0x12);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001649 }
1650
1651 if (rt2x00_rev(&rt2x00dev->chip) > RT2860D_VERSION) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001652 rt2800_bbp_write(rt2x00dev, 84, 0x19);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001653 }
1654
1655 if (rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001656 rt2800_bbp_write(rt2x00dev, 70, 0x0a);
1657 rt2800_bbp_write(rt2x00dev, 84, 0x99);
1658 rt2800_bbp_write(rt2x00dev, 105, 0x05);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001659 }
1660
1661 for (i = 0; i < EEPROM_BBP_SIZE; i++) {
1662 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
1663
1664 if (eeprom != 0xffff && eeprom != 0x0000) {
1665 reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
1666 value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001667 rt2800_bbp_write(rt2x00dev, reg_id, value);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001668 }
1669 }
1670
1671 return 0;
1672}
1673
1674static u8 rt2800usb_init_rx_filter(struct rt2x00_dev *rt2x00dev,
1675 bool bw40, u8 rfcsr24, u8 filter_target)
1676{
1677 unsigned int i;
1678 u8 bbp;
1679 u8 rfcsr;
1680 u8 passband;
1681 u8 stopband;
1682 u8 overtuned = 0;
1683
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001684 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001685
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001686 rt2800_bbp_read(rt2x00dev, 4, &bbp);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001687 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001688 rt2800_bbp_write(rt2x00dev, 4, bbp);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001689
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001690 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001691 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001692 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001693
1694 /*
1695 * Set power & frequency of passband test tone
1696 */
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001697 rt2800_bbp_write(rt2x00dev, 24, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001698
1699 for (i = 0; i < 100; i++) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001700 rt2800_bbp_write(rt2x00dev, 25, 0x90);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001701 msleep(1);
1702
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001703 rt2800_bbp_read(rt2x00dev, 55, &passband);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001704 if (passband)
1705 break;
1706 }
1707
1708 /*
1709 * Set power & frequency of stopband test tone
1710 */
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001711 rt2800_bbp_write(rt2x00dev, 24, 0x06);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001712
1713 for (i = 0; i < 100; i++) {
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001714 rt2800_bbp_write(rt2x00dev, 25, 0x90);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001715 msleep(1);
1716
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001717 rt2800_bbp_read(rt2x00dev, 55, &stopband);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001718
1719 if ((passband - stopband) <= filter_target) {
1720 rfcsr24++;
1721 overtuned += ((passband - stopband) == filter_target);
1722 } else
1723 break;
1724
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001725 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001726 }
1727
1728 rfcsr24 -= !!overtuned;
1729
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001730 rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001731 return rfcsr24;
1732}
1733
1734static int rt2800usb_init_rfcsr(struct rt2x00_dev *rt2x00dev)
1735{
1736 u8 rfcsr;
1737 u8 bbp;
1738
1739 if (rt2x00_rev(&rt2x00dev->chip) != RT3070_VERSION)
1740 return 0;
1741
1742 /*
1743 * Init RF calibration.
1744 */
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001745 rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001746 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001747 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001748 msleep(1);
1749 rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001750 rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001751
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001752 rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
1753 rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
1754 rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
1755 rt2800_rfcsr_write(rt2x00dev, 7, 0x70);
1756 rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
1757 rt2800_rfcsr_write(rt2x00dev, 10, 0x71);
1758 rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
1759 rt2800_rfcsr_write(rt2x00dev, 12, 0x7b);
1760 rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
1761 rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
1762 rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
1763 rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
1764 rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
1765 rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
1766 rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
1767 rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
1768 rt2800_rfcsr_write(rt2x00dev, 24, 0x16);
1769 rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
1770 rt2800_rfcsr_write(rt2x00dev, 27, 0x03);
1771 rt2800_rfcsr_write(rt2x00dev, 29, 0x1f);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001772
1773 /*
1774 * Set RX Filter calibration for 20MHz and 40MHz
1775 */
1776 rt2x00dev->calibration[0] =
1777 rt2800usb_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
1778 rt2x00dev->calibration[1] =
1779 rt2800usb_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
1780
1781 /*
1782 * Set back to initial state
1783 */
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001784 rt2800_bbp_write(rt2x00dev, 24, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001785
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001786 rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001787 rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
Bartlomiej Zolnierkiewicze91fea9b2009-11-04 18:34:04 +01001788 rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001789
1790 /*
1791 * set BBP back to BW20
1792 */
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001793 rt2800_bbp_read(rt2x00dev, 4, &bbp);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001794 rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
Bartlomiej Zolnierkiewiczeff6ece2009-11-04 18:33:50 +01001795 rt2800_bbp_write(rt2x00dev, 4, bbp);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001796
1797 return 0;
1798}
1799
1800/*
1801 * Device state switch handlers.
1802 */
1803static void rt2800usb_toggle_rx(struct rt2x00_dev *rt2x00dev,
1804 enum dev_state state)
1805{
1806 u32 reg;
1807
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001808 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001809 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX,
1810 (state == STATE_RADIO_RX_ON) ||
1811 (state == STATE_RADIO_RX_ON_LINK));
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001812 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001813}
1814
1815static int rt2800usb_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
1816{
1817 unsigned int i;
1818 u32 reg;
1819
1820 for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001821 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001822 if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
1823 !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
1824 return 0;
1825
1826 msleep(1);
1827 }
1828
1829 ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
1830 return -EACCES;
1831}
1832
1833static int rt2800usb_enable_radio(struct rt2x00_dev *rt2x00dev)
1834{
1835 u32 reg;
1836 u16 word;
1837
1838 /*
1839 * Initialize all registers.
1840 */
1841 if (unlikely(rt2800usb_wait_wpdma_ready(rt2x00dev) ||
1842 rt2800usb_init_registers(rt2x00dev) ||
1843 rt2800usb_init_bbp(rt2x00dev) ||
1844 rt2800usb_init_rfcsr(rt2x00dev)))
1845 return -EIO;
1846
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001847 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001848 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001849 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001850
1851 udelay(50);
1852
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001853 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001854 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1855 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
1856 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001857 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001858
1859
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001860 rt2800_register_read(rt2x00dev, USB_DMA_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001861 rt2x00_set_field32(&reg, USB_DMA_CFG_PHY_CLEAR, 0);
1862 /* Don't use bulk in aggregation when working with USB 1.1 */
1863 rt2x00_set_field32(&reg, USB_DMA_CFG_RX_BULK_AGG_EN,
1864 (rt2x00dev->rx->usb_maxpacket == 512));
1865 rt2x00_set_field32(&reg, USB_DMA_CFG_RX_BULK_AGG_TIMEOUT, 128);
Ivo van Doorn15e46922009-04-28 20:14:58 +02001866 /*
1867 * Total room for RX frames in kilobytes, PBF might still exceed
1868 * this limit so reduce the number to prevent errors.
1869 */
1870 rt2x00_set_field32(&reg, USB_DMA_CFG_RX_BULK_AGG_LIMIT,
1871 ((RX_ENTRIES * DATA_FRAME_SIZE) / 1024) - 3);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001872 rt2x00_set_field32(&reg, USB_DMA_CFG_RX_BULK_EN, 1);
1873 rt2x00_set_field32(&reg, USB_DMA_CFG_TX_BULK_EN, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001874 rt2800_register_write(rt2x00dev, USB_DMA_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001875
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001876 rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001877 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
1878 rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001879 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001880
1881 /*
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001882 * Initialize LED control
1883 */
1884 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +01001885 rt2800_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001886 word & 0xff, (word >> 8) & 0xff);
1887
1888 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +01001889 rt2800_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001890 word & 0xff, (word >> 8) & 0xff);
1891
1892 rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +01001893 rt2800_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001894 word & 0xff, (word >> 8) & 0xff);
1895
1896 return 0;
1897}
1898
1899static void rt2800usb_disable_radio(struct rt2x00_dev *rt2x00dev)
1900{
1901 u32 reg;
1902
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001903 rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001904 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1905 rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001906 rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001907
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01001908 rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0);
1909 rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0);
1910 rt2800_register_write(rt2x00dev, TX_PIN_CFG, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001911
1912 /* Wait for DMA, ignore error */
1913 rt2800usb_wait_wpdma_ready(rt2x00dev);
1914
1915 rt2x00usb_disable_radio(rt2x00dev);
1916}
1917
1918static int rt2800usb_set_state(struct rt2x00_dev *rt2x00dev,
1919 enum dev_state state)
1920{
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001921 if (state == STATE_AWAKE)
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +01001922 rt2800_mcu_request(rt2x00dev, MCU_WAKEUP, 0xff, 0, 0);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001923 else
Bartlomiej Zolnierkiewicz4f2c5322009-11-04 18:34:32 +01001924 rt2800_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 2);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001925
1926 return 0;
1927}
1928
1929static int rt2800usb_set_device_state(struct rt2x00_dev *rt2x00dev,
1930 enum dev_state state)
1931{
1932 int retval = 0;
1933
1934 switch (state) {
1935 case STATE_RADIO_ON:
1936 /*
1937 * Before the radio can be enabled, the device first has
1938 * to be woken up. After that it needs a bit of time
Luis Correia49513482009-07-17 21:39:19 +02001939 * to be fully awake and then the radio can be enabled.
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001940 */
1941 rt2800usb_set_state(rt2x00dev, STATE_AWAKE);
1942 msleep(1);
1943 retval = rt2800usb_enable_radio(rt2x00dev);
1944 break;
1945 case STATE_RADIO_OFF:
1946 /*
Luis Correia49513482009-07-17 21:39:19 +02001947 * After the radio has been disabled, the device should
Ivo van Doornd53d9e62009-04-26 15:47:48 +02001948 * be put to sleep for powersaving.
1949 */
1950 rt2800usb_disable_radio(rt2x00dev);
1951 rt2800usb_set_state(rt2x00dev, STATE_SLEEP);
1952 break;
1953 case STATE_RADIO_RX_ON:
1954 case STATE_RADIO_RX_ON_LINK:
1955 case STATE_RADIO_RX_OFF:
1956 case STATE_RADIO_RX_OFF_LINK:
1957 rt2800usb_toggle_rx(rt2x00dev, state);
1958 break;
1959 case STATE_RADIO_IRQ_ON:
1960 case STATE_RADIO_IRQ_OFF:
1961 /* No support, but no error either */
1962 break;
1963 case STATE_DEEP_SLEEP:
1964 case STATE_SLEEP:
1965 case STATE_STANDBY:
1966 case STATE_AWAKE:
1967 retval = rt2800usb_set_state(rt2x00dev, state);
1968 break;
1969 default:
1970 retval = -ENOTSUPP;
1971 break;
1972 }
1973
1974 if (unlikely(retval))
1975 ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
1976 state, retval);
1977
1978 return retval;
1979}
1980
1981/*
1982 * TX descriptor initialization
1983 */
1984static void rt2800usb_write_tx_desc(struct rt2x00_dev *rt2x00dev,
1985 struct sk_buff *skb,
1986 struct txentry_desc *txdesc)
1987{
1988 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
1989 __le32 *txi = skbdesc->desc;
1990 __le32 *txwi = &txi[TXINFO_DESC_SIZE / sizeof(__le32)];
1991 u32 word;
1992
1993 /*
1994 * Initialize TX Info descriptor
1995 */
1996 rt2x00_desc_read(txwi, 0, &word);
1997 rt2x00_set_field32(&word, TXWI_W0_FRAG,
1998 test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
1999 rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
2000 rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
2001 rt2x00_set_field32(&word, TXWI_W0_TS,
2002 test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
2003 rt2x00_set_field32(&word, TXWI_W0_AMPDU,
2004 test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
2005 rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
2006 rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->ifs);
2007 rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
2008 rt2x00_set_field32(&word, TXWI_W0_BW,
2009 test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
2010 rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
2011 test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
2012 rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
2013 rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
2014 rt2x00_desc_write(txwi, 0, word);
2015
2016 rt2x00_desc_read(txwi, 1, &word);
2017 rt2x00_set_field32(&word, TXWI_W1_ACK,
2018 test_bit(ENTRY_TXD_ACK, &txdesc->flags));
2019 rt2x00_set_field32(&word, TXWI_W1_NSEQ,
2020 test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
2021 rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
2022 rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
2023 test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
Benoit PAPILLAULT17616312009-10-15 21:17:09 +02002024 txdesc->key_idx : 0xff);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002025 rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
2026 skb->len - txdesc->l2pad);
2027 rt2x00_set_field32(&word, TXWI_W1_PACKETID,
Ivo van Doorn534aff02009-08-17 18:55:15 +02002028 skbdesc->entry->queue->qid + 1);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002029 rt2x00_desc_write(txwi, 1, word);
2030
2031 /*
2032 * Always write 0 to IV/EIV fields, hardware will insert the IV
2033 * from the IVEIV register when TXINFO_W0_WIV is set to 0.
2034 * When TXINFO_W0_WIV is set to 1 it will use the IV data
2035 * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
2036 * crypto entry in the registers should be used to encrypt the frame.
2037 */
2038 _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
2039 _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
2040
2041 /*
2042 * Initialize TX descriptor
2043 */
2044 rt2x00_desc_read(txi, 0, &word);
2045 rt2x00_set_field32(&word, TXINFO_W0_USB_DMA_TX_PKT_LEN,
2046 skb->len + TXWI_DESC_SIZE);
2047 rt2x00_set_field32(&word, TXINFO_W0_WIV,
2048 !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags));
2049 rt2x00_set_field32(&word, TXINFO_W0_QSEL, 2);
2050 rt2x00_set_field32(&word, TXINFO_W0_SW_USE_LAST_ROUND, 0);
2051 rt2x00_set_field32(&word, TXINFO_W0_USB_DMA_NEXT_VALID, 0);
2052 rt2x00_set_field32(&word, TXINFO_W0_USB_DMA_TX_BURST,
2053 test_bit(ENTRY_TXD_BURST, &txdesc->flags));
2054 rt2x00_desc_write(txi, 0, word);
2055}
2056
2057/*
2058 * TX data initialization
2059 */
2060static void rt2800usb_write_beacon(struct queue_entry *entry)
2061{
2062 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
2063 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
2064 unsigned int beacon_base;
2065 u32 reg;
2066
2067 /*
2068 * Add the descriptor in front of the skb.
2069 */
2070 skb_push(entry->skb, entry->queue->desc_size);
2071 memcpy(entry->skb->data, skbdesc->desc, skbdesc->desc_len);
2072 skbdesc->desc = entry->skb->data;
2073
2074 /*
2075 * Disable beaconing while we are reloading the beacon data,
2076 * otherwise we might be sending out invalid data.
2077 */
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002078 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002079 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002080 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002081
2082 /*
2083 * Write entire beacon with descriptor to register.
2084 */
2085 beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
2086 rt2x00usb_vendor_request_large_buff(rt2x00dev, USB_MULTI_WRITE,
2087 USB_VENDOR_REQUEST_OUT, beacon_base,
2088 entry->skb->data, entry->skb->len,
2089 REGISTER_TIMEOUT32(entry->skb->len));
2090
2091 /*
2092 * Clean up the beacon skb.
2093 */
2094 dev_kfree_skb(entry->skb);
2095 entry->skb = NULL;
2096}
2097
2098static int rt2800usb_get_tx_data_len(struct queue_entry *entry)
2099{
2100 int length;
2101
2102 /*
2103 * The length _must_ include 4 bytes padding,
2104 * it should always be multiple of 4,
2105 * but it must _not_ be a multiple of the USB packet size.
2106 */
2107 length = roundup(entry->skb->len + 4, 4);
2108 length += (4 * !(length % entry->queue->usb_maxpacket));
2109
2110 return length;
2111}
2112
2113static void rt2800usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
2114 const enum data_queue_qid queue)
2115{
2116 u32 reg;
2117
2118 if (queue != QID_BEACON) {
2119 rt2x00usb_kick_tx_queue(rt2x00dev, queue);
2120 return;
2121 }
2122
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002123 rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002124 if (!rt2x00_get_field32(reg, BCN_TIME_CFG_BEACON_GEN)) {
2125 rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
2126 rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
2127 rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002128 rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002129 }
2130}
2131
2132/*
2133 * RX control handlers
2134 */
2135static void rt2800usb_fill_rxdone(struct queue_entry *entry,
2136 struct rxdone_entry_desc *rxdesc)
2137{
2138 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
2139 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
2140 __le32 *rxd = (__le32 *)entry->skb->data;
2141 __le32 *rxwi;
2142 u32 rxd0;
2143 u32 rxwi0;
2144 u32 rxwi1;
2145 u32 rxwi2;
2146 u32 rxwi3;
2147
2148 /*
2149 * Copy descriptor to the skbdesc->desc buffer, making it safe from
2150 * moving of frame data in rt2x00usb.
2151 */
2152 memcpy(skbdesc->desc, rxd, skbdesc->desc_len);
2153 rxd = (__le32 *)skbdesc->desc;
2154 rxwi = &rxd[RXD_DESC_SIZE / sizeof(__le32)];
2155
2156 /*
2157 * It is now safe to read the descriptor on all architectures.
2158 */
2159 rt2x00_desc_read(rxd, 0, &rxd0);
2160 rt2x00_desc_read(rxwi, 0, &rxwi0);
2161 rt2x00_desc_read(rxwi, 1, &rxwi1);
2162 rt2x00_desc_read(rxwi, 2, &rxwi2);
2163 rt2x00_desc_read(rxwi, 3, &rxwi3);
2164
2165 if (rt2x00_get_field32(rxd0, RXD_W0_CRC_ERROR))
2166 rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
2167
2168 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
2169 rxdesc->cipher = rt2x00_get_field32(rxwi0, RXWI_W0_UDF);
2170 rxdesc->cipher_status =
2171 rt2x00_get_field32(rxd0, RXD_W0_CIPHER_ERROR);
2172 }
2173
2174 if (rt2x00_get_field32(rxd0, RXD_W0_DECRYPTED)) {
2175 /*
2176 * Hardware has stripped IV/EIV data from 802.11 frame during
2177 * decryption. Unfortunately the descriptor doesn't contain
2178 * any fields with the EIV/IV data either, so they can't
2179 * be restored by rt2x00lib.
2180 */
2181 rxdesc->flags |= RX_FLAG_IV_STRIPPED;
2182
2183 if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
2184 rxdesc->flags |= RX_FLAG_DECRYPTED;
2185 else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
2186 rxdesc->flags |= RX_FLAG_MMIC_ERROR;
2187 }
2188
2189 if (rt2x00_get_field32(rxd0, RXD_W0_MY_BSS))
2190 rxdesc->dev_flags |= RXDONE_MY_BSS;
2191
Ivo van Doorn0fefe0f2009-08-17 18:54:50 +02002192 if (rt2x00_get_field32(rxd0, RXD_W0_L2PAD)) {
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002193 rxdesc->dev_flags |= RXDONE_L2PAD;
Ivo van Doorn0fefe0f2009-08-17 18:54:50 +02002194 skbdesc->flags |= SKBDESC_L2_PADDED;
2195 }
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002196
2197 if (rt2x00_get_field32(rxwi1, RXWI_W1_SHORT_GI))
2198 rxdesc->flags |= RX_FLAG_SHORT_GI;
2199
2200 if (rt2x00_get_field32(rxwi1, RXWI_W1_BW))
2201 rxdesc->flags |= RX_FLAG_40MHZ;
2202
2203 /*
2204 * Detect RX rate, always use MCS as signal type.
2205 */
2206 rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
2207 rxdesc->rate_mode = rt2x00_get_field32(rxwi1, RXWI_W1_PHYMODE);
2208 rxdesc->signal = rt2x00_get_field32(rxwi1, RXWI_W1_MCS);
2209
2210 /*
2211 * Mask of 0x8 bit to remove the short preamble flag.
2212 */
2213 if (rxdesc->rate_mode == RATE_MODE_CCK)
2214 rxdesc->signal &= ~0x8;
2215
2216 rxdesc->rssi =
2217 (rt2x00_get_field32(rxwi2, RXWI_W2_RSSI0) +
2218 rt2x00_get_field32(rxwi2, RXWI_W2_RSSI1)) / 2;
2219
2220 rxdesc->noise =
2221 (rt2x00_get_field32(rxwi3, RXWI_W3_SNR0) +
2222 rt2x00_get_field32(rxwi3, RXWI_W3_SNR1)) / 2;
2223
2224 rxdesc->size = rt2x00_get_field32(rxwi0, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
2225
2226 /*
2227 * Remove RXWI descriptor from start of buffer.
2228 */
2229 skb_pull(entry->skb, skbdesc->desc_len);
2230 skb_trim(entry->skb, rxdesc->size);
2231}
2232
2233/*
2234 * Device probe functions.
2235 */
2236static int rt2800usb_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2237{
2238 u16 word;
2239 u8 *mac;
2240 u8 default_lna_gain;
2241
2242 rt2x00usb_eeprom_read(rt2x00dev, rt2x00dev->eeprom, EEPROM_SIZE);
2243
2244 /*
2245 * Start validation of the data that has been read.
2246 */
2247 mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2248 if (!is_valid_ether_addr(mac)) {
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002249 random_ether_addr(mac);
Johannes Berge91d8332009-07-15 17:21:41 +02002250 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002251 }
2252
2253 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2254 if (word == 0xffff) {
2255 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2256 rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2257 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2258 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2259 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
2260 } else if (rt2x00_rev(&rt2x00dev->chip) < RT2883_VERSION) {
2261 /*
2262 * There is a max of 2 RX streams for RT2870 series
2263 */
2264 if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2265 rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2266 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2267 }
2268
2269 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2270 if (word == 0xffff) {
2271 rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2272 rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2273 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2274 rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2275 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2276 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2277 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2278 rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2279 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2280 rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
2281 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2282 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2283 }
2284
2285 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2286 if ((word & 0x00ff) == 0x00ff) {
2287 rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
2288 rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2289 LED_MODE_TXRX_ACTIVITY);
2290 rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2291 rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2292 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2293 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2294 rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
2295 EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2296 }
2297
2298 /*
2299 * During the LNA validation we are going to use
2300 * lna0 as correct value. Note that EEPROM_LNA
2301 * is never validated.
2302 */
2303 rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2304 default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2305
2306 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2307 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2308 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2309 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2310 rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2311 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2312
2313 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2314 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2315 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2316 if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2317 rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2318 rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2319 default_lna_gain);
2320 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2321
2322 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2323 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2324 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2325 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2326 rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2327 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2328
2329 rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2330 if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2331 rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2332 if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2333 rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2334 rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2335 default_lna_gain);
2336 rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2337
2338 return 0;
2339}
2340
2341static int rt2800usb_init_eeprom(struct rt2x00_dev *rt2x00dev)
2342{
2343 u32 reg;
2344 u16 value;
2345 u16 eeprom;
2346
2347 /*
2348 * Read EEPROM word for configuration.
2349 */
2350 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2351
2352 /*
2353 * Identify RF chipset.
2354 */
2355 value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002356 rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002357 rt2x00_set_chip(rt2x00dev, RT2870, value, reg);
2358
2359 /*
2360 * The check for rt2860 is not a typo, some rt2870 hardware
2361 * identifies itself as rt2860 in the CSR register.
2362 */
Ivo van Doorn358623c2009-05-05 19:46:08 +02002363 if (!rt2x00_check_rev(&rt2x00dev->chip, 0xfff00000, 0x28600000) &&
2364 !rt2x00_check_rev(&rt2x00dev->chip, 0xfff00000, 0x28700000) &&
2365 !rt2x00_check_rev(&rt2x00dev->chip, 0xfff00000, 0x28800000) &&
2366 !rt2x00_check_rev(&rt2x00dev->chip, 0xffff0000, 0x30700000)) {
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002367 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
2368 return -ENODEV;
2369 }
2370
2371 if (!rt2x00_rf(&rt2x00dev->chip, RF2820) &&
2372 !rt2x00_rf(&rt2x00dev->chip, RF2850) &&
2373 !rt2x00_rf(&rt2x00dev->chip, RF2720) &&
2374 !rt2x00_rf(&rt2x00dev->chip, RF2750) &&
2375 !rt2x00_rf(&rt2x00dev->chip, RF3020) &&
2376 !rt2x00_rf(&rt2x00dev->chip, RF2020)) {
2377 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
2378 return -ENODEV;
2379 }
2380
2381 /*
2382 * Identify default antenna configuration.
2383 */
2384 rt2x00dev->default_ant.tx =
2385 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
2386 rt2x00dev->default_ant.rx =
2387 rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
2388
2389 /*
2390 * Read frequency offset and RF programming sequence.
2391 */
2392 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
2393 rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
2394
2395 /*
2396 * Read external LNA informations.
2397 */
2398 rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2399
2400 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
2401 __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
2402 if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2403 __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
2404
2405 /*
2406 * Detect if this device has an hardware controlled radio.
2407 */
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002408 if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
2409 __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002410
2411 /*
2412 * Store led settings, for correct led behaviour.
2413 */
2414#ifdef CONFIG_RT2X00_LIB_LEDS
2415 rt2800usb_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
2416 rt2800usb_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
2417 rt2800usb_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
2418
2419 rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ,
2420 &rt2x00dev->led_mcu_reg);
2421#endif /* CONFIG_RT2X00_LIB_LEDS */
2422
2423 return 0;
2424}
2425
2426/*
2427 * RF value list for rt2870
2428 * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
2429 */
2430static const struct rf_channel rf_vals[] = {
2431 { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
2432 { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
2433 { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
2434 { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
2435 { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
2436 { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
2437 { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
2438 { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
2439 { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
2440 { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
2441 { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
2442 { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
2443 { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
2444 { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
2445
2446 /* 802.11 UNI / HyperLan 2 */
2447 { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
2448 { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
2449 { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
2450 { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
2451 { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
2452 { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
2453 { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
2454 { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
2455 { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
2456 { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
2457 { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
2458 { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
2459
2460 /* 802.11 HyperLan 2 */
2461 { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
2462 { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
2463 { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
2464 { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
2465 { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
2466 { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
2467 { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
2468 { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
2469 { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
2470 { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
2471 { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
2472 { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
2473 { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
2474 { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
2475 { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
2476 { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
2477
2478 /* 802.11 UNII */
2479 { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
2480 { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
2481 { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
2482 { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
2483 { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
2484 { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
2485 { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
2486 { 167, 0x18402ec4, 0x184c03d2, 0x18179855, 0x1815531f },
2487 { 169, 0x18402ec4, 0x184c03d2, 0x18179855, 0x18155327 },
2488 { 171, 0x18402ec4, 0x184c03d6, 0x18179855, 0x18155307 },
2489 { 173, 0x18402ec4, 0x184c03d6, 0x18179855, 0x1815530f },
2490
2491 /* 802.11 Japan */
2492 { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
2493 { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
2494 { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
2495 { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
2496 { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
2497 { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
2498 { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
2499};
2500
2501/*
2502 * RF value list for rt3070
2503 * Supports: 2.4 GHz
2504 */
2505static const struct rf_channel rf_vals_3070[] = {
2506 {1, 241, 2, 2 },
2507 {2, 241, 2, 7 },
2508 {3, 242, 2, 2 },
2509 {4, 242, 2, 7 },
2510 {5, 243, 2, 2 },
2511 {6, 243, 2, 7 },
2512 {7, 244, 2, 2 },
2513 {8, 244, 2, 7 },
2514 {9, 245, 2, 2 },
2515 {10, 245, 2, 7 },
2516 {11, 246, 2, 2 },
2517 {12, 246, 2, 7 },
2518 {13, 247, 2, 2 },
2519 {14, 248, 2, 4 },
2520};
2521
2522static int rt2800usb_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2523{
2524 struct hw_mode_spec *spec = &rt2x00dev->spec;
2525 struct channel_info *info;
2526 char *tx_power1;
2527 char *tx_power2;
2528 unsigned int i;
2529 u16 eeprom;
2530
2531 /*
2532 * Initialize all hw fields.
2533 */
2534 rt2x00dev->hw->flags =
2535 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2536 IEEE80211_HW_SIGNAL_DBM |
2537 IEEE80211_HW_SUPPORTS_PS |
2538 IEEE80211_HW_PS_NULLFUNC_STACK;
2539 rt2x00dev->hw->extra_tx_headroom = TXINFO_DESC_SIZE + TXWI_DESC_SIZE;
2540
2541 SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2542 SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2543 rt2x00_eeprom_addr(rt2x00dev,
2544 EEPROM_MAC_ADDR_0));
2545
2546 rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2547
2548 /*
2549 * Initialize HT information.
2550 */
2551 spec->ht.ht_supported = true;
2552 spec->ht.cap =
2553 IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2554 IEEE80211_HT_CAP_GRN_FLD |
2555 IEEE80211_HT_CAP_SGI_20 |
2556 IEEE80211_HT_CAP_SGI_40 |
2557 IEEE80211_HT_CAP_TX_STBC |
2558 IEEE80211_HT_CAP_RX_STBC |
2559 IEEE80211_HT_CAP_PSMP_SUPPORT;
2560 spec->ht.ampdu_factor = 3;
2561 spec->ht.ampdu_density = 4;
2562 spec->ht.mcs.tx_params =
2563 IEEE80211_HT_MCS_TX_DEFINED |
2564 IEEE80211_HT_MCS_TX_RX_DIFF |
2565 ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
2566 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
2567
2568 switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
2569 case 3:
2570 spec->ht.mcs.rx_mask[2] = 0xff;
2571 case 2:
2572 spec->ht.mcs.rx_mask[1] = 0xff;
2573 case 1:
2574 spec->ht.mcs.rx_mask[0] = 0xff;
2575 spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
2576 break;
2577 }
2578
2579 /*
2580 * Initialize hw_mode information.
2581 */
2582 spec->supported_bands = SUPPORT_BAND_2GHZ;
2583 spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
2584
2585 if (rt2x00_rf(&rt2x00dev->chip, RF2820) ||
2586 rt2x00_rf(&rt2x00dev->chip, RF2720)) {
2587 spec->num_channels = 14;
2588 spec->channels = rf_vals;
2589 } else if (rt2x00_rf(&rt2x00dev->chip, RF2850) ||
2590 rt2x00_rf(&rt2x00dev->chip, RF2750)) {
2591 spec->supported_bands |= SUPPORT_BAND_5GHZ;
2592 spec->num_channels = ARRAY_SIZE(rf_vals);
2593 spec->channels = rf_vals;
2594 } else if (rt2x00_rf(&rt2x00dev->chip, RF3020) ||
2595 rt2x00_rf(&rt2x00dev->chip, RF2020)) {
2596 spec->num_channels = ARRAY_SIZE(rf_vals_3070);
2597 spec->channels = rf_vals_3070;
2598 }
2599
2600 /*
2601 * Create channel information array
2602 */
2603 info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
2604 if (!info)
2605 return -ENOMEM;
2606
2607 spec->channels_info = info;
2608
2609 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
2610 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
2611
2612 for (i = 0; i < 14; i++) {
2613 info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
2614 info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
2615 }
2616
2617 if (spec->num_channels > 14) {
2618 tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
2619 tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
2620
2621 for (i = 14; i < spec->num_channels; i++) {
2622 info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
2623 info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
2624 }
2625 }
2626
2627 return 0;
2628}
2629
Bartlomiej Zolnierkiewicz7a345d32009-11-04 18:34:53 +01002630static const struct rt2800_ops rt2800usb_rt2800_ops = {
2631 .register_read = rt2x00usb_register_read,
2632 .register_write = rt2x00usb_register_write,
2633 .register_write_lock = rt2x00usb_register_write_lock,
2634
2635 .register_multiread = rt2x00usb_register_multiread,
2636 .register_multiwrite = rt2x00usb_register_multiwrite,
2637
2638 .regbusy_read = rt2x00usb_regbusy_read,
2639};
2640
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002641static int rt2800usb_probe_hw(struct rt2x00_dev *rt2x00dev)
2642{
2643 int retval;
2644
Bartlomiej Zolnierkiewicz7a345d32009-11-04 18:34:53 +01002645 rt2x00dev->priv = (void *)&rt2800usb_rt2800_ops;
2646
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002647 /*
2648 * Allocate eeprom data.
2649 */
2650 retval = rt2800usb_validate_eeprom(rt2x00dev);
2651 if (retval)
2652 return retval;
2653
2654 retval = rt2800usb_init_eeprom(rt2x00dev);
2655 if (retval)
2656 return retval;
2657
2658 /*
2659 * Initialize hw specifications.
2660 */
2661 retval = rt2800usb_probe_hw_mode(rt2x00dev);
2662 if (retval)
2663 return retval;
2664
2665 /*
Igor Perminov1afcfd542009-08-08 23:55:55 +02002666 * This device has multiple filters for control frames
2667 * and has a separate filter for PS Poll frames.
2668 */
2669 __set_bit(DRIVER_SUPPORT_CONTROL_FILTERS, &rt2x00dev->flags);
2670 __set_bit(DRIVER_SUPPORT_CONTROL_FILTER_PSPOLL, &rt2x00dev->flags);
2671
2672 /*
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002673 * This device requires firmware.
2674 */
2675 __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002676 __set_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags);
2677 if (!modparam_nohwcrypt)
2678 __set_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags);
2679
2680 /*
2681 * Set the rssi offset.
2682 */
2683 rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
2684
2685 return 0;
2686}
2687
2688/*
2689 * IEEE80211 stack callback functions.
2690 */
2691static void rt2800usb_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
2692 u32 *iv32, u16 *iv16)
2693{
2694 struct rt2x00_dev *rt2x00dev = hw->priv;
2695 struct mac_iveiv_entry iveiv_entry;
2696 u32 offset;
2697
2698 offset = MAC_IVEIV_ENTRY(hw_key_idx);
Bartlomiej Zolnierkiewicz678b4ee2009-11-04 18:33:20 +01002699 rt2800_register_multiread(rt2x00dev, offset,
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002700 &iveiv_entry, sizeof(iveiv_entry));
2701
2702 memcpy(&iveiv_entry.iv[0], iv16, sizeof(iv16));
2703 memcpy(&iveiv_entry.iv[4], iv32, sizeof(iv32));
2704}
2705
2706static int rt2800usb_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2707{
2708 struct rt2x00_dev *rt2x00dev = hw->priv;
2709 u32 reg;
2710 bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
2711
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002712 rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002713 rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002714 rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002715
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002716 rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002717 rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002718 rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002719
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002720 rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002721 rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002722 rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002723
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002724 rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002725 rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002726 rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002727
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002728 rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002729 rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002730 rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002731
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002732 rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002733 rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002734 rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002735
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002736 rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002737 rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002738 rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002739
2740 return 0;
2741}
2742
2743static int rt2800usb_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
2744 const struct ieee80211_tx_queue_params *params)
2745{
2746 struct rt2x00_dev *rt2x00dev = hw->priv;
2747 struct data_queue *queue;
2748 struct rt2x00_field32 field;
2749 int retval;
2750 u32 reg;
2751 u32 offset;
2752
2753 /*
2754 * First pass the configuration through rt2x00lib, that will
2755 * update the queue settings and validate the input. After that
2756 * we are free to update the registers based on the value
2757 * in the queue parameter.
2758 */
2759 retval = rt2x00mac_conf_tx(hw, queue_idx, params);
2760 if (retval)
2761 return retval;
2762
2763 /*
2764 * We only need to perform additional register initialization
2765 * for WMM queues/
2766 */
2767 if (queue_idx >= 4)
2768 return 0;
2769
2770 queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
2771
2772 /* Update WMM TXOP register */
2773 offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
2774 field.bit_offset = (queue_idx & 1) * 16;
2775 field.bit_mask = 0xffff << field.bit_offset;
2776
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002777 rt2800_register_read(rt2x00dev, offset, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002778 rt2x00_set_field32(&reg, field, queue->txop);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002779 rt2800_register_write(rt2x00dev, offset, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002780
2781 /* Update WMM registers */
2782 field.bit_offset = queue_idx * 4;
2783 field.bit_mask = 0xf << field.bit_offset;
2784
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002785 rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002786 rt2x00_set_field32(&reg, field, queue->aifs);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002787 rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002788
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002789 rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002790 rt2x00_set_field32(&reg, field, queue->cw_min);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002791 rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002792
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002793 rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002794 rt2x00_set_field32(&reg, field, queue->cw_max);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002795 rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002796
2797 /* Update EDCA registers */
2798 offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
2799
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002800 rt2800_register_read(rt2x00dev, offset, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002801 rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
2802 rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
2803 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
2804 rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002805 rt2800_register_write(rt2x00dev, offset, reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002806
2807 return 0;
2808}
2809
2810static u64 rt2800usb_get_tsf(struct ieee80211_hw *hw)
2811{
2812 struct rt2x00_dev *rt2x00dev = hw->priv;
2813 u64 tsf;
2814 u32 reg;
2815
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002816 rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002817 tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
Bartlomiej Zolnierkiewiczabbb5052009-11-04 18:33:05 +01002818 rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002819 tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
2820
2821 return tsf;
2822}
2823
2824static const struct ieee80211_ops rt2800usb_mac80211_ops = {
2825 .tx = rt2x00mac_tx,
2826 .start = rt2x00mac_start,
2827 .stop = rt2x00mac_stop,
2828 .add_interface = rt2x00mac_add_interface,
2829 .remove_interface = rt2x00mac_remove_interface,
2830 .config = rt2x00mac_config,
2831 .configure_filter = rt2x00mac_configure_filter,
Stefan Steuerwald930c06f2009-07-10 20:42:55 +02002832 .set_tim = rt2x00mac_set_tim,
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002833 .set_key = rt2x00mac_set_key,
2834 .get_stats = rt2x00mac_get_stats,
2835 .get_tkip_seq = rt2800usb_get_tkip_seq,
2836 .set_rts_threshold = rt2800usb_set_rts_threshold,
2837 .bss_info_changed = rt2x00mac_bss_info_changed,
2838 .conf_tx = rt2800usb_conf_tx,
2839 .get_tx_stats = rt2x00mac_get_tx_stats,
2840 .get_tsf = rt2800usb_get_tsf,
Ivo van Doorne47a5cd2009-07-01 15:17:35 +02002841 .rfkill_poll = rt2x00mac_rfkill_poll,
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002842};
2843
2844static const struct rt2x00lib_ops rt2800usb_rt2x00_ops = {
2845 .probe_hw = rt2800usb_probe_hw,
2846 .get_firmware_name = rt2800usb_get_firmware_name,
2847 .check_firmware = rt2800usb_check_firmware,
2848 .load_firmware = rt2800usb_load_firmware,
2849 .initialize = rt2x00usb_initialize,
2850 .uninitialize = rt2x00usb_uninitialize,
2851 .clear_entry = rt2x00usb_clear_entry,
2852 .set_device_state = rt2800usb_set_device_state,
2853 .rfkill_poll = rt2800usb_rfkill_poll,
2854 .link_stats = rt2800usb_link_stats,
2855 .reset_tuner = rt2800usb_reset_tuner,
2856 .link_tuner = rt2800usb_link_tuner,
2857 .write_tx_desc = rt2800usb_write_tx_desc,
2858 .write_tx_data = rt2x00usb_write_tx_data,
2859 .write_beacon = rt2800usb_write_beacon,
2860 .get_tx_data_len = rt2800usb_get_tx_data_len,
2861 .kick_tx_queue = rt2800usb_kick_tx_queue,
2862 .kill_tx_queue = rt2x00usb_kill_tx_queue,
2863 .fill_rxdone = rt2800usb_fill_rxdone,
2864 .config_shared_key = rt2800usb_config_shared_key,
2865 .config_pairwise_key = rt2800usb_config_pairwise_key,
2866 .config_filter = rt2800usb_config_filter,
2867 .config_intf = rt2800usb_config_intf,
2868 .config_erp = rt2800usb_config_erp,
2869 .config_ant = rt2800usb_config_ant,
2870 .config = rt2800usb_config,
2871};
2872
2873static const struct data_queue_desc rt2800usb_queue_rx = {
2874 .entry_num = RX_ENTRIES,
2875 .data_size = AGGREGATION_SIZE,
2876 .desc_size = RXD_DESC_SIZE + RXWI_DESC_SIZE,
2877 .priv_size = sizeof(struct queue_entry_priv_usb),
2878};
2879
2880static const struct data_queue_desc rt2800usb_queue_tx = {
2881 .entry_num = TX_ENTRIES,
2882 .data_size = AGGREGATION_SIZE,
2883 .desc_size = TXINFO_DESC_SIZE + TXWI_DESC_SIZE,
2884 .priv_size = sizeof(struct queue_entry_priv_usb),
2885};
2886
2887static const struct data_queue_desc rt2800usb_queue_bcn = {
2888 .entry_num = 8 * BEACON_ENTRIES,
2889 .data_size = MGMT_FRAME_SIZE,
2890 .desc_size = TXINFO_DESC_SIZE + TXWI_DESC_SIZE,
2891 .priv_size = sizeof(struct queue_entry_priv_usb),
2892};
2893
2894static const struct rt2x00_ops rt2800usb_ops = {
2895 .name = KBUILD_MODNAME,
2896 .max_sta_intf = 1,
2897 .max_ap_intf = 8,
2898 .eeprom_size = EEPROM_SIZE,
2899 .rf_size = RF_SIZE,
2900 .tx_queues = NUM_TX_QUEUES,
2901 .rx = &rt2800usb_queue_rx,
2902 .tx = &rt2800usb_queue_tx,
2903 .bcn = &rt2800usb_queue_bcn,
2904 .lib = &rt2800usb_rt2x00_ops,
2905 .hw = &rt2800usb_mac80211_ops,
2906#ifdef CONFIG_RT2X00_LIB_DEBUGFS
2907 .debugfs = &rt2800usb_rt2x00debug,
2908#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
2909};
2910
2911/*
2912 * rt2800usb module information.
2913 */
2914static struct usb_device_id rt2800usb_device_table[] = {
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002915 /* Abocom */
2916 { USB_DEVICE(0x07b8, 0x2870), USB_DEVICE_DATA(&rt2800usb_ops) },
2917 { USB_DEVICE(0x07b8, 0x2770), USB_DEVICE_DATA(&rt2800usb_ops) },
2918 { USB_DEVICE(0x07b8, 0x3070), USB_DEVICE_DATA(&rt2800usb_ops) },
2919 { USB_DEVICE(0x07b8, 0x3071), USB_DEVICE_DATA(&rt2800usb_ops) },
2920 { USB_DEVICE(0x07b8, 0x3072), USB_DEVICE_DATA(&rt2800usb_ops) },
2921 { USB_DEVICE(0x1482, 0x3c09), USB_DEVICE_DATA(&rt2800usb_ops) },
2922 /* AirTies */
2923 { USB_DEVICE(0x1eda, 0x2310), USB_DEVICE_DATA(&rt2800usb_ops) },
2924 /* Amigo */
2925 { USB_DEVICE(0x0e0b, 0x9031), USB_DEVICE_DATA(&rt2800usb_ops) },
2926 { USB_DEVICE(0x0e0b, 0x9041), USB_DEVICE_DATA(&rt2800usb_ops) },
2927 /* Amit */
2928 { USB_DEVICE(0x15c5, 0x0008), USB_DEVICE_DATA(&rt2800usb_ops) },
2929 /* ASUS */
2930 { USB_DEVICE(0x0b05, 0x1731), USB_DEVICE_DATA(&rt2800usb_ops) },
2931 { USB_DEVICE(0x0b05, 0x1732), USB_DEVICE_DATA(&rt2800usb_ops) },
2932 { USB_DEVICE(0x0b05, 0x1742), USB_DEVICE_DATA(&rt2800usb_ops) },
2933 { USB_DEVICE(0x0b05, 0x1760), USB_DEVICE_DATA(&rt2800usb_ops) },
2934 { USB_DEVICE(0x0b05, 0x1761), USB_DEVICE_DATA(&rt2800usb_ops) },
2935 /* AzureWave */
2936 { USB_DEVICE(0x13d3, 0x3247), USB_DEVICE_DATA(&rt2800usb_ops) },
2937 { USB_DEVICE(0x13d3, 0x3262), USB_DEVICE_DATA(&rt2800usb_ops) },
2938 { USB_DEVICE(0x13d3, 0x3273), USB_DEVICE_DATA(&rt2800usb_ops) },
2939 { USB_DEVICE(0x13d3, 0x3284), USB_DEVICE_DATA(&rt2800usb_ops) },
2940 /* Belkin */
2941 { USB_DEVICE(0x050d, 0x8053), USB_DEVICE_DATA(&rt2800usb_ops) },
2942 { USB_DEVICE(0x050d, 0x805c), USB_DEVICE_DATA(&rt2800usb_ops) },
2943 { USB_DEVICE(0x050d, 0x815c), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doorn2c617b02009-05-19 07:26:04 +02002944 { USB_DEVICE(0x050d, 0x825a), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002945 /* Buffalo */
2946 { USB_DEVICE(0x0411, 0x00e8), USB_DEVICE_DATA(&rt2800usb_ops) },
2947 { USB_DEVICE(0x0411, 0x012e), USB_DEVICE_DATA(&rt2800usb_ops) },
2948 /* Conceptronic */
2949 { USB_DEVICE(0x14b2, 0x3c06), USB_DEVICE_DATA(&rt2800usb_ops) },
2950 { USB_DEVICE(0x14b2, 0x3c07), USB_DEVICE_DATA(&rt2800usb_ops) },
2951 { USB_DEVICE(0x14b2, 0x3c08), USB_DEVICE_DATA(&rt2800usb_ops) },
2952 { USB_DEVICE(0x14b2, 0x3c09), USB_DEVICE_DATA(&rt2800usb_ops) },
2953 { USB_DEVICE(0x14b2, 0x3c11), USB_DEVICE_DATA(&rt2800usb_ops) },
2954 { USB_DEVICE(0x14b2, 0x3c12), USB_DEVICE_DATA(&rt2800usb_ops) },
2955 { USB_DEVICE(0x14b2, 0x3c23), USB_DEVICE_DATA(&rt2800usb_ops) },
2956 { USB_DEVICE(0x14b2, 0x3c25), USB_DEVICE_DATA(&rt2800usb_ops) },
2957 { USB_DEVICE(0x14b2, 0x3c27), USB_DEVICE_DATA(&rt2800usb_ops) },
2958 { USB_DEVICE(0x14b2, 0x3c28), USB_DEVICE_DATA(&rt2800usb_ops) },
2959 /* Corega */
2960 { USB_DEVICE(0x07aa, 0x002f), USB_DEVICE_DATA(&rt2800usb_ops) },
2961 { USB_DEVICE(0x07aa, 0x003c), USB_DEVICE_DATA(&rt2800usb_ops) },
2962 { USB_DEVICE(0x07aa, 0x003f), USB_DEVICE_DATA(&rt2800usb_ops) },
2963 { USB_DEVICE(0x18c5, 0x0008), USB_DEVICE_DATA(&rt2800usb_ops) },
2964 { USB_DEVICE(0x18c5, 0x0012), USB_DEVICE_DATA(&rt2800usb_ops) },
2965 /* D-Link */
2966 { USB_DEVICE(0x07d1, 0x3c09), USB_DEVICE_DATA(&rt2800usb_ops) },
2967 { USB_DEVICE(0x07d1, 0x3c0a), USB_DEVICE_DATA(&rt2800usb_ops) },
2968 { USB_DEVICE(0x07d1, 0x3c0b), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornce2ebc92009-05-22 21:33:21 +02002969 { USB_DEVICE(0x07d1, 0x3c0d), USB_DEVICE_DATA(&rt2800usb_ops) },
2970 { USB_DEVICE(0x07d1, 0x3c0e), USB_DEVICE_DATA(&rt2800usb_ops) },
2971 { USB_DEVICE(0x07d1, 0x3c0f), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002972 { USB_DEVICE(0x07d1, 0x3c11), USB_DEVICE_DATA(&rt2800usb_ops) },
2973 { USB_DEVICE(0x07d1, 0x3c13), USB_DEVICE_DATA(&rt2800usb_ops) },
2974 /* Edimax */
2975 { USB_DEVICE(0x7392, 0x7711), USB_DEVICE_DATA(&rt2800usb_ops) },
2976 { USB_DEVICE(0x7392, 0x7717), USB_DEVICE_DATA(&rt2800usb_ops) },
2977 { USB_DEVICE(0x7392, 0x7718), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornce2ebc92009-05-22 21:33:21 +02002978 /* Encore */
2979 { USB_DEVICE(0x203d, 0x1480), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02002980 /* EnGenius */
2981 { USB_DEVICE(0X1740, 0x9701), USB_DEVICE_DATA(&rt2800usb_ops) },
2982 { USB_DEVICE(0x1740, 0x9702), USB_DEVICE_DATA(&rt2800usb_ops) },
2983 { USB_DEVICE(0x1740, 0x9703), USB_DEVICE_DATA(&rt2800usb_ops) },
2984 { USB_DEVICE(0x1740, 0x9705), USB_DEVICE_DATA(&rt2800usb_ops) },
2985 { USB_DEVICE(0x1740, 0x9706), USB_DEVICE_DATA(&rt2800usb_ops) },
2986 { USB_DEVICE(0x1740, 0x9801), USB_DEVICE_DATA(&rt2800usb_ops) },
2987 /* Gemtek */
2988 { USB_DEVICE(0x15a9, 0x0010), USB_DEVICE_DATA(&rt2800usb_ops) },
2989 /* Gigabyte */
2990 { USB_DEVICE(0x1044, 0x800b), USB_DEVICE_DATA(&rt2800usb_ops) },
2991 { USB_DEVICE(0x1044, 0x800c), USB_DEVICE_DATA(&rt2800usb_ops) },
2992 { USB_DEVICE(0x1044, 0x800d), USB_DEVICE_DATA(&rt2800usb_ops) },
2993 /* Hawking */
2994 { USB_DEVICE(0x0e66, 0x0001), USB_DEVICE_DATA(&rt2800usb_ops) },
2995 { USB_DEVICE(0x0e66, 0x0003), USB_DEVICE_DATA(&rt2800usb_ops) },
2996 { USB_DEVICE(0x0e66, 0x0009), USB_DEVICE_DATA(&rt2800usb_ops) },
2997 { USB_DEVICE(0x0e66, 0x000b), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornce2ebc92009-05-22 21:33:21 +02002998 /* I-O DATA */
2999 { USB_DEVICE(0x04bb, 0x0945), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02003000 /* LevelOne */
3001 { USB_DEVICE(0x1740, 0x0605), USB_DEVICE_DATA(&rt2800usb_ops) },
3002 { USB_DEVICE(0x1740, 0x0615), USB_DEVICE_DATA(&rt2800usb_ops) },
3003 /* Linksys */
3004 { USB_DEVICE(0x1737, 0x0070), USB_DEVICE_DATA(&rt2800usb_ops) },
3005 { USB_DEVICE(0x1737, 0x0071), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doorne430d602009-04-27 23:58:31 +02003006 { USB_DEVICE(0x1737, 0x0077), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02003007 /* Logitec */
3008 { USB_DEVICE(0x0789, 0x0162), USB_DEVICE_DATA(&rt2800usb_ops) },
3009 { USB_DEVICE(0x0789, 0x0163), USB_DEVICE_DATA(&rt2800usb_ops) },
3010 { USB_DEVICE(0x0789, 0x0164), USB_DEVICE_DATA(&rt2800usb_ops) },
3011 /* Motorola */
3012 { USB_DEVICE(0x100d, 0x9031), USB_DEVICE_DATA(&rt2800usb_ops) },
3013 { USB_DEVICE(0x100d, 0x9032), USB_DEVICE_DATA(&rt2800usb_ops) },
3014 /* Ovislink */
3015 { USB_DEVICE(0x1b75, 0x3072), USB_DEVICE_DATA(&rt2800usb_ops) },
3016 /* Pegatron */
3017 { USB_DEVICE(0x1d4d, 0x0002), USB_DEVICE_DATA(&rt2800usb_ops) },
3018 { USB_DEVICE(0x1d4d, 0x000c), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornce2ebc92009-05-22 21:33:21 +02003019 { USB_DEVICE(0x1d4d, 0x000e), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02003020 /* Philips */
3021 { USB_DEVICE(0x0471, 0x200f), USB_DEVICE_DATA(&rt2800usb_ops) },
3022 /* Planex */
3023 { USB_DEVICE(0x2019, 0xed06), USB_DEVICE_DATA(&rt2800usb_ops) },
3024 { USB_DEVICE(0x2019, 0xab24), USB_DEVICE_DATA(&rt2800usb_ops) },
3025 { USB_DEVICE(0x2019, 0xab25), USB_DEVICE_DATA(&rt2800usb_ops) },
3026 /* Qcom */
3027 { USB_DEVICE(0x18e8, 0x6259), USB_DEVICE_DATA(&rt2800usb_ops) },
3028 /* Quanta */
3029 { USB_DEVICE(0x1a32, 0x0304), USB_DEVICE_DATA(&rt2800usb_ops) },
3030 /* Ralink */
Ivo van Doornce2ebc92009-05-22 21:33:21 +02003031 { USB_DEVICE(0x0db0, 0x3820), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02003032 { USB_DEVICE(0x0db0, 0x6899), USB_DEVICE_DATA(&rt2800usb_ops) },
3033 { USB_DEVICE(0x148f, 0x2070), USB_DEVICE_DATA(&rt2800usb_ops) },
3034 { USB_DEVICE(0x148f, 0x2770), USB_DEVICE_DATA(&rt2800usb_ops) },
3035 { USB_DEVICE(0x148f, 0x2870), USB_DEVICE_DATA(&rt2800usb_ops) },
3036 { USB_DEVICE(0x148f, 0x3070), USB_DEVICE_DATA(&rt2800usb_ops) },
3037 { USB_DEVICE(0x148f, 0x3071), USB_DEVICE_DATA(&rt2800usb_ops) },
3038 { USB_DEVICE(0x148f, 0x3072), USB_DEVICE_DATA(&rt2800usb_ops) },
3039 { USB_DEVICE(0x148f, 0x3572), USB_DEVICE_DATA(&rt2800usb_ops) },
3040 /* Samsung */
3041 { USB_DEVICE(0x04e8, 0x2018), USB_DEVICE_DATA(&rt2800usb_ops) },
3042 /* Siemens */
3043 { USB_DEVICE(0x129b, 0x1828), USB_DEVICE_DATA(&rt2800usb_ops) },
3044 /* Sitecom */
3045 { USB_DEVICE(0x0df6, 0x0017), USB_DEVICE_DATA(&rt2800usb_ops) },
3046 { USB_DEVICE(0x0df6, 0x002b), USB_DEVICE_DATA(&rt2800usb_ops) },
3047 { USB_DEVICE(0x0df6, 0x002c), USB_DEVICE_DATA(&rt2800usb_ops) },
3048 { USB_DEVICE(0x0df6, 0x002d), USB_DEVICE_DATA(&rt2800usb_ops) },
3049 { USB_DEVICE(0x0df6, 0x0039), USB_DEVICE_DATA(&rt2800usb_ops) },
3050 { USB_DEVICE(0x0df6, 0x003b), USB_DEVICE_DATA(&rt2800usb_ops) },
3051 { USB_DEVICE(0x0df6, 0x003c), USB_DEVICE_DATA(&rt2800usb_ops) },
3052 { USB_DEVICE(0x0df6, 0x003d), USB_DEVICE_DATA(&rt2800usb_ops) },
3053 { USB_DEVICE(0x0df6, 0x003e), USB_DEVICE_DATA(&rt2800usb_ops) },
3054 { USB_DEVICE(0x0df6, 0x003f), USB_DEVICE_DATA(&rt2800usb_ops) },
3055 { USB_DEVICE(0x0df6, 0x0040), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornce2ebc92009-05-22 21:33:21 +02003056 { USB_DEVICE(0x0df6, 0x0042), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02003057 /* SMC */
3058 { USB_DEVICE(0x083a, 0x6618), USB_DEVICE_DATA(&rt2800usb_ops) },
3059 { USB_DEVICE(0x083a, 0x7511), USB_DEVICE_DATA(&rt2800usb_ops) },
3060 { USB_DEVICE(0x083a, 0x7512), USB_DEVICE_DATA(&rt2800usb_ops) },
3061 { USB_DEVICE(0x083a, 0x7522), USB_DEVICE_DATA(&rt2800usb_ops) },
3062 { USB_DEVICE(0x083a, 0x8522), USB_DEVICE_DATA(&rt2800usb_ops) },
3063 { USB_DEVICE(0x083a, 0xa512), USB_DEVICE_DATA(&rt2800usb_ops) },
3064 { USB_DEVICE(0x083a, 0xa618), USB_DEVICE_DATA(&rt2800usb_ops) },
3065 { USB_DEVICE(0x083a, 0xb522), USB_DEVICE_DATA(&rt2800usb_ops) },
3066 { USB_DEVICE(0x083a, 0xc522), USB_DEVICE_DATA(&rt2800usb_ops) },
3067 /* Sparklan */
3068 { USB_DEVICE(0x15a9, 0x0006), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doorn3b91c362009-05-21 19:16:14 +02003069 /* Sweex */
3070 { USB_DEVICE(0x177f, 0x0153), USB_DEVICE_DATA(&rt2800usb_ops) },
3071 { USB_DEVICE(0x177f, 0x0302), USB_DEVICE_DATA(&rt2800usb_ops) },
3072 { USB_DEVICE(0x177f, 0x0313), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02003073 /* U-Media*/
3074 { USB_DEVICE(0x157e, 0x300e), USB_DEVICE_DATA(&rt2800usb_ops) },
3075 /* ZCOM */
3076 { USB_DEVICE(0x0cde, 0x0022), USB_DEVICE_DATA(&rt2800usb_ops) },
3077 { USB_DEVICE(0x0cde, 0x0025), USB_DEVICE_DATA(&rt2800usb_ops) },
3078 /* Zinwell */
3079 { USB_DEVICE(0x5a57, 0x0280), USB_DEVICE_DATA(&rt2800usb_ops) },
3080 { USB_DEVICE(0x5a57, 0x0282), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornce2ebc92009-05-22 21:33:21 +02003081 { USB_DEVICE(0x5a57, 0x0283), USB_DEVICE_DATA(&rt2800usb_ops) },
3082 { USB_DEVICE(0x5a57, 0x5257), USB_DEVICE_DATA(&rt2800usb_ops) },
Ivo van Doornd53d9e62009-04-26 15:47:48 +02003083 /* Zyxel */
3084 { USB_DEVICE(0x0586, 0x3416), USB_DEVICE_DATA(&rt2800usb_ops) },
3085 { USB_DEVICE(0x0586, 0x341a), USB_DEVICE_DATA(&rt2800usb_ops) },
3086 { 0, }
3087};
3088
3089MODULE_AUTHOR(DRV_PROJECT);
3090MODULE_VERSION(DRV_VERSION);
3091MODULE_DESCRIPTION("Ralink RT2800 USB Wireless LAN driver.");
3092MODULE_SUPPORTED_DEVICE("Ralink RT2870 USB chipset based cards");
3093MODULE_DEVICE_TABLE(usb, rt2800usb_device_table);
3094MODULE_FIRMWARE(FIRMWARE_RT2870);
3095MODULE_LICENSE("GPL");
3096
3097static struct usb_driver rt2800usb_driver = {
3098 .name = KBUILD_MODNAME,
3099 .id_table = rt2800usb_device_table,
3100 .probe = rt2x00usb_probe,
3101 .disconnect = rt2x00usb_disconnect,
3102 .suspend = rt2x00usb_suspend,
3103 .resume = rt2x00usb_resume,
3104};
3105
3106static int __init rt2800usb_init(void)
3107{
3108 return usb_register(&rt2800usb_driver);
3109}
3110
3111static void __exit rt2800usb_exit(void)
3112{
3113 usb_deregister(&rt2800usb_driver);
3114}
3115
3116module_init(rt2800usb_init);
3117module_exit(rt2800usb_exit);