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