]> err.no Git - linux-2.6/blob - drivers/net/wireless/rt2x00/rt2x00dev.c
[PATCH] rt2x00: Correctly translate mac80211 antenna setup to rt2x00
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00dev.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: rt2x00lib
23         Abstract: rt2x00 generic device routines.
24  */
25
26 /*
27  * Set enviroment defines for rt2x00.h
28  */
29 #define DRV_NAME "rt2x00lib"
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33
34 #include "rt2x00.h"
35 #include "rt2x00lib.h"
36
37 /*
38  * Ring handler.
39  */
40 struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
41                                      const unsigned int queue)
42 {
43         int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
44
45         /*
46          * Check if we are requesting a reqular TX ring,
47          * or if we are requesting a Beacon or Atim ring.
48          * For Atim rings, we should check if it is supported.
49          */
50         if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
51                 return &rt2x00dev->tx[queue];
52
53         if (!rt2x00dev->bcn || !beacon)
54                 return NULL;
55
56         if (queue == IEEE80211_TX_QUEUE_BEACON)
57                 return &rt2x00dev->bcn[0];
58         else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
59                 return &rt2x00dev->bcn[1];
60
61         return NULL;
62 }
63 EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
64
65 /*
66  * Link tuning handlers
67  */
68 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
69 {
70         rt2x00_clear_link(&rt2x00dev->link);
71
72         /*
73          * Reset the link tuner.
74          */
75         rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
76
77         queue_delayed_work(rt2x00dev->hw->workqueue,
78                            &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
79 }
80
81 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
82 {
83         cancel_delayed_work_sync(&rt2x00dev->link.work);
84 }
85
86 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
87 {
88         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
89                 return;
90
91         rt2x00lib_stop_link_tuner(rt2x00dev);
92         rt2x00lib_start_link_tuner(rt2x00dev);
93 }
94
95 /*
96  * Radio control handlers.
97  */
98 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
99 {
100         int status;
101
102         /*
103          * Don't enable the radio twice.
104          * And check if the hardware button has been disabled.
105          */
106         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
107             test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
108                 return 0;
109
110         /*
111          * Enable radio.
112          */
113         status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
114                                                        STATE_RADIO_ON);
115         if (status)
116                 return status;
117
118         __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
119
120         /*
121          * Enable RX.
122          */
123         rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
124
125         /*
126          * Start the TX queues.
127          */
128         ieee80211_start_queues(rt2x00dev->hw);
129
130         return 0;
131 }
132
133 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
134 {
135         if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
136                 return;
137
138         /*
139          * Stop all scheduled work.
140          */
141         if (work_pending(&rt2x00dev->beacon_work))
142                 cancel_work_sync(&rt2x00dev->beacon_work);
143         if (work_pending(&rt2x00dev->filter_work))
144                 cancel_work_sync(&rt2x00dev->filter_work);
145         if (work_pending(&rt2x00dev->config_work))
146                 cancel_work_sync(&rt2x00dev->config_work);
147
148         /*
149          * Stop the TX queues.
150          */
151         ieee80211_stop_queues(rt2x00dev->hw);
152
153         /*
154          * Disable RX.
155          */
156         rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
157
158         /*
159          * Disable radio.
160          */
161         rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
162 }
163
164 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
165 {
166         /*
167          * When we are disabling the RX, we should also stop the link tuner.
168          */
169         if (state == STATE_RADIO_RX_OFF)
170                 rt2x00lib_stop_link_tuner(rt2x00dev);
171
172         rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
173
174         /*
175          * When we are enabling the RX, we should also start the link tuner.
176          */
177         if (state == STATE_RADIO_RX_ON &&
178             is_interface_present(&rt2x00dev->interface))
179                 rt2x00lib_start_link_tuner(rt2x00dev);
180 }
181
182 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
183 {
184         if (qual->rx_failed || qual->rx_success)
185                 qual->rx_percentage =
186                     (qual->rx_success * 100) /
187                     (qual->rx_failed + qual->rx_success);
188         else
189                 qual->rx_percentage = 50;
190
191         if (qual->tx_failed || qual->tx_success)
192                 qual->tx_percentage =
193                     (qual->tx_success * 100) /
194                     (qual->tx_failed + qual->tx_success);
195         else
196                 qual->tx_percentage = 50;
197
198         qual->rx_success = 0;
199         qual->rx_failed = 0;
200         qual->tx_success = 0;
201         qual->tx_failed = 0;
202 }
203
204 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
205                                            int rssi)
206 {
207         int rssi_percentage = 0;
208         int signal;
209
210         /*
211          * We need a positive value for the RSSI.
212          */
213         if (rssi < 0)
214                 rssi += rt2x00dev->rssi_offset;
215
216         /*
217          * Calculate the different percentages,
218          * which will be used for the signal.
219          */
220         if (rt2x00dev->rssi_offset)
221                 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
222
223         /*
224          * Add the individual percentages and use the WEIGHT
225          * defines to calculate the current link signal.
226          */
227         signal = ((WEIGHT_RSSI * rssi_percentage) +
228                   (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
229                   (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
230
231         return (signal > 100) ? 100 : signal;
232 }
233
234 static void rt2x00lib_link_tuner(struct work_struct *work)
235 {
236         struct rt2x00_dev *rt2x00dev =
237             container_of(work, struct rt2x00_dev, link.work.work);
238
239         /*
240          * When the radio is shutting down we should
241          * immediately cease all link tuning.
242          */
243         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
244                 return;
245
246         /*
247          * Update statistics.
248          */
249         rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
250
251         rt2x00dev->low_level_stats.dot11FCSErrorCount +=
252             rt2x00dev->link.qual.rx_failed;
253
254         /*
255          * Only perform the link tuning when Link tuning
256          * has been enabled (This could have been disabled from the EEPROM).
257          */
258         if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
259                 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
260
261         /*
262          * Precalculate a portion of the link signal which is
263          * in based on the tx/rx success/failure counters.
264          */
265         rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
266
267         /*
268          * Increase tuner counter, and reschedule the next link tuner run.
269          */
270         rt2x00dev->link.count++;
271         queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
272                            LINK_TUNE_INTERVAL);
273 }
274
275 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
276 {
277         struct rt2x00_dev *rt2x00dev =
278             container_of(work, struct rt2x00_dev, filter_work);
279         unsigned int filter = rt2x00dev->interface.filter;
280
281         /*
282          * Since we had stored the filter inside interface.filter,
283          * we should now clear that field. Otherwise the driver will
284          * assume nothing has changed (*total_flags will be compared
285          * to interface.filter to determine if any action is required).
286          */
287         rt2x00dev->interface.filter = 0;
288
289         rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
290                                              filter, &filter, 0, NULL);
291 }
292
293 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
294 {
295         struct rt2x00_dev *rt2x00dev =
296             container_of(work, struct rt2x00_dev, config_work);
297         int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
298
299         rt2x00mac_erp_ie_changed(rt2x00dev->hw,
300                                  IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
301 }
302
303 /*
304  * Interrupt context handlers.
305  */
306 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
307 {
308         struct rt2x00_dev *rt2x00dev =
309             container_of(work, struct rt2x00_dev, beacon_work);
310         struct data_ring *ring =
311             rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
312         struct data_entry *entry = rt2x00_get_data_entry(ring);
313         struct sk_buff *skb;
314
315         skb = ieee80211_beacon_get(rt2x00dev->hw,
316                                    rt2x00dev->interface.id,
317                                    &entry->tx_status.control);
318         if (!skb)
319                 return;
320
321         rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
322                                           &entry->tx_status.control);
323
324         dev_kfree_skb(skb);
325 }
326
327 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
328 {
329         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
330                 return;
331
332         queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
333 }
334 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
335
336 void rt2x00lib_txdone(struct data_entry *entry,
337                       const int status, const int retry)
338 {
339         struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
340         struct ieee80211_tx_status *tx_status = &entry->tx_status;
341         struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
342         int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
343         int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
344                       status == TX_FAIL_OTHER);
345
346         /*
347          * Update TX statistics.
348          */
349         tx_status->flags = 0;
350         tx_status->ack_signal = 0;
351         tx_status->excessive_retries = (status == TX_FAIL_RETRY);
352         tx_status->retry_count = retry;
353         rt2x00dev->link.qual.tx_success += success;
354         rt2x00dev->link.qual.tx_failed += retry + fail;
355
356         if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
357                 if (success)
358                         tx_status->flags |= IEEE80211_TX_STATUS_ACK;
359                 else
360                         stats->dot11ACKFailureCount++;
361         }
362
363         tx_status->queue_length = entry->ring->stats.limit;
364         tx_status->queue_number = tx_status->control.queue;
365
366         if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
367                 if (success)
368                         stats->dot11RTSSuccessCount++;
369                 else
370                         stats->dot11RTSFailureCount++;
371         }
372
373         /*
374          * Send the tx_status to mac80211,
375          * that method also cleans up the skb structure.
376          */
377         ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
378         entry->skb = NULL;
379 }
380 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
381
382 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
383                       struct rxdata_entry_desc *desc)
384 {
385         struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
386         struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
387         struct ieee80211_hw_mode *mode;
388         struct ieee80211_rate *rate;
389         unsigned int i;
390         int val = 0;
391
392         /*
393          * Update RX statistics.
394          */
395         mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
396         for (i = 0; i < mode->num_rates; i++) {
397                 rate = &mode->rates[i];
398
399                 /*
400                  * When frame was received with an OFDM bitrate,
401                  * the signal is the PLCP value. If it was received with
402                  * a CCK bitrate the signal is the rate in 0.5kbit/s.
403                  */
404                 if (!desc->ofdm)
405                         val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
406                 else
407                         val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
408
409                 if (val == desc->signal) {
410                         val = rate->val;
411                         break;
412                 }
413         }
414
415         rt2x00_update_link_rssi(&rt2x00dev->link, desc->rssi);
416         rt2x00dev->link.qual.rx_success++;
417         rx_status->rate = val;
418         rx_status->signal =
419             rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
420         rx_status->ssi = desc->rssi;
421         rx_status->flag = desc->flags;
422         rx_status->antenna = rt2x00dev->link.active_ant.rx;
423
424         /*
425          * Send frame to mac80211
426          */
427         ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
428 }
429 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
430
431 /*
432  * TX descriptor initializer
433  */
434 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
435                              struct data_desc *txd,
436                              struct ieee80211_hdr *ieee80211hdr,
437                              unsigned int length,
438                              struct ieee80211_tx_control *control)
439 {
440         struct txdata_entry_desc desc;
441         struct data_ring *ring;
442         int tx_rate;
443         int bitrate;
444         int duration;
445         int residual;
446         u16 frame_control;
447         u16 seq_ctrl;
448
449         /*
450          * Make sure the descriptor is properly cleared.
451          */
452         memset(&desc, 0x00, sizeof(desc));
453
454         /*
455          * Get ring pointer, if we fail to obtain the
456          * correct ring, then use the first TX ring.
457          */
458         ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
459         if (!ring)
460                 ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
461
462         desc.cw_min = ring->tx_params.cw_min;
463         desc.cw_max = ring->tx_params.cw_max;
464         desc.aifs = ring->tx_params.aifs;
465
466         /*
467          * Identify queue
468          */
469         if (control->queue < rt2x00dev->hw->queues)
470                 desc.queue = control->queue;
471         else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
472                  control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
473                 desc.queue = QUEUE_MGMT;
474         else
475                 desc.queue = QUEUE_OTHER;
476
477         /*
478          * Read required fields from ieee80211 header.
479          */
480         frame_control = le16_to_cpu(ieee80211hdr->frame_control);
481         seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
482
483         tx_rate = control->tx_rate;
484
485         /*
486          * Check if this is a RTS/CTS frame
487          */
488         if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
489                 __set_bit(ENTRY_TXD_BURST, &desc.flags);
490                 if (is_rts_frame(frame_control))
491                         __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
492                 if (control->rts_cts_rate)
493                         tx_rate = control->rts_cts_rate;
494         }
495
496         /*
497          * Check for OFDM
498          */
499         if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
500                 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
501
502         /*
503          * Check if more fragments are pending
504          */
505         if (ieee80211_get_morefrag(ieee80211hdr)) {
506                 __set_bit(ENTRY_TXD_BURST, &desc.flags);
507                 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
508         }
509
510         /*
511          * Beacons and probe responses require the tsf timestamp
512          * to be inserted into the frame.
513          */
514         if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
515             is_probe_resp(frame_control))
516                 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
517
518         /*
519          * Determine with what IFS priority this frame should be send.
520          * Set ifs to IFS_SIFS when the this is not the first fragment,
521          * or this fragment came after RTS/CTS.
522          */
523         if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
524             test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
525                 desc.ifs = IFS_SIFS;
526         else
527                 desc.ifs = IFS_BACKOFF;
528
529         /*
530          * PLCP setup
531          * Length calculation depends on OFDM/CCK rate.
532          */
533         desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
534         desc.service = 0x04;
535
536         if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
537                 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
538                 desc.length_low = ((length + FCS_LEN) & 0x3f);
539         } else {
540                 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
541
542                 /*
543                  * Convert length to microseconds.
544                  */
545                 residual = get_duration_res(length + FCS_LEN, bitrate);
546                 duration = get_duration(length + FCS_LEN, bitrate);
547
548                 if (residual != 0) {
549                         duration++;
550
551                         /*
552                          * Check if we need to set the Length Extension
553                          */
554                         if (bitrate == 110 && residual <= 30)
555                                 desc.service |= 0x80;
556                 }
557
558                 desc.length_high = (duration >> 8) & 0xff;
559                 desc.length_low = duration & 0xff;
560
561                 /*
562                  * When preamble is enabled we should set the
563                  * preamble bit for the signal.
564                  */
565                 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
566                         desc.signal |= 0x08;
567         }
568
569         rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
570                                            ieee80211hdr, length, control);
571 }
572 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
573
574 /*
575  * Driver initialization handlers.
576  */
577 static void rt2x00lib_channel(struct ieee80211_channel *entry,
578                               const int channel, const int tx_power,
579                               const int value)
580 {
581         entry->chan = channel;
582         if (channel <= 14)
583                 entry->freq = 2407 + (5 * channel);
584         else
585                 entry->freq = 5000 + (5 * channel);
586         entry->val = value;
587         entry->flag =
588             IEEE80211_CHAN_W_IBSS |
589             IEEE80211_CHAN_W_ACTIVE_SCAN |
590             IEEE80211_CHAN_W_SCAN;
591         entry->power_level = tx_power;
592         entry->antenna_max = 0xff;
593 }
594
595 static void rt2x00lib_rate(struct ieee80211_rate *entry,
596                            const int rate, const int mask,
597                            const int plcp, const int flags)
598 {
599         entry->rate = rate;
600         entry->val =
601             DEVICE_SET_RATE_FIELD(rate, RATE) |
602             DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
603             DEVICE_SET_RATE_FIELD(plcp, PLCP);
604         entry->flags = flags;
605         entry->val2 = entry->val;
606         if (entry->flags & IEEE80211_RATE_PREAMBLE2)
607                 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
608         entry->min_rssi_ack = 0;
609         entry->min_rssi_ack_delta = 0;
610 }
611
612 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
613                                     struct hw_mode_spec *spec)
614 {
615         struct ieee80211_hw *hw = rt2x00dev->hw;
616         struct ieee80211_hw_mode *hwmodes;
617         struct ieee80211_channel *channels;
618         struct ieee80211_rate *rates;
619         unsigned int i;
620         unsigned char tx_power;
621
622         hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
623         if (!hwmodes)
624                 goto exit;
625
626         channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
627         if (!channels)
628                 goto exit_free_modes;
629
630         rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
631         if (!rates)
632                 goto exit_free_channels;
633
634         /*
635          * Initialize Rate list.
636          */
637         rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
638                        0x00, IEEE80211_RATE_CCK);
639         rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
640                        0x01, IEEE80211_RATE_CCK_2);
641         rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
642                        0x02, IEEE80211_RATE_CCK_2);
643         rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
644                        0x03, IEEE80211_RATE_CCK_2);
645
646         if (spec->num_rates > 4) {
647                 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
648                                0x0b, IEEE80211_RATE_OFDM);
649                 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
650                                0x0f, IEEE80211_RATE_OFDM);
651                 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
652                                0x0a, IEEE80211_RATE_OFDM);
653                 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
654                                0x0e, IEEE80211_RATE_OFDM);
655                 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
656                                0x09, IEEE80211_RATE_OFDM);
657                 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
658                                0x0d, IEEE80211_RATE_OFDM);
659                 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
660                                0x08, IEEE80211_RATE_OFDM);
661                 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
662                                0x0c, IEEE80211_RATE_OFDM);
663         }
664
665         /*
666          * Initialize Channel list.
667          */
668         for (i = 0; i < spec->num_channels; i++) {
669                 if (spec->channels[i].channel <= 14)
670                         tx_power = spec->tx_power_bg[i];
671                 else if (spec->tx_power_a)
672                         tx_power = spec->tx_power_a[i];
673                 else
674                         tx_power = spec->tx_power_default;
675
676                 rt2x00lib_channel(&channels[i],
677                                   spec->channels[i].channel, tx_power, i);
678         }
679
680         /*
681          * Intitialize 802.11b
682          * Rates: CCK.
683          * Channels: OFDM.
684          */
685         if (spec->num_modes > HWMODE_B) {
686                 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
687                 hwmodes[HWMODE_B].num_channels = 14;
688                 hwmodes[HWMODE_B].num_rates = 4;
689                 hwmodes[HWMODE_B].channels = channels;
690                 hwmodes[HWMODE_B].rates = rates;
691         }
692
693         /*
694          * Intitialize 802.11g
695          * Rates: CCK, OFDM.
696          * Channels: OFDM.
697          */
698         if (spec->num_modes > HWMODE_G) {
699                 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
700                 hwmodes[HWMODE_G].num_channels = 14;
701                 hwmodes[HWMODE_G].num_rates = spec->num_rates;
702                 hwmodes[HWMODE_G].channels = channels;
703                 hwmodes[HWMODE_G].rates = rates;
704         }
705
706         /*
707          * Intitialize 802.11a
708          * Rates: OFDM.
709          * Channels: OFDM, UNII, HiperLAN2.
710          */
711         if (spec->num_modes > HWMODE_A) {
712                 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
713                 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
714                 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
715                 hwmodes[HWMODE_A].channels = &channels[14];
716                 hwmodes[HWMODE_A].rates = &rates[4];
717         }
718
719         if (spec->num_modes > HWMODE_G &&
720             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
721                 goto exit_free_rates;
722
723         if (spec->num_modes > HWMODE_B &&
724             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
725                 goto exit_free_rates;
726
727         if (spec->num_modes > HWMODE_A &&
728             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
729                 goto exit_free_rates;
730
731         rt2x00dev->hwmodes = hwmodes;
732
733         return 0;
734
735 exit_free_rates:
736         kfree(rates);
737
738 exit_free_channels:
739         kfree(channels);
740
741 exit_free_modes:
742         kfree(hwmodes);
743
744 exit:
745         ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
746         return -ENOMEM;
747 }
748
749 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
750 {
751         if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
752                 ieee80211_unregister_hw(rt2x00dev->hw);
753
754         if (likely(rt2x00dev->hwmodes)) {
755                 kfree(rt2x00dev->hwmodes->channels);
756                 kfree(rt2x00dev->hwmodes->rates);
757                 kfree(rt2x00dev->hwmodes);
758                 rt2x00dev->hwmodes = NULL;
759         }
760 }
761
762 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
763 {
764         struct hw_mode_spec *spec = &rt2x00dev->spec;
765         int status;
766
767         /*
768          * Initialize HW modes.
769          */
770         status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
771         if (status)
772                 return status;
773
774         /*
775          * Register HW.
776          */
777         status = ieee80211_register_hw(rt2x00dev->hw);
778         if (status) {
779                 rt2x00lib_remove_hw(rt2x00dev);
780                 return status;
781         }
782
783         __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
784
785         return 0;
786 }
787
788 /*
789  * Initialization/uninitialization handlers.
790  */
791 static int rt2x00lib_alloc_entries(struct data_ring *ring,
792                                    const u16 max_entries, const u16 data_size,
793                                    const u16 desc_size)
794 {
795         struct data_entry *entry;
796         unsigned int i;
797
798         ring->stats.limit = max_entries;
799         ring->data_size = data_size;
800         ring->desc_size = desc_size;
801
802         /*
803          * Allocate all ring entries.
804          */
805         entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
806         if (!entry)
807                 return -ENOMEM;
808
809         for (i = 0; i < ring->stats.limit; i++) {
810                 entry[i].flags = 0;
811                 entry[i].ring = ring;
812                 entry[i].skb = NULL;
813         }
814
815         ring->entry = entry;
816
817         return 0;
818 }
819
820 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
821 {
822         struct data_ring *ring;
823
824         /*
825          * Allocate the RX ring.
826          */
827         if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
828                                     rt2x00dev->ops->rxd_size))
829                 return -ENOMEM;
830
831         /*
832          * First allocate the TX rings.
833          */
834         txring_for_each(rt2x00dev, ring) {
835                 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
836                                             rt2x00dev->ops->txd_size))
837                         return -ENOMEM;
838         }
839
840         if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
841                 return 0;
842
843         /*
844          * Allocate the BEACON ring.
845          */
846         if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
847                                     MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
848                 return -ENOMEM;
849
850         /*
851          * Allocate the Atim ring.
852          */
853         if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
854                                     DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
855                 return -ENOMEM;
856
857         return 0;
858 }
859
860 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
861 {
862         struct data_ring *ring;
863
864         ring_for_each(rt2x00dev, ring) {
865                 kfree(ring->entry);
866                 ring->entry = NULL;
867         }
868 }
869
870 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
871 {
872         if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
873                 return;
874
875         /*
876          * Unregister rfkill.
877          */
878         rt2x00rfkill_unregister(rt2x00dev);
879
880         /*
881          * Allow the HW to uninitialize.
882          */
883         rt2x00dev->ops->lib->uninitialize(rt2x00dev);
884
885         /*
886          * Free allocated ring entries.
887          */
888         rt2x00lib_free_ring_entries(rt2x00dev);
889 }
890
891 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
892 {
893         int status;
894
895         if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
896                 return 0;
897
898         /*
899          * Allocate all ring entries.
900          */
901         status = rt2x00lib_alloc_ring_entries(rt2x00dev);
902         if (status) {
903                 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
904                 return status;
905         }
906
907         /*
908          * Initialize the device.
909          */
910         status = rt2x00dev->ops->lib->initialize(rt2x00dev);
911         if (status)
912                 goto exit;
913
914         __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
915
916         /*
917          * Register the rfkill handler.
918          */
919         status = rt2x00rfkill_register(rt2x00dev);
920         if (status)
921                 goto exit_unitialize;
922
923         return 0;
924
925 exit_unitialize:
926         rt2x00lib_uninitialize(rt2x00dev);
927
928 exit:
929         rt2x00lib_free_ring_entries(rt2x00dev);
930
931         return status;
932 }
933
934 /*
935  * driver allocation handlers.
936  */
937 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
938 {
939         struct data_ring *ring;
940
941         /*
942          * We need the following rings:
943          * RX: 1
944          * TX: hw->queues
945          * Beacon: 1 (if required)
946          * Atim: 1 (if required)
947          */
948         rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
949             (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
950
951         ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
952         if (!ring) {
953                 ERROR(rt2x00dev, "Ring allocation failed.\n");
954                 return -ENOMEM;
955         }
956
957         /*
958          * Initialize pointers
959          */
960         rt2x00dev->rx = ring;
961         rt2x00dev->tx = &rt2x00dev->rx[1];
962         if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
963                 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
964
965         /*
966          * Initialize ring parameters.
967          * cw_min: 2^5 = 32.
968          * cw_max: 2^10 = 1024.
969          */
970         ring_for_each(rt2x00dev, ring) {
971                 ring->rt2x00dev = rt2x00dev;
972                 ring->tx_params.aifs = 2;
973                 ring->tx_params.cw_min = 5;
974                 ring->tx_params.cw_max = 10;
975         }
976
977         return 0;
978 }
979
980 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
981 {
982         kfree(rt2x00dev->rx);
983         rt2x00dev->rx = NULL;
984         rt2x00dev->tx = NULL;
985         rt2x00dev->bcn = NULL;
986 }
987
988 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
989 {
990         int retval = -ENOMEM;
991
992         /*
993          * Let the driver probe the device to detect the capabilities.
994          */
995         retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
996         if (retval) {
997                 ERROR(rt2x00dev, "Failed to allocate device.\n");
998                 goto exit;
999         }
1000
1001         /*
1002          * Initialize configuration work.
1003          */
1004         INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1005         INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1006         INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1007         INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1008
1009         /*
1010          * Reset current working type.
1011          */
1012         rt2x00dev->interface.type = INVALID_INTERFACE;
1013
1014         /*
1015          * Allocate ring array.
1016          */
1017         retval = rt2x00lib_alloc_rings(rt2x00dev);
1018         if (retval)
1019                 goto exit;
1020
1021         /*
1022          * Initialize ieee80211 structure.
1023          */
1024         retval = rt2x00lib_probe_hw(rt2x00dev);
1025         if (retval) {
1026                 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1027                 goto exit;
1028         }
1029
1030         /*
1031          * Allocatie rfkill.
1032          */
1033         retval = rt2x00rfkill_allocate(rt2x00dev);
1034         if (retval)
1035                 goto exit;
1036
1037         /*
1038          * Open the debugfs entry.
1039          */
1040         rt2x00debug_register(rt2x00dev);
1041
1042         __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1043
1044         return 0;
1045
1046 exit:
1047         rt2x00lib_remove_dev(rt2x00dev);
1048
1049         return retval;
1050 }
1051 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1052
1053 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1054 {
1055         __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1056
1057         /*
1058          * Disable radio.
1059          */
1060         rt2x00lib_disable_radio(rt2x00dev);
1061
1062         /*
1063          * Uninitialize device.
1064          */
1065         rt2x00lib_uninitialize(rt2x00dev);
1066
1067         /*
1068          * Close debugfs entry.
1069          */
1070         rt2x00debug_deregister(rt2x00dev);
1071
1072         /*
1073          * Free rfkill
1074          */
1075         rt2x00rfkill_free(rt2x00dev);
1076
1077         /*
1078          * Free ieee80211_hw memory.
1079          */
1080         rt2x00lib_remove_hw(rt2x00dev);
1081
1082         /*
1083          * Free firmware image.
1084          */
1085         rt2x00lib_free_firmware(rt2x00dev);
1086
1087         /*
1088          * Free ring structures.
1089          */
1090         rt2x00lib_free_rings(rt2x00dev);
1091 }
1092 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1093
1094 /*
1095  * Device state handlers
1096  */
1097 #ifdef CONFIG_PM
1098 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1099 {
1100         int retval;
1101
1102         NOTICE(rt2x00dev, "Going to sleep.\n");
1103         __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1104
1105         /*
1106          * Only continue if mac80211 has open interfaces.
1107          */
1108         if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1109                 goto exit;
1110         __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1111
1112         /*
1113          * Disable radio and unitialize all items
1114          * that must be recreated on resume.
1115          */
1116         rt2x00mac_stop(rt2x00dev->hw);
1117         rt2x00lib_uninitialize(rt2x00dev);
1118         rt2x00debug_deregister(rt2x00dev);
1119
1120 exit:
1121         /*
1122          * Set device mode to sleep for power management.
1123          */
1124         retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1125         if (retval)
1126                 return retval;
1127
1128         return 0;
1129 }
1130 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1131
1132 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1133 {
1134         struct interface *intf = &rt2x00dev->interface;
1135         int retval;
1136
1137         NOTICE(rt2x00dev, "Waking up.\n");
1138         __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1139
1140         /*
1141          * Open the debugfs entry.
1142          */
1143         rt2x00debug_register(rt2x00dev);
1144
1145         /*
1146          * Only continue if mac80211 had open interfaces.
1147          */
1148         if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1149                 return 0;
1150
1151         /*
1152          * Reinitialize device and all active interfaces.
1153          */
1154         retval = rt2x00mac_start(rt2x00dev->hw);
1155         if (retval)
1156                 goto exit;
1157
1158         /*
1159          * Reconfigure device.
1160          */
1161         rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1162         if (!rt2x00dev->hw->conf.radio_enabled)
1163                 rt2x00lib_disable_radio(rt2x00dev);
1164
1165         rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1166         rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1167         rt2x00lib_config_type(rt2x00dev, intf->type);
1168
1169         /*
1170          * It is possible that during that mac80211 has attempted
1171          * to send frames while we were suspending or resuming.
1172          * In that case we have disabled the TX queue and should
1173          * now enable it again
1174          */
1175         ieee80211_start_queues(rt2x00dev->hw);
1176
1177         /*
1178          * When in Master or Ad-hoc mode,
1179          * restart Beacon transmitting by faking a beacondone event.
1180          */
1181         if (intf->type == IEEE80211_IF_TYPE_AP ||
1182             intf->type == IEEE80211_IF_TYPE_IBSS)
1183                 rt2x00lib_beacondone(rt2x00dev);
1184
1185         return 0;
1186
1187 exit:
1188         rt2x00lib_disable_radio(rt2x00dev);
1189         rt2x00lib_uninitialize(rt2x00dev);
1190         rt2x00debug_deregister(rt2x00dev);
1191
1192         return retval;
1193 }
1194 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1195 #endif /* CONFIG_PM */
1196
1197 /*
1198  * rt2x00lib module information.
1199  */
1200 MODULE_AUTHOR(DRV_PROJECT);
1201 MODULE_VERSION(DRV_VERSION);
1202 MODULE_DESCRIPTION("rt2x00 library");
1203 MODULE_LICENSE("GPL");