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