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