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