rt2x00: Implement Powersaving
[linux-2.6.git] / drivers / net / wireless / rt2x00 / rt2500usb.c
1 /*
2         Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3         <http://rt2x00.serialmonkey.com>
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the
17         Free Software Foundation, Inc.,
18         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 /*
22         Module: rt2500usb
23         Abstract: rt2500usb device specific routines.
24         Supported chipsets: RT2570.
25  */
26
27 #include <linux/delay.h>
28 #include <linux/etherdevice.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/usb.h>
33
34 #include "rt2x00.h"
35 #include "rt2x00usb.h"
36 #include "rt2500usb.h"
37
38 /*
39  * Allow hardware encryption to be disabled.
40  */
41 static int modparam_nohwcrypt = 0;
42 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
43 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
44
45 /*
46  * Register access.
47  * All access to the CSR registers will go through the methods
48  * rt2500usb_register_read and rt2500usb_register_write.
49  * BBP and RF register require indirect register access,
50  * and use the CSR registers BBPCSR and RFCSR to achieve this.
51  * These indirect registers work with busy bits,
52  * and we will try maximal REGISTER_BUSY_COUNT times to access
53  * the register while taking a REGISTER_BUSY_DELAY us delay
54  * between each attampt. When the busy bit is still set at that time,
55  * the access attempt is considered to have failed,
56  * and we will print an error.
57  * If the csr_mutex is already held then the _lock variants must
58  * be used instead.
59  */
60 static inline void rt2500usb_register_read(struct rt2x00_dev *rt2x00dev,
61                                            const unsigned int offset,
62                                            u16 *value)
63 {
64         __le16 reg;
65         rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
66                                       USB_VENDOR_REQUEST_IN, offset,
67                                       &reg, sizeof(reg), REGISTER_TIMEOUT);
68         *value = le16_to_cpu(reg);
69 }
70
71 static inline void rt2500usb_register_read_lock(struct rt2x00_dev *rt2x00dev,
72                                                 const unsigned int offset,
73                                                 u16 *value)
74 {
75         __le16 reg;
76         rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_READ,
77                                        USB_VENDOR_REQUEST_IN, offset,
78                                        &reg, sizeof(reg), REGISTER_TIMEOUT);
79         *value = le16_to_cpu(reg);
80 }
81
82 static inline void rt2500usb_register_multiread(struct rt2x00_dev *rt2x00dev,
83                                                 const unsigned int offset,
84                                                 void *value, const u16 length)
85 {
86         rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_READ,
87                                       USB_VENDOR_REQUEST_IN, offset,
88                                       value, length,
89                                       REGISTER_TIMEOUT16(length));
90 }
91
92 static inline void rt2500usb_register_write(struct rt2x00_dev *rt2x00dev,
93                                             const unsigned int offset,
94                                             u16 value)
95 {
96         __le16 reg = cpu_to_le16(value);
97         rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
98                                       USB_VENDOR_REQUEST_OUT, offset,
99                                       &reg, sizeof(reg), REGISTER_TIMEOUT);
100 }
101
102 static inline void rt2500usb_register_write_lock(struct rt2x00_dev *rt2x00dev,
103                                                  const unsigned int offset,
104                                                  u16 value)
105 {
106         __le16 reg = cpu_to_le16(value);
107         rt2x00usb_vendor_req_buff_lock(rt2x00dev, USB_MULTI_WRITE,
108                                        USB_VENDOR_REQUEST_OUT, offset,
109                                        &reg, sizeof(reg), REGISTER_TIMEOUT);
110 }
111
112 static inline void rt2500usb_register_multiwrite(struct rt2x00_dev *rt2x00dev,
113                                                  const unsigned int offset,
114                                                  void *value, const u16 length)
115 {
116         rt2x00usb_vendor_request_buff(rt2x00dev, USB_MULTI_WRITE,
117                                       USB_VENDOR_REQUEST_OUT, offset,
118                                       value, length,
119                                       REGISTER_TIMEOUT16(length));
120 }
121
122 static int rt2500usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
123                                   const unsigned int offset,
124                                   struct rt2x00_field16 field,
125                                   u16 *reg)
126 {
127         unsigned int i;
128
129         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
130                 rt2500usb_register_read_lock(rt2x00dev, offset, reg);
131                 if (!rt2x00_get_field16(*reg, field))
132                         return 1;
133                 udelay(REGISTER_BUSY_DELAY);
134         }
135
136         ERROR(rt2x00dev, "Indirect register access failed: "
137               "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
138         *reg = ~0;
139
140         return 0;
141 }
142
143 #define WAIT_FOR_BBP(__dev, __reg) \
144         rt2500usb_regbusy_read((__dev), PHY_CSR8, PHY_CSR8_BUSY, (__reg))
145 #define WAIT_FOR_RF(__dev, __reg) \
146         rt2500usb_regbusy_read((__dev), PHY_CSR10, PHY_CSR10_RF_BUSY, (__reg))
147
148 static void rt2500usb_bbp_write(struct rt2x00_dev *rt2x00dev,
149                                 const unsigned int word, const u8 value)
150 {
151         u16 reg;
152
153         mutex_lock(&rt2x00dev->csr_mutex);
154
155         /*
156          * Wait until the BBP becomes available, afterwards we
157          * can safely write the new data into the register.
158          */
159         if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
160                 reg = 0;
161                 rt2x00_set_field16(&reg, PHY_CSR7_DATA, value);
162                 rt2x00_set_field16(&reg, PHY_CSR7_REG_ID, word);
163                 rt2x00_set_field16(&reg, PHY_CSR7_READ_CONTROL, 0);
164
165                 rt2500usb_register_write_lock(rt2x00dev, PHY_CSR7, reg);
166         }
167
168         mutex_unlock(&rt2x00dev->csr_mutex);
169 }
170
171 static void rt2500usb_bbp_read(struct rt2x00_dev *rt2x00dev,
172                                const unsigned int word, u8 *value)
173 {
174         u16 reg;
175
176         mutex_lock(&rt2x00dev->csr_mutex);
177
178         /*
179          * Wait until the BBP becomes available, afterwards we
180          * can safely write the read request into the register.
181          * After the data has been written, we wait until hardware
182          * returns the correct value, if at any time the register
183          * doesn't become available in time, reg will be 0xffffffff
184          * which means we return 0xff to the caller.
185          */
186         if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
187                 reg = 0;
188                 rt2x00_set_field16(&reg, PHY_CSR7_REG_ID, word);
189                 rt2x00_set_field16(&reg, PHY_CSR7_READ_CONTROL, 1);
190
191                 rt2500usb_register_write_lock(rt2x00dev, PHY_CSR7, reg);
192
193                 if (WAIT_FOR_BBP(rt2x00dev, &reg))
194                         rt2500usb_register_read_lock(rt2x00dev, PHY_CSR7, &reg);
195         }
196
197         *value = rt2x00_get_field16(reg, PHY_CSR7_DATA);
198
199         mutex_unlock(&rt2x00dev->csr_mutex);
200 }
201
202 static void rt2500usb_rf_write(struct rt2x00_dev *rt2x00dev,
203                                const unsigned int word, const u32 value)
204 {
205         u16 reg;
206
207         if (!word)
208                 return;
209
210         mutex_lock(&rt2x00dev->csr_mutex);
211
212         /*
213          * Wait until the RF becomes available, afterwards we
214          * can safely write the new data into the register.
215          */
216         if (WAIT_FOR_RF(rt2x00dev, &reg)) {
217                 reg = 0;
218                 rt2x00_set_field16(&reg, PHY_CSR9_RF_VALUE, value);
219                 rt2500usb_register_write_lock(rt2x00dev, PHY_CSR9, reg);
220
221                 reg = 0;
222                 rt2x00_set_field16(&reg, PHY_CSR10_RF_VALUE, value >> 16);
223                 rt2x00_set_field16(&reg, PHY_CSR10_RF_NUMBER_OF_BITS, 20);
224                 rt2x00_set_field16(&reg, PHY_CSR10_RF_IF_SELECT, 0);
225                 rt2x00_set_field16(&reg, PHY_CSR10_RF_BUSY, 1);
226
227                 rt2500usb_register_write_lock(rt2x00dev, PHY_CSR10, reg);
228                 rt2x00_rf_write(rt2x00dev, word, value);
229         }
230
231         mutex_unlock(&rt2x00dev->csr_mutex);
232 }
233
234 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
235 static void _rt2500usb_register_read(struct rt2x00_dev *rt2x00dev,
236                                      const unsigned int offset,
237                                      u32 *value)
238 {
239         rt2500usb_register_read(rt2x00dev, offset, (u16 *)value);
240 }
241
242 static void _rt2500usb_register_write(struct rt2x00_dev *rt2x00dev,
243                                       const unsigned int offset,
244                                       u32 value)
245 {
246         rt2500usb_register_write(rt2x00dev, offset, value);
247 }
248
249 static const struct rt2x00debug rt2500usb_rt2x00debug = {
250         .owner  = THIS_MODULE,
251         .csr    = {
252                 .read           = _rt2500usb_register_read,
253                 .write          = _rt2500usb_register_write,
254                 .flags          = RT2X00DEBUGFS_OFFSET,
255                 .word_base      = CSR_REG_BASE,
256                 .word_size      = sizeof(u16),
257                 .word_count     = CSR_REG_SIZE / sizeof(u16),
258         },
259         .eeprom = {
260                 .read           = rt2x00_eeprom_read,
261                 .write          = rt2x00_eeprom_write,
262                 .word_base      = EEPROM_BASE,
263                 .word_size      = sizeof(u16),
264                 .word_count     = EEPROM_SIZE / sizeof(u16),
265         },
266         .bbp    = {
267                 .read           = rt2500usb_bbp_read,
268                 .write          = rt2500usb_bbp_write,
269                 .word_base      = BBP_BASE,
270                 .word_size      = sizeof(u8),
271                 .word_count     = BBP_SIZE / sizeof(u8),
272         },
273         .rf     = {
274                 .read           = rt2x00_rf_read,
275                 .write          = rt2500usb_rf_write,
276                 .word_base      = RF_BASE,
277                 .word_size      = sizeof(u32),
278                 .word_count     = RF_SIZE / sizeof(u32),
279         },
280 };
281 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
282
283 #ifdef CONFIG_RT2X00_LIB_LEDS
284 static void rt2500usb_brightness_set(struct led_classdev *led_cdev,
285                                      enum led_brightness brightness)
286 {
287         struct rt2x00_led *led =
288             container_of(led_cdev, struct rt2x00_led, led_dev);
289         unsigned int enabled = brightness != LED_OFF;
290         u16 reg;
291
292         rt2500usb_register_read(led->rt2x00dev, MAC_CSR20, &reg);
293
294         if (led->type == LED_TYPE_RADIO || led->type == LED_TYPE_ASSOC)
295                 rt2x00_set_field16(&reg, MAC_CSR20_LINK, enabled);
296         else if (led->type == LED_TYPE_ACTIVITY)
297                 rt2x00_set_field16(&reg, MAC_CSR20_ACTIVITY, enabled);
298
299         rt2500usb_register_write(led->rt2x00dev, MAC_CSR20, reg);
300 }
301
302 static int rt2500usb_blink_set(struct led_classdev *led_cdev,
303                                unsigned long *delay_on,
304                                unsigned long *delay_off)
305 {
306         struct rt2x00_led *led =
307             container_of(led_cdev, struct rt2x00_led, led_dev);
308         u16 reg;
309
310         rt2500usb_register_read(led->rt2x00dev, MAC_CSR21, &reg);
311         rt2x00_set_field16(&reg, MAC_CSR21_ON_PERIOD, *delay_on);
312         rt2x00_set_field16(&reg, MAC_CSR21_OFF_PERIOD, *delay_off);
313         rt2500usb_register_write(led->rt2x00dev, MAC_CSR21, reg);
314
315         return 0;
316 }
317
318 static void rt2500usb_init_led(struct rt2x00_dev *rt2x00dev,
319                                struct rt2x00_led *led,
320                                enum led_type type)
321 {
322         led->rt2x00dev = rt2x00dev;
323         led->type = type;
324         led->led_dev.brightness_set = rt2500usb_brightness_set;
325         led->led_dev.blink_set = rt2500usb_blink_set;
326         led->flags = LED_INITIALIZED;
327 }
328 #endif /* CONFIG_RT2X00_LIB_LEDS */
329
330 /*
331  * Configuration handlers.
332  */
333
334 /*
335  * rt2500usb does not differentiate between shared and pairwise
336  * keys, so we should use the same function for both key types.
337  */
338 static int rt2500usb_config_key(struct rt2x00_dev *rt2x00dev,
339                                 struct rt2x00lib_crypto *crypto,
340                                 struct ieee80211_key_conf *key)
341 {
342         int timeout;
343         u32 mask;
344         u16 reg;
345
346         if (crypto->cmd == SET_KEY) {
347                 /*
348                  * Pairwise key will always be entry 0, but this
349                  * could collide with a shared key on the same
350                  * position...
351                  */
352                 mask = TXRX_CSR0_KEY_ID.bit_mask;
353
354                 rt2500usb_register_read(rt2x00dev, TXRX_CSR0, &reg);
355                 reg &= mask;
356
357                 if (reg && reg == mask)
358                         return -ENOSPC;
359
360                 reg = rt2x00_get_field16(reg, TXRX_CSR0_KEY_ID);
361
362                 key->hw_key_idx += reg ? ffz(reg) : 0;
363
364                 /*
365                  * The encryption key doesn't fit within the CSR cache,
366                  * this means we should allocate it seperately and use
367                  * rt2x00usb_vendor_request() to send the key to the hardware.
368                  */
369                 reg = KEY_ENTRY(key->hw_key_idx);
370                 timeout = REGISTER_TIMEOUT32(sizeof(crypto->key));
371                 rt2x00usb_vendor_request_large_buff(rt2x00dev, USB_MULTI_WRITE,
372                                                     USB_VENDOR_REQUEST_OUT, reg,
373                                                     crypto->key,
374                                                     sizeof(crypto->key),
375                                                     timeout);
376
377                 /*
378                  * The driver does not support the IV/EIV generation
379                  * in hardware. However it demands the data to be provided
380                  * both seperately as well as inside the frame.
381                  * We already provided the CONFIG_CRYPTO_COPY_IV to rt2x00lib
382                  * to ensure rt2x00lib will not strip the data from the
383                  * frame after the copy, now we must tell mac80211
384                  * to generate the IV/EIV data.
385                  */
386                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
387                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
388         }
389
390         /*
391          * TXRX_CSR0_KEY_ID contains only single-bit fields to indicate
392          * a particular key is valid.
393          */
394         rt2500usb_register_read(rt2x00dev, TXRX_CSR0, &reg);
395         rt2x00_set_field16(&reg, TXRX_CSR0_ALGORITHM, crypto->cipher);
396         rt2x00_set_field16(&reg, TXRX_CSR0_IV_OFFSET, IEEE80211_HEADER);
397
398         mask = rt2x00_get_field16(reg, TXRX_CSR0_KEY_ID);
399         if (crypto->cmd == SET_KEY)
400                 mask |= 1 << key->hw_key_idx;
401         else if (crypto->cmd == DISABLE_KEY)
402                 mask &= ~(1 << key->hw_key_idx);
403         rt2x00_set_field16(&reg, TXRX_CSR0_KEY_ID, mask);
404         rt2500usb_register_write(rt2x00dev, TXRX_CSR0, reg);
405
406         return 0;
407 }
408
409 static void rt2500usb_config_filter(struct rt2x00_dev *rt2x00dev,
410                                     const unsigned int filter_flags)
411 {
412         u16 reg;
413
414         /*
415          * Start configuration steps.
416          * Note that the version error will always be dropped
417          * and broadcast frames will always be accepted since
418          * there is no filter for it at this time.
419          */
420         rt2500usb_register_read(rt2x00dev, TXRX_CSR2, &reg);
421         rt2x00_set_field16(&reg, TXRX_CSR2_DROP_CRC,
422                            !(filter_flags & FIF_FCSFAIL));
423         rt2x00_set_field16(&reg, TXRX_CSR2_DROP_PHYSICAL,
424                            !(filter_flags & FIF_PLCPFAIL));
425         rt2x00_set_field16(&reg, TXRX_CSR2_DROP_CONTROL,
426                            !(filter_flags & FIF_CONTROL));
427         rt2x00_set_field16(&reg, TXRX_CSR2_DROP_NOT_TO_ME,
428                            !(filter_flags & FIF_PROMISC_IN_BSS));
429         rt2x00_set_field16(&reg, TXRX_CSR2_DROP_TODS,
430                            !(filter_flags & FIF_PROMISC_IN_BSS) &&
431                            !rt2x00dev->intf_ap_count);
432         rt2x00_set_field16(&reg, TXRX_CSR2_DROP_VERSION_ERROR, 1);
433         rt2x00_set_field16(&reg, TXRX_CSR2_DROP_MULTICAST,
434                            !(filter_flags & FIF_ALLMULTI));
435         rt2x00_set_field16(&reg, TXRX_CSR2_DROP_BROADCAST, 0);
436         rt2500usb_register_write(rt2x00dev, TXRX_CSR2, reg);
437 }
438
439 static void rt2500usb_config_intf(struct rt2x00_dev *rt2x00dev,
440                                   struct rt2x00_intf *intf,
441                                   struct rt2x00intf_conf *conf,
442                                   const unsigned int flags)
443 {
444         unsigned int bcn_preload;
445         u16 reg;
446
447         if (flags & CONFIG_UPDATE_TYPE) {
448                 /*
449                  * Enable beacon config
450                  */
451                 bcn_preload = PREAMBLE + GET_DURATION(IEEE80211_HEADER, 20);
452                 rt2500usb_register_read(rt2x00dev, TXRX_CSR20, &reg);
453                 rt2x00_set_field16(&reg, TXRX_CSR20_OFFSET, bcn_preload >> 6);
454                 rt2x00_set_field16(&reg, TXRX_CSR20_BCN_EXPECT_WINDOW,
455                                    2 * (conf->type != NL80211_IFTYPE_STATION));
456                 rt2500usb_register_write(rt2x00dev, TXRX_CSR20, reg);
457
458                 /*
459                  * Enable synchronisation.
460                  */
461                 rt2500usb_register_read(rt2x00dev, TXRX_CSR18, &reg);
462                 rt2x00_set_field16(&reg, TXRX_CSR18_OFFSET, 0);
463                 rt2500usb_register_write(rt2x00dev, TXRX_CSR18, reg);
464
465                 rt2500usb_register_read(rt2x00dev, TXRX_CSR19, &reg);
466                 rt2x00_set_field16(&reg, TXRX_CSR19_TSF_COUNT, 1);
467                 rt2x00_set_field16(&reg, TXRX_CSR19_TSF_SYNC, conf->sync);
468                 rt2x00_set_field16(&reg, TXRX_CSR19_TBCN, 1);
469                 rt2500usb_register_write(rt2x00dev, TXRX_CSR19, reg);
470         }
471
472         if (flags & CONFIG_UPDATE_MAC)
473                 rt2500usb_register_multiwrite(rt2x00dev, MAC_CSR2, conf->mac,
474                                               (3 * sizeof(__le16)));
475
476         if (flags & CONFIG_UPDATE_BSSID)
477                 rt2500usb_register_multiwrite(rt2x00dev, MAC_CSR5, conf->bssid,
478                                               (3 * sizeof(__le16)));
479 }
480
481 static void rt2500usb_config_erp(struct rt2x00_dev *rt2x00dev,
482                                  struct rt2x00lib_erp *erp)
483 {
484         u16 reg;
485
486         rt2500usb_register_read(rt2x00dev, TXRX_CSR1, &reg);
487         rt2x00_set_field16(&reg, TXRX_CSR1_ACK_TIMEOUT, erp->ack_timeout);
488         rt2500usb_register_write(rt2x00dev, TXRX_CSR1, reg);
489
490         rt2500usb_register_read(rt2x00dev, TXRX_CSR10, &reg);
491         rt2x00_set_field16(&reg, TXRX_CSR10_AUTORESPOND_PREAMBLE,
492                            !!erp->short_preamble);
493         rt2500usb_register_write(rt2x00dev, TXRX_CSR10, reg);
494
495         rt2500usb_register_write(rt2x00dev, TXRX_CSR11, erp->basic_rates);
496
497         rt2500usb_register_write(rt2x00dev, MAC_CSR10, erp->slot_time);
498         rt2500usb_register_write(rt2x00dev, MAC_CSR11, erp->sifs);
499         rt2500usb_register_write(rt2x00dev, MAC_CSR12, erp->eifs);
500 }
501
502 static void rt2500usb_config_ant(struct rt2x00_dev *rt2x00dev,
503                                  struct antenna_setup *ant)
504 {
505         u8 r2;
506         u8 r14;
507         u16 csr5;
508         u16 csr6;
509
510         /*
511          * We should never come here because rt2x00lib is supposed
512          * to catch this and send us the correct antenna explicitely.
513          */
514         BUG_ON(ant->rx == ANTENNA_SW_DIVERSITY ||
515                ant->tx == ANTENNA_SW_DIVERSITY);
516
517         rt2500usb_bbp_read(rt2x00dev, 2, &r2);
518         rt2500usb_bbp_read(rt2x00dev, 14, &r14);
519         rt2500usb_register_read(rt2x00dev, PHY_CSR5, &csr5);
520         rt2500usb_register_read(rt2x00dev, PHY_CSR6, &csr6);
521
522         /*
523          * Configure the TX antenna.
524          */
525         switch (ant->tx) {
526         case ANTENNA_HW_DIVERSITY:
527                 rt2x00_set_field8(&r2, BBP_R2_TX_ANTENNA, 1);
528                 rt2x00_set_field16(&csr5, PHY_CSR5_CCK, 1);
529                 rt2x00_set_field16(&csr6, PHY_CSR6_OFDM, 1);
530                 break;
531         case ANTENNA_A:
532                 rt2x00_set_field8(&r2, BBP_R2_TX_ANTENNA, 0);
533                 rt2x00_set_field16(&csr5, PHY_CSR5_CCK, 0);
534                 rt2x00_set_field16(&csr6, PHY_CSR6_OFDM, 0);
535                 break;
536         case ANTENNA_B:
537         default:
538                 rt2x00_set_field8(&r2, BBP_R2_TX_ANTENNA, 2);
539                 rt2x00_set_field16(&csr5, PHY_CSR5_CCK, 2);
540                 rt2x00_set_field16(&csr6, PHY_CSR6_OFDM, 2);
541                 break;
542         }
543
544         /*
545          * Configure the RX antenna.
546          */
547         switch (ant->rx) {
548         case ANTENNA_HW_DIVERSITY:
549                 rt2x00_set_field8(&r14, BBP_R14_RX_ANTENNA, 1);
550                 break;
551         case ANTENNA_A:
552                 rt2x00_set_field8(&r14, BBP_R14_RX_ANTENNA, 0);
553                 break;
554         case ANTENNA_B:
555         default:
556                 rt2x00_set_field8(&r14, BBP_R14_RX_ANTENNA, 2);
557                 break;
558         }
559
560         /*
561          * RT2525E and RT5222 need to flip TX I/Q
562          */
563         if (rt2x00_rf(&rt2x00dev->chip, RF2525E) ||
564             rt2x00_rf(&rt2x00dev->chip, RF5222)) {
565                 rt2x00_set_field8(&r2, BBP_R2_TX_IQ_FLIP, 1);
566                 rt2x00_set_field16(&csr5, PHY_CSR5_CCK_FLIP, 1);
567                 rt2x00_set_field16(&csr6, PHY_CSR6_OFDM_FLIP, 1);
568
569                 /*
570                  * RT2525E does not need RX I/Q Flip.
571                  */
572                 if (rt2x00_rf(&rt2x00dev->chip, RF2525E))
573                         rt2x00_set_field8(&r14, BBP_R14_RX_IQ_FLIP, 0);
574         } else {
575                 rt2x00_set_field16(&csr5, PHY_CSR5_CCK_FLIP, 0);
576                 rt2x00_set_field16(&csr6, PHY_CSR6_OFDM_FLIP, 0);
577         }
578
579         rt2500usb_bbp_write(rt2x00dev, 2, r2);
580         rt2500usb_bbp_write(rt2x00dev, 14, r14);
581         rt2500usb_register_write(rt2x00dev, PHY_CSR5, csr5);
582         rt2500usb_register_write(rt2x00dev, PHY_CSR6, csr6);
583 }
584
585 static void rt2500usb_config_channel(struct rt2x00_dev *rt2x00dev,
586                                      struct rf_channel *rf, const int txpower)
587 {
588         /*
589          * Set TXpower.
590          */
591         rt2x00_set_field32(&rf->rf3, RF3_TXPOWER, TXPOWER_TO_DEV(txpower));
592
593         /*
594          * For RT2525E we should first set the channel to half band higher.
595          */
596         if (rt2x00_rf(&rt2x00dev->chip, RF2525E)) {
597                 static const u32 vals[] = {
598                         0x000008aa, 0x000008ae, 0x000008ae, 0x000008b2,
599                         0x000008b2, 0x000008b6, 0x000008b6, 0x000008ba,
600                         0x000008ba, 0x000008be, 0x000008b7, 0x00000902,
601                         0x00000902, 0x00000906
602                 };
603
604                 rt2500usb_rf_write(rt2x00dev, 2, vals[rf->channel - 1]);
605                 if (rf->rf4)
606                         rt2500usb_rf_write(rt2x00dev, 4, rf->rf4);
607         }
608
609         rt2500usb_rf_write(rt2x00dev, 1, rf->rf1);
610         rt2500usb_rf_write(rt2x00dev, 2, rf->rf2);
611         rt2500usb_rf_write(rt2x00dev, 3, rf->rf3);
612         if (rf->rf4)
613                 rt2500usb_rf_write(rt2x00dev, 4, rf->rf4);
614 }
615
616 static void rt2500usb_config_txpower(struct rt2x00_dev *rt2x00dev,
617                                      const int txpower)
618 {
619         u32 rf3;
620
621         rt2x00_rf_read(rt2x00dev, 3, &rf3);
622         rt2x00_set_field32(&rf3, RF3_TXPOWER, TXPOWER_TO_DEV(txpower));
623         rt2500usb_rf_write(rt2x00dev, 3, rf3);
624 }
625
626 static void rt2500usb_config_duration(struct rt2x00_dev *rt2x00dev,
627                                       struct rt2x00lib_conf *libconf)
628 {
629         u16 reg;
630
631         rt2500usb_register_read(rt2x00dev, TXRX_CSR18, &reg);
632         rt2x00_set_field16(&reg, TXRX_CSR18_INTERVAL,
633                            libconf->conf->beacon_int * 4);
634         rt2500usb_register_write(rt2x00dev, TXRX_CSR18, reg);
635 }
636
637 static void rt2500usb_config_ps(struct rt2x00_dev *rt2x00dev,
638                                 struct rt2x00lib_conf *libconf)
639 {
640         enum dev_state state =
641             (libconf->conf->flags & IEEE80211_CONF_PS) ?
642                 STATE_SLEEP : STATE_AWAKE;
643         u16 reg;
644
645         if (state == STATE_SLEEP) {
646                 rt2500usb_register_read(rt2x00dev, MAC_CSR18, &reg);
647                 rt2x00_set_field16(&reg, MAC_CSR18_DELAY_AFTER_BEACON,
648                                    libconf->conf->beacon_int - 20);
649                 rt2x00_set_field16(&reg, MAC_CSR18_BEACONS_BEFORE_WAKEUP,
650                                    libconf->conf->listen_interval - 1);
651
652                 /* We must first disable autowake before it can be enabled */
653                 rt2x00_set_field16(&reg, MAC_CSR18_AUTO_WAKE, 0);
654                 rt2500usb_register_write(rt2x00dev, MAC_CSR18, reg);
655
656                 rt2x00_set_field16(&reg, MAC_CSR18_AUTO_WAKE, 1);
657                 rt2500usb_register_write(rt2x00dev, MAC_CSR18, reg);
658         }
659
660         rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
661 }
662
663 static void rt2500usb_config(struct rt2x00_dev *rt2x00dev,
664                              struct rt2x00lib_conf *libconf,
665                              const unsigned int flags)
666 {
667         if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
668                 rt2500usb_config_channel(rt2x00dev, &libconf->rf,
669                                          libconf->conf->power_level);
670         if ((flags & IEEE80211_CONF_CHANGE_POWER) &&
671             !(flags & IEEE80211_CONF_CHANGE_CHANNEL))
672                 rt2500usb_config_txpower(rt2x00dev,
673                                          libconf->conf->power_level);
674         if (flags & IEEE80211_CONF_CHANGE_BEACON_INTERVAL)
675                 rt2500usb_config_duration(rt2x00dev, libconf);
676         if (flags & IEEE80211_CONF_CHANGE_PS)
677                 rt2500usb_config_ps(rt2x00dev, libconf);
678 }
679
680 /*
681  * Link tuning
682  */
683 static void rt2500usb_link_stats(struct rt2x00_dev *rt2x00dev,
684                                  struct link_qual *qual)
685 {
686         u16 reg;
687
688         /*
689          * Update FCS error count from register.
690          */
691         rt2500usb_register_read(rt2x00dev, STA_CSR0, &reg);
692         qual->rx_failed = rt2x00_get_field16(reg, STA_CSR0_FCS_ERROR);
693
694         /*
695          * Update False CCA count from register.
696          */
697         rt2500usb_register_read(rt2x00dev, STA_CSR3, &reg);
698         qual->false_cca = rt2x00_get_field16(reg, STA_CSR3_FALSE_CCA_ERROR);
699 }
700
701 static void rt2500usb_reset_tuner(struct rt2x00_dev *rt2x00dev)
702 {
703         u16 eeprom;
704         u16 value;
705
706         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R24, &eeprom);
707         value = rt2x00_get_field16(eeprom, EEPROM_BBPTUNE_R24_LOW);
708         rt2500usb_bbp_write(rt2x00dev, 24, value);
709
710         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R25, &eeprom);
711         value = rt2x00_get_field16(eeprom, EEPROM_BBPTUNE_R25_LOW);
712         rt2500usb_bbp_write(rt2x00dev, 25, value);
713
714         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R61, &eeprom);
715         value = rt2x00_get_field16(eeprom, EEPROM_BBPTUNE_R61_LOW);
716         rt2500usb_bbp_write(rt2x00dev, 61, value);
717
718         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_VGC, &eeprom);
719         value = rt2x00_get_field16(eeprom, EEPROM_BBPTUNE_VGCUPPER);
720         rt2500usb_bbp_write(rt2x00dev, 17, value);
721
722         rt2x00dev->link.vgc_level = value;
723 }
724
725 /*
726  * NOTE: This function is directly ported from legacy driver, but
727  * despite it being declared it was never called. Although link tuning
728  * sounds like a good idea, and usually works well for the other drivers,
729  * it does _not_ work with rt2500usb. Enabling this function will result
730  * in TX capabilities only until association kicks in. Immediately
731  * after the successful association all TX frames will be kept in the
732  * hardware queue and never transmitted.
733  */
734 #if 0
735 static void rt2500usb_link_tuner(struct rt2x00_dev *rt2x00dev)
736 {
737         int rssi = rt2x00_get_link_rssi(&rt2x00dev->link);
738         u16 bbp_thresh;
739         u16 vgc_bound;
740         u16 sens;
741         u16 r24;
742         u16 r25;
743         u16 r61;
744         u16 r17_sens;
745         u8 r17;
746         u8 up_bound;
747         u8 low_bound;
748
749         /*
750          * Read current r17 value, as well as the sensitivity values
751          * for the r17 register.
752          */
753         rt2500usb_bbp_read(rt2x00dev, 17, &r17);
754         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R17, &r17_sens);
755
756         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_VGC, &vgc_bound);
757         up_bound = rt2x00_get_field16(vgc_bound, EEPROM_BBPTUNE_VGCUPPER);
758         low_bound = rt2x00_get_field16(vgc_bound, EEPROM_BBPTUNE_VGCLOWER);
759
760         /*
761          * If we are not associated, we should go straight to the
762          * dynamic CCA tuning.
763          */
764         if (!rt2x00dev->intf_associated)
765                 goto dynamic_cca_tune;
766
767         /*
768          * Determine the BBP tuning threshold and correctly
769          * set BBP 24, 25 and 61.
770          */
771         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE, &bbp_thresh);
772         bbp_thresh = rt2x00_get_field16(bbp_thresh, EEPROM_BBPTUNE_THRESHOLD);
773
774         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R24, &r24);
775         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R25, &r25);
776         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R61, &r61);
777
778         if ((rssi + bbp_thresh) > 0) {
779                 r24 = rt2x00_get_field16(r24, EEPROM_BBPTUNE_R24_HIGH);
780                 r25 = rt2x00_get_field16(r25, EEPROM_BBPTUNE_R25_HIGH);
781                 r61 = rt2x00_get_field16(r61, EEPROM_BBPTUNE_R61_HIGH);
782         } else {
783                 r24 = rt2x00_get_field16(r24, EEPROM_BBPTUNE_R24_LOW);
784                 r25 = rt2x00_get_field16(r25, EEPROM_BBPTUNE_R25_LOW);
785                 r61 = rt2x00_get_field16(r61, EEPROM_BBPTUNE_R61_LOW);
786         }
787
788         rt2500usb_bbp_write(rt2x00dev, 24, r24);
789         rt2500usb_bbp_write(rt2x00dev, 25, r25);
790         rt2500usb_bbp_write(rt2x00dev, 61, r61);
791
792         /*
793          * A too low RSSI will cause too much false CCA which will
794          * then corrupt the R17 tuning. To remidy this the tuning should
795          * be stopped (While making sure the R17 value will not exceed limits)
796          */
797         if (rssi >= -40) {
798                 if (r17 != 0x60)
799                         rt2500usb_bbp_write(rt2x00dev, 17, 0x60);
800                 return;
801         }
802
803         /*
804          * Special big-R17 for short distance
805          */
806         if (rssi >= -58) {
807                 sens = rt2x00_get_field16(r17_sens, EEPROM_BBPTUNE_R17_LOW);
808                 if (r17 != sens)
809                         rt2500usb_bbp_write(rt2x00dev, 17, sens);
810                 return;
811         }
812
813         /*
814          * Special mid-R17 for middle distance
815          */
816         if (rssi >= -74) {
817                 sens = rt2x00_get_field16(r17_sens, EEPROM_BBPTUNE_R17_HIGH);
818                 if (r17 != sens)
819                         rt2500usb_bbp_write(rt2x00dev, 17, sens);
820                 return;
821         }
822
823         /*
824          * Leave short or middle distance condition, restore r17
825          * to the dynamic tuning range.
826          */
827         low_bound = 0x32;
828         if (rssi < -77)
829                 up_bound -= (-77 - rssi);
830
831         if (up_bound < low_bound)
832                 up_bound = low_bound;
833
834         if (r17 > up_bound) {
835                 rt2500usb_bbp_write(rt2x00dev, 17, up_bound);
836                 rt2x00dev->link.vgc_level = up_bound;
837                 return;
838         }
839
840 dynamic_cca_tune:
841
842         /*
843          * R17 is inside the dynamic tuning range,
844          * start tuning the link based on the false cca counter.
845          */
846         if (rt2x00dev->link.qual.false_cca > 512 && r17 < up_bound) {
847                 rt2500usb_bbp_write(rt2x00dev, 17, ++r17);
848                 rt2x00dev->link.vgc_level = r17;
849         } else if (rt2x00dev->link.qual.false_cca < 100 && r17 > low_bound) {
850                 rt2500usb_bbp_write(rt2x00dev, 17, --r17);
851                 rt2x00dev->link.vgc_level = r17;
852         }
853 }
854 #else
855 #define rt2500usb_link_tuner    NULL
856 #endif
857
858 /*
859  * Initialization functions.
860  */
861 static int rt2500usb_init_registers(struct rt2x00_dev *rt2x00dev)
862 {
863         u16 reg;
864
865         rt2x00usb_vendor_request_sw(rt2x00dev, USB_DEVICE_MODE, 0x0001,
866                                     USB_MODE_TEST, REGISTER_TIMEOUT);
867         rt2x00usb_vendor_request_sw(rt2x00dev, USB_SINGLE_WRITE, 0x0308,
868                                     0x00f0, REGISTER_TIMEOUT);
869
870         rt2500usb_register_read(rt2x00dev, TXRX_CSR2, &reg);
871         rt2x00_set_field16(&reg, TXRX_CSR2_DISABLE_RX, 1);
872         rt2500usb_register_write(rt2x00dev, TXRX_CSR2, reg);
873
874         rt2500usb_register_write(rt2x00dev, MAC_CSR13, 0x1111);
875         rt2500usb_register_write(rt2x00dev, MAC_CSR14, 0x1e11);
876
877         rt2500usb_register_read(rt2x00dev, MAC_CSR1, &reg);
878         rt2x00_set_field16(&reg, MAC_CSR1_SOFT_RESET, 1);
879         rt2x00_set_field16(&reg, MAC_CSR1_BBP_RESET, 1);
880         rt2x00_set_field16(&reg, MAC_CSR1_HOST_READY, 0);
881         rt2500usb_register_write(rt2x00dev, MAC_CSR1, reg);
882
883         rt2500usb_register_read(rt2x00dev, MAC_CSR1, &reg);
884         rt2x00_set_field16(&reg, MAC_CSR1_SOFT_RESET, 0);
885         rt2x00_set_field16(&reg, MAC_CSR1_BBP_RESET, 0);
886         rt2x00_set_field16(&reg, MAC_CSR1_HOST_READY, 0);
887         rt2500usb_register_write(rt2x00dev, MAC_CSR1, reg);
888
889         rt2500usb_register_read(rt2x00dev, TXRX_CSR5, &reg);
890         rt2x00_set_field16(&reg, TXRX_CSR5_BBP_ID0, 13);
891         rt2x00_set_field16(&reg, TXRX_CSR5_BBP_ID0_VALID, 1);
892         rt2x00_set_field16(&reg, TXRX_CSR5_BBP_ID1, 12);
893         rt2x00_set_field16(&reg, TXRX_CSR5_BBP_ID1_VALID, 1);
894         rt2500usb_register_write(rt2x00dev, TXRX_CSR5, reg);
895
896         rt2500usb_register_read(rt2x00dev, TXRX_CSR6, &reg);
897         rt2x00_set_field16(&reg, TXRX_CSR6_BBP_ID0, 10);
898         rt2x00_set_field16(&reg, TXRX_CSR6_BBP_ID0_VALID, 1);
899         rt2x00_set_field16(&reg, TXRX_CSR6_BBP_ID1, 11);
900         rt2x00_set_field16(&reg, TXRX_CSR6_BBP_ID1_VALID, 1);
901         rt2500usb_register_write(rt2x00dev, TXRX_CSR6, reg);
902
903         rt2500usb_register_read(rt2x00dev, TXRX_CSR7, &reg);
904         rt2x00_set_field16(&reg, TXRX_CSR7_BBP_ID0, 7);
905         rt2x00_set_field16(&reg, TXRX_CSR7_BBP_ID0_VALID, 1);
906         rt2x00_set_field16(&reg, TXRX_CSR7_BBP_ID1, 6);
907         rt2x00_set_field16(&reg, TXRX_CSR7_BBP_ID1_VALID, 1);
908         rt2500usb_register_write(rt2x00dev, TXRX_CSR7, reg);
909
910         rt2500usb_register_read(rt2x00dev, TXRX_CSR8, &reg);
911         rt2x00_set_field16(&reg, TXRX_CSR8_BBP_ID0, 5);
912         rt2x00_set_field16(&reg, TXRX_CSR8_BBP_ID0_VALID, 1);
913         rt2x00_set_field16(&reg, TXRX_CSR8_BBP_ID1, 0);
914         rt2x00_set_field16(&reg, TXRX_CSR8_BBP_ID1_VALID, 0);
915         rt2500usb_register_write(rt2x00dev, TXRX_CSR8, reg);
916
917         rt2500usb_register_read(rt2x00dev, TXRX_CSR19, &reg);
918         rt2x00_set_field16(&reg, TXRX_CSR19_TSF_COUNT, 0);
919         rt2x00_set_field16(&reg, TXRX_CSR19_TSF_SYNC, 0);
920         rt2x00_set_field16(&reg, TXRX_CSR19_TBCN, 0);
921         rt2x00_set_field16(&reg, TXRX_CSR19_BEACON_GEN, 0);
922         rt2500usb_register_write(rt2x00dev, TXRX_CSR19, reg);
923
924         rt2500usb_register_write(rt2x00dev, TXRX_CSR21, 0xe78f);
925         rt2500usb_register_write(rt2x00dev, MAC_CSR9, 0xff1d);
926
927         if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
928                 return -EBUSY;
929
930         rt2500usb_register_read(rt2x00dev, MAC_CSR1, &reg);
931         rt2x00_set_field16(&reg, MAC_CSR1_SOFT_RESET, 0);
932         rt2x00_set_field16(&reg, MAC_CSR1_BBP_RESET, 0);
933         rt2x00_set_field16(&reg, MAC_CSR1_HOST_READY, 1);
934         rt2500usb_register_write(rt2x00dev, MAC_CSR1, reg);
935
936         if (rt2x00_rev(&rt2x00dev->chip) >= RT2570_VERSION_C) {
937                 rt2500usb_register_read(rt2x00dev, PHY_CSR2, &reg);
938                 rt2x00_set_field16(&reg, PHY_CSR2_LNA, 0);
939         } else {
940                 reg = 0;
941                 rt2x00_set_field16(&reg, PHY_CSR2_LNA, 1);
942                 rt2x00_set_field16(&reg, PHY_CSR2_LNA_MODE, 3);
943         }
944         rt2500usb_register_write(rt2x00dev, PHY_CSR2, reg);
945
946         rt2500usb_register_write(rt2x00dev, MAC_CSR11, 0x0002);
947         rt2500usb_register_write(rt2x00dev, MAC_CSR22, 0x0053);
948         rt2500usb_register_write(rt2x00dev, MAC_CSR15, 0x01ee);
949         rt2500usb_register_write(rt2x00dev, MAC_CSR16, 0x0000);
950
951         rt2500usb_register_read(rt2x00dev, MAC_CSR8, &reg);
952         rt2x00_set_field16(&reg, MAC_CSR8_MAX_FRAME_UNIT,
953                            rt2x00dev->rx->data_size);
954         rt2500usb_register_write(rt2x00dev, MAC_CSR8, reg);
955
956         rt2500usb_register_read(rt2x00dev, TXRX_CSR0, &reg);
957         rt2x00_set_field16(&reg, TXRX_CSR0_IV_OFFSET, IEEE80211_HEADER);
958         rt2x00_set_field16(&reg, TXRX_CSR0_KEY_ID, 0);
959         rt2500usb_register_write(rt2x00dev, TXRX_CSR0, reg);
960
961         rt2500usb_register_read(rt2x00dev, MAC_CSR18, &reg);
962         rt2x00_set_field16(&reg, MAC_CSR18_DELAY_AFTER_BEACON, 90);
963         rt2500usb_register_write(rt2x00dev, MAC_CSR18, reg);
964
965         rt2500usb_register_read(rt2x00dev, PHY_CSR4, &reg);
966         rt2x00_set_field16(&reg, PHY_CSR4_LOW_RF_LE, 1);
967         rt2500usb_register_write(rt2x00dev, PHY_CSR4, reg);
968
969         rt2500usb_register_read(rt2x00dev, TXRX_CSR1, &reg);
970         rt2x00_set_field16(&reg, TXRX_CSR1_AUTO_SEQUENCE, 1);
971         rt2500usb_register_write(rt2x00dev, TXRX_CSR1, reg);
972
973         return 0;
974 }
975
976 static int rt2500usb_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
977 {
978         unsigned int i;
979         u8 value;
980
981         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
982                 rt2500usb_bbp_read(rt2x00dev, 0, &value);
983                 if ((value != 0xff) && (value != 0x00))
984                         return 0;
985                 udelay(REGISTER_BUSY_DELAY);
986         }
987
988         ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
989         return -EACCES;
990 }
991
992 static int rt2500usb_init_bbp(struct rt2x00_dev *rt2x00dev)
993 {
994         unsigned int i;
995         u16 eeprom;
996         u8 value;
997         u8 reg_id;
998
999         if (unlikely(rt2500usb_wait_bbp_ready(rt2x00dev)))
1000                 return -EACCES;
1001
1002         rt2500usb_bbp_write(rt2x00dev, 3, 0x02);
1003         rt2500usb_bbp_write(rt2x00dev, 4, 0x19);
1004         rt2500usb_bbp_write(rt2x00dev, 14, 0x1c);
1005         rt2500usb_bbp_write(rt2x00dev, 15, 0x30);
1006         rt2500usb_bbp_write(rt2x00dev, 16, 0xac);
1007         rt2500usb_bbp_write(rt2x00dev, 18, 0x18);
1008         rt2500usb_bbp_write(rt2x00dev, 19, 0xff);
1009         rt2500usb_bbp_write(rt2x00dev, 20, 0x1e);
1010         rt2500usb_bbp_write(rt2x00dev, 21, 0x08);
1011         rt2500usb_bbp_write(rt2x00dev, 22, 0x08);
1012         rt2500usb_bbp_write(rt2x00dev, 23, 0x08);
1013         rt2500usb_bbp_write(rt2x00dev, 24, 0x80);
1014         rt2500usb_bbp_write(rt2x00dev, 25, 0x50);
1015         rt2500usb_bbp_write(rt2x00dev, 26, 0x08);
1016         rt2500usb_bbp_write(rt2x00dev, 27, 0x23);
1017         rt2500usb_bbp_write(rt2x00dev, 30, 0x10);
1018         rt2500usb_bbp_write(rt2x00dev, 31, 0x2b);
1019         rt2500usb_bbp_write(rt2x00dev, 32, 0xb9);
1020         rt2500usb_bbp_write(rt2x00dev, 34, 0x12);
1021         rt2500usb_bbp_write(rt2x00dev, 35, 0x50);
1022         rt2500usb_bbp_write(rt2x00dev, 39, 0xc4);
1023         rt2500usb_bbp_write(rt2x00dev, 40, 0x02);
1024         rt2500usb_bbp_write(rt2x00dev, 41, 0x60);
1025         rt2500usb_bbp_write(rt2x00dev, 53, 0x10);
1026         rt2500usb_bbp_write(rt2x00dev, 54, 0x18);
1027         rt2500usb_bbp_write(rt2x00dev, 56, 0x08);
1028         rt2500usb_bbp_write(rt2x00dev, 57, 0x10);
1029         rt2500usb_bbp_write(rt2x00dev, 58, 0x08);
1030         rt2500usb_bbp_write(rt2x00dev, 61, 0x60);
1031         rt2500usb_bbp_write(rt2x00dev, 62, 0x10);
1032         rt2500usb_bbp_write(rt2x00dev, 75, 0xff);
1033
1034         for (i = 0; i < EEPROM_BBP_SIZE; i++) {
1035                 rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
1036
1037                 if (eeprom != 0xffff && eeprom != 0x0000) {
1038                         reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
1039                         value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
1040                         rt2500usb_bbp_write(rt2x00dev, reg_id, value);
1041                 }
1042         }
1043
1044         return 0;
1045 }
1046
1047 /*
1048  * Device state switch handlers.
1049  */
1050 static void rt2500usb_toggle_rx(struct rt2x00_dev *rt2x00dev,
1051                                 enum dev_state state)
1052 {
1053         u16 reg;
1054
1055         rt2500usb_register_read(rt2x00dev, TXRX_CSR2, &reg);
1056         rt2x00_set_field16(&reg, TXRX_CSR2_DISABLE_RX,
1057                            (state == STATE_RADIO_RX_OFF) ||
1058                            (state == STATE_RADIO_RX_OFF_LINK));
1059         rt2500usb_register_write(rt2x00dev, TXRX_CSR2, reg);
1060 }
1061
1062 static int rt2500usb_enable_radio(struct rt2x00_dev *rt2x00dev)
1063 {
1064         /*
1065          * Initialize all registers.
1066          */
1067         if (unlikely(rt2500usb_init_registers(rt2x00dev) ||
1068                      rt2500usb_init_bbp(rt2x00dev)))
1069                 return -EIO;
1070
1071         return 0;
1072 }
1073
1074 static void rt2500usb_disable_radio(struct rt2x00_dev *rt2x00dev)
1075 {
1076         rt2500usb_register_write(rt2x00dev, MAC_CSR13, 0x2121);
1077         rt2500usb_register_write(rt2x00dev, MAC_CSR14, 0x2121);
1078
1079         /*
1080          * Disable synchronisation.
1081          */
1082         rt2500usb_register_write(rt2x00dev, TXRX_CSR19, 0);
1083
1084         rt2x00usb_disable_radio(rt2x00dev);
1085 }
1086
1087 static int rt2500usb_set_state(struct rt2x00_dev *rt2x00dev,
1088                                enum dev_state state)
1089 {
1090         u16 reg;
1091         u16 reg2;
1092         unsigned int i;
1093         char put_to_sleep;
1094         char bbp_state;
1095         char rf_state;
1096
1097         put_to_sleep = (state != STATE_AWAKE);
1098
1099         reg = 0;
1100         rt2x00_set_field16(&reg, MAC_CSR17_BBP_DESIRE_STATE, state);
1101         rt2x00_set_field16(&reg, MAC_CSR17_RF_DESIRE_STATE, state);
1102         rt2x00_set_field16(&reg, MAC_CSR17_PUT_TO_SLEEP, put_to_sleep);
1103         rt2500usb_register_write(rt2x00dev, MAC_CSR17, reg);
1104         rt2x00_set_field16(&reg, MAC_CSR17_SET_STATE, 1);
1105         rt2500usb_register_write(rt2x00dev, MAC_CSR17, reg);
1106
1107         /*
1108          * Device is not guaranteed to be in the requested state yet.
1109          * We must wait until the register indicates that the
1110          * device has entered the correct state.
1111          */
1112         for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
1113                 rt2500usb_register_read(rt2x00dev, MAC_CSR17, &reg2);
1114                 bbp_state = rt2x00_get_field16(reg2, MAC_CSR17_BBP_CURR_STATE);
1115                 rf_state = rt2x00_get_field16(reg2, MAC_CSR17_RF_CURR_STATE);
1116                 if (bbp_state == state && rf_state == state)
1117                         return 0;
1118                 rt2500usb_register_write(rt2x00dev, MAC_CSR17, reg);
1119                 msleep(30);
1120         }
1121
1122         return -EBUSY;
1123 }
1124
1125 static int rt2500usb_set_device_state(struct rt2x00_dev *rt2x00dev,
1126                                       enum dev_state state)
1127 {
1128         int retval = 0;
1129
1130         switch (state) {
1131         case STATE_RADIO_ON:
1132                 retval = rt2500usb_enable_radio(rt2x00dev);
1133                 break;
1134         case STATE_RADIO_OFF:
1135                 rt2500usb_disable_radio(rt2x00dev);
1136                 break;
1137         case STATE_RADIO_RX_ON:
1138         case STATE_RADIO_RX_ON_LINK:
1139         case STATE_RADIO_RX_OFF:
1140         case STATE_RADIO_RX_OFF_LINK:
1141                 rt2500usb_toggle_rx(rt2x00dev, state);
1142                 break;
1143         case STATE_RADIO_IRQ_ON:
1144         case STATE_RADIO_IRQ_OFF:
1145                 /* No support, but no error either */
1146                 break;
1147         case STATE_DEEP_SLEEP:
1148         case STATE_SLEEP:
1149         case STATE_STANDBY:
1150         case STATE_AWAKE:
1151                 retval = rt2500usb_set_state(rt2x00dev, state);
1152                 break;
1153         default:
1154                 retval = -ENOTSUPP;
1155                 break;
1156         }
1157
1158         if (unlikely(retval))
1159                 ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
1160                       state, retval);
1161
1162         return retval;
1163 }
1164
1165 /*
1166  * TX descriptor initialization
1167  */
1168 static void rt2500usb_write_tx_desc(struct rt2x00_dev *rt2x00dev,
1169                                     struct sk_buff *skb,
1170                                     struct txentry_desc *txdesc)
1171 {
1172         struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
1173         __le32 *txd = skbdesc->desc;
1174         u32 word;
1175
1176         /*
1177          * Start writing the descriptor words.
1178          */
1179         rt2x00_desc_read(txd, 1, &word);
1180         rt2x00_set_field32(&word, TXD_W1_IV_OFFSET, txdesc->iv_offset);
1181         rt2x00_set_field32(&word, TXD_W1_AIFS, txdesc->aifs);
1182         rt2x00_set_field32(&word, TXD_W1_CWMIN, txdesc->cw_min);
1183         rt2x00_set_field32(&word, TXD_W1_CWMAX, txdesc->cw_max);
1184         rt2x00_desc_write(txd, 1, word);
1185
1186         rt2x00_desc_read(txd, 2, &word);
1187         rt2x00_set_field32(&word, TXD_W2_PLCP_SIGNAL, txdesc->signal);
1188         rt2x00_set_field32(&word, TXD_W2_PLCP_SERVICE, txdesc->service);
1189         rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_LOW, txdesc->length_low);
1190         rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_HIGH, txdesc->length_high);
1191         rt2x00_desc_write(txd, 2, word);
1192
1193         if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags)) {
1194                 _rt2x00_desc_write(txd, 3, skbdesc->iv[0]);
1195                 _rt2x00_desc_write(txd, 4, skbdesc->iv[1]);
1196         }
1197
1198         rt2x00_desc_read(txd, 0, &word);
1199         rt2x00_set_field32(&word, TXD_W0_RETRY_LIMIT, txdesc->retry_limit);
1200         rt2x00_set_field32(&word, TXD_W0_MORE_FRAG,
1201                            test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
1202         rt2x00_set_field32(&word, TXD_W0_ACK,
1203                            test_bit(ENTRY_TXD_ACK, &txdesc->flags));
1204         rt2x00_set_field32(&word, TXD_W0_TIMESTAMP,
1205                            test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
1206         rt2x00_set_field32(&word, TXD_W0_OFDM,
1207                            test_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags));
1208         rt2x00_set_field32(&word, TXD_W0_NEW_SEQ,
1209                            test_bit(ENTRY_TXD_FIRST_FRAGMENT, &txdesc->flags));
1210         rt2x00_set_field32(&word, TXD_W0_IFS, txdesc->ifs);
1211         rt2x00_set_field32(&word, TXD_W0_DATABYTE_COUNT, skb->len);
1212         rt2x00_set_field32(&word, TXD_W0_CIPHER, !!txdesc->cipher);
1213         rt2x00_set_field32(&word, TXD_W0_KEY_ID, txdesc->key_idx);
1214         rt2x00_desc_write(txd, 0, word);
1215 }
1216
1217 /*
1218  * TX data initialization
1219  */
1220 static void rt2500usb_beacondone(struct urb *urb);
1221
1222 static void rt2500usb_write_beacon(struct queue_entry *entry)
1223 {
1224         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
1225         struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
1226         struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
1227         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
1228         int pipe = usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint);
1229         int length;
1230         u16 reg;
1231
1232         /*
1233          * Add the descriptor in front of the skb.
1234          */
1235         skb_push(entry->skb, entry->queue->desc_size);
1236         memcpy(entry->skb->data, skbdesc->desc, skbdesc->desc_len);
1237         skbdesc->desc = entry->skb->data;
1238
1239         /*
1240          * Disable beaconing while we are reloading the beacon data,
1241          * otherwise we might be sending out invalid data.
1242          */
1243         rt2500usb_register_read(rt2x00dev, TXRX_CSR19, &reg);
1244         rt2x00_set_field16(&reg, TXRX_CSR19_TSF_COUNT, 0);
1245         rt2x00_set_field16(&reg, TXRX_CSR19_TBCN, 0);
1246         rt2x00_set_field16(&reg, TXRX_CSR19_BEACON_GEN, 0);
1247         rt2500usb_register_write(rt2x00dev, TXRX_CSR19, reg);
1248
1249         /*
1250          * USB devices cannot blindly pass the skb->len as the
1251          * length of the data to usb_fill_bulk_urb. Pass the skb
1252          * to the driver to determine what the length should be.
1253          */
1254         length = rt2x00dev->ops->lib->get_tx_data_len(entry);
1255
1256         usb_fill_bulk_urb(bcn_priv->urb, usb_dev, pipe,
1257                           entry->skb->data, length, rt2500usb_beacondone,
1258                           entry);
1259
1260         /*
1261          * Second we need to create the guardian byte.
1262          * We only need a single byte, so lets recycle
1263          * the 'flags' field we are not using for beacons.
1264          */
1265         bcn_priv->guardian_data = 0;
1266         usb_fill_bulk_urb(bcn_priv->guardian_urb, usb_dev, pipe,
1267                           &bcn_priv->guardian_data, 1, rt2500usb_beacondone,
1268                           entry);
1269
1270         /*
1271          * Send out the guardian byte.
1272          */
1273         usb_submit_urb(bcn_priv->guardian_urb, GFP_ATOMIC);
1274 }
1275
1276 static int rt2500usb_get_tx_data_len(struct queue_entry *entry)
1277 {
1278         int length;
1279
1280         /*
1281          * The length _must_ be a multiple of 2,
1282          * but it must _not_ be a multiple of the USB packet size.
1283          */
1284         length = roundup(entry->skb->len, 2);
1285         length += (2 * !(length % entry->queue->usb_maxpacket));
1286
1287         return length;
1288 }
1289
1290 static void rt2500usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
1291                                     const enum data_queue_qid queue)
1292 {
1293         u16 reg;
1294
1295         if (queue != QID_BEACON) {
1296                 rt2x00usb_kick_tx_queue(rt2x00dev, queue);
1297                 return;
1298         }
1299
1300         rt2500usb_register_read(rt2x00dev, TXRX_CSR19, &reg);
1301         if (!rt2x00_get_field16(reg, TXRX_CSR19_BEACON_GEN)) {
1302                 rt2x00_set_field16(&reg, TXRX_CSR19_TSF_COUNT, 1);
1303                 rt2x00_set_field16(&reg, TXRX_CSR19_TBCN, 1);
1304                 rt2x00_set_field16(&reg, TXRX_CSR19_BEACON_GEN, 1);
1305                 /*
1306                  * Beacon generation will fail initially.
1307                  * To prevent this we need to register the TXRX_CSR19
1308                  * register several times.
1309                  */
1310                 rt2500usb_register_write(rt2x00dev, TXRX_CSR19, reg);
1311                 rt2500usb_register_write(rt2x00dev, TXRX_CSR19, 0);
1312                 rt2500usb_register_write(rt2x00dev, TXRX_CSR19, reg);
1313                 rt2500usb_register_write(rt2x00dev, TXRX_CSR19, 0);
1314                 rt2500usb_register_write(rt2x00dev, TXRX_CSR19, reg);
1315         }
1316 }
1317
1318 /*
1319  * RX control handlers
1320  */
1321 static void rt2500usb_fill_rxdone(struct queue_entry *entry,
1322                                   struct rxdone_entry_desc *rxdesc)
1323 {
1324         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
1325         struct queue_entry_priv_usb *entry_priv = entry->priv_data;
1326         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
1327         __le32 *rxd =
1328             (__le32 *)(entry->skb->data +
1329                        (entry_priv->urb->actual_length -
1330                         entry->queue->desc_size));
1331         u32 word0;
1332         u32 word1;
1333
1334         /*
1335          * Copy descriptor to the skbdesc->desc buffer, making it safe from moving of
1336          * frame data in rt2x00usb.
1337          */
1338         memcpy(skbdesc->desc, rxd, skbdesc->desc_len);
1339         rxd = (__le32 *)skbdesc->desc;
1340
1341         /*
1342          * It is now safe to read the descriptor on all architectures.
1343          */
1344         rt2x00_desc_read(rxd, 0, &word0);
1345         rt2x00_desc_read(rxd, 1, &word1);
1346
1347         if (rt2x00_get_field32(word0, RXD_W0_CRC_ERROR))
1348                 rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
1349         if (rt2x00_get_field32(word0, RXD_W0_PHYSICAL_ERROR))
1350                 rxdesc->flags |= RX_FLAG_FAILED_PLCP_CRC;
1351
1352         if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
1353                 rxdesc->cipher = rt2x00_get_field32(word0, RXD_W0_CIPHER);
1354                 if (rt2x00_get_field32(word0, RXD_W0_CIPHER_ERROR))
1355                         rxdesc->cipher_status = RX_CRYPTO_FAIL_KEY;
1356         }
1357
1358         if (rxdesc->cipher != CIPHER_NONE) {
1359                 _rt2x00_desc_read(rxd, 2, &rxdesc->iv[0]);
1360                 _rt2x00_desc_read(rxd, 3, &rxdesc->iv[1]);
1361                 rxdesc->dev_flags |= RXDONE_CRYPTO_IV;
1362
1363                 /* ICV is located at the end of frame */
1364
1365                 rxdesc->flags |= RX_FLAG_MMIC_STRIPPED;
1366                 if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
1367                         rxdesc->flags |= RX_FLAG_DECRYPTED;
1368                 else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
1369                         rxdesc->flags |= RX_FLAG_MMIC_ERROR;
1370         }
1371
1372         /*
1373          * Obtain the status about this packet.
1374          * When frame was received with an OFDM bitrate,
1375          * the signal is the PLCP value. If it was received with
1376          * a CCK bitrate the signal is the rate in 100kbit/s.
1377          */
1378         rxdesc->signal = rt2x00_get_field32(word1, RXD_W1_SIGNAL);
1379         rxdesc->rssi =
1380             rt2x00_get_field32(word1, RXD_W1_RSSI) - rt2x00dev->rssi_offset;
1381         rxdesc->size = rt2x00_get_field32(word0, RXD_W0_DATABYTE_COUNT);
1382
1383         if (rt2x00_get_field32(word0, RXD_W0_OFDM))
1384                 rxdesc->dev_flags |= RXDONE_SIGNAL_PLCP;
1385         else
1386                 rxdesc->dev_flags |= RXDONE_SIGNAL_BITRATE;
1387         if (rt2x00_get_field32(word0, RXD_W0_MY_BSS))
1388                 rxdesc->dev_flags |= RXDONE_MY_BSS;
1389
1390         /*
1391          * Adjust the skb memory window to the frame boundaries.
1392          */
1393         skb_trim(entry->skb, rxdesc->size);
1394 }
1395
1396 /*
1397  * Interrupt functions.
1398  */
1399 static void rt2500usb_beacondone(struct urb *urb)
1400 {
1401         struct queue_entry *entry = (struct queue_entry *)urb->context;
1402         struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
1403
1404         if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &entry->queue->rt2x00dev->flags))
1405                 return;
1406
1407         /*
1408          * Check if this was the guardian beacon,
1409          * if that was the case we need to send the real beacon now.
1410          * Otherwise we should free the sk_buffer, the device
1411          * should be doing the rest of the work now.
1412          */
1413         if (bcn_priv->guardian_urb == urb) {
1414                 usb_submit_urb(bcn_priv->urb, GFP_ATOMIC);
1415         } else if (bcn_priv->urb == urb) {
1416                 dev_kfree_skb(entry->skb);
1417                 entry->skb = NULL;
1418         }
1419 }
1420
1421 /*
1422  * Device probe functions.
1423  */
1424 static int rt2500usb_validate_eeprom(struct rt2x00_dev *rt2x00dev)
1425 {
1426         u16 word;
1427         u8 *mac;
1428         u8 bbp;
1429
1430         rt2x00usb_eeprom_read(rt2x00dev, rt2x00dev->eeprom, EEPROM_SIZE);
1431
1432         /*
1433          * Start validation of the data that has been read.
1434          */
1435         mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
1436         if (!is_valid_ether_addr(mac)) {
1437                 random_ether_addr(mac);
1438                 EEPROM(rt2x00dev, "MAC: %pM\n", mac);
1439         }
1440
1441         rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
1442         if (word == 0xffff) {
1443                 rt2x00_set_field16(&word, EEPROM_ANTENNA_NUM, 2);
1444                 rt2x00_set_field16(&word, EEPROM_ANTENNA_TX_DEFAULT,
1445                                    ANTENNA_SW_DIVERSITY);
1446                 rt2x00_set_field16(&word, EEPROM_ANTENNA_RX_DEFAULT,
1447                                    ANTENNA_SW_DIVERSITY);
1448                 rt2x00_set_field16(&word, EEPROM_ANTENNA_LED_MODE,
1449                                    LED_MODE_DEFAULT);
1450                 rt2x00_set_field16(&word, EEPROM_ANTENNA_DYN_TXAGC, 0);
1451                 rt2x00_set_field16(&word, EEPROM_ANTENNA_HARDWARE_RADIO, 0);
1452                 rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2522);
1453                 rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
1454                 EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
1455         }
1456
1457         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
1458         if (word == 0xffff) {
1459                 rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
1460                 rt2x00_set_field16(&word, EEPROM_NIC_DYN_BBP_TUNE, 0);
1461                 rt2x00_set_field16(&word, EEPROM_NIC_CCK_TX_POWER, 0);
1462                 rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
1463                 EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
1464         }
1465
1466         rt2x00_eeprom_read(rt2x00dev, EEPROM_CALIBRATE_OFFSET, &word);
1467         if (word == 0xffff) {
1468                 rt2x00_set_field16(&word, EEPROM_CALIBRATE_OFFSET_RSSI,
1469                                    DEFAULT_RSSI_OFFSET);
1470                 rt2x00_eeprom_write(rt2x00dev, EEPROM_CALIBRATE_OFFSET, word);
1471                 EEPROM(rt2x00dev, "Calibrate offset: 0x%04x\n", word);
1472         }
1473
1474         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE, &word);
1475         if (word == 0xffff) {
1476                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_THRESHOLD, 45);
1477                 rt2x00_eeprom_write(rt2x00dev, EEPROM_BBPTUNE, word);
1478                 EEPROM(rt2x00dev, "BBPtune: 0x%04x\n", word);
1479         }
1480
1481         /*
1482          * Switch lower vgc bound to current BBP R17 value,
1483          * lower the value a bit for better quality.
1484          */
1485         rt2500usb_bbp_read(rt2x00dev, 17, &bbp);
1486         bbp -= 6;
1487
1488         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_VGC, &word);
1489         if (word == 0xffff) {
1490                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_VGCUPPER, 0x40);
1491                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_VGCLOWER, bbp);
1492                 rt2x00_eeprom_write(rt2x00dev, EEPROM_BBPTUNE_VGC, word);
1493                 EEPROM(rt2x00dev, "BBPtune vgc: 0x%04x\n", word);
1494         } else {
1495                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_VGCLOWER, bbp);
1496                 rt2x00_eeprom_write(rt2x00dev, EEPROM_BBPTUNE_VGC, word);
1497         }
1498
1499         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R17, &word);
1500         if (word == 0xffff) {
1501                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_R17_LOW, 0x48);
1502                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_R17_HIGH, 0x41);
1503                 rt2x00_eeprom_write(rt2x00dev, EEPROM_BBPTUNE_R17, word);
1504                 EEPROM(rt2x00dev, "BBPtune r17: 0x%04x\n", word);
1505         }
1506
1507         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R24, &word);
1508         if (word == 0xffff) {
1509                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_R24_LOW, 0x40);
1510                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_R24_HIGH, 0x80);
1511                 rt2x00_eeprom_write(rt2x00dev, EEPROM_BBPTUNE_R24, word);
1512                 EEPROM(rt2x00dev, "BBPtune r24: 0x%04x\n", word);
1513         }
1514
1515         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R25, &word);
1516         if (word == 0xffff) {
1517                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_R25_LOW, 0x40);
1518                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_R25_HIGH, 0x50);
1519                 rt2x00_eeprom_write(rt2x00dev, EEPROM_BBPTUNE_R25, word);
1520                 EEPROM(rt2x00dev, "BBPtune r25: 0x%04x\n", word);
1521         }
1522
1523         rt2x00_eeprom_read(rt2x00dev, EEPROM_BBPTUNE_R61, &word);
1524         if (word == 0xffff) {
1525                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_R61_LOW, 0x60);
1526                 rt2x00_set_field16(&word, EEPROM_BBPTUNE_R61_HIGH, 0x6d);
1527                 rt2x00_eeprom_write(rt2x00dev, EEPROM_BBPTUNE_R61, word);
1528                 EEPROM(rt2x00dev, "BBPtune r61: 0x%04x\n", word);
1529         }
1530
1531         return 0;
1532 }
1533
1534 static int rt2500usb_init_eeprom(struct rt2x00_dev *rt2x00dev)
1535 {
1536         u16 reg;
1537         u16 value;
1538         u16 eeprom;
1539
1540         /*
1541          * Read EEPROM word for configuration.
1542          */
1543         rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
1544
1545         /*
1546          * Identify RF chipset.
1547          */
1548         value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
1549         rt2500usb_register_read(rt2x00dev, MAC_CSR0, &reg);
1550         rt2x00_set_chip(rt2x00dev, RT2570, value, reg);
1551
1552         if (!rt2x00_check_rev(&rt2x00dev->chip, 0)) {
1553                 ERROR(rt2x00dev, "Invalid RT chipset detected.\n");
1554                 return -ENODEV;
1555         }
1556
1557         if (!rt2x00_rf(&rt2x00dev->chip, RF2522) &&
1558             !rt2x00_rf(&rt2x00dev->chip, RF2523) &&
1559             !rt2x00_rf(&rt2x00dev->chip, RF2524) &&
1560             !rt2x00_rf(&rt2x00dev->chip, RF2525) &&
1561             !rt2x00_rf(&rt2x00dev->chip, RF2525E) &&
1562             !rt2x00_rf(&rt2x00dev->chip, RF5222)) {
1563                 ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
1564                 return -ENODEV;
1565         }
1566
1567         /*
1568          * Identify default antenna configuration.
1569          */
1570         rt2x00dev->default_ant.tx =
1571             rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TX_DEFAULT);
1572         rt2x00dev->default_ant.rx =
1573             rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_DEFAULT);
1574
1575         /*
1576          * When the eeprom indicates SW_DIVERSITY use HW_DIVERSITY instead.
1577          * I am not 100% sure about this, but the legacy drivers do not
1578          * indicate antenna swapping in software is required when
1579          * diversity is enabled.
1580          */
1581         if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
1582                 rt2x00dev->default_ant.tx = ANTENNA_HW_DIVERSITY;
1583         if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
1584                 rt2x00dev->default_ant.rx = ANTENNA_HW_DIVERSITY;
1585
1586         /*
1587          * Store led mode, for correct led behaviour.
1588          */
1589 #ifdef CONFIG_RT2X00_LIB_LEDS
1590         value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_LED_MODE);
1591
1592         rt2500usb_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
1593         if (value == LED_MODE_TXRX_ACTIVITY)
1594                 rt2500usb_init_led(rt2x00dev, &rt2x00dev->led_qual,
1595                                    LED_TYPE_ACTIVITY);
1596 #endif /* CONFIG_RT2X00_LIB_LEDS */
1597
1598         /*
1599          * Check if the BBP tuning should be disabled.
1600          */
1601         rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
1602         if (rt2x00_get_field16(eeprom, EEPROM_NIC_DYN_BBP_TUNE))
1603                 __set_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags);
1604
1605         /*
1606          * Read the RSSI <-> dBm offset information.
1607          */
1608         rt2x00_eeprom_read(rt2x00dev, EEPROM_CALIBRATE_OFFSET, &eeprom);
1609         rt2x00dev->rssi_offset =
1610             rt2x00_get_field16(eeprom, EEPROM_CALIBRATE_OFFSET_RSSI);
1611
1612         return 0;
1613 }
1614
1615 /*
1616  * RF value list for RF2522
1617  * Supports: 2.4 GHz
1618  */
1619 static const struct rf_channel rf_vals_bg_2522[] = {
1620         { 1,  0x00002050, 0x000c1fda, 0x00000101, 0 },
1621         { 2,  0x00002050, 0x000c1fee, 0x00000101, 0 },
1622         { 3,  0x00002050, 0x000c2002, 0x00000101, 0 },
1623         { 4,  0x00002050, 0x000c2016, 0x00000101, 0 },
1624         { 5,  0x00002050, 0x000c202a, 0x00000101, 0 },
1625         { 6,  0x00002050, 0x000c203e, 0x00000101, 0 },
1626         { 7,  0x00002050, 0x000c2052, 0x00000101, 0 },
1627         { 8,  0x00002050, 0x000c2066, 0x00000101, 0 },
1628         { 9,  0x00002050, 0x000c207a, 0x00000101, 0 },
1629         { 10, 0x00002050, 0x000c208e, 0x00000101, 0 },
1630         { 11, 0x00002050, 0x000c20a2, 0x00000101, 0 },
1631         { 12, 0x00002050, 0x000c20b6, 0x00000101, 0 },
1632         { 13, 0x00002050, 0x000c20ca, 0x00000101, 0 },
1633         { 14, 0x00002050, 0x000c20fa, 0x00000101, 0 },
1634 };
1635
1636 /*
1637  * RF value list for RF2523
1638  * Supports: 2.4 GHz
1639  */
1640 static const struct rf_channel rf_vals_bg_2523[] = {
1641         { 1,  0x00022010, 0x00000c9e, 0x000e0111, 0x00000a1b },
1642         { 2,  0x00022010, 0x00000ca2, 0x000e0111, 0x00000a1b },
1643         { 3,  0x00022010, 0x00000ca6, 0x000e0111, 0x00000a1b },
1644         { 4,  0x00022010, 0x00000caa, 0x000e0111, 0x00000a1b },
1645         { 5,  0x00022010, 0x00000cae, 0x000e0111, 0x00000a1b },
1646         { 6,  0x00022010, 0x00000cb2, 0x000e0111, 0x00000a1b },
1647         { 7,  0x00022010, 0x00000cb6, 0x000e0111, 0x00000a1b },
1648         { 8,  0x00022010, 0x00000cba, 0x000e0111, 0x00000a1b },
1649         { 9,  0x00022010, 0x00000cbe, 0x000e0111, 0x00000a1b },
1650         { 10, 0x00022010, 0x00000d02, 0x000e0111, 0x00000a1b },
1651         { 11, 0x00022010, 0x00000d06, 0x000e0111, 0x00000a1b },
1652         { 12, 0x00022010, 0x00000d0a, 0x000e0111, 0x00000a1b },
1653         { 13, 0x00022010, 0x00000d0e, 0x000e0111, 0x00000a1b },
1654         { 14, 0x00022010, 0x00000d1a, 0x000e0111, 0x00000a03 },
1655 };
1656
1657 /*
1658  * RF value list for RF2524
1659  * Supports: 2.4 GHz
1660  */
1661 static const struct rf_channel rf_vals_bg_2524[] = {
1662         { 1,  0x00032020, 0x00000c9e, 0x00000101, 0x00000a1b },
1663         { 2,  0x00032020, 0x00000ca2, 0x00000101, 0x00000a1b },
1664         { 3,  0x00032020, 0x00000ca6, 0x00000101, 0x00000a1b },
1665         { 4,  0x00032020, 0x00000caa, 0x00000101, 0x00000a1b },
1666         { 5,  0x00032020, 0x00000cae, 0x00000101, 0x00000a1b },
1667         { 6,  0x00032020, 0x00000cb2, 0x00000101, 0x00000a1b },
1668         { 7,  0x00032020, 0x00000cb6, 0x00000101, 0x00000a1b },
1669         { 8,  0x00032020, 0x00000cba, 0x00000101, 0x00000a1b },
1670         { 9,  0x00032020, 0x00000cbe, 0x00000101, 0x00000a1b },
1671         { 10, 0x00032020, 0x00000d02, 0x00000101, 0x00000a1b },
1672         { 11, 0x00032020, 0x00000d06, 0x00000101, 0x00000a1b },
1673         { 12, 0x00032020, 0x00000d0a, 0x00000101, 0x00000a1b },
1674         { 13, 0x00032020, 0x00000d0e, 0x00000101, 0x00000a1b },
1675         { 14, 0x00032020, 0x00000d1a, 0x00000101, 0x00000a03 },
1676 };
1677
1678 /*
1679  * RF value list for RF2525
1680  * Supports: 2.4 GHz
1681  */
1682 static const struct rf_channel rf_vals_bg_2525[] = {
1683         { 1,  0x00022020, 0x00080c9e, 0x00060111, 0x00000a1b },
1684         { 2,  0x00022020, 0x00080ca2, 0x00060111, 0x00000a1b },
1685         { 3,  0x00022020, 0x00080ca6, 0x00060111, 0x00000a1b },
1686         { 4,  0x00022020, 0x00080caa, 0x00060111, 0x00000a1b },
1687         { 5,  0x00022020, 0x00080cae, 0x00060111, 0x00000a1b },
1688         { 6,  0x00022020, 0x00080cb2, 0x00060111, 0x00000a1b },
1689         { 7,  0x00022020, 0x00080cb6, 0x00060111, 0x00000a1b },
1690         { 8,  0x00022020, 0x00080cba, 0x00060111, 0x00000a1b },
1691         { 9,  0x00022020, 0x00080cbe, 0x00060111, 0x00000a1b },
1692         { 10, 0x00022020, 0x00080d02, 0x00060111, 0x00000a1b },
1693         { 11, 0x00022020, 0x00080d06, 0x00060111, 0x00000a1b },
1694         { 12, 0x00022020, 0x00080d0a, 0x00060111, 0x00000a1b },
1695         { 13, 0x00022020, 0x00080d0e, 0x00060111, 0x00000a1b },
1696         { 14, 0x00022020, 0x00080d1a, 0x00060111, 0x00000a03 },
1697 };
1698
1699 /*
1700  * RF value list for RF2525e
1701  * Supports: 2.4 GHz
1702  */
1703 static const struct rf_channel rf_vals_bg_2525e[] = {
1704         { 1,  0x00022010, 0x0000089a, 0x00060111, 0x00000e1b },
1705         { 2,  0x00022010, 0x0000089e, 0x00060111, 0x00000e07 },
1706         { 3,  0x00022010, 0x0000089e, 0x00060111, 0x00000e1b },
1707         { 4,  0x00022010, 0x000008a2, 0x00060111, 0x00000e07 },
1708         { 5,  0x00022010, 0x000008a2, 0x00060111, 0x00000e1b },
1709         { 6,  0x00022010, 0x000008a6, 0x00060111, 0x00000e07 },
1710         { 7,  0x00022010, 0x000008a6, 0x00060111, 0x00000e1b },
1711         { 8,  0x00022010, 0x000008aa, 0x00060111, 0x00000e07 },
1712         { 9,  0x00022010, 0x000008aa, 0x00060111, 0x00000e1b },
1713         { 10, 0x00022010, 0x000008ae, 0x00060111, 0x00000e07 },
1714         { 11, 0x00022010, 0x000008ae, 0x00060111, 0x00000e1b },
1715         { 12, 0x00022010, 0x000008b2, 0x00060111, 0x00000e07 },
1716         { 13, 0x00022010, 0x000008b2, 0x00060111, 0x00000e1b },
1717         { 14, 0x00022010, 0x000008b6, 0x00060111, 0x00000e23 },
1718 };
1719
1720 /*
1721  * RF value list for RF5222
1722  * Supports: 2.4 GHz & 5.2 GHz
1723  */
1724 static const struct rf_channel rf_vals_5222[] = {
1725         { 1,  0x00022020, 0x00001136, 0x00000101, 0x00000a0b },
1726         { 2,  0x00022020, 0x0000113a, 0x00000101, 0x00000a0b },
1727         { 3,  0x00022020, 0x0000113e, 0x00000101, 0x00000a0b },
1728         { 4,  0x00022020, 0x00001182, 0x00000101, 0x00000a0b },
1729         { 5,  0x00022020, 0x00001186, 0x00000101, 0x00000a0b },
1730         { 6,  0x00022020, 0x0000118a, 0x00000101, 0x00000a0b },
1731         { 7,  0x00022020, 0x0000118e, 0x00000101, 0x00000a0b },
1732         { 8,  0x00022020, 0x00001192, 0x00000101, 0x00000a0b },
1733         { 9,  0x00022020, 0x00001196, 0x00000101, 0x00000a0b },
1734         { 10, 0x00022020, 0x0000119a, 0x00000101, 0x00000a0b },
1735         { 11, 0x00022020, 0x0000119e, 0x00000101, 0x00000a0b },
1736         { 12, 0x00022020, 0x000011a2, 0x00000101, 0x00000a0b },
1737         { 13, 0x00022020, 0x000011a6, 0x00000101, 0x00000a0b },
1738         { 14, 0x00022020, 0x000011ae, 0x00000101, 0x00000a1b },
1739
1740         /* 802.11 UNI / HyperLan 2 */
1741         { 36, 0x00022010, 0x00018896, 0x00000101, 0x00000a1f },
1742         { 40, 0x00022010, 0x0001889a, 0x00000101, 0x00000a1f },
1743         { 44, 0x00022010, 0x0001889e, 0x00000101, 0x00000a1f },
1744         { 48, 0x00022010, 0x000188a2, 0x00000101, 0x00000a1f },
1745         { 52, 0x00022010, 0x000188a6, 0x00000101, 0x00000a1f },
1746         { 66, 0x00022010, 0x000188aa, 0x00000101, 0x00000a1f },
1747         { 60, 0x00022010, 0x000188ae, 0x00000101, 0x00000a1f },
1748         { 64, 0x00022010, 0x000188b2, 0x00000101, 0x00000a1f },
1749
1750         /* 802.11 HyperLan 2 */
1751         { 100, 0x00022010, 0x00008802, 0x00000101, 0x00000a0f },
1752         { 104, 0x00022010, 0x00008806, 0x00000101, 0x00000a0f },
1753         { 108, 0x00022010, 0x0000880a, 0x00000101, 0x00000a0f },
1754         { 112, 0x00022010, 0x0000880e, 0x00000101, 0x00000a0f },
1755         { 116, 0x00022010, 0x00008812, 0x00000101, 0x00000a0f },
1756         { 120, 0x00022010, 0x00008816, 0x00000101, 0x00000a0f },
1757         { 124, 0x00022010, 0x0000881a, 0x00000101, 0x00000a0f },
1758         { 128, 0x00022010, 0x0000881e, 0x00000101, 0x00000a0f },
1759         { 132, 0x00022010, 0x00008822, 0x00000101, 0x00000a0f },
1760         { 136, 0x00022010, 0x00008826, 0x00000101, 0x00000a0f },
1761
1762         /* 802.11 UNII */
1763         { 140, 0x00022010, 0x0000882a, 0x00000101, 0x00000a0f },
1764         { 149, 0x00022020, 0x000090a6, 0x00000101, 0x00000a07 },
1765         { 153, 0x00022020, 0x000090ae, 0x00000101, 0x00000a07 },
1766         { 157, 0x00022020, 0x000090b6, 0x00000101, 0x00000a07 },
1767         { 161, 0x00022020, 0x000090be, 0x00000101, 0x00000a07 },
1768 };
1769
1770 static int rt2500usb_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
1771 {
1772         struct hw_mode_spec *spec = &rt2x00dev->spec;
1773         struct channel_info *info;
1774         char *tx_power;
1775         unsigned int i;
1776
1777         /*
1778          * Initialize all hw fields.
1779          */
1780         rt2x00dev->hw->flags =
1781             IEEE80211_HW_RX_INCLUDES_FCS |
1782             IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
1783             IEEE80211_HW_SIGNAL_DBM;
1784
1785         rt2x00dev->hw->extra_tx_headroom = TXD_DESC_SIZE;
1786
1787         SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
1788         SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
1789                                 rt2x00_eeprom_addr(rt2x00dev,
1790                                                    EEPROM_MAC_ADDR_0));
1791
1792         /*
1793          * Initialize hw_mode information.
1794          */
1795         spec->supported_bands = SUPPORT_BAND_2GHZ;
1796         spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
1797
1798         if (rt2x00_rf(&rt2x00dev->chip, RF2522)) {
1799                 spec->num_channels = ARRAY_SIZE(rf_vals_bg_2522);
1800                 spec->channels = rf_vals_bg_2522;
1801         } else if (rt2x00_rf(&rt2x00dev->chip, RF2523)) {
1802                 spec->num_channels = ARRAY_SIZE(rf_vals_bg_2523);
1803                 spec->channels = rf_vals_bg_2523;
1804         } else if (rt2x00_rf(&rt2x00dev->chip, RF2524)) {
1805                 spec->num_channels = ARRAY_SIZE(rf_vals_bg_2524);
1806                 spec->channels = rf_vals_bg_2524;
1807         } else if (rt2x00_rf(&rt2x00dev->chip, RF2525)) {
1808                 spec->num_channels = ARRAY_SIZE(rf_vals_bg_2525);
1809                 spec->channels = rf_vals_bg_2525;
1810         } else if (rt2x00_rf(&rt2x00dev->chip, RF2525E)) {
1811                 spec->num_channels = ARRAY_SIZE(rf_vals_bg_2525e);
1812                 spec->channels = rf_vals_bg_2525e;
1813         } else if (rt2x00_rf(&rt2x00dev->chip, RF5222)) {
1814                 spec->supported_bands |= SUPPORT_BAND_5GHZ;
1815                 spec->num_channels = ARRAY_SIZE(rf_vals_5222);
1816                 spec->channels = rf_vals_5222;
1817         }
1818
1819         /*
1820          * Create channel information array
1821          */
1822         info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
1823         if (!info)
1824                 return -ENOMEM;
1825
1826         spec->channels_info = info;
1827
1828         tx_power = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_START);
1829         for (i = 0; i < 14; i++)
1830                 info[i].tx_power1 = TXPOWER_FROM_DEV(tx_power[i]);
1831
1832         if (spec->num_channels > 14) {
1833                 for (i = 14; i < spec->num_channels; i++)
1834                         info[i].tx_power1 = DEFAULT_TXPOWER;
1835         }
1836
1837         return 0;
1838 }
1839
1840 static int rt2500usb_probe_hw(struct rt2x00_dev *rt2x00dev)
1841 {
1842         int retval;
1843
1844         /*
1845          * Allocate eeprom data.
1846          */
1847         retval = rt2500usb_validate_eeprom(rt2x00dev);
1848         if (retval)
1849                 return retval;
1850
1851         retval = rt2500usb_init_eeprom(rt2x00dev);
1852         if (retval)
1853                 return retval;
1854
1855         /*
1856          * Initialize hw specifications.
1857          */
1858         retval = rt2500usb_probe_hw_mode(rt2x00dev);
1859         if (retval)
1860                 return retval;
1861
1862         /*
1863          * This device requires the atim queue
1864          */
1865         __set_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags);
1866         __set_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags);
1867         __set_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags);
1868         if (!modparam_nohwcrypt) {
1869                 __set_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags);
1870                 __set_bit(CONFIG_CRYPTO_COPY_IV, &rt2x00dev->flags);
1871         }
1872         __set_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags);
1873
1874         /*
1875          * Set the rssi offset.
1876          */
1877         rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
1878
1879         return 0;
1880 }
1881
1882 static const struct ieee80211_ops rt2500usb_mac80211_ops = {
1883         .tx                     = rt2x00mac_tx,
1884         .start                  = rt2x00mac_start,
1885         .stop                   = rt2x00mac_stop,
1886         .add_interface          = rt2x00mac_add_interface,
1887         .remove_interface       = rt2x00mac_remove_interface,
1888         .config                 = rt2x00mac_config,
1889         .config_interface       = rt2x00mac_config_interface,
1890         .configure_filter       = rt2x00mac_configure_filter,
1891         .set_key                = rt2x00mac_set_key,
1892         .get_stats              = rt2x00mac_get_stats,
1893         .bss_info_changed       = rt2x00mac_bss_info_changed,
1894         .conf_tx                = rt2x00mac_conf_tx,
1895         .get_tx_stats           = rt2x00mac_get_tx_stats,
1896 };
1897
1898 static const struct rt2x00lib_ops rt2500usb_rt2x00_ops = {
1899         .probe_hw               = rt2500usb_probe_hw,
1900         .initialize             = rt2x00usb_initialize,
1901         .uninitialize           = rt2x00usb_uninitialize,
1902         .clear_entry            = rt2x00usb_clear_entry,
1903         .set_device_state       = rt2500usb_set_device_state,
1904         .link_stats             = rt2500usb_link_stats,
1905         .reset_tuner            = rt2500usb_reset_tuner,
1906         .link_tuner             = rt2500usb_link_tuner,
1907         .write_tx_desc          = rt2500usb_write_tx_desc,
1908         .write_tx_data          = rt2x00usb_write_tx_data,
1909         .write_beacon           = rt2500usb_write_beacon,
1910         .get_tx_data_len        = rt2500usb_get_tx_data_len,
1911         .kick_tx_queue          = rt2500usb_kick_tx_queue,
1912         .fill_rxdone            = rt2500usb_fill_rxdone,
1913         .config_shared_key      = rt2500usb_config_key,
1914         .config_pairwise_key    = rt2500usb_config_key,
1915         .config_filter          = rt2500usb_config_filter,
1916         .config_intf            = rt2500usb_config_intf,
1917         .config_erp             = rt2500usb_config_erp,
1918         .config_ant             = rt2500usb_config_ant,
1919         .config                 = rt2500usb_config,
1920 };
1921
1922 static const struct data_queue_desc rt2500usb_queue_rx = {
1923         .entry_num              = RX_ENTRIES,
1924         .data_size              = DATA_FRAME_SIZE,
1925         .desc_size              = RXD_DESC_SIZE,
1926         .priv_size              = sizeof(struct queue_entry_priv_usb),
1927 };
1928
1929 static const struct data_queue_desc rt2500usb_queue_tx = {
1930         .entry_num              = TX_ENTRIES,
1931         .data_size              = DATA_FRAME_SIZE,
1932         .desc_size              = TXD_DESC_SIZE,
1933         .priv_size              = sizeof(struct queue_entry_priv_usb),
1934 };
1935
1936 static const struct data_queue_desc rt2500usb_queue_bcn = {
1937         .entry_num              = BEACON_ENTRIES,
1938         .data_size              = MGMT_FRAME_SIZE,
1939         .desc_size              = TXD_DESC_SIZE,
1940         .priv_size              = sizeof(struct queue_entry_priv_usb_bcn),
1941 };
1942
1943 static const struct data_queue_desc rt2500usb_queue_atim = {
1944         .entry_num              = ATIM_ENTRIES,
1945         .data_size              = DATA_FRAME_SIZE,
1946         .desc_size              = TXD_DESC_SIZE,
1947         .priv_size              = sizeof(struct queue_entry_priv_usb),
1948 };
1949
1950 static const struct rt2x00_ops rt2500usb_ops = {
1951         .name           = KBUILD_MODNAME,
1952         .max_sta_intf   = 1,
1953         .max_ap_intf    = 1,
1954         .eeprom_size    = EEPROM_SIZE,
1955         .rf_size        = RF_SIZE,
1956         .tx_queues      = NUM_TX_QUEUES,
1957         .rx             = &rt2500usb_queue_rx,
1958         .tx             = &rt2500usb_queue_tx,
1959         .bcn            = &rt2500usb_queue_bcn,
1960         .atim           = &rt2500usb_queue_atim,
1961         .lib            = &rt2500usb_rt2x00_ops,
1962         .hw             = &rt2500usb_mac80211_ops,
1963 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
1964         .debugfs        = &rt2500usb_rt2x00debug,
1965 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
1966 };
1967
1968 /*
1969  * rt2500usb module information.
1970  */
1971 static struct usb_device_id rt2500usb_device_table[] = {
1972         /* ASUS */
1973         { USB_DEVICE(0x0b05, 0x1706), USB_DEVICE_DATA(&rt2500usb_ops) },
1974         { USB_DEVICE(0x0b05, 0x1707), USB_DEVICE_DATA(&rt2500usb_ops) },
1975         /* Belkin */
1976         { USB_DEVICE(0x050d, 0x7050), USB_DEVICE_DATA(&rt2500usb_ops) },
1977         { USB_DEVICE(0x050d, 0x7051), USB_DEVICE_DATA(&rt2500usb_ops) },
1978         { USB_DEVICE(0x050d, 0x705a), USB_DEVICE_DATA(&rt2500usb_ops) },
1979         /* Cisco Systems */
1980         { USB_DEVICE(0x13b1, 0x000d), USB_DEVICE_DATA(&rt2500usb_ops) },
1981         { USB_DEVICE(0x13b1, 0x0011), USB_DEVICE_DATA(&rt2500usb_ops) },
1982         { USB_DEVICE(0x13b1, 0x001a), USB_DEVICE_DATA(&rt2500usb_ops) },
1983         /* Conceptronic */
1984         { USB_DEVICE(0x14b2, 0x3c02), USB_DEVICE_DATA(&rt2500usb_ops) },
1985         /* D-LINK */
1986         { USB_DEVICE(0x2001, 0x3c00), USB_DEVICE_DATA(&rt2500usb_ops) },
1987         /* Gigabyte */
1988         { USB_DEVICE(0x1044, 0x8001), USB_DEVICE_DATA(&rt2500usb_ops) },
1989         { USB_DEVICE(0x1044, 0x8007), USB_DEVICE_DATA(&rt2500usb_ops) },
1990         /* Hercules */
1991         { USB_DEVICE(0x06f8, 0xe000), USB_DEVICE_DATA(&rt2500usb_ops) },
1992         /* Melco */
1993         { USB_DEVICE(0x0411, 0x005e), USB_DEVICE_DATA(&rt2500usb_ops) },
1994         { USB_DEVICE(0x0411, 0x0066), USB_DEVICE_DATA(&rt2500usb_ops) },
1995         { USB_DEVICE(0x0411, 0x0067), USB_DEVICE_DATA(&rt2500usb_ops) },
1996         { USB_DEVICE(0x0411, 0x008b), USB_DEVICE_DATA(&rt2500usb_ops) },
1997         { USB_DEVICE(0x0411, 0x0097), USB_DEVICE_DATA(&rt2500usb_ops) },
1998         /* MSI */
1999         { USB_DEVICE(0x0db0, 0x6861), USB_DEVICE_DATA(&rt2500usb_ops) },
2000         { USB_DEVICE(0x0db0, 0x6865), USB_DEVICE_DATA(&rt2500usb_ops) },
2001         { USB_DEVICE(0x0db0, 0x6869), USB_DEVICE_DATA(&rt2500usb_ops) },
2002         /* Ralink */
2003         { USB_DEVICE(0x148f, 0x1706), USB_DEVICE_DATA(&rt2500usb_ops) },
2004         { USB_DEVICE(0x148f, 0x2570), USB_DEVICE_DATA(&rt2500usb_ops) },
2005         { USB_DEVICE(0x148f, 0x2573), USB_DEVICE_DATA(&rt2500usb_ops) },
2006         { USB_DEVICE(0x148f, 0x9020), USB_DEVICE_DATA(&rt2500usb_ops) },
2007         /* Siemens */
2008         { USB_DEVICE(0x0681, 0x3c06), USB_DEVICE_DATA(&rt2500usb_ops) },
2009         /* SMC */
2010         { USB_DEVICE(0x0707, 0xee13), USB_DEVICE_DATA(&rt2500usb_ops) },
2011         /* Spairon */
2012         { USB_DEVICE(0x114b, 0x0110), USB_DEVICE_DATA(&rt2500usb_ops) },
2013         /* Trust */
2014         { USB_DEVICE(0x0eb0, 0x9020), USB_DEVICE_DATA(&rt2500usb_ops) },
2015         /* Zinwell */
2016         { USB_DEVICE(0x5a57, 0x0260), USB_DEVICE_DATA(&rt2500usb_ops) },
2017         { 0, }
2018 };
2019
2020 MODULE_AUTHOR(DRV_PROJECT);
2021 MODULE_VERSION(DRV_VERSION);
2022 MODULE_DESCRIPTION("Ralink RT2500 USB Wireless LAN driver.");
2023 MODULE_SUPPORTED_DEVICE("Ralink RT2570 USB chipset based cards");
2024 MODULE_DEVICE_TABLE(usb, rt2500usb_device_table);
2025 MODULE_LICENSE("GPL");
2026
2027 static struct usb_driver rt2500usb_driver = {
2028         .name           = KBUILD_MODNAME,
2029         .id_table       = rt2500usb_device_table,
2030         .probe          = rt2x00usb_probe,
2031         .disconnect     = rt2x00usb_disconnect,
2032         .suspend        = rt2x00usb_suspend,
2033         .resume         = rt2x00usb_resume,
2034 };
2035
2036 static int __init rt2500usb_init(void)
2037 {
2038         return usb_register(&rt2500usb_driver);
2039 }
2040
2041 static void __exit rt2500usb_exit(void)
2042 {
2043         usb_deregister(&rt2500usb_driver);
2044 }
2045
2046 module_init(rt2500usb_init);
2047 module_exit(rt2500usb_exit);