[package] mac80211: update compat-wireless to 2009-03-04, and refresh patches
[openwrt/svn-archive/archive.git] / package / mac80211 / patches / 303-rt2x00-Implement-support-for-rt2800usb.patch
1 From b249bc450f982cd2491448c91faf797ed37b69b8 Mon Sep 17 00:00:00 2001
2 From: Ivo van Doorn <IvDoorn@gmail.com>
3 Date: Tue, 3 Mar 2009 19:25:49 +0100
4 Subject: [PATCH] rt2x00: Implement support for rt2800usb
5
6 Add support for the rt2800usb chipset.
7
8 Includes various patches from Mattias, Felix, Xose and Axel.
9
10 Signed-off-by: Mattias Nissler <mattias.nissler@gmx.de>
11 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
12 Signed-off-by: Xose Vazquez Perez <xose.vazquez@gmail.com>
13 Signed-off-by: Axel Kollhofer <rain_maker@root-forum.org>
14 Signed-off-by: Ivo van Doorn <IvDoorn@gmail.com>
15 ---
16 drivers/net/wireless/rt2x00/Kconfig | 14 +
17 drivers/net/wireless/rt2x00/Makefile | 1 +
18 drivers/net/wireless/rt2x00/rt2800usb.c | 3032 +++++++++++++++++++++++++++++++
19 drivers/net/wireless/rt2x00/rt2800usb.h | 1932 ++++++++++++++++++++
20 drivers/net/wireless/rt2x00/rt2x00.h | 7 +
21 5 files changed, 4986 insertions(+), 0 deletions(-)
22 create mode 100644 drivers/net/wireless/rt2x00/rt2800usb.c
23 create mode 100644 drivers/net/wireless/rt2x00/rt2800usb.h
24
25 config RT2X00_LIB_PCI
26 tristate
27 select RT2X00_LIB
28 --- a/drivers/net/wireless/rt2x00/Makefile
29 +++ b/drivers/net/wireless/rt2x00/Makefile
30 @@ -19,3 +19,4 @@ obj-$(CONFIG_RT61PCI) += rt61pci.o
31 obj-$(CONFIG_RT2800PCI) += rt2800pci.o
32 obj-$(CONFIG_RT2500USB) += rt2500usb.o
33 obj-$(CONFIG_RT73USB) += rt73usb.o
34 +obj-$(CONFIG_RT2800USB) += rt2800usb.o
35 --- /dev/null
36 +++ b/drivers/net/wireless/rt2x00/rt2800usb.c
37 @@ -0,0 +1,3032 @@
38 +/*
39 + Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
40 + <http://rt2x00.serialmonkey.com>
41 +
42 + This program is free software; you can redistribute it and/or modify
43 + it under the terms of the GNU General Public License as published by
44 + the Free Software Foundation; either version 2 of the License, or
45 + (at your option) any later version.
46 +
47 + This program is distributed in the hope that it will be useful,
48 + but WITHOUT ANY WARRANTY; without even the implied warranty of
49 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 + GNU General Public License for more details.
51 +
52 + You should have received a copy of the GNU General Public License
53 + along with this program; if not, write to the
54 + Free Software Foundation, Inc.,
55 + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
56 + */
57 +
58 +/*
59 + Module: rt2800usb
60 + Abstract: rt2800usb device specific routines.
61 + Supported chipsets: RT2800U.
62 + */
63 +
64 +#include <linux/crc-ccitt.h>
65 +#include <linux/delay.h>
66 +#include <linux/etherdevice.h>
67 +#include <linux/init.h>
68 +#include <linux/kernel.h>
69 +#include <linux/module.h>
70 +#include <linux/usb.h>
71 +
72 +#include "rt2x00.h"
73 +#include "rt2x00usb.h"
74 +#include "rt2800usb.h"
75 +
76 +/*
77 + * Allow hardware encryption to be disabled.
78 + */
79 +static int modparam_nohwcrypt = 0;
80 +module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
81 +MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
82 +
83 +/*
84 + * Register access.
85 + * All access to the CSR registers will go through the methods
86 + * rt2x00usb_register_read and rt2x00usb_register_write.
87 + * BBP and RF register require indirect register access,
88 + * and use the CSR registers BBPCSR and RFCSR to achieve this.
89 + * These indirect registers work with busy bits,
90 + * and we will try maximal REGISTER_BUSY_COUNT times to access
91 + * the register while taking a REGISTER_BUSY_DELAY us delay
92 + * between each attampt. When the busy bit is still set at that time,
93 + * the access attempt is considered to have failed,
94 + * and we will print an error.
95 + * The _lock versions must be used if you already hold the csr_mutex
96 + */
97 +#define WAIT_FOR_BBP(__dev, __reg) \
98 + rt2x00usb_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
99 +#define WAIT_FOR_RFCSR(__dev, __reg) \
100 + rt2x00usb_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
101 +#define WAIT_FOR_RF(__dev, __reg) \
102 + rt2x00usb_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
103 +#define WAIT_FOR_MCU(__dev, __reg) \
104 + rt2x00usb_regbusy_read((__dev), H2M_MAILBOX_CSR, \
105 + H2M_MAILBOX_CSR_OWNER, (__reg))
106 +
107 +static void rt2800usb_bbp_write(struct rt2x00_dev *rt2x00dev,
108 + const unsigned int word, const u8 value)
109 +{
110 + u32 reg;
111 +
112 + mutex_lock(&rt2x00dev->csr_mutex);
113 +
114 + /*
115 + * Wait until the BBP becomes available, afterwards we
116 + * can safely write the new data into the register.
117 + */
118 + if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
119 + reg = 0;
120 + rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
121 + rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
122 + rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
123 + rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
124 +
125 + rt2x00usb_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
126 + }
127 +
128 + mutex_unlock(&rt2x00dev->csr_mutex);
129 +}
130 +
131 +static void rt2800usb_bbp_read(struct rt2x00_dev *rt2x00dev,
132 + const unsigned int word, u8 *value)
133 +{
134 + u32 reg;
135 +
136 + mutex_lock(&rt2x00dev->csr_mutex);
137 +
138 + /*
139 + * Wait until the BBP becomes available, afterwards we
140 + * can safely write the read request into the register.
141 + * After the data has been written, we wait until hardware
142 + * returns the correct value, if at any time the register
143 + * doesn't become available in time, reg will be 0xffffffff
144 + * which means we return 0xff to the caller.
145 + */
146 + if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
147 + reg = 0;
148 + rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
149 + rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
150 + rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
151 +
152 + rt2x00usb_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
153 +
154 + WAIT_FOR_BBP(rt2x00dev, &reg);
155 + }
156 +
157 + *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
158 +
159 + mutex_unlock(&rt2x00dev->csr_mutex);
160 +}
161 +
162 +static void rt2800usb_rfcsr_write(struct rt2x00_dev *rt2x00dev,
163 + const unsigned int word, const u8 value)
164 +{
165 + u32 reg;
166 +
167 + mutex_lock(&rt2x00dev->csr_mutex);
168 +
169 + /*
170 + * Wait until the RFCSR becomes available, afterwards we
171 + * can safely write the new data into the register.
172 + */
173 + if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
174 + reg = 0;
175 + rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
176 + rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
177 + rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
178 + rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
179 +
180 + rt2x00usb_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
181 + }
182 +
183 + mutex_unlock(&rt2x00dev->csr_mutex);
184 +}
185 +
186 +static void rt2800usb_rfcsr_read(struct rt2x00_dev *rt2x00dev,
187 + const unsigned int word, u8 *value)
188 +{
189 + u32 reg;
190 +
191 + mutex_lock(&rt2x00dev->csr_mutex);
192 +
193 + /*
194 + * Wait until the RFCSR becomes available, afterwards we
195 + * can safely write the read request into the register.
196 + * After the data has been written, we wait until hardware
197 + * returns the correct value, if at any time the register
198 + * doesn't become available in time, reg will be 0xffffffff
199 + * which means we return 0xff to the caller.
200 + */
201 + if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
202 + reg = 0;
203 + rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
204 + rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
205 + rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
206 +
207 + rt2x00usb_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
208 +
209 + WAIT_FOR_RFCSR(rt2x00dev, &reg);
210 + }
211 +
212 + *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
213 +
214 + mutex_unlock(&rt2x00dev->csr_mutex);
215 +}
216 +
217 +static void rt2800usb_rf_write(struct rt2x00_dev *rt2x00dev,
218 + const unsigned int word, const u32 value)
219 +{
220 + u32 reg;
221 +
222 + mutex_lock(&rt2x00dev->csr_mutex);
223 +
224 + /*
225 + * Wait until the RF becomes available, afterwards we
226 + * can safely write the new data into the register.
227 + */
228 + if (WAIT_FOR_RF(rt2x00dev, &reg)) {
229 + reg = 0;
230 + rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
231 + rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
232 + rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
233 + rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
234 +
235 + rt2x00usb_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
236 + rt2x00_rf_write(rt2x00dev, word, value);
237 + }
238 +
239 + mutex_unlock(&rt2x00dev->csr_mutex);
240 +}
241 +
242 +static void rt2800usb_mcu_request(struct rt2x00_dev *rt2x00dev,
243 + const u8 command, const u8 token,
244 + const u8 arg0, const u8 arg1)
245 +{
246 + u32 reg;
247 +
248 + mutex_lock(&rt2x00dev->csr_mutex);
249 +
250 + /*
251 + * Wait until the MCU becomes available, afterwards we
252 + * can safely write the new data into the register.
253 + */
254 + if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
255 + rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
256 + rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
257 + rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
258 + rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
259 + rt2x00usb_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
260 +
261 + reg = 0;
262 + rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
263 + rt2x00usb_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
264 + }
265 +
266 + mutex_unlock(&rt2x00dev->csr_mutex);
267 +}
268 +
269 +#ifdef CONFIG_RT2X00_LIB_DEBUGFS
270 +static const struct rt2x00debug rt2800usb_rt2x00debug = {
271 + .owner = THIS_MODULE,
272 + .csr = {
273 + .read = rt2x00usb_register_read,
274 + .write = rt2x00usb_register_write,
275 + .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 = {
288 + .read = rt2800usb_bbp_read,
289 + .write = rt2800usb_bbp_write,
290 + .word_base = BBP_BASE,
291 + .word_size = sizeof(u8),
292 + .word_count = BBP_SIZE / sizeof(u8),
293 + },
294 + .rf = {
295 + .read = rt2x00_rf_read,
296 + .write = rt2800usb_rf_write,
297 + .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 +
304 +#ifdef CONFIG_RT2X00_LIB_RFKILL
305 +static int rt2800usb_rfkill_poll(struct rt2x00_dev *rt2x00dev)
306 +{
307 + u32 reg;
308 +
309 + rt2x00usb_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
310 + return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
311 +}
312 +#else
313 +#define rt2800usb_rfkill_poll NULL
314 +#endif /* CONFIG_RT2X00_LIB_RFKILL */
315 +
316 +#ifdef CONFIG_RT2X00_LIB_LEDS
317 +static void rt2800usb_brightness_set(struct led_classdev *led_cdev,
318 + enum led_brightness brightness)
319 +{
320 + struct rt2x00_led *led =
321 + container_of(led_cdev, struct rt2x00_led, led_dev);
322 + unsigned int enabled = brightness != LED_OFF;
323 + unsigned int bg_mode =
324 + (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
325 + unsigned int polarity =
326 + rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
327 + EEPROM_FREQ_LED_POLARITY);
328 + unsigned int ledmode =
329 + rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
330 + EEPROM_FREQ_LED_MODE);
331 +
332 + if (led->type == LED_TYPE_RADIO) {
333 + rt2800usb_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
334 + enabled ? 0x20 : 0);
335 + } else if (led->type == LED_TYPE_ASSOC) {
336 + rt2800usb_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
337 + enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
338 + } else if (led->type == LED_TYPE_QUALITY) {
339 + /*
340 + * The brightness is divided into 6 levels (0 - 5),
341 + * The specs tell us the following levels:
342 + * 0, 1 ,3, 7, 15, 31
343 + * to determine the level in a simple way we can simply
344 + * work with bitshifting:
345 + * (1 << level) - 1
346 + */
347 + rt2800usb_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
348 + (1 << brightness / (LED_FULL / 6)) - 1,
349 + polarity);
350 + }
351 +}
352 +
353 +static int rt2800usb_blink_set(struct led_classdev *led_cdev,
354 + unsigned long *delay_on,
355 + unsigned long *delay_off)
356 +{
357 + struct rt2x00_led *led =
358 + container_of(led_cdev, struct rt2x00_led, led_dev);
359 + u32 reg;
360 +
361 + rt2x00usb_register_read(led->rt2x00dev, LED_CFG, &reg);
362 + rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
363 + rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
364 + rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
365 + rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
366 + rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 12);
367 + rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
368 + rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
369 + rt2x00usb_register_write(led->rt2x00dev, LED_CFG, reg);
370 +
371 + return 0;
372 +}
373 +
374 +static void rt2800usb_init_led(struct rt2x00_dev *rt2x00dev,
375 + struct rt2x00_led *led,
376 + enum led_type type)
377 +{
378 + led->rt2x00dev = rt2x00dev;
379 + led->type = type;
380 + led->led_dev.brightness_set = rt2800usb_brightness_set;
381 + led->led_dev.blink_set = rt2800usb_blink_set;
382 + led->flags = LED_INITIALIZED;
383 +}
384 +#endif /* CONFIG_RT2X00_LIB_LEDS */
385 +
386 +/*
387 + * Configuration handlers.
388 + */
389 +static void rt2800usb_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
390 + struct rt2x00lib_crypto *crypto,
391 + struct ieee80211_key_conf *key)
392 +{
393 + struct mac_wcid_entry wcid_entry;
394 + struct mac_iveiv_entry iveiv_entry;
395 + u32 offset;
396 + u32 reg;
397 +
398 + offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
399 +
400 + rt2x00usb_register_read(rt2x00dev, offset, &reg);
401 + rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
402 + !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
403 + rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER, crypto->cipher);
404 + rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
405 + (crypto->cmd == SET_KEY) * crypto->bssidx);
406 + rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, 0);
407 + rt2x00usb_register_write(rt2x00dev, offset, reg);
408 +
409 + offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
410 +
411 + memset(&iveiv_entry, 0, sizeof(iveiv_entry));
412 + if ((crypto->cipher == CIPHER_TKIP) ||
413 + (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
414 + (crypto->cipher == CIPHER_AES))
415 + iveiv_entry.iv[3] |= 0x20;
416 + iveiv_entry.iv[3] |= key->keyidx << 6;
417 + rt2x00usb_register_multiwrite(rt2x00dev, offset,
418 + &iveiv_entry, sizeof(iveiv_entry));
419 +
420 + offset = MAC_WCID_ENTRY(key->hw_key_idx);
421 +
422 + memset(&wcid_entry, 0, sizeof(wcid_entry));
423 + if (crypto->cmd == SET_KEY)
424 + memcpy(&wcid_entry, crypto->address, ETH_ALEN);
425 + rt2x00usb_register_multiwrite(rt2x00dev, offset,
426 + &wcid_entry, sizeof(wcid_entry));
427 +}
428 +
429 +static int rt2800usb_config_shared_key(struct rt2x00_dev *rt2x00dev,
430 + struct rt2x00lib_crypto *crypto,
431 + struct ieee80211_key_conf *key)
432 +{
433 + struct hw_key_entry key_entry;
434 + struct rt2x00_field32 field;
435 + int timeout;
436 + u32 offset;
437 + u32 reg;
438 +
439 + if (crypto->cmd == SET_KEY) {
440 + key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
441 +
442 + memcpy(key_entry.key, crypto->key,
443 + sizeof(key_entry.key));
444 + memcpy(key_entry.tx_mic, crypto->tx_mic,
445 + sizeof(key_entry.tx_mic));
446 + memcpy(key_entry.rx_mic, crypto->rx_mic,
447 + sizeof(key_entry.rx_mic));
448 +
449 + offset = SHARED_KEY_ENTRY(key->hw_key_idx);
450 + timeout = REGISTER_TIMEOUT32(sizeof(key_entry));
451 + rt2x00usb_vendor_request_large_buff(rt2x00dev, USB_MULTI_WRITE,
452 + USB_VENDOR_REQUEST_OUT,
453 + offset, &key_entry,
454 + sizeof(key_entry),
455 + timeout);
456 + }
457 +
458 + /*
459 + * The cipher types are stored over multiple registers
460 + * starting with SHARED_KEY_MODE_BASE each word will have
461 + * 32 bits and contains the cipher types for 2 bssidx each.
462 + * Using the correct defines correctly will cause overhead,
463 + * so just calculate the correct offset.
464 + */
465 + field.bit_offset = (4 * key->keyidx) + (16 * (crypto->bssidx & 1));
466 + field.bit_mask = 0x7 << field.bit_offset;
467 +
468 + offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 2);
469 +
470 + rt2x00usb_register_read(rt2x00dev, offset, &reg);
471 + rt2x00_set_field32(&reg, field,
472 + (crypto->cmd == SET_KEY) * crypto->cipher);
473 + rt2x00usb_register_write(rt2x00dev, offset, reg);
474 +
475 + /*
476 + * Update WCID information
477 + */
478 + rt2800usb_config_wcid_attr(rt2x00dev, crypto, key);
479 +
480 + return 0;
481 +}
482 +
483 +static int rt2800usb_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
484 + struct rt2x00lib_crypto *crypto,
485 + struct ieee80211_key_conf *key)
486 +{
487 + struct hw_key_entry key_entry;
488 + int timeout;
489 + u32 offset;
490 +
491 + if (crypto->cmd == SET_KEY) {
492 + /*
493 + * 1 pairwise key is possible per AID, this means that the AID
494 + * equals our hw_key_idx.
495 + */
496 + key->hw_key_idx = crypto->aid;
497 +
498 + memcpy(key_entry.key, crypto->key,
499 + sizeof(key_entry.key));
500 + memcpy(key_entry.tx_mic, crypto->tx_mic,
501 + sizeof(key_entry.tx_mic));
502 + memcpy(key_entry.rx_mic, crypto->rx_mic,
503 + sizeof(key_entry.rx_mic));
504 +
505 + offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
506 + timeout = REGISTER_TIMEOUT32(sizeof(key_entry));
507 + rt2x00usb_vendor_request_large_buff(rt2x00dev, USB_MULTI_WRITE,
508 + USB_VENDOR_REQUEST_OUT,
509 + offset, &key_entry,
510 + sizeof(key_entry),
511 + timeout);
512 + }
513 +
514 + /*
515 + * Update WCID information
516 + */
517 + rt2800usb_config_wcid_attr(rt2x00dev, crypto, key);
518 +
519 + return 0;
520 +}
521 +
522 +static void rt2800usb_config_filter(struct rt2x00_dev *rt2x00dev,
523 + const unsigned int filter_flags)
524 +{
525 + u32 reg;
526 +
527 + /*
528 + * Start configuration steps.
529 + * Note that the version error will always be dropped
530 + * and broadcast frames will always be accepted since
531 + * there is no filter for it at this time.
532 + */
533 + rt2x00usb_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
534 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
535 + !(filter_flags & FIF_FCSFAIL));
536 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
537 + !(filter_flags & FIF_PLCPFAIL));
538 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
539 + !(filter_flags & FIF_PROMISC_IN_BSS));
540 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
541 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
542 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
543 + !(filter_flags & FIF_ALLMULTI));
544 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
545 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
546 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
547 + !(filter_flags & FIF_CONTROL));
548 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
549 + !(filter_flags & FIF_CONTROL));
550 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
551 + !(filter_flags & FIF_CONTROL));
552 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
553 + !(filter_flags & FIF_CONTROL));
554 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
555 + !(filter_flags & FIF_CONTROL));
556 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
557 + !(filter_flags & FIF_CONTROL));
558 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
559 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
560 + rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
561 + !(filter_flags & FIF_CONTROL));
562 + rt2x00usb_register_write(rt2x00dev, RX_FILTER_CFG, reg);
563 +}
564 +
565 +static void rt2800usb_config_intf(struct rt2x00_dev *rt2x00dev,
566 + struct rt2x00_intf *intf,
567 + struct rt2x00intf_conf *conf,
568 + const unsigned int flags)
569 +{
570 + unsigned int beacon_base;
571 + u32 reg;
572 +
573 + if (flags & CONFIG_UPDATE_TYPE) {
574 + /*
575 + * Clear current synchronisation setup.
576 + * For the Beacon base registers we only need to clear
577 + * the first byte since that byte contains the VALID and OWNER
578 + * bits which (when set to 0) will invalidate the entire beacon.
579 + */
580 + beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
581 + rt2x00usb_register_write(rt2x00dev, beacon_base, 0);
582 +
583 + /*
584 + * Enable synchronisation.
585 + */
586 + rt2x00usb_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
587 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
588 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
589 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
590 + rt2x00usb_register_write(rt2x00dev, BCN_TIME_CFG, reg);
591 + }
592 +
593 + if (flags & CONFIG_UPDATE_MAC) {
594 + reg = le32_to_cpu(conf->mac[1]);
595 + rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
596 + conf->mac[1] = cpu_to_le32(reg);
597 +
598 + rt2x00usb_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
599 + conf->mac, sizeof(conf->mac));
600 + }
601 +
602 + if (flags & CONFIG_UPDATE_BSSID) {
603 + reg = le32_to_cpu(conf->bssid[1]);
604 + rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 0);
605 + rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
606 + conf->bssid[1] = cpu_to_le32(reg);
607 +
608 + rt2x00usb_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
609 + conf->bssid, sizeof(conf->bssid));
610 + }
611 +}
612 +
613 +static void rt2800usb_config_erp(struct rt2x00_dev *rt2x00dev,
614 + struct rt2x00lib_erp *erp)
615 +{
616 + u32 reg;
617 +
618 + rt2x00usb_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
619 + rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT,
620 + DIV_ROUND_UP(erp->ack_timeout, erp->slot_time));
621 + rt2x00usb_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
622 +
623 + rt2x00usb_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
624 + rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
625 + !!erp->short_preamble);
626 + rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
627 + !!erp->short_preamble);
628 + rt2x00usb_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
629 +
630 + rt2x00usb_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
631 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
632 + erp->cts_protection ? 2 : 0);
633 + rt2x00usb_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
634 +
635 + rt2x00usb_register_write(rt2x00dev, LEGACY_BASIC_RATE,
636 + erp->basic_rates);
637 + rt2x00usb_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
638 +
639 + rt2x00usb_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
640 + rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
641 + rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
642 + rt2x00usb_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
643 +
644 + rt2x00usb_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
645 + rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, erp->sifs);
646 + rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, erp->sifs);
647 + rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
648 + rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
649 + rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
650 + rt2x00usb_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
651 +}
652 +
653 +static void rt2800usb_config_ant(struct rt2x00_dev *rt2x00dev,
654 + struct antenna_setup *ant)
655 +{
656 + u16 eeprom;
657 + u8 r1;
658 + u8 r3;
659 +
660 + /*
661 + * FIXME: Use requested antenna configuration.
662 + */
663 +
664 + rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
665 +
666 + rt2800usb_bbp_read(rt2x00dev, 1, &r1);
667 + rt2800usb_bbp_read(rt2x00dev, 3, &r3);
668 +
669 + /*
670 + * Configure the TX antenna.
671 + */
672 + switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH)) {
673 + case 1:
674 + rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
675 + break;
676 + case 2:
677 + rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
678 + break;
679 + case 3:
680 + /* Do nothing */
681 + break;
682 + }
683 +
684 + /*
685 + * Configure the RX antenna.
686 + */
687 + switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
688 + case 1:
689 + rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
690 + break;
691 + case 2:
692 + rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
693 + break;
694 + case 3:
695 + rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
696 + break;
697 + }
698 +
699 + rt2800usb_bbp_write(rt2x00dev, 3, r3);
700 + rt2800usb_bbp_write(rt2x00dev, 1, r1);
701 +}
702 +
703 +static void rt2800usb_config_lna_gain(struct rt2x00_dev *rt2x00dev,
704 + struct rt2x00lib_conf *libconf)
705 +{
706 + u16 eeprom;
707 + short lna_gain;
708 +
709 + if (libconf->rf.channel <= 14) {
710 + rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
711 + lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
712 + } else if (libconf->rf.channel <= 64) {
713 + rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
714 + lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
715 + } else if (libconf->rf.channel <= 128) {
716 + rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
717 + lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
718 + } else {
719 + rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
720 + lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
721 + }
722 +
723 + rt2x00dev->lna_gain = lna_gain;
724 +}
725 +
726 +static void rt2800usb_config_channel_rt2x(struct rt2x00_dev *rt2x00dev,
727 + struct ieee80211_conf *conf,
728 + struct rf_channel *rf,
729 + struct channel_info *info)
730 +{
731 + u16 eeprom;
732 +
733 + rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
734 +
735 + /*
736 + * Determine antenna settings from EEPROM
737 + */
738 + rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
739 +
740 + if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) == 1)
741 + rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
742 +
743 + if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 1) {
744 + rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
745 + rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
746 + } else if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) == 2)
747 + rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
748 +
749 + if (rf->channel > 14) {
750 + /*
751 + * When TX power is below 0, we should increase it by 7 to
752 + * make it a positive value (Minumum value is -7).
753 + * However this means that values between 0 and 7 have
754 + * double meaning, and we should set a 7DBm boost flag.
755 + */
756 + rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
757 + (info->tx_power1 >= 0));
758 +
759 + if (info->tx_power1 < 0)
760 + info->tx_power1 += 7;
761 +
762 + rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
763 + TXPOWER_A_TO_DEV(info->tx_power1));
764 +
765 + rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
766 + (info->tx_power2 >= 0));
767 +
768 + if (info->tx_power2 < 0)
769 + info->tx_power2 += 7;
770 +
771 + rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
772 + TXPOWER_A_TO_DEV(info->tx_power2));
773 + } else {
774 + rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
775 + TXPOWER_G_TO_DEV(info->tx_power1));
776 + rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
777 + TXPOWER_G_TO_DEV(info->tx_power2));
778 + }
779 +
780 + rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
781 +
782 + rt2800usb_rf_write(rt2x00dev, 1, rf->rf1);
783 + rt2800usb_rf_write(rt2x00dev, 2, rf->rf2);
784 + rt2800usb_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
785 + rt2800usb_rf_write(rt2x00dev, 4, rf->rf4);
786 +
787 + udelay(200);
788 +
789 + rt2800usb_rf_write(rt2x00dev, 1, rf->rf1);
790 + rt2800usb_rf_write(rt2x00dev, 2, rf->rf2);
791 + rt2800usb_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
792 + rt2800usb_rf_write(rt2x00dev, 4, rf->rf4);
793 +
794 + udelay(200);
795 +
796 + rt2800usb_rf_write(rt2x00dev, 1, rf->rf1);
797 + rt2800usb_rf_write(rt2x00dev, 2, rf->rf2);
798 + rt2800usb_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
799 + rt2800usb_rf_write(rt2x00dev, 4, rf->rf4);
800 +}
801 +
802 +static void rt2800usb_config_channel_rt3x(struct rt2x00_dev *rt2x00dev,
803 + struct ieee80211_conf *conf,
804 + struct rf_channel *rf,
805 + struct channel_info *info)
806 +{
807 + u8 rfcsr;
808 +
809 + rt2800usb_rfcsr_write(rt2x00dev, 2, rf->rf1);
810 + rt2800usb_rfcsr_write(rt2x00dev, 2, rf->rf3);
811 +
812 + rt2800usb_rfcsr_read(rt2x00dev, 6, &rfcsr);
813 + rt2x00_set_field8(&rfcsr, RFCSR6_R, rf->rf2);
814 + rt2800usb_rfcsr_write(rt2x00dev, 6, rfcsr);
815 +
816 + rt2800usb_rfcsr_read(rt2x00dev, 12, &rfcsr);
817 + rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
818 + TXPOWER_G_TO_DEV(info->tx_power1));
819 + rt2800usb_rfcsr_write(rt2x00dev, 12, rfcsr);
820 +
821 + rt2800usb_rfcsr_read(rt2x00dev, 23, &rfcsr);
822 + rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
823 + rt2800usb_rfcsr_write(rt2x00dev, 23, rfcsr);
824 +
825 + if (conf_is_ht40(conf))
826 + rt2800usb_rfcsr_write(rt2x00dev, 24,
827 + rt2x00dev->calibration_bw40);
828 + else
829 + rt2800usb_rfcsr_write(rt2x00dev, 24,
830 + rt2x00dev->calibration_bw20);
831 +
832 + rt2800usb_rfcsr_read(rt2x00dev, 23, &rfcsr);
833 + rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
834 + rt2800usb_rfcsr_write(rt2x00dev, 23, rfcsr);
835 +}
836 +
837 +static void rt2800usb_config_channel(struct rt2x00_dev *rt2x00dev,
838 + struct ieee80211_conf *conf,
839 + struct rf_channel *rf,
840 + struct channel_info *info)
841 +{
842 + u32 reg;
843 + unsigned int tx_pin;
844 + u16 eeprom;
845 + u8 bbp;
846 +
847 + /*
848 + * Determine antenna settings from EEPROM
849 + */
850 + rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
851 +
852 + if (rt2x00_rev(&rt2x00dev->chip) != RT3070_VERSION)
853 + rt2800usb_config_channel_rt2x(rt2x00dev, conf, rf, info);
854 + else
855 + rt2800usb_config_channel_rt3x(rt2x00dev, conf, rf, info);
856 +
857 + /*
858 + * Change BBP settings
859 + */
860 + rt2800usb_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
861 + rt2800usb_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
862 + rt2800usb_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
863 + rt2800usb_bbp_write(rt2x00dev, 86, 0);
864 +
865 + if (rf->channel <= 14) {
866 + if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
867 + rt2800usb_bbp_write(rt2x00dev, 82, 0x62);
868 + rt2800usb_bbp_write(rt2x00dev, 75, 0x46);
869 + } else {
870 + rt2800usb_bbp_write(rt2x00dev, 82, 0x84);
871 + rt2800usb_bbp_write(rt2x00dev, 75, 0x50);
872 + }
873 + } else {
874 + rt2800usb_bbp_write(rt2x00dev, 82, 0xf2);
875 +
876 + if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
877 + rt2800usb_bbp_write(rt2x00dev, 75, 0x46);
878 + else
879 + rt2800usb_bbp_write(rt2x00dev, 75, 0x50);
880 + }
881 +
882 + rt2x00usb_register_read(rt2x00dev, TX_BAND_CFG, &reg);
883 + rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_PLUS, conf_is_ht40_plus(conf));
884 + rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
885 + rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
886 + rt2x00usb_register_write(rt2x00dev, TX_BAND_CFG, reg);
887 +
888 + tx_pin = 0;
889 +
890 + /* Turn on unused PA or LNA when not using 1T or 1R */
891 + if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) != 1) {
892 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
893 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
894 + }
895 +
896 + /* Turn on unused PA or LNA when not using 1T or 1R */
897 + if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH) != 1) {
898 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
899 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
900 + }
901 +
902 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
903 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
904 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
905 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
906 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
907 + rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
908 +
909 + rt2x00usb_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
910 +
911 + rt2800usb_bbp_read(rt2x00dev, 4, &bbp);
912 + rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
913 + rt2800usb_bbp_write(rt2x00dev, 4, bbp);
914 +
915 + rt2800usb_bbp_read(rt2x00dev, 3, &bbp);
916 + rt2x00_set_field8(&bbp, BBP3_HT40_PLUS, conf_is_ht40_plus(conf));
917 + rt2800usb_bbp_write(rt2x00dev, 3, bbp);
918 +
919 + if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
920 + if (conf_is_ht40(conf)) {
921 + rt2800usb_bbp_write(rt2x00dev, 69, 0x1a);
922 + rt2800usb_bbp_write(rt2x00dev, 70, 0x0a);
923 + rt2800usb_bbp_write(rt2x00dev, 73, 0x16);
924 + } else {
925 + rt2800usb_bbp_write(rt2x00dev, 69, 0x16);
926 + rt2800usb_bbp_write(rt2x00dev, 70, 0x08);
927 + rt2800usb_bbp_write(rt2x00dev, 73, 0x11);
928 + }
929 + }
930 +
931 + msleep(1);
932 +}
933 +
934 +static void rt2800usb_config_txpower(struct rt2x00_dev *rt2x00dev,
935 + const int txpower)
936 +{
937 + u32 reg;
938 + u32 value = TXPOWER_G_TO_DEV(txpower);
939 + u8 r1;
940 +
941 + rt2800usb_bbp_read(rt2x00dev, 1, &r1);
942 + rt2x00_set_field8(&reg, BBP1_TX_POWER, 0);
943 + rt2800usb_bbp_write(rt2x00dev, 1, r1);
944 +
945 + rt2x00usb_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
946 + rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
947 + rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
948 + rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
949 + rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
950 + rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
951 + rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
952 + rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
953 + rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
954 + rt2x00usb_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
955 +
956 + rt2x00usb_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
957 + rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
958 + rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
959 + rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
960 + rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
961 + rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
962 + rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
963 + rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
964 + rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
965 + rt2x00usb_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
966 +
967 + rt2x00usb_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
968 + rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
969 + rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
970 + rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
971 + rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
972 + rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
973 + rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
974 + rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
975 + rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
976 + rt2x00usb_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
977 +
978 + rt2x00usb_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
979 + rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
980 + rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
981 + rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
982 + rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
983 + rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
984 + rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
985 + rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
986 + rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
987 + rt2x00usb_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
988 +
989 + rt2x00usb_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
990 + rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
991 + rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
992 + rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
993 + rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
994 + rt2x00usb_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
995 +}
996 +
997 +static void rt2800usb_config_retry_limit(struct rt2x00_dev *rt2x00dev,
998 + struct rt2x00lib_conf *libconf)
999 +{
1000 + u32 reg;
1001 +
1002 + rt2x00usb_register_read(rt2x00dev, TX_RTY_CFG, &reg);
1003 + rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
1004 + libconf->conf->short_frame_max_tx_count);
1005 + rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
1006 + libconf->conf->long_frame_max_tx_count);
1007 + rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
1008 + rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
1009 + rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
1010 + rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
1011 + rt2x00usb_register_write(rt2x00dev, TX_RTY_CFG, reg);
1012 +}
1013 +
1014 +static void rt2800usb_config_duration(struct rt2x00_dev *rt2x00dev,
1015 + struct rt2x00lib_conf *libconf)
1016 +{
1017 + u32 reg;
1018 +
1019 + rt2x00usb_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1020 + rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
1021 + libconf->conf->beacon_int * 16);
1022 + rt2x00usb_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1023 +}
1024 +
1025 +static void rt2800usb_config_ps(struct rt2x00_dev *rt2x00dev,
1026 + struct rt2x00lib_conf *libconf)
1027 +{
1028 + enum dev_state state =
1029 + (libconf->conf->flags & IEEE80211_CONF_PS) ?
1030 + STATE_SLEEP : STATE_AWAKE;
1031 + u32 reg;
1032 +
1033 + if (state == STATE_SLEEP) {
1034 + rt2x00usb_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1035 +
1036 + rt2x00usb_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1037 + rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
1038 + rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
1039 + libconf->conf->listen_interval - 1);
1040 + rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
1041 + rt2x00usb_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1042 +
1043 + rt2800usb_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 0);
1044 + } else {
1045 + rt2800usb_mcu_request(rt2x00dev, MCU_WAKEUP, 0xff, 0, 0);
1046 +
1047 + rt2x00usb_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
1048 + rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
1049 + rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
1050 + rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
1051 + rt2x00usb_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
1052 + }
1053 +}
1054 +
1055 +static void rt2800usb_config(struct rt2x00_dev *rt2x00dev,
1056 + struct rt2x00lib_conf *libconf,
1057 + const unsigned int flags)
1058 +{
1059 + /* Always recalculate LNA gain before changing configuration */
1060 + rt2800usb_config_lna_gain(rt2x00dev, libconf);
1061 +
1062 + if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
1063 + rt2800usb_config_channel(rt2x00dev, libconf->conf,
1064 + &libconf->rf, &libconf->channel);
1065 + if (flags & IEEE80211_CONF_CHANGE_POWER)
1066 + rt2800usb_config_txpower(rt2x00dev, libconf->conf->power_level);
1067 + if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
1068 + rt2800usb_config_retry_limit(rt2x00dev, libconf);
1069 + if (flags & IEEE80211_CONF_CHANGE_BEACON_INTERVAL)
1070 + rt2800usb_config_duration(rt2x00dev, libconf);
1071 + if (flags & IEEE80211_CONF_CHANGE_PS)
1072 + rt2800usb_config_ps(rt2x00dev, libconf);
1073 +}
1074 +
1075 +/*
1076 + * Link tuning
1077 + */
1078 +static void rt2800usb_link_stats(struct rt2x00_dev *rt2x00dev,
1079 + struct link_qual *qual)
1080 +{
1081 + u32 reg;
1082 +
1083 + /*
1084 + * Update FCS error count from register.
1085 + */
1086 + rt2x00usb_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1087 + qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
1088 +}
1089 +
1090 +static u8 rt2800usb_get_default_vgc(struct rt2x00_dev *rt2x00dev)
1091 +{
1092 + if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ) {
1093 + if (rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION)
1094 + return 0x1c + (2 * rt2x00dev->lna_gain);
1095 + else
1096 + return 0x2e + rt2x00dev->lna_gain;
1097 + }
1098 +
1099 + if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
1100 + return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
1101 + else
1102 + return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
1103 +}
1104 +
1105 +static inline void rt2800usb_set_vgc(struct rt2x00_dev *rt2x00dev,
1106 + struct link_qual *qual, u8 vgc_level)
1107 +{
1108 + if (qual->vgc_level != vgc_level) {
1109 + rt2800usb_bbp_write(rt2x00dev, 66, vgc_level);
1110 + qual->vgc_level = vgc_level;
1111 + qual->vgc_level_reg = vgc_level;
1112 + }
1113 +}
1114 +
1115 +static void rt2800usb_reset_tuner(struct rt2x00_dev *rt2x00dev,
1116 + struct link_qual *qual)
1117 +{
1118 + rt2800usb_set_vgc(rt2x00dev, qual,
1119 + rt2800usb_get_default_vgc(rt2x00dev));
1120 +}
1121 +
1122 +static void rt2800usb_link_tuner(struct rt2x00_dev *rt2x00dev,
1123 + struct link_qual *qual, const u32 count)
1124 +{
1125 + if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION)
1126 + return;
1127 +
1128 + /*
1129 + * When RSSI is better then -80 increase VGC level with 0x10
1130 + */
1131 + rt2800usb_set_vgc(rt2x00dev, qual,
1132 + rt2800usb_get_default_vgc(rt2x00dev) +
1133 + ((qual->rssi > -80) * 0x10));
1134 +}
1135 +
1136 +/*
1137 + * Firmware functions
1138 + */
1139 +static char *rt2800usb_get_firmware_name(struct rt2x00_dev *rt2x00dev)
1140 +{
1141 + return FIRMWARE_RT2870;
1142 +}
1143 +
1144 +static bool rt2800usb_check_crc(const u8 *data, const size_t len)
1145 +{
1146 + u16 fw_crc;
1147 + u16 crc;
1148 +
1149 + /*
1150 + * The last 2 bytes in the firmware array are the crc checksum itself,
1151 + * this means that we should never pass those 2 bytes to the crc
1152 + * algorithm.
1153 + */
1154 + fw_crc = (data[len - 2] << 8 | data[len - 1]);
1155 +
1156 + /*
1157 + * Use the crc ccitt algorithm.
1158 + * This will return the same value as the legacy driver which
1159 + * used bit ordering reversion on the both the firmware bytes
1160 + * before input input as well as on the final output.
1161 + * Obviously using crc ccitt directly is much more efficient.
1162 + */
1163 + crc = crc_ccitt(~0, data, len - 2);
1164 +
1165 + /*
1166 + * There is a small difference between the crc-itu-t + bitrev and
1167 + * the crc-ccitt crc calculation. In the latter method the 2 bytes
1168 + * will be swapped, use swab16 to convert the crc to the correct
1169 + * value.
1170 + */
1171 + crc = swab16(crc);
1172 +
1173 + return fw_crc == crc;
1174 +}
1175 +
1176 +static int rt2800usb_check_firmware(struct rt2x00_dev *rt2x00dev,
1177 + const u8 *data, const size_t len)
1178 +{
1179 + u16 chipset = (rt2x00_rev(&rt2x00dev->chip) >> 16) & 0xffff;
1180 + size_t offset = 0;
1181 +
1182 + /*
1183 + * Firmware files:
1184 + * There are 2 variations of the rt2870 firmware.
1185 + * a) size: 4kb
1186 + * b) size: 8kb
1187 + * Note that (b) contains 2 seperate firmware blobs of 4k
1188 + * within the file. The first blob is the same firmware as (a),
1189 + * but the second blob is for the additional chipsets.
1190 + */
1191 + if (len != 4096 && len != 8192)
1192 + return FW_BAD_LENGTH;
1193 +
1194 + /*
1195 + * Check if we need the upper 4kb firmware data or not.
1196 + */
1197 + if ((len == 4096) &&
1198 + (chipset != 0x2860) &&
1199 + (chipset != 0x2872) &&
1200 + (chipset != 0x3070))
1201 + return FW_BAD_VERSION;
1202 +
1203 + /*
1204 + * 8kb firmware files must be checked as if it were
1205 + * 2 seperate firmware files.
1206 + */
1207 + while (offset < len) {
1208 + if (!rt2800usb_check_crc(data + offset, 4096))
1209 + return FW_BAD_CRC;
1210 +
1211 + offset += 4096;
1212 + }
1213 +
1214 + return FW_OK;
1215 +}
1216 +
1217 +static int rt2800usb_load_firmware(struct rt2x00_dev *rt2x00dev,
1218 + const u8 *data, const size_t len)
1219 +{
1220 + unsigned int i;
1221 + int status;
1222 + u32 reg;
1223 + u32 offset;
1224 + u32 length;
1225 + u16 chipset = (rt2x00_rev(&rt2x00dev->chip) >> 16) & 0xffff;
1226 +
1227 + /*
1228 + * Check which section of the firmware we need.
1229 + */
1230 + if ((chipset == 0x2860) || (chipset == 0x2872) || (chipset == 0x3070)) {
1231 + offset = 0;
1232 + length = 4096;
1233 + } else {
1234 + offset = 4096;
1235 + length = 4096;
1236 + }
1237 +
1238 + /*
1239 + * Wait for stable hardware.
1240 + */
1241 + for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1242 + rt2x00usb_register_read(rt2x00dev, MAC_CSR0, &reg);
1243 + if (reg && reg != ~0)
1244 + break;
1245 + msleep(1);
1246 + }
1247 +
1248 + if (i == REGISTER_BUSY_COUNT) {
1249 + ERROR(rt2x00dev, "Unstable hardware.\n");
1250 + return -EBUSY;
1251 + }
1252 +
1253 + /*
1254 + * Write firmware to device.
1255 + */
1256 + rt2x00usb_vendor_request_large_buff(rt2x00dev, USB_MULTI_WRITE,
1257 + USB_VENDOR_REQUEST_OUT,
1258 + FIRMWARE_IMAGE_BASE,
1259 + data + offset, length,
1260 + REGISTER_TIMEOUT32(length));
1261 +
1262 + rt2x00usb_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
1263 + rt2x00usb_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
1264 +
1265 + /*
1266 + * Send firmware request to device to load firmware,
1267 + * we need to specify a long timeout time.
1268 + */
1269 + status = rt2x00usb_vendor_request_sw(rt2x00dev, USB_DEVICE_MODE,
1270 + 0, USB_MODE_FIRMWARE,
1271 + REGISTER_TIMEOUT_FIRMWARE);
1272 + if (status < 0) {
1273 + ERROR(rt2x00dev, "Failed to write Firmware to device.\n");
1274 + return status;
1275 + }
1276 +
1277 + /*
1278 + * Wait for device to stabilize.
1279 + */
1280 + for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1281 + rt2x00usb_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
1282 + if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
1283 + break;
1284 + msleep(1);
1285 + }
1286 +
1287 + if (i == REGISTER_BUSY_COUNT) {
1288 + ERROR(rt2x00dev, "PBF system register not ready.\n");
1289 + return -EBUSY;
1290 + }
1291 +
1292 + /*
1293 + * Initialize firmware.
1294 + */
1295 + rt2x00usb_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
1296 + rt2x00usb_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
1297 + msleep(1);
1298 +
1299 + return 0;
1300 +}
1301 +
1302 +/*
1303 + * Initialization functions.
1304 + */
1305 +static int rt2800usb_init_registers(struct rt2x00_dev *rt2x00dev)
1306 +{
1307 + u32 reg;
1308 + unsigned int i;
1309 +
1310 + /*
1311 + * Wait untill BBP and RF are ready.
1312 + */
1313 + for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1314 + rt2x00usb_register_read(rt2x00dev, MAC_CSR0, &reg);
1315 + if (reg && reg != ~0)
1316 + break;
1317 + msleep(1);
1318 + }
1319 +
1320 + if (i == REGISTER_BUSY_COUNT) {
1321 + ERROR(rt2x00dev, "Unstable hardware.\n");
1322 + return -EBUSY;
1323 + }
1324 +
1325 + rt2x00usb_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
1326 + rt2x00usb_register_write(rt2x00dev, PBF_SYS_CTRL, reg & ~0x00002000);
1327 +
1328 + rt2x00usb_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
1329 + rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_CSR, 1);
1330 + rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_BBP, 1);
1331 + rt2x00usb_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
1332 +
1333 + rt2x00usb_register_write(rt2x00dev, USB_DMA_CFG, 0x00000000);
1334 +
1335 + rt2x00usb_vendor_request_sw(rt2x00dev, USB_DEVICE_MODE, 0,
1336 + USB_MODE_RESET, REGISTER_TIMEOUT);
1337 +
1338 + rt2x00usb_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1339 +
1340 + rt2x00usb_register_read(rt2x00dev, BCN_OFFSET0, &reg);
1341 + rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
1342 + rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
1343 + rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
1344 + rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
1345 + rt2x00usb_register_write(rt2x00dev, BCN_OFFSET0, reg);
1346 +
1347 + rt2x00usb_register_read(rt2x00dev, BCN_OFFSET1, &reg);
1348 + rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
1349 + rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
1350 + rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
1351 + rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
1352 + rt2x00usb_register_write(rt2x00dev, BCN_OFFSET1, reg);
1353 +
1354 + rt2x00usb_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
1355 + rt2x00usb_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
1356 +
1357 + rt2x00usb_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
1358 +
1359 + rt2x00usb_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
1360 + rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
1361 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
1362 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
1363 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
1364 + rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
1365 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
1366 + rt2x00usb_register_write(rt2x00dev, BCN_TIME_CFG, reg);
1367 +
1368 + if (rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION) {
1369 + rt2x00usb_register_write(rt2x00dev, TX_SW_CFG0, 0x00000400);
1370 + rt2x00usb_register_write(rt2x00dev, TX_SW_CFG1, 0x00000000);
1371 + rt2x00usb_register_write(rt2x00dev, TX_SW_CFG2, 0x00000000);
1372 + } else {
1373 + rt2x00usb_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
1374 + rt2x00usb_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
1375 + }
1376 +
1377 + rt2x00usb_register_read(rt2x00dev, TX_LINK_CFG, &reg);
1378 + rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
1379 + rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
1380 + rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
1381 + rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
1382 + rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
1383 + rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
1384 + rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
1385 + rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
1386 + rt2x00usb_register_write(rt2x00dev, TX_LINK_CFG, reg);
1387 +
1388 + rt2x00usb_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
1389 + rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
1390 + rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
1391 + rt2x00usb_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
1392 +
1393 + rt2x00usb_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
1394 + rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
1395 + if (rt2x00_rev(&rt2x00dev->chip) >= RT2880E_VERSION &&
1396 + rt2x00_rev(&rt2x00dev->chip) < RT3070_VERSION)
1397 + rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
1398 + else
1399 + rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
1400 + rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
1401 + rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
1402 + rt2x00usb_register_write(rt2x00dev, MAX_LEN_CFG, reg);
1403 +
1404 + rt2x00usb_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
1405 +
1406 + rt2x00usb_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
1407 + rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
1408 + rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
1409 + rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
1410 + rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
1411 + rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
1412 + rt2x00usb_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
1413 +
1414 + rt2x00usb_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
1415 + rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 8);
1416 + rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
1417 + rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
1418 + rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1419 + rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1420 + rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1421 + rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1422 + rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1423 + rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 1);
1424 + rt2x00usb_register_write(rt2x00dev, CCK_PROT_CFG, reg);
1425 +
1426 + rt2x00usb_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
1427 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 8);
1428 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
1429 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
1430 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1431 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1432 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1433 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1434 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1435 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 1);
1436 + rt2x00usb_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
1437 +
1438 + rt2x00usb_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
1439 + rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
1440 + rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
1441 + rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
1442 + rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1443 + rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1444 + rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1445 + rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1446 + rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1447 + rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1448 + rt2x00usb_register_write(rt2x00dev, MM20_PROT_CFG, reg);
1449 +
1450 + rt2x00usb_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
1451 + rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
1452 + rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
1453 + rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
1454 + rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1455 + rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1456 + rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1457 + rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1458 + rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1459 + rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
1460 + rt2x00usb_register_write(rt2x00dev, MM40_PROT_CFG, reg);
1461 +
1462 + rt2x00usb_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
1463 + rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
1464 + rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
1465 + rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
1466 + rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1467 + rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1468 + rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1469 + rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
1470 + rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1471 + rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
1472 + rt2x00usb_register_write(rt2x00dev, GF20_PROT_CFG, reg);
1473 +
1474 + rt2x00usb_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
1475 + rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
1476 + rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
1477 + rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
1478 + rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
1479 + rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
1480 + rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
1481 + rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
1482 + rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
1483 + rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
1484 + rt2x00usb_register_write(rt2x00dev, GF40_PROT_CFG, reg);
1485 +
1486 + rt2x00usb_register_write(rt2x00dev, PBF_CFG, 0xf40006);
1487 +
1488 + rt2x00usb_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1489 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1490 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
1491 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1492 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
1493 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 3);
1494 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 0);
1495 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_BIG_ENDIAN, 0);
1496 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_HDR_SCATTER, 0);
1497 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_HDR_SEG_LEN, 0);
1498 + rt2x00usb_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1499 +
1500 + rt2x00usb_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
1501 + rt2x00usb_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
1502 +
1503 + rt2x00usb_register_read(rt2x00dev, TX_RTS_CFG, &reg);
1504 + rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
1505 + rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
1506 + IEEE80211_MAX_RTS_THRESHOLD);
1507 + rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
1508 + rt2x00usb_register_write(rt2x00dev, TX_RTS_CFG, reg);
1509 +
1510 + rt2x00usb_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
1511 + rt2x00usb_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
1512 +
1513 + /*
1514 + * ASIC will keep garbage value after boot, clear encryption keys.
1515 + */
1516 + for (i = 0; i < 256; i++) {
1517 + u32 wcid[2] = { 0xffffffff, 0x00ffffff };
1518 + rt2x00usb_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
1519 + wcid, sizeof(wcid));
1520 +
1521 + rt2x00usb_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
1522 + rt2x00usb_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
1523 + }
1524 +
1525 + for (i = 0; i < 16; i++)
1526 + rt2x00usb_register_write(rt2x00dev,
1527 + SHARED_KEY_MODE_ENTRY(i), 0);
1528 +
1529 + /*
1530 + * Clear all beacons
1531 + * For the Beacon base registers we only need to clear
1532 + * the first byte since that byte contains the VALID and OWNER
1533 + * bits which (when set to 0) will invalidate the entire beacon.
1534 + */
1535 + rt2x00usb_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
1536 + rt2x00usb_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
1537 + rt2x00usb_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
1538 + rt2x00usb_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
1539 + rt2x00usb_register_write(rt2x00dev, HW_BEACON_BASE4, 0);
1540 + rt2x00usb_register_write(rt2x00dev, HW_BEACON_BASE5, 0);
1541 + rt2x00usb_register_write(rt2x00dev, HW_BEACON_BASE6, 0);
1542 + rt2x00usb_register_write(rt2x00dev, HW_BEACON_BASE7, 0);
1543 +
1544 + rt2x00usb_register_read(rt2x00dev, USB_CYC_CFG, &reg);
1545 + rt2x00_set_field32(&reg, USB_CYC_CFG_CLOCK_CYCLE, 30);
1546 + rt2x00usb_register_write(rt2x00dev, USB_CYC_CFG, reg);
1547 +
1548 + rt2x00usb_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
1549 + rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
1550 + rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
1551 + rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
1552 + rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
1553 + rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
1554 + rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
1555 + rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
1556 + rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
1557 + rt2x00usb_register_write(rt2x00dev, HT_FBK_CFG0, reg);
1558 +
1559 + rt2x00usb_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
1560 + rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
1561 + rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
1562 + rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
1563 + rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
1564 + rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
1565 + rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
1566 + rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
1567 + rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
1568 + rt2x00usb_register_write(rt2x00dev, HT_FBK_CFG1, reg);
1569 +
1570 + rt2x00usb_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
1571 + rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
1572 + rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
1573 + rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 3);
1574 + rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
1575 + rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
1576 + rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
1577 + rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
1578 + rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
1579 + rt2x00usb_register_write(rt2x00dev, LG_FBK_CFG0, reg);
1580 +
1581 + rt2x00usb_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
1582 + rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
1583 + rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
1584 + rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
1585 + rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
1586 + rt2x00usb_register_write(rt2x00dev, LG_FBK_CFG1, reg);
1587 +
1588 + /*
1589 + * We must clear the error counters.
1590 + * These registers are cleared on read,
1591 + * so we may pass a useless variable to store the value.
1592 + */
1593 + rt2x00usb_register_read(rt2x00dev, RX_STA_CNT0, &reg);
1594 + rt2x00usb_register_read(rt2x00dev, RX_STA_CNT1, &reg);
1595 + rt2x00usb_register_read(rt2x00dev, RX_STA_CNT2, &reg);
1596 + rt2x00usb_register_read(rt2x00dev, TX_STA_CNT0, &reg);
1597 + rt2x00usb_register_read(rt2x00dev, TX_STA_CNT1, &reg);
1598 + rt2x00usb_register_read(rt2x00dev, TX_STA_CNT2, &reg);
1599 +
1600 + return 0;
1601 +}
1602 +
1603 +static int rt2800usb_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
1604 +{
1605 + unsigned int i;
1606 + u32 reg;
1607 +
1608 + for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1609 + rt2x00usb_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
1610 + if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
1611 + return 0;
1612 +
1613 + udelay(REGISTER_BUSY_DELAY);
1614 + }
1615 +
1616 + ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
1617 + return -EACCES;
1618 +}
1619 +
1620 +static int rt2800usb_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
1621 +{
1622 + unsigned int i;
1623 + u8 value;
1624 +
1625 + for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1626 + rt2800usb_bbp_read(rt2x00dev, 0, &value);
1627 + if ((value != 0xff) && (value != 0x00))
1628 + return 0;
1629 + udelay(REGISTER_BUSY_DELAY);
1630 + }
1631 +
1632 + ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
1633 + return -EACCES;
1634 +}
1635 +
1636 +static int rt2800usb_init_bbp(struct rt2x00_dev *rt2x00dev)
1637 +{
1638 + unsigned int i;
1639 + u16 eeprom;
1640 + u8 reg_id;
1641 + u8 value;
1642 +
1643 + if (unlikely(rt2800usb_wait_bbp_rf_ready(rt2x00dev) ||
1644 + rt2800usb_wait_bbp_ready(rt2x00dev)))
1645 + return -EACCES;
1646 +
1647 + rt2800usb_bbp_write(rt2x00dev, 65, 0x2c);
1648 + rt2800usb_bbp_write(rt2x00dev, 66, 0x38);
1649 + rt2800usb_bbp_write(rt2x00dev, 69, 0x12);
1650 + rt2800usb_bbp_write(rt2x00dev, 70, 0x0a);
1651 + rt2800usb_bbp_write(rt2x00dev, 73, 0x10);
1652 + rt2800usb_bbp_write(rt2x00dev, 81, 0x37);
1653 + rt2800usb_bbp_write(rt2x00dev, 82, 0x62);
1654 + rt2800usb_bbp_write(rt2x00dev, 83, 0x6a);
1655 + rt2800usb_bbp_write(rt2x00dev, 84, 0x99);
1656 + rt2800usb_bbp_write(rt2x00dev, 86, 0x00);
1657 + rt2800usb_bbp_write(rt2x00dev, 91, 0x04);
1658 + rt2800usb_bbp_write(rt2x00dev, 92, 0x00);
1659 + rt2800usb_bbp_write(rt2x00dev, 103, 0x00);
1660 + rt2800usb_bbp_write(rt2x00dev, 105, 0x05);
1661 +
1662 + if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
1663 + rt2800usb_bbp_write(rt2x00dev, 69, 0x16);
1664 + rt2800usb_bbp_write(rt2x00dev, 73, 0x12);
1665 + }
1666 +
1667 + if (rt2x00_rev(&rt2x00dev->chip) > RT2860D_VERSION) {
1668 + rt2800usb_bbp_write(rt2x00dev, 84, 0x19);
1669 + }
1670 +
1671 + if (rt2x00_rev(&rt2x00dev->chip) == RT3070_VERSION) {
1672 + rt2800usb_bbp_write(rt2x00dev, 70, 0x0a);
1673 + rt2800usb_bbp_write(rt2x00dev, 84, 0x99);
1674 + rt2800usb_bbp_write(rt2x00dev, 105, 0x05);
1675 + }
1676 +
1677 + for (i = 0; i < EEPROM_BBP_SIZE; i++) {
1678 + rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
1679 +
1680 + if (eeprom != 0xffff && eeprom != 0x0000) {
1681 + reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
1682 + value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
1683 + rt2800usb_bbp_write(rt2x00dev, reg_id, value);
1684 + }
1685 + }
1686 +
1687 + return 0;
1688 +}
1689 +
1690 +static u8 rt2800usb_init_rx_filter(struct rt2x00_dev *rt2x00dev,
1691 + bool bw40, u8 rfcsr24, u8 filter_target)
1692 +{
1693 + unsigned int i;
1694 + u8 bbp;
1695 + u8 rfcsr;
1696 + u8 passband;
1697 + u8 stopband;
1698 + u8 overtuned = 0;
1699 +
1700 + rt2800usb_rfcsr_write(rt2x00dev, 24, rfcsr24);
1701 +
1702 + rt2800usb_bbp_read(rt2x00dev, 4, &bbp);
1703 + rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
1704 + rt2800usb_bbp_write(rt2x00dev, 4, bbp);
1705 +
1706 + rt2800usb_rfcsr_read(rt2x00dev, 22, &rfcsr);
1707 + rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
1708 + rt2800usb_rfcsr_write(rt2x00dev, 22, rfcsr);
1709 +
1710 + /*
1711 + * Set power & frequency of passband test tone
1712 + */
1713 + rt2800usb_bbp_write(rt2x00dev, 24, 0);
1714 +
1715 + for (i = 0; i < 100; i++) {
1716 + rt2800usb_bbp_write(rt2x00dev, 25, 0x90);
1717 + msleep(1);
1718 +
1719 + rt2800usb_bbp_read(rt2x00dev, 55, &passband);
1720 + if (passband)
1721 + break;
1722 + }
1723 +
1724 + /*
1725 + * Set power & frequency of stopband test tone
1726 + */
1727 + rt2800usb_bbp_write(rt2x00dev, 24, 0x06);
1728 +
1729 + for (i = 0; i < 100; i++) {
1730 + rt2800usb_bbp_write(rt2x00dev, 25, 0x90);
1731 + msleep(1);
1732 +
1733 + rt2800usb_bbp_read(rt2x00dev, 55, &stopband);
1734 +
1735 + if ((passband - stopband) <= filter_target) {
1736 + rfcsr24++;
1737 + overtuned += ((passband - stopband) == filter_target);
1738 + } else
1739 + break;
1740 +
1741 + rt2800usb_rfcsr_write(rt2x00dev, 24, rfcsr24);
1742 + }
1743 +
1744 + rfcsr24 -= !!overtuned;
1745 +
1746 + rt2800usb_rfcsr_write(rt2x00dev, 24, rfcsr24);
1747 + return rfcsr24;
1748 +}
1749 +
1750 +static int rt2800usb_init_rfcsr(struct rt2x00_dev *rt2x00dev)
1751 +{
1752 + u8 rfcsr;
1753 + u8 bbp;
1754 +
1755 + if (rt2x00_rev(&rt2x00dev->chip) != RT3070_VERSION)
1756 + return 0;
1757 +
1758 + /*
1759 + * Init RF calibration.
1760 + */
1761 + rt2800usb_rfcsr_read(rt2x00dev, 30, &rfcsr);
1762 + rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
1763 + rt2800usb_rfcsr_write(rt2x00dev, 30, rfcsr);
1764 + msleep(1);
1765 + rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
1766 + rt2800usb_rfcsr_write(rt2x00dev, 30, rfcsr);
1767 +
1768 + rt2800usb_rfcsr_write(rt2x00dev, 4, 0x40);
1769 + rt2800usb_rfcsr_write(rt2x00dev, 5, 0x03);
1770 + rt2800usb_rfcsr_write(rt2x00dev, 6, 0x02);
1771 + rt2800usb_rfcsr_write(rt2x00dev, 7, 0x70);
1772 + rt2800usb_rfcsr_write(rt2x00dev, 9, 0x0f);
1773 + rt2800usb_rfcsr_write(rt2x00dev, 10, 0x71);
1774 + rt2800usb_rfcsr_write(rt2x00dev, 11, 0x21);
1775 + rt2800usb_rfcsr_write(rt2x00dev, 12, 0x7b);
1776 + rt2800usb_rfcsr_write(rt2x00dev, 14, 0x90);
1777 + rt2800usb_rfcsr_write(rt2x00dev, 15, 0x58);
1778 + rt2800usb_rfcsr_write(rt2x00dev, 16, 0xb3);
1779 + rt2800usb_rfcsr_write(rt2x00dev, 17, 0x92);
1780 + rt2800usb_rfcsr_write(rt2x00dev, 18, 0x2c);
1781 + rt2800usb_rfcsr_write(rt2x00dev, 19, 0x02);
1782 + rt2800usb_rfcsr_write(rt2x00dev, 20, 0xba);
1783 + rt2800usb_rfcsr_write(rt2x00dev, 21, 0xdb);
1784 + rt2800usb_rfcsr_write(rt2x00dev, 24, 0x16);
1785 + rt2800usb_rfcsr_write(rt2x00dev, 25, 0x01);
1786 + rt2800usb_rfcsr_write(rt2x00dev, 27, 0x03);
1787 + rt2800usb_rfcsr_write(rt2x00dev, 29, 0x1f);
1788 +
1789 + /*
1790 + * Set RX Filter calibration for 20MHz and 40MHz
1791 + */
1792 + rt2x00dev->calibration_bw20 =
1793 + rt2800usb_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
1794 + rt2x00dev->calibration_bw40 =
1795 + rt2800usb_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
1796 +
1797 + /*
1798 + * Set back to initial state
1799 + */
1800 + rt2800usb_bbp_write(rt2x00dev, 24, 0);
1801 +
1802 + rt2800usb_rfcsr_read(rt2x00dev, 22, &rfcsr);
1803 + rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
1804 + rt2800usb_rfcsr_write(rt2x00dev, 22, rfcsr);
1805 +
1806 + /*
1807 + * set BBP back to BW20
1808 + */
1809 + rt2800usb_bbp_read(rt2x00dev, 4, &bbp);
1810 + rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
1811 + rt2800usb_bbp_write(rt2x00dev, 4, bbp);
1812 +
1813 + return 0;
1814 +}
1815 +
1816 +/*
1817 + * Device state switch handlers.
1818 + */
1819 +static void rt2800usb_toggle_rx(struct rt2x00_dev *rt2x00dev,
1820 + enum dev_state state)
1821 +{
1822 + u32 reg;
1823 +
1824 + rt2x00usb_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
1825 + rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX,
1826 + (state == STATE_RADIO_RX_ON) ||
1827 + (state == STATE_RADIO_RX_ON_LINK));
1828 + rt2x00usb_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
1829 +}
1830 +
1831 +static int rt2800usb_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
1832 +{
1833 + unsigned int i;
1834 + u32 reg;
1835 +
1836 + for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1837 + rt2x00usb_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1838 + if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
1839 + !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
1840 + return 0;
1841 +
1842 + msleep(1);
1843 + }
1844 +
1845 + ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
1846 + return -EACCES;
1847 +}
1848 +
1849 +static int rt2800usb_enable_radio(struct rt2x00_dev *rt2x00dev)
1850 +{
1851 + u32 reg;
1852 + u16 word;
1853 +
1854 + /*
1855 + * Initialize all registers.
1856 + */
1857 + if (unlikely(rt2800usb_wait_wpdma_ready(rt2x00dev) ||
1858 + rt2800usb_init_registers(rt2x00dev) ||
1859 + rt2800usb_init_bbp(rt2x00dev) ||
1860 + rt2800usb_init_rfcsr(rt2x00dev)))
1861 + return -EIO;
1862 +
1863 + rt2x00usb_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
1864 + rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
1865 + rt2x00usb_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
1866 +
1867 + udelay(50);
1868 +
1869 + rt2x00usb_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1870 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
1871 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
1872 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
1873 + rt2x00usb_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1874 +
1875 +
1876 + rt2x00usb_register_read(rt2x00dev, USB_DMA_CFG, &reg);
1877 + rt2x00_set_field32(&reg, USB_DMA_CFG_PHY_CLEAR, 0);
1878 + /* Don't use bulk in aggregation when working with USB 1.1 */
1879 + rt2x00_set_field32(&reg, USB_DMA_CFG_RX_BULK_AGG_EN,
1880 + (rt2x00dev->rx->usb_maxpacket == 512));
1881 + rt2x00_set_field32(&reg, USB_DMA_CFG_RX_BULK_AGG_TIMEOUT, 128);
1882 + /* FIXME: Calculate this value based on Aggregation defines */
1883 + rt2x00_set_field32(&reg, USB_DMA_CFG_RX_BULK_AGG_LIMIT, 21);
1884 + rt2x00_set_field32(&reg, USB_DMA_CFG_RX_BULK_EN, 1);
1885 + rt2x00_set_field32(&reg, USB_DMA_CFG_TX_BULK_EN, 1);
1886 + rt2x00usb_register_write(rt2x00dev, USB_DMA_CFG, reg);
1887 +
1888 + rt2x00usb_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
1889 + rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
1890 + rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
1891 + rt2x00usb_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
1892 +
1893 + /*
1894 + * Send signal to firmware during boot time.
1895 + */
1896 + rt2800usb_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0xff, 0, 0);
1897 +
1898 + /*
1899 + * Initialize LED control
1900 + */
1901 + rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
1902 + rt2800usb_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
1903 + word & 0xff, (word >> 8) & 0xff);
1904 +
1905 + rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
1906 + rt2800usb_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
1907 + word & 0xff, (word >> 8) & 0xff);
1908 +
1909 + rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
1910 + rt2800usb_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
1911 + word & 0xff, (word >> 8) & 0xff);
1912 +
1913 + return 0;
1914 +}
1915 +
1916 +static void rt2800usb_disable_radio(struct rt2x00_dev *rt2x00dev)
1917 +{
1918 + u32 reg;
1919 +
1920 + rt2x00usb_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
1921 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
1922 + rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
1923 + rt2x00usb_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
1924 +
1925 + rt2x00usb_register_write(rt2x00dev, MAC_SYS_CTRL, 0);
1926 + rt2x00usb_register_write(rt2x00dev, PWR_PIN_CFG, 0);
1927 + rt2x00usb_register_write(rt2x00dev, TX_PIN_CFG, 0);
1928 +
1929 + /* Wait for DMA, ignore error */
1930 + rt2800usb_wait_wpdma_ready(rt2x00dev);
1931 +
1932 + rt2x00usb_disable_radio(rt2x00dev);
1933 +}
1934 +
1935 +static int rt2800usb_set_state(struct rt2x00_dev *rt2x00dev,
1936 + enum dev_state state)
1937 +{
1938 + rt2x00usb_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
1939 +
1940 + if (state == STATE_AWAKE)
1941 + rt2800usb_mcu_request(rt2x00dev, MCU_WAKEUP, 0xff, 0, 0);
1942 + else
1943 + rt2800usb_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 2);
1944 +
1945 + return 0;
1946 +}
1947 +
1948 +static int rt2800usb_set_device_state(struct rt2x00_dev *rt2x00dev,
1949 + enum dev_state state)
1950 +{
1951 + int retval = 0;
1952 +
1953 + switch (state) {
1954 + case STATE_RADIO_ON:
1955 + /*
1956 + * Before the radio can be enabled, the device first has
1957 + * to be woken up. After that it needs a bit of time
1958 + * to be fully awake and the radio can be enabled.
1959 + */
1960 + rt2800usb_set_state(rt2x00dev, STATE_AWAKE);
1961 + msleep(1);
1962 + retval = rt2800usb_enable_radio(rt2x00dev);
1963 + break;
1964 + case STATE_RADIO_OFF:
1965 + /*
1966 + * After the radio has been disablee, the device should
1967 + * be put to sleep for powersaving.
1968 + */
1969 + rt2800usb_disable_radio(rt2x00dev);
1970 + rt2800usb_set_state(rt2x00dev, STATE_SLEEP);
1971 + break;
1972 + case STATE_RADIO_RX_ON:
1973 + case STATE_RADIO_RX_ON_LINK:
1974 + case STATE_RADIO_RX_OFF:
1975 + case STATE_RADIO_RX_OFF_LINK:
1976 + rt2800usb_toggle_rx(rt2x00dev, state);
1977 + break;
1978 + case STATE_RADIO_IRQ_ON:
1979 + case STATE_RADIO_IRQ_OFF:
1980 + /* No support, but no error either */
1981 + break;
1982 + case STATE_DEEP_SLEEP:
1983 + case STATE_SLEEP:
1984 + case STATE_STANDBY:
1985 + case STATE_AWAKE:
1986 + retval = rt2800usb_set_state(rt2x00dev, state);
1987 + break;
1988 + default:
1989 + retval = -ENOTSUPP;
1990 + break;
1991 + }
1992 +
1993 + if (unlikely(retval))
1994 + ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
1995 + state, retval);
1996 +
1997 + return retval;
1998 +}
1999 +
2000 +/*
2001 + * TX descriptor initialization
2002 + */
2003 +static void rt2800usb_write_tx_desc(struct rt2x00_dev *rt2x00dev,
2004 + struct sk_buff *skb,
2005 + struct txentry_desc *txdesc)
2006 +{
2007 + struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
2008 + __le32 *txi = skbdesc->desc;
2009 + __le32 *txwi = &txi[TXINFO_DESC_SIZE / sizeof(__le32)];
2010 + u32 word;
2011 +
2012 + /*
2013 + * Initialize TX Info descriptor
2014 + */
2015 + rt2x00_desc_read(txwi, 0, &word);
2016 + rt2x00_set_field32(&word, TXWI_W0_FRAG,
2017 + test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
2018 + rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
2019 + rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
2020 + rt2x00_set_field32(&word, TXWI_W0_TS,
2021 + test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
2022 + rt2x00_set_field32(&word, TXWI_W0_AMPDU,
2023 + test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
2024 + rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
2025 + rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->ifs);
2026 + rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
2027 + rt2x00_set_field32(&word, TXWI_W0_BW,
2028 + test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
2029 + rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
2030 + test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
2031 + rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
2032 + rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
2033 + rt2x00_desc_write(txwi, 0, word);
2034 +
2035 + rt2x00_desc_read(txwi, 1, &word);
2036 + rt2x00_set_field32(&word, TXWI_W1_ACK,
2037 + test_bit(ENTRY_TXD_ACK, &txdesc->flags));
2038 + rt2x00_set_field32(&word, TXWI_W1_NSEQ,
2039 + test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
2040 + rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
2041 + rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
2042 + test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
2043 + txdesc->key_idx : 0xff);
2044 + rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT, skb->len);
2045 + rt2x00_set_field32(&word, TXWI_W1_PACKETID,
2046 + skbdesc->entry->entry_idx);
2047 + rt2x00_desc_write(txwi, 1, word);
2048 +
2049 + /*
2050 + * Always write 0 to IV/EIV fields, hardware will insert the IV
2051 + * from the IVEIV register when TXINFO_W0_WIV is set to 0.
2052 + * When TXINFO_W0_WIV is set to 1 it will use the IV data
2053 + * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
2054 + * crypto entry in the registers should be used to encrypt the frame.
2055 + */
2056 + _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
2057 + _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
2058 +
2059 + /*
2060 + * Initialize TX descriptor
2061 + */
2062 + rt2x00_desc_read(txi, 0, &word);
2063 + rt2x00_set_field32(&word, TXINFO_W0_USB_DMA_TX_PKT_LEN,
2064 + skb->len + TXWI_DESC_SIZE);
2065 + rt2x00_set_field32(&word, TXINFO_W0_WIV,
2066 + !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags));
2067 + rt2x00_set_field32(&word, TXINFO_W0_QSEL, 2);
2068 + rt2x00_set_field32(&word, TXINFO_W0_SW_USE_LAST_ROUND, 0);
2069 + rt2x00_set_field32(&word, TXINFO_W0_USB_DMA_NEXT_VALID, 0);
2070 + rt2x00_set_field32(&word, TXINFO_W0_USB_DMA_TX_BURST,
2071 + test_bit(ENTRY_TXD_BURST, &txdesc->flags));
2072 + rt2x00_desc_write(txi, 0, word);
2073 +}
2074 +
2075 +/*
2076 + * TX data initialization
2077 + */
2078 +static void rt2800usb_write_beacon(struct queue_entry *entry)
2079 +{
2080 + struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
2081 + struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
2082 + unsigned int beacon_base;
2083 + u32 reg;
2084 +
2085 + /*
2086 + * Add the descriptor in front of the skb.
2087 + */
2088 + skb_push(entry->skb, entry->queue->desc_size);
2089 + memcpy(entry->skb->data, skbdesc->desc, skbdesc->desc_len);
2090 + skbdesc->desc = entry->skb->data;
2091 +
2092 + /*
2093 + * Disable beaconing while we are reloading the beacon data,
2094 + * otherwise we might be sending out invalid data.
2095 + */
2096 + rt2x00usb_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
2097 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
2098 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
2099 + rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
2100 + rt2x00usb_register_write(rt2x00dev, BCN_TIME_CFG, reg);
2101 +
2102 + /*
2103 + * Write entire beacon with descriptor to register.
2104 + */
2105 + beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
2106 + rt2x00usb_vendor_request_large_buff(rt2x00dev, USB_MULTI_WRITE,
2107 + USB_VENDOR_REQUEST_OUT, beacon_base,
2108 + entry->skb->data, entry->skb->len,
2109 + REGISTER_TIMEOUT32(entry->skb->len));
2110 +
2111 + /*
2112 + * Clean up the beacon skb.
2113 + */
2114 + dev_kfree_skb(entry->skb);
2115 + entry->skb = NULL;
2116 +}
2117 +
2118 +static int rt2800usb_get_tx_data_len(struct queue_entry *entry)
2119 +{
2120 + int length;
2121 +
2122 + /*
2123 + * The length _must_ include 4 bytes padding,
2124 + * it should always be multiple of 4,
2125 + * but it must _not_ be a multiple of the USB packet size.
2126 + */
2127 + length = roundup(entry->skb->len + 4, 4);
2128 + length += (4 * !(length % entry->queue->usb_maxpacket));
2129 +
2130 + return length;
2131 +}
2132 +
2133 +static void rt2800usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
2134 + const enum data_queue_qid queue)
2135 +{
2136 + u32 reg;
2137 +
2138 + if (queue != QID_BEACON) {
2139 + rt2x00usb_kick_tx_queue(rt2x00dev, queue);
2140 + return;
2141 + }
2142 +
2143 + rt2x00usb_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
2144 + if (!rt2x00_get_field32(reg, BCN_TIME_CFG_BEACON_GEN)) {
2145 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
2146 + rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
2147 + rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
2148 + rt2x00usb_register_write(rt2x00dev, BCN_TIME_CFG, reg);
2149 + }
2150 +}
2151 +
2152 +/*
2153 + * RX control handlers
2154 + */
2155 +static void rt2800usb_fill_rxdone(struct queue_entry *entry,
2156 + struct rxdone_entry_desc *rxdesc)
2157 +{
2158 + struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
2159 + struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
2160 + __le32 *rxd = (__le32 *)entry->skb->data;
2161 + __le32 *rxwi;
2162 + u32 rxd0;
2163 + u32 rxwi0;
2164 + u32 rxwi1;
2165 + u32 rxwi2;
2166 + u32 rxwi3;
2167 +
2168 + /*
2169 + * Copy descriptor to the skbdesc->desc buffer, making it safe from
2170 + * moving of frame data in rt2x00usb.
2171 + */
2172 + memcpy(skbdesc->desc, rxd, skbdesc->desc_len);
2173 + rxd = (__le32 *)skbdesc->desc;
2174 + rxwi = &rxd[RXD_DESC_SIZE / sizeof(__le32)];
2175 +
2176 + /*
2177 + * It is now safe to read the descriptor on all architectures.
2178 + */
2179 + rt2x00_desc_read(rxd, 0, &rxd0);
2180 + rt2x00_desc_read(rxwi, 0, &rxwi0);
2181 + rt2x00_desc_read(rxwi, 1, &rxwi1);
2182 + rt2x00_desc_read(rxwi, 2, &rxwi2);
2183 + rt2x00_desc_read(rxwi, 3, &rxwi3);
2184 +
2185 + if (rt2x00_get_field32(rxd0, RXD_W0_CRC_ERROR))
2186 + rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
2187 +
2188 + if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
2189 + /*
2190 + * Unfortunately we don't know the cipher type used during
2191 + * decryption. This prevents us from correct providing
2192 + * correct statistics through debugfs.
2193 + */
2194 + rxdesc->cipher = CIPHER_NONE;
2195 + rxdesc->cipher_status =
2196 + rt2x00_get_field32(rxd0, RXD_W0_CIPHER_ERROR);
2197 + }
2198 +
2199 + if (rt2x00_get_field32(rxd0, RXD_W0_DECRYPTED)) {
2200 + /*
2201 + * Hardware has stripped IV/EIV data from 802.11 frame during
2202 + * decryption. Unfortunately the descriptor doesn't contain
2203 + * any fields with the EIV/IV data either, so they can't
2204 + * be restored by rt2x00lib.
2205 + */
2206 + rxdesc->flags |= RX_FLAG_IV_STRIPPED;
2207 +
2208 + if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
2209 + rxdesc->flags |= RX_FLAG_DECRYPTED;
2210 + else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
2211 + rxdesc->flags |= RX_FLAG_MMIC_ERROR;
2212 + }
2213 +
2214 + if (rt2x00_get_field32(rxd0, RXD_W0_MY_BSS))
2215 + rxdesc->dev_flags |= RXDONE_MY_BSS;
2216 +
2217 + if (rt2x00_get_field32(rxwi1, RXWI_W1_SHORT_GI))
2218 + rxdesc->flags |= RX_FLAG_SHORT_GI;
2219 +
2220 + if (rt2x00_get_field32(rxwi1, RXWI_W1_BW))
2221 + rxdesc->flags |= RX_FLAG_40MHZ;
2222 +
2223 + /*
2224 + * Detect RX rate, always use MCS as signal type.
2225 + */
2226 + rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
2227 + rxdesc->rate_mode = rt2x00_get_field32(rxwi1, RXWI_W1_PHYMODE);
2228 + rxdesc->signal = rt2x00_get_field32(rxwi1, RXWI_W1_MCS);
2229 +
2230 + /*
2231 + * Mask of 0x8 bit to remove the short preamble flag.
2232 + */
2233 + if (rxdesc->dev_flags == RATE_MODE_CCK)
2234 + rxdesc->signal &= ~0x8;
2235 +
2236 + rxdesc->rssi =
2237 + (rt2x00_get_field32(rxwi2, RXWI_W2_RSSI0) +
2238 + rt2x00_get_field32(rxwi2, RXWI_W2_RSSI1)) / 2;
2239 +
2240 + rxdesc->noise =
2241 + (rt2x00_get_field32(rxwi3, RXWI_W3_SNR0) +
2242 + rt2x00_get_field32(rxwi3, RXWI_W3_SNR1)) / 2;
2243 +
2244 + rxdesc->size = rt2x00_get_field32(rxwi0, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
2245 +
2246 + /*
2247 + * Remove RXWI descriptor from start of buffer.
2248 + */
2249 + skb_pull(entry->skb, skbdesc->desc_len);
2250 + skb_trim(entry->skb, rxdesc->size);
2251 +}
2252 +
2253 +/*
2254 + * Device probe functions.
2255 + */
2256 +static int rt2800usb_validate_eeprom(struct rt2x00_dev *rt2x00dev)
2257 +{
2258 + u16 word;
2259 + u8 *mac;
2260 + u8 default_lna_gain;
2261 +
2262 + rt2x00usb_eeprom_read(rt2x00dev, rt2x00dev->eeprom, EEPROM_SIZE);
2263 +
2264 + /*
2265 + * Start validation of the data that has been read.
2266 + */
2267 + mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
2268 + if (!is_valid_ether_addr(mac)) {
2269 + DECLARE_MAC_BUF(macbuf);
2270 +
2271 + random_ether_addr(mac);
2272 + EEPROM(rt2x00dev, "MAC: %s\n", print_mac(macbuf, mac));
2273 + }
2274 +
2275 + rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
2276 + if (word == 0xffff) {
2277 + rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2278 + rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
2279 + rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
2280 + rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2281 + EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
2282 + } else if (rt2x00_rev(&rt2x00dev->chip) < RT2883_VERSION) {
2283 + /*
2284 + * There is a max of 2 RX streams for RT2860 series
2285 + */
2286 + if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
2287 + rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
2288 + rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
2289 + }
2290 +
2291 + rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
2292 + if (word == 0xffff) {
2293 + rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
2294 + rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
2295 + rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
2296 + rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
2297 + rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
2298 + rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
2299 + rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
2300 + rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
2301 + rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
2302 + rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
2303 + rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
2304 + EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
2305 + }
2306 +
2307 + rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
2308 + if ((word & 0x00ff) == 0x00ff) {
2309 + rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
2310 + rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
2311 + LED_MODE_TXRX_ACTIVITY);
2312 + rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
2313 + rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
2314 + rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
2315 + rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
2316 + rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
2317 + EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
2318 + }
2319 +
2320 + /*
2321 + * During the LNA validation we are going to use
2322 + * lna0 as correct value. Note that EEPROM_LNA
2323 + * is never validated.
2324 + */
2325 + rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
2326 + default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
2327 +
2328 + rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
2329 + if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
2330 + rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
2331 + if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
2332 + rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
2333 + rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
2334 +
2335 + rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
2336 + if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
2337 + rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
2338 + if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
2339 + rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
2340 + rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
2341 + default_lna_gain);
2342 + rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
2343 +
2344 + rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
2345 + if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
2346 + rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
2347 + if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
2348 + rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
2349 + rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
2350 +
2351 + rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
2352 + if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
2353 + rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
2354 + if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
2355 + rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
2356 + rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
2357 + default_lna_gain);
2358 + rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
2359 +
2360 + return 0;
2361 +}
2362 +
2363 +static int rt2800usb_init_eeprom(struct rt2x00_dev *rt2x00dev)
2364 +{
2365 + u32 reg;
2366 + u16 value;
2367 + u16 eeprom;
2368 +
2369 + /*
2370 + * Read EEPROM word for configuration.
2371 + */
2372 + rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2373 +
2374 + /*
2375 + * Identify RF chipset.
2376 + */
2377 + value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
2378 + rt2x00usb_register_read(rt2x00dev, MAC_CSR0, &reg);
2379 + rt2x00_set_chip(rt2x00dev, RT2870, value, reg);
2380 +
2381 + /*
2382 + * The check for rt2860 is not a typo, some rt2870 hardware
2383 + * identifies itself as rt2860 in the CSR register.
2384 + */
2385 + if ((rt2x00_get_field32(reg, MAC_CSR0_ASIC_VER) != 0x2860) &&
2386 + (rt2x00_get_field32(reg, MAC_CSR0_ASIC_VER) != 0x2870) &&
2387 + (rt2x00_get_field32(reg, MAC_CSR0_ASIC_VER) != 0x3070)) {
2388 + ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
2389 + return -ENODEV;
2390 + }
2391 +
2392 + if (!rt2x00_rf(&rt2x00dev->chip, RF2820) &&
2393 + !rt2x00_rf(&rt2x00dev->chip, RF2850) &&
2394 + !rt2x00_rf(&rt2x00dev->chip, RF2720) &&
2395 + !rt2x00_rf(&rt2x00dev->chip, RF2750) &&
2396 + !rt2x00_rf(&rt2x00dev->chip, RF3020) &&
2397 + !rt2x00_rf(&rt2x00dev->chip, RF2020)) {
2398 + ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
2399 + return -ENODEV;
2400 + }
2401 +
2402 + /*
2403 + * Read frequency offset and RF programming sequence.
2404 + */
2405 + rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
2406 + rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
2407 +
2408 + /*
2409 + * Read external LNA informations.
2410 + */
2411 + rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
2412 +
2413 + if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
2414 + __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
2415 + if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
2416 + __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
2417 +
2418 + /*
2419 + * Detect if this device has an hardware controlled radio.
2420 + */
2421 +#ifdef CONFIG_RT2X00_LIB_RFKILL
2422 + if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
2423 + __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
2424 +#endif /* CONFIG_RT2X00_LIB_RFKILL */
2425 +
2426 + /*
2427 + * Store led settings, for correct led behaviour.
2428 + */
2429 +#ifdef CONFIG_RT2X00_LIB_LEDS
2430 + rt2800usb_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
2431 + rt2800usb_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
2432 + rt2800usb_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
2433 +
2434 + rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ,
2435 + &rt2x00dev->led_mcu_reg);
2436 +#endif /* CONFIG_RT2X00_LIB_LEDS */
2437 +
2438 + return 0;
2439 +}
2440 +
2441 +/*
2442 + * RF value list for rt2870
2443 + * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
2444 + */
2445 +static const struct rf_channel rf_vals[] = {
2446 + { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
2447 + { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
2448 + { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
2449 + { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
2450 + { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
2451 + { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
2452 + { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
2453 + { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
2454 + { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
2455 + { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
2456 + { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
2457 + { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
2458 + { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
2459 + { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
2460 +
2461 + /* 802.11 UNI / HyperLan 2 */
2462 + { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
2463 + { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
2464 + { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
2465 + { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
2466 + { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
2467 + { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
2468 + { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
2469 + { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
2470 + { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
2471 + { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
2472 + { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
2473 + { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
2474 +
2475 + /* 802.11 HyperLan 2 */
2476 + { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
2477 + { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
2478 + { 104, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed1a3 },
2479 + { 108, 0x18402ecc, 0x184c0a32, 0x18578a55, 0x180ed193 },
2480 + { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
2481 + { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
2482 + { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
2483 + { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
2484 + { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
2485 + { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
2486 + { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
2487 + { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
2488 + { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
2489 + { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
2490 + { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
2491 + { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
2492 +
2493 + /* 802.11 UNII */
2494 + { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
2495 + { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
2496 + { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
2497 + { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
2498 + { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
2499 + { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
2500 + { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
2501 +
2502 + /* 802.11 Japan */
2503 + { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
2504 + { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
2505 + { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
2506 + { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
2507 + { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
2508 + { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
2509 + { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
2510 +};
2511 +
2512 +/*
2513 + * RF value list for rt3070
2514 + * Supports: 2.4 GHz
2515 + */
2516 +static const struct rf_channel rf_vals_3070[] = {
2517 + {1, 241, 2, 2 },
2518 + {2, 241, 2, 7 },
2519 + {3, 242, 2, 2 },
2520 + {4, 242, 2, 7 },
2521 + {5, 243, 2, 2 },
2522 + {6, 243, 2, 7 },
2523 + {7, 244, 2, 2 },
2524 + {8, 244, 2, 7 },
2525 + {9, 245, 2, 2 },
2526 + {10, 245, 2, 7 },
2527 + {11, 246, 2, 2 },
2528 + {12, 246, 2, 7 },
2529 + {13, 247, 2, 2 },
2530 + {14, 248, 2, 4 },
2531 +};
2532 +
2533 +static int rt2800usb_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
2534 +{
2535 + struct hw_mode_spec *spec = &rt2x00dev->spec;
2536 + struct channel_info *info;
2537 + char *tx_power1;
2538 + char *tx_power2;
2539 + unsigned int i;
2540 + u16 eeprom;
2541 +
2542 + /*
2543 + * Initialize all hw fields.
2544 + */
2545 + rt2x00dev->hw->flags =
2546 + IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2547 + IEEE80211_HW_SIGNAL_DBM |
2548 + IEEE80211_HW_SUPPORTS_PS |
2549 + IEEE80211_HW_PS_NULLFUNC_STACK;
2550 + rt2x00dev->hw->extra_tx_headroom = TXINFO_DESC_SIZE + TXWI_DESC_SIZE;
2551 +
2552 + SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
2553 + SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
2554 + rt2x00_eeprom_addr(rt2x00dev,
2555 + EEPROM_MAC_ADDR_0));
2556 +
2557 + rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
2558 +
2559 + /*
2560 + * Initialize HT information.
2561 + */
2562 + spec->ht.ht_supported = true;
2563 + spec->ht.cap =
2564 + IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2565 + IEEE80211_HT_CAP_GRN_FLD |
2566 + IEEE80211_HT_CAP_SGI_20 |
2567 + IEEE80211_HT_CAP_SGI_40 |
2568 + IEEE80211_HT_CAP_TX_STBC |
2569 + IEEE80211_HT_CAP_RX_STBC |
2570 + IEEE80211_HT_CAP_PSMP_SUPPORT;
2571 + spec->ht.ampdu_factor = 3;
2572 + spec->ht.ampdu_density = 4;
2573 + spec->ht.mcs.tx_params =
2574 + IEEE80211_HT_MCS_TX_DEFINED |
2575 + IEEE80211_HT_MCS_TX_RX_DIFF |
2576 + ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
2577 + IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
2578 +
2579 + switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
2580 + case 3:
2581 + spec->ht.mcs.rx_mask[2] = 0xff;
2582 + case 2:
2583 + spec->ht.mcs.rx_mask[1] = 0xff;
2584 + case 1:
2585 + spec->ht.mcs.rx_mask[0] = 0xff;
2586 + spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
2587 + break;
2588 + }
2589 +
2590 + /*
2591 + * Initialize hw_mode information.
2592 + */
2593 + spec->supported_bands = SUPPORT_BAND_2GHZ;
2594 + spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
2595 +
2596 + if (rt2x00_rf(&rt2x00dev->chip, RF2820) ||
2597 + rt2x00_rf(&rt2x00dev->chip, RF2720)) {
2598 + spec->num_channels = 14;
2599 + spec->channels = rf_vals;
2600 + } else if (rt2x00_rf(&rt2x00dev->chip, RF2850) ||
2601 + rt2x00_rf(&rt2x00dev->chip, RF2750)) {
2602 + spec->supported_bands |= SUPPORT_BAND_5GHZ;
2603 + spec->num_channels = ARRAY_SIZE(rf_vals);
2604 + spec->channels = rf_vals;
2605 + } else if (rt2x00_rf(&rt2x00dev->chip, RF3020) ||
2606 + rt2x00_rf(&rt2x00dev->chip, RF2020)) {
2607 + spec->num_channels = ARRAY_SIZE(rf_vals_3070);
2608 + spec->channels = rf_vals_3070;
2609 + }
2610 +
2611 + /*
2612 + * Create channel information array
2613 + */
2614 + info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
2615 + if (!info)
2616 + return -ENOMEM;
2617 +
2618 + spec->channels_info = info;
2619 +
2620 + tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
2621 + tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
2622 +
2623 + for (i = 0; i < 14; i++) {
2624 + info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
2625 + info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
2626 + }
2627 +
2628 + if (spec->num_channels > 14) {
2629 + tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
2630 + tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
2631 +
2632 + for (i = 14; i < spec->num_channels; i++) {
2633 + info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
2634 + info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
2635 + }
2636 + }
2637 +
2638 + return 0;
2639 +}
2640 +
2641 +static int rt2800usb_probe_hw(struct rt2x00_dev *rt2x00dev)
2642 +{
2643 + int retval;
2644 +
2645 + /*
2646 + * Allocate eeprom data.
2647 + */
2648 + retval = rt2800usb_validate_eeprom(rt2x00dev);
2649 + if (retval)
2650 + return retval;
2651 +
2652 + retval = rt2800usb_init_eeprom(rt2x00dev);
2653 + if (retval)
2654 + return retval;
2655 +
2656 + /*
2657 + * Initialize hw specifications.
2658 + */
2659 + retval = rt2800usb_probe_hw_mode(rt2x00dev);
2660 + if (retval)
2661 + return retval;
2662 +
2663 + /*
2664 + * This device requires firmware.
2665 + */
2666 + __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
2667 + __set_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags);
2668 + if (!modparam_nohwcrypt)
2669 + __set_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags);
2670 +
2671 + /*
2672 + * Set the rssi offset.
2673 + */
2674 + rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
2675 +
2676 + return 0;
2677 +}
2678 +
2679 +/*
2680 + * IEEE80211 stack callback functions.
2681 + */
2682 +static void rt2800usb_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
2683 + u32 *iv32, u16 *iv16)
2684 +{
2685 + struct rt2x00_dev *rt2x00dev = hw->priv;
2686 + struct mac_iveiv_entry iveiv_entry;
2687 + u32 offset;
2688 +
2689 + offset = MAC_IVEIV_ENTRY(hw_key_idx);
2690 + rt2x00usb_register_multiread(rt2x00dev, offset,
2691 + &iveiv_entry, sizeof(iveiv_entry));
2692 +
2693 + memcpy(&iveiv_entry.iv[0], iv16, sizeof(iv16));
2694 + memcpy(&iveiv_entry.iv[4], iv32, sizeof(iv32));
2695 +}
2696 +
2697 +static int rt2800usb_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2698 +{
2699 + struct rt2x00_dev *rt2x00dev = hw->priv;
2700 + u32 reg;
2701 + bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
2702 +
2703 + rt2x00usb_register_read(rt2x00dev, TX_RTS_CFG, &reg);
2704 + rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
2705 + rt2x00usb_register_write(rt2x00dev, TX_RTS_CFG, reg);
2706 +
2707 + rt2x00usb_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
2708 + rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
2709 + rt2x00usb_register_write(rt2x00dev, CCK_PROT_CFG, reg);
2710 +
2711 + rt2x00usb_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
2712 + rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
2713 + rt2x00usb_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
2714 +
2715 + rt2x00usb_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
2716 + rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
2717 + rt2x00usb_register_write(rt2x00dev, MM20_PROT_CFG, reg);
2718 +
2719 + rt2x00usb_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
2720 + rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
2721 + rt2x00usb_register_write(rt2x00dev, MM40_PROT_CFG, reg);
2722 +
2723 + rt2x00usb_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
2724 + rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
2725 + rt2x00usb_register_write(rt2x00dev, GF20_PROT_CFG, reg);
2726 +
2727 + rt2x00usb_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
2728 + rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
2729 + rt2x00usb_register_write(rt2x00dev, GF40_PROT_CFG, reg);
2730 +
2731 + return 0;
2732 +}
2733 +
2734 +static int rt2800usb_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
2735 + const struct ieee80211_tx_queue_params *params)
2736 +{
2737 + struct rt2x00_dev *rt2x00dev = hw->priv;
2738 + struct data_queue *queue;
2739 + struct rt2x00_field32 field;
2740 + int retval;
2741 + u32 reg;
2742 + u32 offset;
2743 +
2744 + /*
2745 + * First pass the configuration through rt2x00lib, that will
2746 + * update the queue settings and validate the input. After that
2747 + * we are free to update the registers based on the value
2748 + * in the queue parameter.
2749 + */
2750 + retval = rt2x00mac_conf_tx(hw, queue_idx, params);
2751 + if (retval)
2752 + return retval;
2753 +
2754 + /*
2755 + * We only need to perform additional register initialization
2756 + * for WMM queues/
2757 + */
2758 + if (queue_idx >= 4)
2759 + return 0;
2760 +
2761 + queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
2762 +
2763 + /* Update WMM TXOP register */
2764 + offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
2765 + field.bit_offset = (queue_idx & 1) * 16;
2766 + field.bit_mask = 0xffff << field.bit_offset;
2767 +
2768 + rt2x00usb_register_read(rt2x00dev, offset, &reg);
2769 + rt2x00_set_field32(&reg, field, queue->txop);
2770 + rt2x00usb_register_write(rt2x00dev, offset, reg);
2771 +
2772 + /* Update WMM registers */
2773 + field.bit_offset = queue_idx * 4;
2774 + field.bit_mask = 0xf << field.bit_offset;
2775 +
2776 + rt2x00usb_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
2777 + rt2x00_set_field32(&reg, field, queue->aifs);
2778 + rt2x00usb_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
2779 +
2780 + rt2x00usb_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
2781 + rt2x00_set_field32(&reg, field, queue->cw_min);
2782 + rt2x00usb_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
2783 +
2784 + rt2x00usb_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
2785 + rt2x00_set_field32(&reg, field, queue->cw_max);
2786 + rt2x00usb_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
2787 +
2788 + /* Update EDCA registers */
2789 + offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
2790 +
2791 + rt2x00usb_register_read(rt2x00dev, offset, &reg);
2792 + rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
2793 + rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
2794 + rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
2795 + rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
2796 + rt2x00usb_register_write(rt2x00dev, offset, reg);
2797 +
2798 + return 0;
2799 +}
2800 +
2801 +static u64 rt2800usb_get_tsf(struct ieee80211_hw *hw)
2802 +{
2803 + struct rt2x00_dev *rt2x00dev = hw->priv;
2804 + u64 tsf;
2805 + u32 reg;
2806 +
2807 + rt2x00usb_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
2808 + tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
2809 + rt2x00usb_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
2810 + tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
2811 +
2812 + return tsf;
2813 +}
2814 +
2815 +static const struct ieee80211_ops rt2800usb_mac80211_ops = {
2816 + .tx = rt2x00mac_tx,
2817 + .start = rt2x00mac_start,
2818 + .stop = rt2x00mac_stop,
2819 + .add_interface = rt2x00mac_add_interface,
2820 + .remove_interface = rt2x00mac_remove_interface,
2821 + .config = rt2x00mac_config,
2822 + .config_interface = rt2x00mac_config_interface,
2823 + .configure_filter = rt2x00mac_configure_filter,
2824 + .set_key = rt2x00mac_set_key,
2825 + .get_stats = rt2x00mac_get_stats,
2826 + .get_tkip_seq = rt2800usb_get_tkip_seq,
2827 + .set_rts_threshold = rt2800usb_set_rts_threshold,
2828 + .bss_info_changed = rt2x00mac_bss_info_changed,
2829 + .conf_tx = rt2800usb_conf_tx,
2830 + .get_tx_stats = rt2x00mac_get_tx_stats,
2831 + .get_tsf = rt2800usb_get_tsf,
2832 +};
2833 +
2834 +static const struct rt2x00lib_ops rt2800usb_rt2x00_ops = {
2835 + .probe_hw = rt2800usb_probe_hw,
2836 + .get_firmware_name = rt2800usb_get_firmware_name,
2837 + .check_firmware = rt2800usb_check_firmware,
2838 + .load_firmware = rt2800usb_load_firmware,
2839 + .initialize = rt2x00usb_initialize,
2840 + .uninitialize = rt2x00usb_uninitialize,
2841 + .clear_entry = rt2x00usb_clear_entry,
2842 + .set_device_state = rt2800usb_set_device_state,
2843 + .rfkill_poll = rt2800usb_rfkill_poll,
2844 + .link_stats = rt2800usb_link_stats,
2845 + .reset_tuner = rt2800usb_reset_tuner,
2846 + .link_tuner = rt2800usb_link_tuner,
2847 + .write_tx_desc = rt2800usb_write_tx_desc,
2848 + .write_tx_data = rt2x00usb_write_tx_data,
2849 + .write_beacon = rt2800usb_write_beacon,
2850 + .get_tx_data_len = rt2800usb_get_tx_data_len,
2851 + .kick_tx_queue = rt2800usb_kick_tx_queue,
2852 + .kill_tx_queue = rt2x00usb_kill_tx_queue,
2853 + .fill_rxdone = rt2800usb_fill_rxdone,
2854 + .config_shared_key = rt2800usb_config_shared_key,
2855 + .config_pairwise_key = rt2800usb_config_pairwise_key,
2856 + .config_filter = rt2800usb_config_filter,
2857 + .config_intf = rt2800usb_config_intf,
2858 + .config_erp = rt2800usb_config_erp,
2859 + .config_ant = rt2800usb_config_ant,
2860 + .config = rt2800usb_config,
2861 +};
2862 +
2863 +static const struct data_queue_desc rt2800usb_queue_rx = {
2864 + .entry_num = RX_ENTRIES,
2865 + .data_size = AGGREGATION_SIZE,
2866 + .desc_size = RXD_DESC_SIZE + RXWI_DESC_SIZE,
2867 + .priv_size = sizeof(struct queue_entry_priv_usb),
2868 +};
2869 +
2870 +static const struct data_queue_desc rt2800usb_queue_tx = {
2871 + .entry_num = TX_ENTRIES,
2872 + .data_size = AGGREGATION_SIZE,
2873 + .desc_size = TXINFO_DESC_SIZE + TXWI_DESC_SIZE,
2874 + .priv_size = sizeof(struct queue_entry_priv_usb),
2875 +};
2876 +
2877 +static const struct data_queue_desc rt2800usb_queue_bcn = {
2878 + .entry_num = 8 * BEACON_ENTRIES,
2879 + .data_size = MGMT_FRAME_SIZE,
2880 + .desc_size = TXINFO_DESC_SIZE + TXWI_DESC_SIZE,
2881 + .priv_size = sizeof(struct queue_entry_priv_usb),
2882 +};
2883 +
2884 +static const struct rt2x00_ops rt2800usb_ops = {
2885 + .name = KBUILD_MODNAME,
2886 + .max_sta_intf = 1,
2887 + .max_ap_intf = 8,
2888 + .eeprom_size = EEPROM_SIZE,
2889 + .rf_size = RF_SIZE,
2890 + .tx_queues = NUM_TX_QUEUES,
2891 + .rx = &rt2800usb_queue_rx,
2892 + .tx = &rt2800usb_queue_tx,
2893 + .bcn = &rt2800usb_queue_bcn,
2894 + .lib = &rt2800usb_rt2x00_ops,
2895 + .hw = &rt2800usb_mac80211_ops,
2896 +#ifdef CONFIG_RT2X00_LIB_DEBUGFS
2897 + .debugfs = &rt2800usb_rt2x00debug,
2898 +#endif /* CONFIG_RT2X00_LIB_DEBUGFS */
2899 +};
2900 +
2901 +/*
2902 + * rt2800usb module information.
2903 + */
2904 +static struct usb_device_id rt2800usb_device_table[] = {
2905 + /* Abocom */
2906 + { USB_DEVICE(0x07b8, 0x2870), USB_DEVICE_DATA(&rt2800usb_ops) },
2907 + { USB_DEVICE(0x07b8, 0x2770), USB_DEVICE_DATA(&rt2800usb_ops) },
2908 + { USB_DEVICE(0x07b8, 0x3070), USB_DEVICE_DATA(&rt2800usb_ops) },
2909 + { USB_DEVICE(0x07b8, 0x3071), USB_DEVICE_DATA(&rt2800usb_ops) },
2910 + { USB_DEVICE(0x07b8, 0x3072), USB_DEVICE_DATA(&rt2800usb_ops) },
2911 + { USB_DEVICE(0x1482, 0x3c09), USB_DEVICE_DATA(&rt2800usb_ops) },
2912 + /* AirTies */
2913 + { USB_DEVICE(0x1eda, 0x2310), USB_DEVICE_DATA(&rt2800usb_ops) },
2914 + /* Amigo */
2915 + { USB_DEVICE(0x0e0b, 0x9031), USB_DEVICE_DATA(&rt2800usb_ops) },
2916 + { USB_DEVICE(0x0e0b, 0x9041), USB_DEVICE_DATA(&rt2800usb_ops) },
2917 + /* Amit */
2918 + { USB_DEVICE(0x15c5, 0x0008), USB_DEVICE_DATA(&rt2800usb_ops) },
2919 + /* ASUS */
2920 + { USB_DEVICE(0x0b05, 0x1731), USB_DEVICE_DATA(&rt2800usb_ops) },
2921 + { USB_DEVICE(0x0b05, 0x1732), USB_DEVICE_DATA(&rt2800usb_ops) },
2922 + { USB_DEVICE(0x0b05, 0x1742), USB_DEVICE_DATA(&rt2800usb_ops) },
2923 + { USB_DEVICE(0x0b05, 0x1760), USB_DEVICE_DATA(&rt2800usb_ops) },
2924 + { USB_DEVICE(0x0b05, 0x1761), USB_DEVICE_DATA(&rt2800usb_ops) },
2925 + /* AzureWave */
2926 + { USB_DEVICE(0x13d3, 0x3247), USB_DEVICE_DATA(&rt2800usb_ops) },
2927 + { USB_DEVICE(0x13d3, 0x3262), USB_DEVICE_DATA(&rt2800usb_ops) },
2928 + { USB_DEVICE(0x13d3, 0x3273), USB_DEVICE_DATA(&rt2800usb_ops) },
2929 + /* Belkin */
2930 + { USB_DEVICE(0x050d, 0x8053), USB_DEVICE_DATA(&rt2800usb_ops) },
2931 + { USB_DEVICE(0x050d, 0x805c), USB_DEVICE_DATA(&rt2800usb_ops) },
2932 + { USB_DEVICE(0x050d, 0x815c), USB_DEVICE_DATA(&rt2800usb_ops) },
2933 + /* Buffalo */
2934 + { USB_DEVICE(0x0411, 0x012e), USB_DEVICE_DATA(&rt2800usb_ops) },
2935 + /* Conceptronic */
2936 + { USB_DEVICE(0x14b2, 0x3c06), USB_DEVICE_DATA(&rt2800usb_ops) },
2937 + { USB_DEVICE(0x14b2, 0x3c07), USB_DEVICE_DATA(&rt2800usb_ops) },
2938 + { USB_DEVICE(0x14b2, 0x3c09), USB_DEVICE_DATA(&rt2800usb_ops) },
2939 + { USB_DEVICE(0x14b2, 0x3c12), USB_DEVICE_DATA(&rt2800usb_ops) },
2940 + { USB_DEVICE(0x14b2, 0x3c23), USB_DEVICE_DATA(&rt2800usb_ops) },
2941 + { USB_DEVICE(0x14b2, 0x3c25), USB_DEVICE_DATA(&rt2800usb_ops) },
2942 + { USB_DEVICE(0x14b2, 0x3c27), USB_DEVICE_DATA(&rt2800usb_ops) },
2943 + { USB_DEVICE(0x14b2, 0x3c28), USB_DEVICE_DATA(&rt2800usb_ops) },
2944 + /* Corega */
2945 + { USB_DEVICE(0x07aa, 0x002f), USB_DEVICE_DATA(&rt2800usb_ops) },
2946 + { USB_DEVICE(0x07aa, 0x003c), USB_DEVICE_DATA(&rt2800usb_ops) },
2947 + { USB_DEVICE(0x07aa, 0x003f), USB_DEVICE_DATA(&rt2800usb_ops) },
2948 + { USB_DEVICE(0x18c5, 0x0008), USB_DEVICE_DATA(&rt2800usb_ops) },
2949 + { USB_DEVICE(0x18c5, 0x0012), USB_DEVICE_DATA(&rt2800usb_ops) },
2950 + /* D-Link */
2951 + { USB_DEVICE(0x07d1, 0x3c09), USB_DEVICE_DATA(&rt2800usb_ops) },
2952 + { USB_DEVICE(0x07d1, 0x3c11), USB_DEVICE_DATA(&rt2800usb_ops) },
2953 + { USB_DEVICE(0x07d1, 0x3c13), USB_DEVICE_DATA(&rt2800usb_ops) },
2954 + { USB_DEVICE(0x2001, 0x3c09), USB_DEVICE_DATA(&rt2800usb_ops) },
2955 + { USB_DEVICE(0x2001, 0x3c0a), USB_DEVICE_DATA(&rt2800usb_ops) },
2956 + /* Edimax */
2957 + { USB_DEVICE(0x7392, 0x7711), USB_DEVICE_DATA(&rt2800usb_ops) },
2958 + { USB_DEVICE(0x7392, 0x7717), USB_DEVICE_DATA(&rt2800usb_ops) },
2959 + { USB_DEVICE(0x7392, 0x7718), USB_DEVICE_DATA(&rt2800usb_ops) },
2960 + /* EnGenius */
2961 + { USB_DEVICE(0X1740, 0x9701), USB_DEVICE_DATA(&rt2800usb_ops) },
2962 + { USB_DEVICE(0x1740, 0x9702), USB_DEVICE_DATA(&rt2800usb_ops) },
2963 + { USB_DEVICE(0x1740, 0x9703), USB_DEVICE_DATA(&rt2800usb_ops) },
2964 + /* Gemtek */
2965 + { USB_DEVICE(0x15a9, 0x0010), USB_DEVICE_DATA(&rt2800usb_ops) },
2966 + /* Gigabyte */
2967 + { USB_DEVICE(0x1044, 0x800b), USB_DEVICE_DATA(&rt2800usb_ops) },
2968 + { USB_DEVICE(0x1044, 0x800c), USB_DEVICE_DATA(&rt2800usb_ops) },
2969 + { USB_DEVICE(0x1044, 0x800d), USB_DEVICE_DATA(&rt2800usb_ops) },
2970 + /* Hawking */
2971 + { USB_DEVICE(0x0e66, 0x0001), USB_DEVICE_DATA(&rt2800usb_ops) },
2972 + { USB_DEVICE(0x0e66, 0x0003), USB_DEVICE_DATA(&rt2800usb_ops) },
2973 + { USB_DEVICE(0x0e66, 0x0009), USB_DEVICE_DATA(&rt2800usb_ops) },
2974 + { USB_DEVICE(0x0e66, 0x000b), USB_DEVICE_DATA(&rt2800usb_ops) },
2975 + /* LevelOne */
2976 + { USB_DEVICE(0x1740, 0x0605), USB_DEVICE_DATA(&rt2800usb_ops) },
2977 + { USB_DEVICE(0x1740, 0x0615), USB_DEVICE_DATA(&rt2800usb_ops) },
2978 + /* Linksys */
2979 + { USB_DEVICE(0x1737, 0x0071), USB_DEVICE_DATA(&rt2800usb_ops) },
2980 + /* Logitec */
2981 + { USB_DEVICE(0x0789, 0x0162), USB_DEVICE_DATA(&rt2800usb_ops) },
2982 + { USB_DEVICE(0x0789, 0x0163), USB_DEVICE_DATA(&rt2800usb_ops) },
2983 + { USB_DEVICE(0x0789, 0x0164), USB_DEVICE_DATA(&rt2800usb_ops) },
2984 + /* Pegatron */
2985 + { USB_DEVICE(0x1d4d, 0x0002), USB_DEVICE_DATA(&rt2800usb_ops) },
2986 + /* Philips */
2987 + { USB_DEVICE(0x0471, 0x200f), USB_DEVICE_DATA(&rt2800usb_ops) },
2988 + /* Planex */
2989 + { USB_DEVICE(0x2019, 0xed06), USB_DEVICE_DATA(&rt2800usb_ops) },
2990 + { USB_DEVICE(0x2019, 0xab24), USB_DEVICE_DATA(&rt2800usb_ops) },
2991 + { USB_DEVICE(0x2019, 0xab25), USB_DEVICE_DATA(&rt2800usb_ops) },
2992 + /* Qcom */
2993 + { USB_DEVICE(0x18e8, 0x6259), USB_DEVICE_DATA(&rt2800usb_ops) },
2994 + /* Quanta */
2995 + { USB_DEVICE(0x1a32, 0x0304), USB_DEVICE_DATA(&rt2800usb_ops) },
2996 + /* Ralink */
2997 + { USB_DEVICE(0x148f, 0x2070), USB_DEVICE_DATA(&rt2800usb_ops) },
2998 + { USB_DEVICE(0x148f, 0x2770), USB_DEVICE_DATA(&rt2800usb_ops) },
2999 + { USB_DEVICE(0x148f, 0x2870), USB_DEVICE_DATA(&rt2800usb_ops) },
3000 + { USB_DEVICE(0x148f, 0x3070), USB_DEVICE_DATA(&rt2800usb_ops) },
3001 + { USB_DEVICE(0x148f, 0x3071), USB_DEVICE_DATA(&rt2800usb_ops) },
3002 + { USB_DEVICE(0x148f, 0x3072), USB_DEVICE_DATA(&rt2800usb_ops) },
3003 + /* Samsung */
3004 + { USB_DEVICE(0x04e8, 0x2018), USB_DEVICE_DATA(&rt2800usb_ops) },
3005 + /* Siemens */
3006 + { USB_DEVICE(0x129b, 0x1828), USB_DEVICE_DATA(&rt2800usb_ops) },
3007 + /* Sitecom */
3008 + { USB_DEVICE(0x0df6, 0x0017), USB_DEVICE_DATA(&rt2800usb_ops) },
3009 + { USB_DEVICE(0x0df6, 0x002b), USB_DEVICE_DATA(&rt2800usb_ops) },
3010 + { USB_DEVICE(0x0df6, 0x002c), USB_DEVICE_DATA(&rt2800usb_ops) },
3011 + { USB_DEVICE(0x0df6, 0x002d), USB_DEVICE_DATA(&rt2800usb_ops) },
3012 + { USB_DEVICE(0x0df6, 0x0039), USB_DEVICE_DATA(&rt2800usb_ops) },
3013 + { USB_DEVICE(0x0df6, 0x003b), USB_DEVICE_DATA(&rt2800usb_ops) },
3014 + { USB_DEVICE(0x0df6, 0x003c), USB_DEVICE_DATA(&rt2800usb_ops) },
3015 + { USB_DEVICE(0x0df6, 0x003d), USB_DEVICE_DATA(&rt2800usb_ops) },
3016 + { USB_DEVICE(0x0df6, 0x003e), USB_DEVICE_DATA(&rt2800usb_ops) },
3017 + { USB_DEVICE(0x0df6, 0x003f), USB_DEVICE_DATA(&rt2800usb_ops) },
3018 + /* SMC */
3019 + { USB_DEVICE(0x083a, 0x6618), USB_DEVICE_DATA(&rt2800usb_ops) },
3020 + { USB_DEVICE(0x083a, 0x7511), USB_DEVICE_DATA(&rt2800usb_ops) },
3021 + { USB_DEVICE(0x083a, 0x7512), USB_DEVICE_DATA(&rt2800usb_ops) },
3022 + { USB_DEVICE(0x083a, 0x7522), USB_DEVICE_DATA(&rt2800usb_ops) },
3023 + { USB_DEVICE(0x083a, 0x8522), USB_DEVICE_DATA(&rt2800usb_ops) },
3024 + { USB_DEVICE(0x083a, 0xb522), USB_DEVICE_DATA(&rt2800usb_ops) },
3025 + { USB_DEVICE(0x083a, 0xa618), USB_DEVICE_DATA(&rt2800usb_ops) },
3026 + /* Sparklan */
3027 + { USB_DEVICE(0x15a9, 0x0006), USB_DEVICE_DATA(&rt2800usb_ops) },
3028 + /* U-Media*/
3029 + { USB_DEVICE(0x157e, 0x300e), USB_DEVICE_DATA(&rt2800usb_ops) },
3030 + /* ZCOM */
3031 + { USB_DEVICE(0x0cde, 0x0022), USB_DEVICE_DATA(&rt2800usb_ops) },
3032 + { USB_DEVICE(0x0cde, 0x0025), USB_DEVICE_DATA(&rt2800usb_ops) },
3033 + /* Zinwell */
3034 + { USB_DEVICE(0x5a57, 0x0280), USB_DEVICE_DATA(&rt2800usb_ops) },
3035 + { USB_DEVICE(0x5a57, 0x0282), USB_DEVICE_DATA(&rt2800usb_ops) },
3036 + /* Zyxel */
3037 + { USB_DEVICE(0x0586, 0x3416), USB_DEVICE_DATA(&rt2800usb_ops) },
3038 + { 0, }
3039 +};
3040 +
3041 +MODULE_AUTHOR(DRV_PROJECT);
3042 +MODULE_VERSION(DRV_VERSION);
3043 +MODULE_DESCRIPTION("Ralink RT2800 USB Wireless LAN driver.");
3044 +MODULE_SUPPORTED_DEVICE("Ralink RT2870 USB chipset based cards");
3045 +MODULE_DEVICE_TABLE(usb, rt2800usb_device_table);
3046 +MODULE_FIRMWARE(FIRMWARE_RT2870);
3047 +MODULE_LICENSE("GPL");
3048 +
3049 +static struct usb_driver rt2800usb_driver = {
3050 + .name = KBUILD_MODNAME,
3051 + .id_table = rt2800usb_device_table,
3052 + .probe = rt2x00usb_probe,
3053 + .disconnect = rt2x00usb_disconnect,
3054 + .suspend = rt2x00usb_suspend,
3055 + .resume = rt2x00usb_resume,
3056 +};
3057 +
3058 +static int __init rt2800usb_init(void)
3059 +{
3060 + return usb_register(&rt2800usb_driver);
3061 +}
3062 +
3063 +static void __exit rt2800usb_exit(void)
3064 +{
3065 + usb_deregister(&rt2800usb_driver);
3066 +}
3067 +
3068 +module_init(rt2800usb_init);
3069 +module_exit(rt2800usb_exit);
3070 --- /dev/null
3071 +++ b/drivers/net/wireless/rt2x00/rt2800usb.h
3072 @@ -0,0 +1,1932 @@
3073 +/*
3074 + Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
3075 + <http://rt2x00.serialmonkey.com>
3076 +
3077 + This program is free software; you can redistribute it and/or modify
3078 + it under the terms of the GNU General Public License as published by
3079 + the Free Software Foundation; either version 2 of the License, or
3080 + (at your option) any later version.
3081 +
3082 + This program is distributed in the hope that it will be useful,
3083 + but WITHOUT ANY WARRANTY; without even the implied warranty of
3084 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3085 + GNU General Public License for more details.
3086 +
3087 + You should have received a copy of the GNU General Public License
3088 + along with this program; if not, write to the
3089 + Free Software Foundation, Inc.,
3090 + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3091 + */
3092 +
3093 +/*
3094 + Module: rt2800usb
3095 + Abstract: Data structures and registers for the rt2800usb module.
3096 + Supported chipsets: RT2800U.
3097 + */
3098 +
3099 +#ifndef RT2800USB_H
3100 +#define RT2800USB_H
3101 +
3102 +/*
3103 + * RF chip defines.
3104 + *
3105 + * RF2820 2.4G 2T3R
3106 + * RF2850 2.4G/5G 2T3R
3107 + * RF2720 2.4G 1T2R
3108 + * RF2750 2.4G/5G 1T2R
3109 + * RF3020 2.4G 1T1R
3110 + * RF2020 2.4G B/G
3111 + */
3112 +#define RF2820 0x0001
3113 +#define RF2850 0x0002
3114 +#define RF2720 0x0003
3115 +#define RF2750 0x0004
3116 +#define RF3020 0x0005
3117 +#define RF2020 0x0006
3118 +
3119 +/*
3120 + * RT2870 version
3121 + */
3122 +#define RT2860C_VERSION 0x28600100
3123 +#define RT2860D_VERSION 0x28600101
3124 +#define RT2880E_VERSION 0x28720200
3125 +#define RT2883_VERSION 0x28830300
3126 +#define RT3070_VERSION 0x30700200
3127 +
3128 +/*
3129 + * Signal information.
3130 + * Defaul offset is required for RSSI <-> dBm conversion.
3131 + */
3132 +#define DEFAULT_RSSI_OFFSET 120 /* FIXME */
3133 +
3134 +/*
3135 + * Register layout information.
3136 + */
3137 +#define CSR_REG_BASE 0x1000
3138 +#define CSR_REG_SIZE 0x0800
3139 +#define EEPROM_BASE 0x0000
3140 +#define EEPROM_SIZE 0x0110
3141 +#define BBP_BASE 0x0000
3142 +#define BBP_SIZE 0x0080
3143 +#define RF_BASE 0x0004
3144 +#define RF_SIZE 0x0010
3145 +
3146 +/*
3147 + * Number of TX queues.
3148 + */
3149 +#define NUM_TX_QUEUES 4
3150 +
3151 +/*
3152 + * USB registers.
3153 + */
3154 +
3155 +/*
3156 + * HOST-MCU shared memory
3157 + */
3158 +#define HOST_CMD_CSR 0x0404
3159 +#define HOST_CMD_CSR_HOST_COMMAND FIELD32(0x000000ff)
3160 +
3161 +/*
3162 + * INT_SOURCE_CSR: Interrupt source register.
3163 + * Write one to clear corresponding bit.
3164 + * TX_FIFO_STATUS: FIFO Statistics is full, sw should read 0x171c
3165 + */
3166 +#define INT_SOURCE_CSR 0x0200
3167 +#define INT_SOURCE_CSR_RXDELAYINT FIELD32(0x00000001)
3168 +#define INT_SOURCE_CSR_TXDELAYINT FIELD32(0x00000002)
3169 +#define INT_SOURCE_CSR_RX_DONE FIELD32(0x00000004)
3170 +#define INT_SOURCE_CSR_AC0_DMA_DONE FIELD32(0x00000008)
3171 +#define INT_SOURCE_CSR_AC1_DMA_DONE FIELD32(0x00000010)
3172 +#define INT_SOURCE_CSR_AC2_DMA_DONE FIELD32(0x00000020)
3173 +#define INT_SOURCE_CSR_AC3_DMA_DONE FIELD32(0x00000040)
3174 +#define INT_SOURCE_CSR_HCCA_DMA_DONE FIELD32(0x00000080)
3175 +#define INT_SOURCE_CSR_MGMT_DMA_DONE FIELD32(0x00000100)
3176 +#define INT_SOURCE_CSR_MCU_COMMAND FIELD32(0x00000200)
3177 +#define INT_SOURCE_CSR_RXTX_COHERENT FIELD32(0x00000400)
3178 +#define INT_SOURCE_CSR_TBTT FIELD32(0x00000800)
3179 +#define INT_SOURCE_CSR_PRE_TBTT FIELD32(0x00001000)
3180 +#define INT_SOURCE_CSR_TX_FIFO_STATUS FIELD32(0x00002000)
3181 +#define INT_SOURCE_CSR_AUTO_WAKEUP FIELD32(0x00004000)
3182 +#define INT_SOURCE_CSR_GPTIMER FIELD32(0x00008000)
3183 +#define INT_SOURCE_CSR_RX_COHERENT FIELD32(0x00010000)
3184 +#define INT_SOURCE_CSR_TX_COHERENT FIELD32(0x00020000)
3185 +
3186 +/*
3187 + * INT_MASK_CSR: Interrupt MASK register. 1: the interrupt is mask OFF.
3188 + */
3189 +#define INT_MASK_CSR 0x0204
3190 +#define INT_MASK_CSR_RXDELAYINT FIELD32(0x00000001)
3191 +#define INT_MASK_CSR_TXDELAYINT FIELD32(0x00000002)
3192 +#define INT_MASK_CSR_RX_DONE FIELD32(0x00000004)
3193 +#define INT_MASK_CSR_AC0_DMA_DONE FIELD32(0x00000008)
3194 +#define INT_MASK_CSR_AC1_DMA_DONE FIELD32(0x00000010)
3195 +#define INT_MASK_CSR_AC2_DMA_DONE FIELD32(0x00000020)
3196 +#define INT_MASK_CSR_AC3_DMA_DONE FIELD32(0x00000040)
3197 +#define INT_MASK_CSR_HCCA_DMA_DONE FIELD32(0x00000080)
3198 +#define INT_MASK_CSR_MGMT_DMA_DONE FIELD32(0x00000100)
3199 +#define INT_MASK_CSR_MCU_COMMAND FIELD32(0x00000200)
3200 +#define INT_MASK_CSR_RXTX_COHERENT FIELD32(0x00000400)
3201 +#define INT_MASK_CSR_TBTT FIELD32(0x00000800)
3202 +#define INT_MASK_CSR_PRE_TBTT FIELD32(0x00001000)
3203 +#define INT_MASK_CSR_TX_FIFO_STATUS FIELD32(0x00002000)
3204 +#define INT_MASK_CSR_AUTO_WAKEUP FIELD32(0x00004000)
3205 +#define INT_MASK_CSR_GPTIMER FIELD32(0x00008000)
3206 +#define INT_MASK_CSR_RX_COHERENT FIELD32(0x00010000)
3207 +#define INT_MASK_CSR_TX_COHERENT FIELD32(0x00020000)
3208 +
3209 +/*
3210 + * WPDMA_GLO_CFG
3211 + */
3212 +#define WPDMA_GLO_CFG 0x0208
3213 +#define WPDMA_GLO_CFG_ENABLE_TX_DMA FIELD32(0x00000001)
3214 +#define WPDMA_GLO_CFG_TX_DMA_BUSY FIELD32(0x00000002)
3215 +#define WPDMA_GLO_CFG_ENABLE_RX_DMA FIELD32(0x00000004)
3216 +#define WPDMA_GLO_CFG_RX_DMA_BUSY FIELD32(0x00000008)
3217 +#define WPDMA_GLO_CFG_WP_DMA_BURST_SIZE FIELD32(0x00000030)
3218 +#define WPDMA_GLO_CFG_TX_WRITEBACK_DONE FIELD32(0x00000040)
3219 +#define WPDMA_GLO_CFG_BIG_ENDIAN FIELD32(0x00000080)
3220 +#define WPDMA_GLO_CFG_RX_HDR_SCATTER FIELD32(0x0000ff00)
3221 +#define WPDMA_GLO_CFG_HDR_SEG_LEN FIELD32(0xffff0000)
3222 +
3223 +/*
3224 + * WPDMA_RST_IDX
3225 + */
3226 +#define WPDMA_RST_IDX 0x020c
3227 +#define WPDMA_RST_IDX_DTX_IDX0 FIELD32(0x00000001)
3228 +#define WPDMA_RST_IDX_DTX_IDX1 FIELD32(0x00000002)
3229 +#define WPDMA_RST_IDX_DTX_IDX2 FIELD32(0x00000004)
3230 +#define WPDMA_RST_IDX_DTX_IDX3 FIELD32(0x00000008)
3231 +#define WPDMA_RST_IDX_DTX_IDX4 FIELD32(0x00000010)
3232 +#define WPDMA_RST_IDX_DTX_IDX5 FIELD32(0x00000020)
3233 +#define WPDMA_RST_IDX_DRX_IDX0 FIELD32(0x00010000)
3234 +
3235 +/*
3236 + * DELAY_INT_CFG
3237 + */
3238 +#define DELAY_INT_CFG 0x0210
3239 +#define DELAY_INT_CFG_RXMAX_PTIME FIELD32(0x000000ff)
3240 +#define DELAY_INT_CFG_RXMAX_PINT FIELD32(0x00007f00)
3241 +#define DELAY_INT_CFG_RXDLY_INT_EN FIELD32(0x00008000)
3242 +#define DELAY_INT_CFG_TXMAX_PTIME FIELD32(0x00ff0000)
3243 +#define DELAY_INT_CFG_TXMAX_PINT FIELD32(0x7f000000)
3244 +#define DELAY_INT_CFG_TXDLY_INT_EN FIELD32(0x80000000)
3245 +
3246 +/*
3247 + * WMM_AIFSN_CFG: Aifsn for each EDCA AC
3248 + * AIFSN0: AC_BE