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