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